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