1 /****************************************************************************
2 * Copyright 2018-2019,2020 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.104 2020/09/05 18:08:11 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; /* used by 'tack' program */
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 (!HasTInfoTerminal(SP_PARM))
96 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
99 NCURSES_SP_OUTC my_outch = GetOutCh();
100 register int nullcount;
102 nullcount = (ms * _nc_baudrate(ospeed)) / (BAUDBYTE * 1000);
103 for (_nc_nulls_sent += nullcount; nullcount > 0; nullcount--)
104 my_outch(NCURSES_SP_ARGx PC);
105 if (my_outch == NCURSES_SP_NAME(_nc_outch))
106 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
116 return NCURSES_SP_NAME(delay_output) (CURRENT_SCREEN, ms);
121 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_DCL0)
123 if (SP_PARM != 0 && SP_PARM->_ofd >= 0) {
124 if (SP_PARM->out_inuse) {
125 char *buf = SP_PARM->out_buffer;
126 size_t amount = SP->out_inuse;
129 TR(TRACE_CHARPUT, ("flushing %ld bytes", (unsigned long) amount));
131 ssize_t res = write(SP_PARM->_ofd, buf, amount);
133 /* if the write was incomplete, try again */
134 amount -= (size_t) res;
136 } else if (errno == EAGAIN) {
138 } else if (errno == EINTR) {
141 break; /* an error we can not recover from */
154 NCURSES_SP_NAME(_nc_flush) (CURRENT_SCREEN);
159 NCURSES_SP_NAME(_nc_outch) (NCURSES_SP_DCLx int ch)
165 if (HasTInfoTerminal(SP_PARM)
167 if (SP_PARM->out_buffer != 0) {
168 if (SP_PARM->out_inuse + 1 >= SP_PARM->out_limit)
169 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
170 SP_PARM->out_buffer[SP_PARM->out_inuse++] = (char) ch;
172 char tmp = (char) ch;
174 * POSIX says write() is safe in a signal handler, but the
175 * buffered I/O is not.
177 if (write(fileno(NC_OUTPUT(SP_PARM)), &tmp, (size_t) 1) == -1)
181 char tmp = (char) ch;
182 if (write(fileno(stdout), &tmp, (size_t) 1) == -1)
192 return NCURSES_SP_NAME(_nc_outch) (CURRENT_SCREEN, ch);
197 * This is used for the putp special case.
200 NCURSES_SP_NAME(_nc_putchar) (NCURSES_SP_DCLx int ch)
215 * putp is special - per documentation it calls tputs with putchar as the
216 * parameter for outputting characters. This means that it uses stdio, which
217 * is not signal-safe. Applications call this entrypoint; we do not call it
218 * from within the library.
221 NCURSES_SP_NAME(putp) (NCURSES_SP_DCLx const char *string)
223 return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
224 string, 1, NCURSES_SP_NAME(_nc_putchar));
229 putp(const char *string)
231 return NCURSES_SP_NAME(putp) (CURRENT_SCREEN, string);
236 * Use these entrypoints rather than "putp" within the library.
239 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_DCLx
240 const char *name GCC_UNUSED,
247 rc = NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
248 string, 1, NCURSES_SP_NAME(_nc_outch));
255 _nc_putp(const char *name, const char *string)
257 return NCURSES_SP_NAME(_nc_putp) (CURRENT_SCREEN, name, string);
262 NCURSES_SP_NAME(tputs) (NCURSES_SP_DCLx
265 NCURSES_SP_OUTC outc)
267 NCURSES_SP_OUTC my_outch = GetOutCh();
273 #endif /* BSD_TPUTS */
276 if (USE_TRACEF(TRACE_TPUTS)) {
280 if (outc == NCURSES_SP_NAME(_nc_outch)) {
281 _nc_STRCPY(addrbuf, "_nc_outch", sizeof(addrbuf));
283 _nc_SPRINTF(addrbuf, _nc_SLIMIT(sizeof(addrbuf)) "%s",
284 TR_FUNC_ARG(0, outc));
286 if (_nc_tputs_trace) {
287 _tracef("tputs(%s = %s, %d, %s) called", _nc_tputs_trace,
288 _nc_visbuf(string), affcnt, addrbuf);
290 _tracef("tputs(%s, %d, %s) called", _nc_visbuf(string), affcnt, addrbuf);
293 _nc_unlock_global(tracef);
297 if (SP_PARM != 0 && !HasTInfoTerminal(SP_PARM))
300 if (!VALID_STRING(string))
305 (SP_PARM != 0 && SP_PARM->_term == 0)
310 always_delay = FALSE;
313 always_delay = (string == bell) || (string == flash_screen);
317 #if NCURSES_NO_PADDING
318 && !GetNoPadding(SP_PARM)
320 && (_nc_baudrate(ospeed) >= padding_baud_rate);
325 * This ugly kluge deals with the fact that some ancient BSD programs
326 * (like nethack) actually do the likes of tputs("50") to get delays.
329 if (isdigit(UChar(*string))) {
330 while (isdigit(UChar(*string))) {
331 trailpad = trailpad * 10 + (*string - '0');
335 if (*string == '.') {
337 if (isdigit(UChar(*string))) {
338 trailpad += (*string - '0');
341 while (isdigit(UChar(*string)))
345 if (*string == '*') {
350 #endif /* BSD_TPUTS */
352 SetOutCh(outc); /* redirect delay_output() */
355 (*outc) (NCURSES_SP_ARGx *string);
358 if (*string != '<') {
359 (*outc) (NCURSES_SP_ARGx '$');
361 (*outc) (NCURSES_SP_ARGx *string);
366 if ((!isdigit(UChar(*string)) && *string != '.')
367 || !strchr(string, '>')) {
368 (*outc) (NCURSES_SP_ARGx '$');
369 (*outc) (NCURSES_SP_ARGx '<');
374 while (isdigit(UChar(*string))) {
375 number = number * 10 + (*string - '0');
379 if (*string == '.') {
381 if (isdigit(UChar(*string))) {
382 number += (*string - '0');
385 while (isdigit(UChar(*string)))
390 while (*string == '*' || *string == '/') {
391 if (*string == '*') {
394 } else { /* if (*string == '/') */
404 NCURSES_SP_NAME(delay_output) (NCURSES_SP_ARGx number / 10);
406 } /* endelse (*string == '<') */
407 } /* endelse (*string == '$') */
417 * Emit any BSD-style prefix padding that we've accumulated now.
420 && (always_delay || normal_delay))
421 delay_output(trailpad / 10);
422 #endif /* BSD_TPUTS */
430 _nc_outc_wrapper(SCREEN *sp, int c)
433 return fputc(c, stdout);
440 tputs(const char *string, int affcnt, int (*outc) (int))
442 SetSafeOutcWrapper(outc);
443 return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx string, affcnt, _nc_outc_wrapper);