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