1 /****************************************************************************
2 * Copyright 2018-2024,2025 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 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 2009 *
35 ****************************************************************************/
36
37 /*
38 * tputs.c
39 * delay_output()
40 * _nc_outch()
41 * tputs()
42 *
43 */
44
45 #include <curses.priv.h>
46
47 #ifndef CUR
48 #define CUR SP_TERMTYPE
49 #endif
50
51 #include <ctype.h>
52 #include <termcap.h> /* ospeed */
53 #include <tic.h>
54
55 MODULE_ID("$Id: lib_tputs.c,v 1.116 2025/01/12 00:41:56 tom Exp $")
56
57 NCURSES_EXPORT_VAR(char) PC = 0; /* used by termcap library */
58 NCURSES_EXPORT_VAR(NCURSES_OSPEED) ospeed = 0; /* used by termcap library */
59
60 NCURSES_EXPORT_VAR(int) _nc_nulls_sent = 0;
61
62 #if NCURSES_NO_PADDING
63 NCURSES_EXPORT(void)
_nc_set_no_padding(SCREEN * sp)64 _nc_set_no_padding(SCREEN *sp)
65 {
66 bool no_padding = (getenv("NCURSES_NO_PADDING") != NULL);
67
68 if (sp)
69 sp->_no_padding = no_padding;
70 else
71 _nc_prescreen._no_padding = no_padding;
72
73 TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used",
74 GetNoPadding(sp) ? " not" : ""));
75 }
76 #endif
77
78 #if NCURSES_SP_FUNCS
79 #define SetOutCh(func) if (SP_PARM) SP_PARM->_outch = func; else _nc_prescreen._outch = func
80 #define GetOutCh() (SP_PARM ? SP_PARM->_outch : _nc_prescreen._outch)
81 #else
82 #define SetOutCh(func) static_outch = func
83 #define GetOutCh() static_outch
84 static NCURSES_SP_OUTC static_outch = NCURSES_SP_NAME(_nc_outch);
85 #endif
86
87 NCURSES_EXPORT(int)
delay_output(NCURSES_SP_DCLx int ms)88 NCURSES_SP_NAME(delay_output) (NCURSES_SP_DCLx int ms)
89 {
90 T((T_CALLED("delay_output(%p,%d)"), (void *) SP_PARM, ms));
91
92 if (ms > MAX_DELAY_MSECS)
93 ms = MAX_DELAY_MSECS;
94
95 if (!HasTInfoTerminal(SP_PARM))
96 returnCode(ERR);
97
98 if (no_pad_char) {
99 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
100 napms(ms);
101 } else {
102 NCURSES_SP_OUTC my_outch = GetOutCh();
103 register int nullcount;
104
105 nullcount = (ms * _nc_baudrate(ospeed)) / (BAUDBYTE * 1000);
106 for (_nc_nulls_sent += nullcount; nullcount > 0; nullcount--)
107 my_outch(NCURSES_SP_ARGx PC);
108 if (my_outch == NCURSES_SP_NAME(_nc_outch))
109 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
110 }
111
112 returnCode(OK);
113 }
114
115 #if NCURSES_SP_FUNCS
116 NCURSES_EXPORT(int)
delay_output(int ms)117 delay_output(int ms)
118 {
119 return NCURSES_SP_NAME(delay_output) (CURRENT_SCREEN, ms);
120 }
121 #endif
122
123 NCURSES_EXPORT(void)
_nc_flush(NCURSES_SP_DCL0)124 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_DCL0)
125 {
126 T((T_CALLED("_nc_flush(%p)"), (void *) SP_PARM));
127 if (SP_PARM != NULL && SP_PARM->_ofd >= 0) {
128 TR(TRACE_CHARPUT, ("ofd:%d inuse:%lu buffer:%p",
129 SP_PARM->_ofd,
130 (unsigned long) SP_PARM->out_inuse,
131 SP_PARM->out_buffer));
132 if (SP_PARM->out_inuse) {
133 char *buf = SP_PARM->out_buffer;
134 size_t amount = SP_PARM->out_inuse;
135
136 TR(TRACE_CHARPUT, ("flushing %ld/%ld bytes",
137 (unsigned long) amount, _nc_outchars));
138 while (amount) {
139 ssize_t res = write(SP_PARM->_ofd, buf, amount);
140 if (res > 0) {
141 /* if the write was incomplete, try again */
142 amount -= (size_t) res;
143 buf += res;
144 } else if (errno == EAGAIN) {
145 continue;
146 } else if (errno == EINTR) {
147 continue;
148 } else {
149 break; /* an error we can not recover from */
150 }
151 }
152 } else if (SP_PARM->out_buffer == NULL) {
153 TR(TRACE_CHARPUT, ("flushing stdout/stderr"));
154 fflush(stdout);
155 fflush(stderr);
156 }
157 } else {
158 TR(TRACE_CHARPUT, ("flushing stdout/stderr"));
159 fflush(stdout);
160 fflush(stderr);
161 }
162 if (SP_PARM != NULL)
163 SP_PARM->out_inuse = 0;
164 returnVoid;
165 }
166
167 #if NCURSES_SP_FUNCS
168 NCURSES_EXPORT(void)
_nc_flush(void)169 _nc_flush(void)
170 {
171 NCURSES_SP_NAME(_nc_flush) (CURRENT_SCREEN);
172 }
173 #endif
174
175 NCURSES_EXPORT(int)
_nc_outch(NCURSES_SP_DCLx int ch)176 NCURSES_SP_NAME(_nc_outch) (NCURSES_SP_DCLx int ch)
177 {
178 int rc = OK;
179
180 COUNT_OUTCHARS(1);
181
182 if (HasTInfoTerminal(SP_PARM)
183 && SP_PARM != NULL) {
184 if (SP_PARM->out_buffer != NULL) {
185 if (SP_PARM->out_inuse + 1 >= SP_PARM->out_limit)
186 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
187 SP_PARM->out_buffer[SP_PARM->out_inuse++] = (char) ch;
188 } else {
189 char tmp = (char) ch;
190 /*
191 * POSIX says write() is safe in a signal handler, but the
192 * buffered I/O is not.
193 */
194 if (write(fileno(NC_OUTPUT(SP_PARM)), &tmp, (size_t) 1) == -1)
195 rc = ERR;
196 }
197 } else {
198 char tmp = (char) ch;
199 if (write(fileno(stdout), &tmp, (size_t) 1) == -1)
200 rc = ERR;
201 }
202 return rc;
203 }
204
205 #if NCURSES_SP_FUNCS
206 NCURSES_EXPORT(int)
_nc_outch(int ch)207 _nc_outch(int ch)
208 {
209 return NCURSES_SP_NAME(_nc_outch) (CURRENT_SCREEN, ch);
210 }
211 #endif
212
213 /*
214 * This is used for the putp special case.
215 */
216 NCURSES_EXPORT(int)
_nc_putchar(NCURSES_SP_DCLx int ch)217 NCURSES_SP_NAME(_nc_putchar) (NCURSES_SP_DCLx int ch)
218 {
219 (void) SP_PARM;
220 return putchar(ch);
221 }
222
223 #if NCURSES_SP_FUNCS
224 NCURSES_EXPORT(int)
_nc_putchar(int ch)225 _nc_putchar(int ch)
226 {
227 return putchar(ch);
228 }
229 #endif
230
231 /*
232 * putp is special - per documentation it calls tputs with putchar as the
233 * parameter for outputting characters. This means that it uses stdio, which
234 * is not signal-safe. Applications call this entrypoint; we do not call it
235 * from within the library.
236 */
237 NCURSES_EXPORT(int)
putp(NCURSES_SP_DCLx const char * string)238 NCURSES_SP_NAME(putp) (NCURSES_SP_DCLx const char *string)
239 {
240 return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
241 string, 1, NCURSES_SP_NAME(_nc_putchar));
242 }
243
244 #if NCURSES_SP_FUNCS
245 NCURSES_EXPORT(int)
putp(const char * string)246 putp(const char *string)
247 {
248 return NCURSES_SP_NAME(putp) (CURRENT_SCREEN, string);
249 }
250 #endif
251
252 /*
253 * Use these entrypoints rather than "putp" within the library.
254 */
255 NCURSES_EXPORT(int)
_nc_putp(NCURSES_SP_DCLx const char * name GCC_UNUSED,const char * string)256 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_DCLx
257 const char *name GCC_UNUSED,
258 const char *string)
259 {
260 int rc = ERR;
261
262 if (string != NULL) {
263 TPUTS_TRACE(name);
264 rc = NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
265 string, 1, NCURSES_SP_NAME(_nc_outch));
266 }
267 return rc;
268 }
269
270 #if NCURSES_SP_FUNCS
271 NCURSES_EXPORT(int)
_nc_putp(const char * name,const char * string)272 _nc_putp(const char *name, const char *string)
273 {
274 return NCURSES_SP_NAME(_nc_putp) (CURRENT_SCREEN, name, string);
275 }
276 #endif
277
278 NCURSES_EXPORT(int)
tputs(NCURSES_SP_DCLx const char * string,int affcnt,NCURSES_SP_OUTC outc)279 NCURSES_SP_NAME(tputs) (NCURSES_SP_DCLx
280 const char *string,
281 int affcnt,
282 NCURSES_SP_OUTC outc)
283 {
284 NCURSES_SP_OUTC my_outch = GetOutCh();
285 bool always_delay = FALSE;
286 bool normal_delay = FALSE;
287 int number;
288 #if BSD_TPUTS
289 int trailpad;
290 #endif /* BSD_TPUTS */
291
292 #ifdef TRACE
293 if (USE_TRACEF(TRACE_TPUTS)) {
294 char addrbuf[32];
295 TR_FUNC_BFR(1);
296
297 if (outc == NCURSES_SP_NAME(_nc_outch)) {
298 _nc_STRCPY(addrbuf, "_nc_outch", sizeof(addrbuf));
299 } else {
300 _nc_SPRINTF(addrbuf, _nc_SLIMIT(sizeof(addrbuf)) "%s",
301 TR_FUNC_ARG(0, outc));
302 }
303 if (_nc_tputs_trace) {
304 _tracef("tputs(%s = %s, %d, %s) called", _nc_tputs_trace,
305 _nc_visbuf(string), affcnt, addrbuf);
306 } else {
307 _tracef("tputs(%s, %d, %s) called", _nc_visbuf(string), affcnt, addrbuf);
308 }
309 TPUTS_TRACE(NULL);
310 _nc_unlock_global(tracef);
311 }
312 #endif /* TRACE */
313
314 if (!VALID_STRING(string))
315 return ERR;
316
317 if (SP_PARM != NULL && HasTInfoTerminal(SP_PARM)) {
318 if (
319 #if NCURSES_SP_FUNCS
320 (SP_PARM != NULL && SP_PARM->_term == NULL)
321 #else
322 cur_term == NULL
323 #endif
324 ) {
325 always_delay = FALSE;
326 normal_delay = TRUE;
327 } else {
328 always_delay = (string == bell) || (string == flash_screen);
329 normal_delay =
330 !xon_xoff
331 && padding_baud_rate
332 #if NCURSES_NO_PADDING
333 && !GetNoPadding(SP_PARM)
334 #endif
335 && (_nc_baudrate(ospeed) >= padding_baud_rate);
336 }
337 }
338 #if BSD_TPUTS
339 /*
340 * This ugly kluge deals with the fact that some ancient BSD programs
341 * (like nethack) actually do the likes of tputs("50") to get delays.
342 */
343 trailpad = 0;
344 if (isdigit(UChar(*string))) {
345 while (isdigit(UChar(*string))) {
346 trailpad = trailpad * 10 + (*string - '0');
347 string++;
348 }
349 trailpad *= 10;
350 if (*string == '.') {
351 string++;
352 if (isdigit(UChar(*string))) {
353 trailpad += (*string - '0');
354 string++;
355 }
356 while (isdigit(UChar(*string)))
357 string++;
358 }
359
360 if (*string == '*') {
361 trailpad *= affcnt;
362 string++;
363 }
364 }
365 #endif /* BSD_TPUTS */
366
367 SetOutCh(outc); /* redirect delay_output() */
368 while (*string) {
369 if (*string != '$')
370 (*outc) (NCURSES_SP_ARGx *string);
371 else {
372 string++;
373 if (*string != '<') {
374 (*outc) (NCURSES_SP_ARGx '$');
375 if (*string)
376 (*outc) (NCURSES_SP_ARGx *string);
377 } else {
378 bool mandatory;
379
380 string++;
381 if ((!isdigit(UChar(*string)) && *string != '.')
382 || !strchr(string, '>')) {
383 (*outc) (NCURSES_SP_ARGx '$');
384 (*outc) (NCURSES_SP_ARGx '<');
385 continue;
386 }
387
388 number = 0;
389 while (isdigit(UChar(*string))) {
390 number = number * 10 + (*string - '0');
391 string++;
392 }
393 number *= 10;
394 if (*string == '.') {
395 string++;
396 if (isdigit(UChar(*string))) {
397 number += (*string - '0');
398 string++;
399 }
400 while (isdigit(UChar(*string)))
401 string++;
402 }
403
404 mandatory = FALSE;
405 while (*string == '*' || *string == '/') {
406 if (*string == '*') {
407 number *= affcnt;
408 string++;
409 } else { /* if (*string == '/') */
410 mandatory = TRUE;
411 string++;
412 }
413 }
414
415 if (number > 0
416 && (always_delay
417 || normal_delay
418 || mandatory))
419 NCURSES_SP_NAME(delay_output) (NCURSES_SP_ARGx number / 10);
420
421 } /* endelse (*string == '<') */
422 } /* endelse (*string == '$') */
423
424 if (*string == '\0')
425 break;
426
427 string++;
428 }
429
430 #if BSD_TPUTS
431 /*
432 * Emit any BSD-style prefix padding that we've accumulated now.
433 */
434 if (trailpad > 0
435 && (always_delay || normal_delay))
436 NCURSES_SP_NAME(delay_output) (NCURSES_SP_ARGx trailpad / 10);
437 #endif /* BSD_TPUTS */
438
439 SetOutCh(my_outch);
440 return OK;
441 }
442
443 #if NCURSES_SP_FUNCS
444 NCURSES_EXPORT(int)
_nc_outc_wrapper(SCREEN * sp,int c)445 _nc_outc_wrapper(SCREEN *sp, int c)
446 {
447 if (NULL == sp) {
448 return fputc(c, stdout);
449 } else {
450 return sp->jump(c);
451 }
452 }
453
454 NCURSES_EXPORT(int)
tputs(const char * string,int affcnt,int (* outc)(int))455 tputs(const char *string, int affcnt, int (*outc) (int))
456 {
457 SetSafeOutcWrapper(outc);
458 return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx string, affcnt, _nc_outc_wrapper);
459 }
460 #endif
461