1 /****************************************************************************
2 * Copyright 2018-2022,2023 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
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 ****************************************************************************/
45 #include <curses.priv.h>
48 #define CUR SP_TERMTYPE
52 #include <termcap.h> /* ospeed */
55 MODULE_ID("$Id: lib_tputs.c,v 1.111 2023/09/16 16:05:15 tom Exp $")
57 NCURSES_EXPORT_VAR(char) PC = 0; /* used by termcap library */
58 NCURSES_EXPORT_VAR(NCURSES_OSPEED) ospeed = 0; /* used by termcap library */
60 NCURSES_EXPORT_VAR(int) _nc_nulls_sent = 0;
62 #if NCURSES_NO_PADDING
64 _nc_set_no_padding(SCREEN *sp)
66 bool no_padding = (getenv("NCURSES_NO_PADDING") != 0);
69 sp->_no_padding = no_padding;
71 _nc_prescreen._no_padding = no_padding;
73 TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used",
74 GetNoPadding(sp) ? " not" : ""));
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)
82 #define SetOutCh(func) static_outch = func
83 #define GetOutCh() static_outch
84 static NCURSES_SP_OUTC static_outch = NCURSES_SP_NAME(_nc_outch);
88 NCURSES_SP_NAME(delay_output) (NCURSES_SP_DCLx int ms)
90 T((T_CALLED("delay_output(%p,%d)"), (void *) SP_PARM, ms));
92 if (ms > MAX_DELAY_MSECS)
95 if (!HasTInfoTerminal(SP_PARM))
99 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
102 NCURSES_SP_OUTC my_outch = GetOutCh();
103 register int nullcount;
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);
119 return NCURSES_SP_NAME(delay_output) (CURRENT_SCREEN, ms);
124 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_DCL0)
126 T((T_CALLED("_nc_flush(%p)"), (void *) SP_PARM));
127 if (SP_PARM != 0 && SP_PARM->_ofd >= 0) {
128 TR(TRACE_CHARPUT, ("ofd:%d inuse:%lu buffer:%p",
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;
136 TR(TRACE_CHARPUT, ("flushing %ld/%ld bytes",
137 (unsigned long) amount, _nc_outchars));
139 ssize_t res = write(SP_PARM->_ofd, buf, amount);
141 /* if the write was incomplete, try again */
142 amount -= (size_t) res;
144 } else if (errno == EAGAIN) {
146 } else if (errno == EINTR) {
149 break; /* an error we can not recover from */
152 } else if (SP_PARM->out_buffer == 0) {
153 TR(TRACE_CHARPUT, ("flushing stdout"));
157 TR(TRACE_CHARPUT, ("flushing stdout"));
161 SP_PARM->out_inuse = 0;
169 NCURSES_SP_NAME(_nc_flush) (CURRENT_SCREEN);
174 NCURSES_SP_NAME(_nc_outch) (NCURSES_SP_DCLx int ch)
180 if (HasTInfoTerminal(SP_PARM)
182 if (SP_PARM->out_buffer != 0) {
183 if (SP_PARM->out_inuse + 1 >= SP_PARM->out_limit)
184 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
185 SP_PARM->out_buffer[SP_PARM->out_inuse++] = (char) ch;
187 char tmp = (char) ch;
189 * POSIX says write() is safe in a signal handler, but the
190 * buffered I/O is not.
192 if (write(fileno(NC_OUTPUT(SP_PARM)), &tmp, (size_t) 1) == -1)
196 char tmp = (char) ch;
197 if (write(fileno(stdout), &tmp, (size_t) 1) == -1)
207 return NCURSES_SP_NAME(_nc_outch) (CURRENT_SCREEN, ch);
212 * This is used for the putp special case.
215 NCURSES_SP_NAME(_nc_putchar) (NCURSES_SP_DCLx int ch)
230 * putp is special - per documentation it calls tputs with putchar as the
231 * parameter for outputting characters. This means that it uses stdio, which
232 * is not signal-safe. Applications call this entrypoint; we do not call it
233 * from within the library.
236 NCURSES_SP_NAME(putp) (NCURSES_SP_DCLx const char *string)
238 return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
239 string, 1, NCURSES_SP_NAME(_nc_putchar));
244 putp(const char *string)
246 return NCURSES_SP_NAME(putp) (CURRENT_SCREEN, string);
251 * Use these entrypoints rather than "putp" within the library.
254 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_DCLx
255 const char *name GCC_UNUSED,
262 rc = NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
263 string, 1, NCURSES_SP_NAME(_nc_outch));
270 _nc_putp(const char *name, const char *string)
272 return NCURSES_SP_NAME(_nc_putp) (CURRENT_SCREEN, name, string);
277 NCURSES_SP_NAME(tputs) (NCURSES_SP_DCLx
280 NCURSES_SP_OUTC outc)
282 NCURSES_SP_OUTC my_outch = GetOutCh();
283 bool always_delay = FALSE;
284 bool normal_delay = FALSE;
288 #endif /* BSD_TPUTS */
291 if (USE_TRACEF(TRACE_TPUTS)) {
295 if (outc == NCURSES_SP_NAME(_nc_outch)) {
296 _nc_STRCPY(addrbuf, "_nc_outch", sizeof(addrbuf));
298 _nc_SPRINTF(addrbuf, _nc_SLIMIT(sizeof(addrbuf)) "%s",
299 TR_FUNC_ARG(0, outc));
301 if (_nc_tputs_trace) {
302 _tracef("tputs(%s = %s, %d, %s) called", _nc_tputs_trace,
303 _nc_visbuf(string), affcnt, addrbuf);
305 _tracef("tputs(%s, %d, %s) called", _nc_visbuf(string), affcnt, addrbuf);
308 _nc_unlock_global(tracef);
312 if (!VALID_STRING(string))
315 if (SP_PARM != 0 && HasTInfoTerminal(SP_PARM)) {
318 (SP_PARM != 0 && SP_PARM->_term == 0)
323 always_delay = FALSE;
326 always_delay = (string == bell) || (string == flash_screen);
330 #if NCURSES_NO_PADDING
331 && !GetNoPadding(SP_PARM)
333 && (_nc_baudrate(ospeed) >= padding_baud_rate);
338 * This ugly kluge deals with the fact that some ancient BSD programs
339 * (like nethack) actually do the likes of tputs("50") to get delays.
342 if (isdigit(UChar(*string))) {
343 while (isdigit(UChar(*string))) {
344 trailpad = trailpad * 10 + (*string - '0');
348 if (*string == '.') {
350 if (isdigit(UChar(*string))) {
351 trailpad += (*string - '0');
354 while (isdigit(UChar(*string)))
358 if (*string == '*') {
363 #endif /* BSD_TPUTS */
365 SetOutCh(outc); /* redirect delay_output() */
368 (*outc) (NCURSES_SP_ARGx *string);
371 if (*string != '<') {
372 (*outc) (NCURSES_SP_ARGx '$');
374 (*outc) (NCURSES_SP_ARGx *string);
379 if ((!isdigit(UChar(*string)) && *string != '.')
380 || !strchr(string, '>')) {
381 (*outc) (NCURSES_SP_ARGx '$');
382 (*outc) (NCURSES_SP_ARGx '<');
387 while (isdigit(UChar(*string))) {
388 number = number * 10 + (*string - '0');
392 if (*string == '.') {
394 if (isdigit(UChar(*string))) {
395 number += (*string - '0');
398 while (isdigit(UChar(*string)))
403 while (*string == '*' || *string == '/') {
404 if (*string == '*') {
407 } else { /* if (*string == '/') */
417 NCURSES_SP_NAME(delay_output) (NCURSES_SP_ARGx number / 10);
419 } /* endelse (*string == '<') */
420 } /* endelse (*string == '$') */
430 * Emit any BSD-style prefix padding that we've accumulated now.
433 && (always_delay || normal_delay))
434 NCURSES_SP_NAME(delay_output) (NCURSES_SP_ARGx trailpad / 10);
435 #endif /* BSD_TPUTS */
443 _nc_outc_wrapper(SCREEN *sp, int c)
446 return fputc(c, stdout);
453 tputs(const char *string, int affcnt, int (*outc) (int))
455 SetSafeOutcWrapper(outc);
456 return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx string, affcnt, _nc_outc_wrapper);