1 /****************************************************************************
2 * Copyright 2018-2024,2025 Thomas E. Dickey *
3 * Copyright 1998-2011,2012 Free Software Foundation, Inc. *
4 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 * and: Juergen Pfeifer *
35 ****************************************************************************/
36
37 #include <curses.priv.h>
38
39 #ifndef CUR
40 #define CUR SP_TERMTYPE
41 #endif
42
43 MODULE_ID("$Id: lib_print.c,v 1.35 2025/12/27 12:41:23 tom Exp $")
44
NCURSES_EXPORT(int)45 NCURSES_EXPORT(int)
46 NCURSES_SP_NAME(mcprint) (NCURSES_SP_DCLx char *data, int len)
47 /* ship binary character data to the printer via mc4/mc5/mc5p */
48 {
49 int result;
50 char *mybuf = NULL;
51 const char *switchon;
52 size_t onsize, offsize;
53 size_t need;
54
55 errno = 0;
56 if (!HasTInfoTerminal(SP_PARM)
57 || len <= 0
58 || (!prtr_non && (!prtr_on || !prtr_off))) {
59 errno = ENODEV;
60 return (ERR);
61 }
62
63 if (prtr_non) {
64 switchon = TIPARM_1(prtr_non, len);
65 onsize = strlen(switchon);
66 offsize = 0;
67 } else {
68 switchon = prtr_on;
69 onsize = strlen(prtr_on);
70 offsize = strlen(prtr_off);
71 }
72
73 need = onsize + (size_t) len + offsize;
74
75 if (switchon == NULL
76 || (mybuf = typeMalloc(char, need + 1)) == NULL) {
77 free(mybuf);
78 errno = ENOMEM;
79 return (ERR);
80 }
81
82 _nc_STRCPY(mybuf, switchon, need);
83 memcpy(mybuf + onsize, data, (size_t) len);
84 if (offsize)
85 _nc_STRCPY(mybuf + onsize + len, prtr_off, need);
86
87 /*
88 * We're relying on the atomicity of UNIX writes here. The
89 * danger is that output from a refresh() might get interspersed
90 * with the printer data after the write call returns but before the
91 * data has actually been shipped to the terminal. If the write(2)
92 * operation is truly atomic we're protected from this.
93 */
94 result = (int) write(SP_PARM->_ofd, mybuf, need);
95
96 /*
97 * By giving up our scheduler slot here we increase the odds that the
98 * kernel will ship the contiguous clist items from the last write
99 * immediately.
100 */
101 #ifndef _NC_WINDOWS_NATIVE
102 (void) sleep(0);
103 #endif
104 free(mybuf);
105 return (result);
106 }
107
108 #if NCURSES_SP_FUNCS && !USE_TERM_DRIVER
109 NCURSES_EXPORT(int)
mcprint(char * data,int len)110 mcprint(char *data, int len)
111 {
112 return NCURSES_SP_NAME(mcprint) (CURRENT_SCREEN, data, len);
113 }
114 #endif
115