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