]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_tputs.c
1c4a4fe838874de7b146527cdc8f0b21bbe22527
[ncurses.git] / ncurses / tinfo / lib_tputs.c
1 /****************************************************************************
2  * Copyright (c) 1998-2011,2012 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.86 2012/08/25 21:22:08 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->_ofd >= 0) {
123         if (SP_PARM->out_inuse) {
124             size_t amount = SP->out_inuse;
125             /*
126              * Help a little, if the write is interrupted, by first resetting
127              * our amount.
128              */
129             SP->out_inuse = 0;
130             (void) write(SP_PARM->_ofd, SP_PARM->out_buffer, amount);
131         }
132     }
133 }
134
135 #if NCURSES_SP_FUNCS
136 NCURSES_EXPORT(void)
137 _nc_flush(void)
138 {
139     NCURSES_SP_NAME(_nc_flush) (CURRENT_SCREEN);
140 }
141 #endif
142
143 NCURSES_EXPORT(int)
144 NCURSES_SP_NAME(_nc_outch) (NCURSES_SP_DCLx int ch)
145 {
146     int rc = OK;
147
148     COUNT_OUTCHARS(1);
149
150     if (HasTInfoTerminal(SP_PARM)
151         && SP_PARM != 0) {
152         if (SP_PARM->out_buffer != 0) {
153             if (SP_PARM->out_inuse + 1 >= SP_PARM->out_limit)
154                 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
155             SP_PARM->out_buffer[SP_PARM->out_inuse++] = (char) ch;
156         } else {
157             char tmp = (char) ch;
158             /*
159              * POSIX says write() is safe in a signal handler, but the
160              * buffered I/O is not.
161              */
162             if (write(fileno(NC_OUTPUT(SP_PARM)), &tmp, (size_t) 1) == -1)
163                 rc = ERR;
164         }
165     } else {
166         char tmp = (char) ch;
167         if (write(fileno(stdout), &tmp, (size_t) 1) == -1)
168             rc = ERR;
169     }
170     return rc;
171 }
172
173 #if NCURSES_SP_FUNCS
174 NCURSES_EXPORT(int)
175 _nc_outch(int ch)
176 {
177     return NCURSES_SP_NAME(_nc_outch) (CURRENT_SCREEN, ch);
178 }
179 #endif
180
181 NCURSES_EXPORT(int)
182 NCURSES_SP_NAME(putp) (NCURSES_SP_DCLx const char *string)
183 {
184     return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
185                                    string, 1, NCURSES_SP_NAME(_nc_outch));
186 }
187
188 NCURSES_EXPORT(int)
189 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_DCLx
190                            const char *name GCC_UNUSED,
191                            const char *string)
192 {
193     int rc = ERR;
194
195     if (string != 0) {
196         TPUTS_TRACE(name);
197         rc = NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx string);
198     }
199     return rc;
200 }
201
202 #if NCURSES_SP_FUNCS
203 NCURSES_EXPORT(int)
204 putp(const char *string)
205 {
206     return NCURSES_SP_NAME(putp) (CURRENT_SCREEN, string);
207 }
208
209 NCURSES_EXPORT(int)
210 _nc_putp(const char *name, const char *string)
211 {
212     return NCURSES_SP_NAME(_nc_putp) (CURRENT_SCREEN, name, string);
213 }
214 #endif
215
216 NCURSES_EXPORT(int)
217 NCURSES_SP_NAME(tputs) (NCURSES_SP_DCLx
218                         const char *string,
219                         int affcnt,
220                         NCURSES_SP_OUTC outc)
221 {
222     NCURSES_SP_OUTC my_outch = GetOutCh();
223     bool always_delay;
224     bool normal_delay;
225     int number;
226 #if BSD_TPUTS
227     int trailpad;
228 #endif /* BSD_TPUTS */
229
230 #ifdef TRACE
231     char addrbuf[32];
232
233     if (USE_TRACEF(TRACE_TPUTS)) {
234         if (outc == NCURSES_SP_NAME(_nc_outch))
235             _nc_STRCPY(addrbuf, "_nc_outch", sizeof(addrbuf));
236         else
237             _nc_SPRINTF(addrbuf, _nc_SLIMIT(sizeof(addrbuf)) "%p", outc);
238         if (_nc_tputs_trace) {
239             _tracef("tputs(%s = %s, %d, %s) called", _nc_tputs_trace,
240                     _nc_visbuf(string), affcnt, addrbuf);
241         } else {
242             _tracef("tputs(%s, %d, %s) called", _nc_visbuf(string), affcnt, addrbuf);
243         }
244         TPUTS_TRACE(NULL);
245         _nc_unlock_global(tracef);
246     }
247 #endif /* TRACE */
248
249     if (SP_PARM != 0 && !HasTInfoTerminal(SP_PARM))
250         return ERR;
251
252     if (!VALID_STRING(string))
253         return ERR;
254
255     if (
256 #if NCURSES_SP_FUNCS
257            (SP_PARM != 0 && SP_PARM->_term == 0)
258 #else
259            cur_term == 0
260 #endif
261         ) {
262         always_delay = FALSE;
263         normal_delay = TRUE;
264     } else {
265         always_delay = (string == bell) || (string == flash_screen);
266         normal_delay =
267             !xon_xoff
268             && padding_baud_rate
269 #if NCURSES_NO_PADDING
270             && !GetNoPadding(SP_PARM)
271 #endif
272             && (_nc_baudrate(ospeed) >= padding_baud_rate);
273     }
274
275 #if BSD_TPUTS
276     /*
277      * This ugly kluge deals with the fact that some ancient BSD programs
278      * (like nethack) actually do the likes of tputs("50") to get delays.
279      */
280     trailpad = 0;
281     if (isdigit(UChar(*string))) {
282         while (isdigit(UChar(*string))) {
283             trailpad = trailpad * 10 + (*string - '0');
284             string++;
285         }
286         trailpad *= 10;
287         if (*string == '.') {
288             string++;
289             if (isdigit(UChar(*string))) {
290                 trailpad += (*string - '0');
291                 string++;
292             }
293             while (isdigit(UChar(*string)))
294                 string++;
295         }
296
297         if (*string == '*') {
298             trailpad *= affcnt;
299             string++;
300         }
301     }
302 #endif /* BSD_TPUTS */
303
304     SetOutCh(outc);             /* redirect delay_output() */
305     while (*string) {
306         if (*string != '$')
307             (*outc) (NCURSES_SP_ARGx *string);
308         else {
309             string++;
310             if (*string != '<') {
311                 (*outc) (NCURSES_SP_ARGx '$');
312                 if (*string)
313                     (*outc) (NCURSES_SP_ARGx *string);
314             } else {
315                 bool mandatory;
316
317                 string++;
318                 if ((!isdigit(UChar(*string)) && *string != '.')
319                     || !strchr(string, '>')) {
320                     (*outc) (NCURSES_SP_ARGx '$');
321                     (*outc) (NCURSES_SP_ARGx '<');
322                     continue;
323                 }
324
325                 number = 0;
326                 while (isdigit(UChar(*string))) {
327                     number = number * 10 + (*string - '0');
328                     string++;
329                 }
330                 number *= 10;
331                 if (*string == '.') {
332                     string++;
333                     if (isdigit(UChar(*string))) {
334                         number += (*string - '0');
335                         string++;
336                     }
337                     while (isdigit(UChar(*string)))
338                         string++;
339                 }
340
341                 mandatory = FALSE;
342                 while (*string == '*' || *string == '/') {
343                     if (*string == '*') {
344                         number *= affcnt;
345                         string++;
346                     } else {    /* if (*string == '/') */
347                         mandatory = TRUE;
348                         string++;
349                     }
350                 }
351
352                 if (number > 0
353                     && (always_delay
354                         || normal_delay
355                         || mandatory))
356                     NCURSES_SP_NAME(delay_output) (NCURSES_SP_ARGx number / 10);
357
358             }                   /* endelse (*string == '<') */
359         }                       /* endelse (*string == '$') */
360
361         if (*string == '\0')
362             break;
363
364         string++;
365     }
366
367 #if BSD_TPUTS
368     /*
369      * Emit any BSD-style prefix padding that we've accumulated now.
370      */
371     if (trailpad > 0
372         && (always_delay || normal_delay))
373         delay_output(trailpad / 10);
374 #endif /* BSD_TPUTS */
375
376     SetOutCh(my_outch);
377     return OK;
378 }
379
380 #if NCURSES_SP_FUNCS
381 NCURSES_EXPORT(int)
382 _nc_outc_wrapper(SCREEN *sp, int c)
383 {
384     if (0 == sp) {
385         return (ERR);
386     } else {
387         return sp->jump(c);
388     }
389 }
390
391 NCURSES_EXPORT(int)
392 tputs(const char *string, int affcnt, int (*outc) (int))
393 {
394     SetSafeOutcWrapper(outc);
395     return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx string, affcnt, _nc_outc_wrapper);
396 }
397 #endif