]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_tputs.c
ncurses 6.1 - patch 20191214
[ncurses.git] / ncurses / tinfo / lib_tputs.c
1 /****************************************************************************
2  * Copyright (c) 1998-2018,2019 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  *     and: Thomas E. Dickey                        1996-on                 *
33  *     and: Juergen Pfeifer                         2009                    *
34  ****************************************************************************/
35
36 /*
37  *      tputs.c
38  *              delay_output()
39  *              _nc_outch()
40  *              tputs()
41  *
42  */
43
44 #include <curses.priv.h>
45
46 #ifndef CUR
47 #define CUR SP_TERMTYPE
48 #endif
49
50 #include <ctype.h>
51 #include <termcap.h>            /* ospeed */
52 #include <tic.h>
53
54 MODULE_ID("$Id: lib_tputs.c,v 1.102 2019/03/17 21:57:04 tom Exp $")
55
56 NCURSES_EXPORT_VAR(char) PC = 0;              /* used by termcap library */
57 NCURSES_EXPORT_VAR(NCURSES_OSPEED) ospeed = 0;        /* used by termcap library */
58
59 NCURSES_EXPORT_VAR(int) _nc_nulls_sent = 0;   /* used by 'tack' program */
60
61 #if NCURSES_NO_PADDING
62 NCURSES_EXPORT(void)
63 _nc_set_no_padding(SCREEN *sp)
64 {
65     bool no_padding = (getenv("NCURSES_NO_PADDING") != 0);
66
67     if (sp)
68         sp->_no_padding = no_padding;
69     else
70         _nc_prescreen._no_padding = no_padding;
71
72     TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used",
73                                     GetNoPadding(sp) ? " not" : ""));
74 }
75 #endif
76
77 #if NCURSES_SP_FUNCS
78 #define SetOutCh(func) if (SP_PARM) SP_PARM->_outch = func; else _nc_prescreen._outch = func
79 #define GetOutCh()     (SP_PARM ? SP_PARM->_outch : _nc_prescreen._outch)
80 #else
81 #define SetOutCh(func) static_outch = func
82 #define GetOutCh()     static_outch
83 static NCURSES_SP_OUTC static_outch = NCURSES_SP_NAME(_nc_outch);
84 #endif
85
86 NCURSES_EXPORT(int)
87 NCURSES_SP_NAME(delay_output) (NCURSES_SP_DCLx int ms)
88 {
89     T((T_CALLED("delay_output(%p,%d)"), (void *) SP_PARM, ms));
90
91     if (!HasTInfoTerminal(SP_PARM))
92         returnCode(ERR);
93
94     if (no_pad_char) {
95         NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
96         napms(ms);
97     } else {
98         NCURSES_SP_OUTC my_outch = GetOutCh();
99         register int nullcount;
100
101         nullcount = (ms * _nc_baudrate(ospeed)) / (BAUDBYTE * 1000);
102         for (_nc_nulls_sent += nullcount; nullcount > 0; nullcount--)
103             my_outch(NCURSES_SP_ARGx PC);
104         if (my_outch == NCURSES_SP_NAME(_nc_outch))
105             NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
106     }
107
108     returnCode(OK);
109 }
110
111 #if NCURSES_SP_FUNCS
112 NCURSES_EXPORT(int)
113 delay_output(int ms)
114 {
115     return NCURSES_SP_NAME(delay_output) (CURRENT_SCREEN, ms);
116 }
117 #endif
118
119 NCURSES_EXPORT(void)
120 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_DCL0)
121 {
122     if (SP_PARM != 0 && SP_PARM->_ofd >= 0) {
123         if (SP_PARM->out_inuse) {
124             char *buf = SP_PARM->out_buffer;
125             size_t amount = SP->out_inuse;
126
127             SP->out_inuse = 0;
128             TR(TRACE_CHARPUT, ("flushing %ld bytes", (unsigned long) amount));
129             while (amount) {
130                 ssize_t res = write(SP_PARM->_ofd, buf, amount);
131
132                 if (res > 0) {
133                     /* if the write was incomplete, try again */
134                     amount -= (size_t) res;
135                     buf += res;
136                 } else if (errno == EAGAIN) {
137                     continue;
138                 } else if (errno == EINTR) {
139                     continue;
140                 } else {
141                     break;      /* an error we can not recover from */
142                 }
143             }
144         }
145     } else {
146         fflush(stdout);
147     }
148 }
149
150 #if NCURSES_SP_FUNCS
151 NCURSES_EXPORT(void)
152 _nc_flush(void)
153 {
154     NCURSES_SP_NAME(_nc_flush) (CURRENT_SCREEN);
155 }
156 #endif
157
158 NCURSES_EXPORT(int)
159 NCURSES_SP_NAME(_nc_outch) (NCURSES_SP_DCLx int ch)
160 {
161     int rc = OK;
162
163     COUNT_OUTCHARS(1);
164
165     if (HasTInfoTerminal(SP_PARM)
166         && SP_PARM != 0) {
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;
171         } else {
172             char tmp = (char) ch;
173             /*
174              * POSIX says write() is safe in a signal handler, but the
175              * buffered I/O is not.
176              */
177             if (write(fileno(NC_OUTPUT(SP_PARM)), &tmp, (size_t) 1) == -1)
178                 rc = ERR;
179         }
180     } else {
181         char tmp = (char) ch;
182         if (write(fileno(stdout), &tmp, (size_t) 1) == -1)
183             rc = ERR;
184     }
185     return rc;
186 }
187
188 #if NCURSES_SP_FUNCS
189 NCURSES_EXPORT(int)
190 _nc_outch(int ch)
191 {
192     return NCURSES_SP_NAME(_nc_outch) (CURRENT_SCREEN, ch);
193 }
194 #endif
195
196 /*
197  * This is used for the putp special case.
198  */
199 NCURSES_EXPORT(int)
200 NCURSES_SP_NAME(_nc_putchar) (NCURSES_SP_DCLx int ch)
201 {
202     (void) SP_PARM;
203     return putchar(ch);
204 }
205
206 #if NCURSES_SP_FUNCS
207 NCURSES_EXPORT(int)
208 _nc_putchar(int ch)
209 {
210     return putchar(ch);
211 }
212 #endif
213
214 /*
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.
219  */
220 NCURSES_EXPORT(int)
221 NCURSES_SP_NAME(putp) (NCURSES_SP_DCLx const char *string)
222 {
223     return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
224                                    string, 1, NCURSES_SP_NAME(_nc_putchar));
225 }
226
227 #if NCURSES_SP_FUNCS
228 NCURSES_EXPORT(int)
229 putp(const char *string)
230 {
231     return NCURSES_SP_NAME(putp) (CURRENT_SCREEN, string);
232 }
233 #endif
234
235 /*
236  * Use these entrypoints rather than "putp" within the library.
237  */
238 NCURSES_EXPORT(int)
239 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_DCLx
240                            const char *name GCC_UNUSED,
241                            const char *string)
242 {
243     int rc = ERR;
244
245     if (string != 0) {
246         TPUTS_TRACE(name);
247         rc = NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
248                                      string, 1, NCURSES_SP_NAME(_nc_outch));
249     }
250     return rc;
251 }
252
253 #if NCURSES_SP_FUNCS
254 NCURSES_EXPORT(int)
255 _nc_putp(const char *name, const char *string)
256 {
257     return NCURSES_SP_NAME(_nc_putp) (CURRENT_SCREEN, name, string);
258 }
259 #endif
260
261 NCURSES_EXPORT(int)
262 NCURSES_SP_NAME(tputs) (NCURSES_SP_DCLx
263                         const char *string,
264                         int affcnt,
265                         NCURSES_SP_OUTC outc)
266 {
267     NCURSES_SP_OUTC my_outch = GetOutCh();
268     bool always_delay;
269     bool normal_delay;
270     int number;
271 #if BSD_TPUTS
272     int trailpad;
273 #endif /* BSD_TPUTS */
274
275 #ifdef TRACE
276     if (USE_TRACEF(TRACE_TPUTS)) {
277         char addrbuf[32];
278         TR_FUNC_BFR(1);
279
280         if (outc == NCURSES_SP_NAME(_nc_outch)) {
281             _nc_STRCPY(addrbuf, "_nc_outch", sizeof(addrbuf));
282         } else {
283             _nc_SPRINTF(addrbuf, _nc_SLIMIT(sizeof(addrbuf)) "%s",
284                         TR_FUNC_ARG(0, outc));
285         }
286         if (_nc_tputs_trace) {
287             _tracef("tputs(%s = %s, %d, %s) called", _nc_tputs_trace,
288                     _nc_visbuf(string), affcnt, addrbuf);
289         } else {
290             _tracef("tputs(%s, %d, %s) called", _nc_visbuf(string), affcnt, addrbuf);
291         }
292         TPUTS_TRACE(NULL);
293         _nc_unlock_global(tracef);
294     }
295 #endif /* TRACE */
296
297     if (SP_PARM != 0 && !HasTInfoTerminal(SP_PARM))
298         return ERR;
299
300     if (!VALID_STRING(string))
301         return ERR;
302
303     if (
304 #if NCURSES_SP_FUNCS
305            (SP_PARM != 0 && SP_PARM->_term == 0)
306 #else
307            cur_term == 0
308 #endif
309         ) {
310         always_delay = FALSE;
311         normal_delay = TRUE;
312     } else {
313         always_delay = (string == bell) || (string == flash_screen);
314         normal_delay =
315             !xon_xoff
316             && padding_baud_rate
317 #if NCURSES_NO_PADDING
318             && !GetNoPadding(SP_PARM)
319 #endif
320             && (_nc_baudrate(ospeed) >= padding_baud_rate);
321     }
322
323 #if BSD_TPUTS
324     /*
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.
327      */
328     trailpad = 0;
329     if (isdigit(UChar(*string))) {
330         while (isdigit(UChar(*string))) {
331             trailpad = trailpad * 10 + (*string - '0');
332             string++;
333         }
334         trailpad *= 10;
335         if (*string == '.') {
336             string++;
337             if (isdigit(UChar(*string))) {
338                 trailpad += (*string - '0');
339                 string++;
340             }
341             while (isdigit(UChar(*string)))
342                 string++;
343         }
344
345         if (*string == '*') {
346             trailpad *= affcnt;
347             string++;
348         }
349     }
350 #endif /* BSD_TPUTS */
351
352     SetOutCh(outc);             /* redirect delay_output() */
353     while (*string) {
354         if (*string != '$')
355             (*outc) (NCURSES_SP_ARGx *string);
356         else {
357             string++;
358             if (*string != '<') {
359                 (*outc) (NCURSES_SP_ARGx '$');
360                 if (*string)
361                     (*outc) (NCURSES_SP_ARGx *string);
362             } else {
363                 bool mandatory;
364
365                 string++;
366                 if ((!isdigit(UChar(*string)) && *string != '.')
367                     || !strchr(string, '>')) {
368                     (*outc) (NCURSES_SP_ARGx '$');
369                     (*outc) (NCURSES_SP_ARGx '<');
370                     continue;
371                 }
372
373                 number = 0;
374                 while (isdigit(UChar(*string))) {
375                     number = number * 10 + (*string - '0');
376                     string++;
377                 }
378                 number *= 10;
379                 if (*string == '.') {
380                     string++;
381                     if (isdigit(UChar(*string))) {
382                         number += (*string - '0');
383                         string++;
384                     }
385                     while (isdigit(UChar(*string)))
386                         string++;
387                 }
388
389                 mandatory = FALSE;
390                 while (*string == '*' || *string == '/') {
391                     if (*string == '*') {
392                         number *= affcnt;
393                         string++;
394                     } else {    /* if (*string == '/') */
395                         mandatory = TRUE;
396                         string++;
397                     }
398                 }
399
400                 if (number > 0
401                     && (always_delay
402                         || normal_delay
403                         || mandatory))
404                     NCURSES_SP_NAME(delay_output) (NCURSES_SP_ARGx number / 10);
405
406             }                   /* endelse (*string == '<') */
407         }                       /* endelse (*string == '$') */
408
409         if (*string == '\0')
410             break;
411
412         string++;
413     }
414
415 #if BSD_TPUTS
416     /*
417      * Emit any BSD-style prefix padding that we've accumulated now.
418      */
419     if (trailpad > 0
420         && (always_delay || normal_delay))
421         delay_output(trailpad / 10);
422 #endif /* BSD_TPUTS */
423
424     SetOutCh(my_outch);
425     return OK;
426 }
427
428 #if NCURSES_SP_FUNCS
429 NCURSES_EXPORT(int)
430 _nc_outc_wrapper(SCREEN *sp, int c)
431 {
432     if (0 == sp) {
433         return fputc(c, stdout);
434     } else {
435         return sp->jump(c);
436     }
437 }
438
439 NCURSES_EXPORT(int)
440 tputs(const char *string, int affcnt, int (*outc) (int))
441 {
442     SetSafeOutcWrapper(outc);
443     return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx string, affcnt, _nc_outc_wrapper);
444 }
445 #endif