blob: f834532c91936f24fcfb58ef637e73e8d9a5ae1a [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2018-2022,2023 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
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 *
Steve Kondikae271bc2015-11-15 02:50:53 +010034 * and: Juergen Pfeifer 2009 *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053035 ****************************************************************************/
36
37/*
38 * tputs.c
39 * delay_output()
40 * _nc_outch()
41 * tputs()
42 *
43 */
44
45#include <curses.priv.h>
Steve Kondikae271bc2015-11-15 02:50:53 +010046
47#ifndef CUR
48#define CUR SP_TERMTYPE
49#endif
50
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053051#include <ctype.h>
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053052#include <termcap.h> /* ospeed */
53#include <tic.h>
54
micky3879b9f5e72025-07-08 18:04:53 -040055MODULE_ID("$Id: lib_tputs.c,v 1.111 2023/09/16 16:05:15 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053056
57NCURSES_EXPORT_VAR(char) PC = 0; /* used by termcap library */
58NCURSES_EXPORT_VAR(NCURSES_OSPEED) ospeed = 0; /* used by termcap library */
59
micky3879b9f5e72025-07-08 18:04:53 -040060NCURSES_EXPORT_VAR(int) _nc_nulls_sent = 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053061
62#if NCURSES_NO_PADDING
63NCURSES_EXPORT(void)
64_nc_set_no_padding(SCREEN *sp)
65{
66 bool no_padding = (getenv("NCURSES_NO_PADDING") != 0);
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
Steve Kondikae271bc2015-11-15 02:50:53 +010078#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
84static NCURSES_SP_OUTC static_outch = NCURSES_SP_NAME(_nc_outch);
85#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053086
87NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +010088NCURSES_SP_NAME(delay_output) (NCURSES_SP_DCLx int ms)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053089{
Steve Kondikae271bc2015-11-15 02:50:53 +010090 T((T_CALLED("delay_output(%p,%d)"), (void *) SP_PARM, ms));
91
micky3879b9f5e72025-07-08 18:04:53 -040092 if (ms > MAX_DELAY_MSECS)
93 ms = MAX_DELAY_MSECS;
94
Steve Kondikae271bc2015-11-15 02:50:53 +010095 if (!HasTInfoTerminal(SP_PARM))
96 returnCode(ERR);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053097
98 if (no_pad_char) {
Steve Kondikae271bc2015-11-15 02:50:53 +010099 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530100 napms(ms);
101 } else {
Steve Kondikae271bc2015-11-15 02:50:53 +0100102 NCURSES_SP_OUTC my_outch = GetOutCh();
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530103 register int nullcount;
104
105 nullcount = (ms * _nc_baudrate(ospeed)) / (BAUDBYTE * 1000);
106 for (_nc_nulls_sent += nullcount; nullcount > 0; nullcount--)
Steve Kondikae271bc2015-11-15 02:50:53 +0100107 my_outch(NCURSES_SP_ARGx PC);
108 if (my_outch == NCURSES_SP_NAME(_nc_outch))
109 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530110 }
111
112 returnCode(OK);
113}
114
Steve Kondikae271bc2015-11-15 02:50:53 +0100115#if NCURSES_SP_FUNCS
116NCURSES_EXPORT(int)
117delay_output(int ms)
118{
119 return NCURSES_SP_NAME(delay_output) (CURRENT_SCREEN, ms);
120}
121#endif
122
123NCURSES_EXPORT(void)
124NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_DCL0)
125{
micky3879b9f5e72025-07-08 18:04:53 -0400126 T((T_CALLED("_nc_flush(%p)"), (void *) SP_PARM));
Steve Kondikae271bc2015-11-15 02:50:53 +0100127 if (SP_PARM != 0 && SP_PARM->_ofd >= 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400128 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));
Steve Kondikae271bc2015-11-15 02:50:53 +0100132 if (SP_PARM->out_inuse) {
133 char *buf = SP_PARM->out_buffer;
micky3879b9f5e72025-07-08 18:04:53 -0400134 size_t amount = SP_PARM->out_inuse;
Steve Kondikae271bc2015-11-15 02:50:53 +0100135
micky3879b9f5e72025-07-08 18:04:53 -0400136 TR(TRACE_CHARPUT, ("flushing %ld/%ld bytes",
137 (unsigned long) amount, _nc_outchars));
Steve Kondikae271bc2015-11-15 02:50:53 +0100138 while (amount) {
micky3879b9f5e72025-07-08 18:04:53 -0400139 ssize_t res = write(SP_PARM->_ofd, buf, amount);
Steve Kondikae271bc2015-11-15 02:50:53 +0100140 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 }
micky3879b9f5e72025-07-08 18:04:53 -0400152 } else if (SP_PARM->out_buffer == 0) {
153 TR(TRACE_CHARPUT, ("flushing stdout"));
154 fflush(stdout);
Steve Kondikae271bc2015-11-15 02:50:53 +0100155 }
micky3879b9f5e72025-07-08 18:04:53 -0400156 } else {
157 TR(TRACE_CHARPUT, ("flushing stdout"));
158 fflush(stdout);
Steve Kondikae271bc2015-11-15 02:50:53 +0100159 }
micky3879b9f5e72025-07-08 18:04:53 -0400160 if (SP_PARM != 0)
161 SP_PARM->out_inuse = 0;
162 returnVoid;
Steve Kondikae271bc2015-11-15 02:50:53 +0100163}
164
165#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530166NCURSES_EXPORT(void)
167_nc_flush(void)
168{
Steve Kondikae271bc2015-11-15 02:50:53 +0100169 NCURSES_SP_NAME(_nc_flush) (CURRENT_SCREEN);
170}
171#endif
172
173NCURSES_EXPORT(int)
174NCURSES_SP_NAME(_nc_outch) (NCURSES_SP_DCLx int ch)
175{
176 int rc = OK;
177
178 COUNT_OUTCHARS(1);
179
180 if (HasTInfoTerminal(SP_PARM)
181 && SP_PARM != 0) {
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;
186 } else {
187 char tmp = (char) ch;
188 /*
189 * POSIX says write() is safe in a signal handler, but the
190 * buffered I/O is not.
191 */
192 if (write(fileno(NC_OUTPUT(SP_PARM)), &tmp, (size_t) 1) == -1)
193 rc = ERR;
194 }
195 } else {
196 char tmp = (char) ch;
197 if (write(fileno(stdout), &tmp, (size_t) 1) == -1)
198 rc = ERR;
199 }
200 return rc;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530201}
202
Steve Kondikae271bc2015-11-15 02:50:53 +0100203#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530204NCURSES_EXPORT(int)
205_nc_outch(int ch)
206{
Steve Kondikae271bc2015-11-15 02:50:53 +0100207 return NCURSES_SP_NAME(_nc_outch) (CURRENT_SCREEN, ch);
208}
209#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530210
Steve Kondikae271bc2015-11-15 02:50:53 +0100211/*
212 * This is used for the putp special case.
213 */
214NCURSES_EXPORT(int)
215NCURSES_SP_NAME(_nc_putchar) (NCURSES_SP_DCLx int ch)
216{
217 (void) SP_PARM;
218 return putchar(ch);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530219}
220
Steve Kondikae271bc2015-11-15 02:50:53 +0100221#if NCURSES_SP_FUNCS
222NCURSES_EXPORT(int)
223_nc_putchar(int ch)
224{
225 return putchar(ch);
226}
227#endif
228
229/*
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.
234 */
235NCURSES_EXPORT(int)
236NCURSES_SP_NAME(putp) (NCURSES_SP_DCLx const char *string)
237{
238 return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
239 string, 1, NCURSES_SP_NAME(_nc_putchar));
240}
241
242#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530243NCURSES_EXPORT(int)
244putp(const char *string)
245{
Steve Kondikae271bc2015-11-15 02:50:53 +0100246 return NCURSES_SP_NAME(putp) (CURRENT_SCREEN, string);
247}
248#endif
249
250/*
251 * Use these entrypoints rather than "putp" within the library.
252 */
253NCURSES_EXPORT(int)
254NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_DCLx
255 const char *name GCC_UNUSED,
256 const char *string)
257{
258 int rc = ERR;
259
260 if (string != 0) {
261 TPUTS_TRACE(name);
262 rc = NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
263 string, 1, NCURSES_SP_NAME(_nc_outch));
264 }
265 return rc;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530266}
267
Steve Kondikae271bc2015-11-15 02:50:53 +0100268#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530269NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +0100270_nc_putp(const char *name, const char *string)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530271{
Steve Kondikae271bc2015-11-15 02:50:53 +0100272 return NCURSES_SP_NAME(_nc_putp) (CURRENT_SCREEN, name, string);
273}
274#endif
275
276NCURSES_EXPORT(int)
277NCURSES_SP_NAME(tputs) (NCURSES_SP_DCLx
278 const char *string,
279 int affcnt,
280 NCURSES_SP_OUTC outc)
281{
282 NCURSES_SP_OUTC my_outch = GetOutCh();
micky3879b9f5e72025-07-08 18:04:53 -0400283 bool always_delay = FALSE;
284 bool normal_delay = FALSE;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530285 int number;
286#if BSD_TPUTS
287 int trailpad;
288#endif /* BSD_TPUTS */
289
290#ifdef TRACE
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530291 if (USE_TRACEF(TRACE_TPUTS)) {
micky3879b9f5e72025-07-08 18:04:53 -0400292 char addrbuf[32];
293 TR_FUNC_BFR(1);
294
295 if (outc == NCURSES_SP_NAME(_nc_outch)) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100296 _nc_STRCPY(addrbuf, "_nc_outch", sizeof(addrbuf));
micky3879b9f5e72025-07-08 18:04:53 -0400297 } else {
298 _nc_SPRINTF(addrbuf, _nc_SLIMIT(sizeof(addrbuf)) "%s",
299 TR_FUNC_ARG(0, outc));
300 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530301 if (_nc_tputs_trace) {
302 _tracef("tputs(%s = %s, %d, %s) called", _nc_tputs_trace,
303 _nc_visbuf(string), affcnt, addrbuf);
304 } else {
305 _tracef("tputs(%s, %d, %s) called", _nc_visbuf(string), affcnt, addrbuf);
306 }
307 TPUTS_TRACE(NULL);
308 _nc_unlock_global(tracef);
309 }
310#endif /* TRACE */
311
312 if (!VALID_STRING(string))
313 return ERR;
314
micky3879b9f5e72025-07-08 18:04:53 -0400315 if (SP_PARM != 0 && HasTInfoTerminal(SP_PARM)) {
316 if (
Steve Kondikae271bc2015-11-15 02:50:53 +0100317#if NCURSES_SP_FUNCS
micky3879b9f5e72025-07-08 18:04:53 -0400318 (SP_PARM != 0 && SP_PARM->_term == 0)
Steve Kondikae271bc2015-11-15 02:50:53 +0100319#else
micky3879b9f5e72025-07-08 18:04:53 -0400320 cur_term == 0
Steve Kondikae271bc2015-11-15 02:50:53 +0100321#endif
micky3879b9f5e72025-07-08 18:04:53 -0400322 ) {
323 always_delay = FALSE;
324 normal_delay = TRUE;
325 } else {
326 always_delay = (string == bell) || (string == flash_screen);
327 normal_delay =
328 !xon_xoff
329 && padding_baud_rate
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530330#if NCURSES_NO_PADDING
micky3879b9f5e72025-07-08 18:04:53 -0400331 && !GetNoPadding(SP_PARM)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530332#endif
micky3879b9f5e72025-07-08 18:04:53 -0400333 && (_nc_baudrate(ospeed) >= padding_baud_rate);
334 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530335 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530336#if BSD_TPUTS
337 /*
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.
340 */
341 trailpad = 0;
342 if (isdigit(UChar(*string))) {
343 while (isdigit(UChar(*string))) {
344 trailpad = trailpad * 10 + (*string - '0');
345 string++;
346 }
347 trailpad *= 10;
348 if (*string == '.') {
349 string++;
350 if (isdigit(UChar(*string))) {
351 trailpad += (*string - '0');
352 string++;
353 }
354 while (isdigit(UChar(*string)))
355 string++;
356 }
357
358 if (*string == '*') {
359 trailpad *= affcnt;
360 string++;
361 }
362 }
363#endif /* BSD_TPUTS */
364
Steve Kondikae271bc2015-11-15 02:50:53 +0100365 SetOutCh(outc); /* redirect delay_output() */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530366 while (*string) {
367 if (*string != '$')
Steve Kondikae271bc2015-11-15 02:50:53 +0100368 (*outc) (NCURSES_SP_ARGx *string);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530369 else {
370 string++;
371 if (*string != '<') {
Steve Kondikae271bc2015-11-15 02:50:53 +0100372 (*outc) (NCURSES_SP_ARGx '$');
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530373 if (*string)
Steve Kondikae271bc2015-11-15 02:50:53 +0100374 (*outc) (NCURSES_SP_ARGx *string);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530375 } else {
376 bool mandatory;
377
378 string++;
379 if ((!isdigit(UChar(*string)) && *string != '.')
380 || !strchr(string, '>')) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100381 (*outc) (NCURSES_SP_ARGx '$');
382 (*outc) (NCURSES_SP_ARGx '<');
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530383 continue;
384 }
385
386 number = 0;
387 while (isdigit(UChar(*string))) {
388 number = number * 10 + (*string - '0');
389 string++;
390 }
391 number *= 10;
392 if (*string == '.') {
393 string++;
394 if (isdigit(UChar(*string))) {
395 number += (*string - '0');
396 string++;
397 }
398 while (isdigit(UChar(*string)))
399 string++;
400 }
401
402 mandatory = FALSE;
403 while (*string == '*' || *string == '/') {
404 if (*string == '*') {
405 number *= affcnt;
406 string++;
407 } else { /* if (*string == '/') */
408 mandatory = TRUE;
409 string++;
410 }
411 }
412
413 if (number > 0
414 && (always_delay
415 || normal_delay
416 || mandatory))
Steve Kondikae271bc2015-11-15 02:50:53 +0100417 NCURSES_SP_NAME(delay_output) (NCURSES_SP_ARGx number / 10);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530418
419 } /* endelse (*string == '<') */
420 } /* endelse (*string == '$') */
421
422 if (*string == '\0')
423 break;
424
425 string++;
426 }
427
428#if BSD_TPUTS
429 /*
430 * Emit any BSD-style prefix padding that we've accumulated now.
431 */
432 if (trailpad > 0
433 && (always_delay || normal_delay))
micky3879b9f5e72025-07-08 18:04:53 -0400434 NCURSES_SP_NAME(delay_output) (NCURSES_SP_ARGx trailpad / 10);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530435#endif /* BSD_TPUTS */
436
Steve Kondikae271bc2015-11-15 02:50:53 +0100437 SetOutCh(my_outch);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530438 return OK;
439}
Steve Kondikae271bc2015-11-15 02:50:53 +0100440
441#if NCURSES_SP_FUNCS
442NCURSES_EXPORT(int)
443_nc_outc_wrapper(SCREEN *sp, int c)
444{
445 if (0 == sp) {
micky3879b9f5e72025-07-08 18:04:53 -0400446 return fputc(c, stdout);
Steve Kondikae271bc2015-11-15 02:50:53 +0100447 } else {
448 return sp->jump(c);
449 }
450}
451
452NCURSES_EXPORT(int)
453tputs(const char *string, int affcnt, int (*outc) (int))
454{
455 SetSafeOutcWrapper(outc);
456 return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx string, affcnt, _nc_outc_wrapper);
457}
458#endif