]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_tputs.c
ncurses 5.7 - patch 20090510
[ncurses.git] / ncurses / tinfo / lib_tputs.c
1 /****************************************************************************
2  * Copyright (c) 1998-2008,2009 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 <term.h>               /* padding_baud_rate, xon_xoff */
52 #include <termcap.h>            /* ospeed */
53 #include <tic.h>
54
55 MODULE_ID("$Id: lib_tputs.c,v 1.73 2009/05/10 00:54:17 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 my_outch SP_PARM->_outch
80 #else
81 static NCURSES_SP_OUTC my_outch = NCURSES_SP_NAME(_nc_outch);
82 #endif
83
84 NCURSES_EXPORT(int)
85 NCURSES_SP_NAME(delay_output) (NCURSES_SP_DCLx int ms)
86 {
87     T((T_CALLED("delay_output(%p,%d)"), SP_PARM, ms));
88
89     if (no_pad_char) {
90         NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
91         napms(ms);
92     } else {
93         register int nullcount;
94
95         nullcount = (ms * _nc_baudrate(ospeed)) / (BAUDBYTE * 1000);
96         for (_nc_nulls_sent += nullcount; nullcount > 0; nullcount--)
97             my_outch(NCURSES_SP_ARGx PC);
98         if (my_outch == NCURSES_SP_NAME(_nc_outch))
99             NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
100     }
101
102     returnCode(OK);
103 }
104
105 #if NCURSES_SP_FUNCS
106 NCURSES_EXPORT(int)
107 delay_output(int ms)
108 {
109     return NCURSES_SP_NAME(delay_output) (CURRENT_SCREEN, ms);
110 }
111 #endif
112
113 NCURSES_EXPORT(void)
114 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_DCL0)
115 {
116     (void) fflush(NC_OUTPUT(SP_PARM));
117 }
118
119 #if NCURSES_SP_FUNCS
120 NCURSES_EXPORT(void)
121 _nc_flush(void)
122 {
123     NCURSES_SP_NAME(_nc_flush) (CURRENT_SCREEN);
124 }
125 #endif
126
127 NCURSES_EXPORT(int)
128 NCURSES_SP_NAME(_nc_outch) (NCURSES_SP_DCLx int ch)
129 {
130     COUNT_OUTCHARS(1);
131
132     if (SP_PARM != 0
133         && SP_PARM->_cleanup) {
134         char tmp = ch;
135         /*
136          * POSIX says write() is safe in a signal handler, but the
137          * buffered I/O is not.
138          */
139         write(fileno(NC_OUTPUT(SP_PARM)), &tmp, 1);
140     } else {
141         putc(ch, NC_OUTPUT(SP_PARM));
142     }
143     return OK;
144 }
145
146 #if NCURSES_SP_FUNCS
147 NCURSES_EXPORT(int)
148 _nc_outch(int ch)
149 {
150     return NCURSES_SP_NAME(_nc_outch) (CURRENT_SCREEN, ch);
151 }
152 #endif
153
154 NCURSES_EXPORT(int)
155 NCURSES_SP_NAME(putp) (NCURSES_SP_DCLx const char *string)
156 {
157     return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
158                                    string, 1, NCURSES_SP_NAME(_nc_outch));
159 }
160
161 #if NCURSES_SP_FUNCS
162 NCURSES_EXPORT(int)
163 putp(const char *string)
164 {
165     return NCURSES_SP_NAME(putp) (CURRENT_SCREEN, string);
166 }
167 #endif
168
169 NCURSES_EXPORT(int)
170 NCURSES_SP_NAME(tputs) (NCURSES_SP_DCLx
171                         const char *string,
172                         int affcnt,
173                         NCURSES_SP_OUTC outc)
174 {
175     bool always_delay;
176     bool normal_delay;
177     int number;
178 #if BSD_TPUTS
179     int trailpad;
180 #endif /* BSD_TPUTS */
181
182 #ifdef TRACE
183     char addrbuf[32];
184
185     if (USE_TRACEF(TRACE_TPUTS)) {
186         if (outc == NCURSES_SP_NAME(_nc_outch))
187             (void) strcpy(addrbuf, "_nc_outch");
188         else
189             (void) sprintf(addrbuf, "%p", outc);
190         if (_nc_tputs_trace) {
191             _tracef("tputs(%s = %s, %d, %s) called", _nc_tputs_trace,
192                     _nc_visbuf(string), affcnt, addrbuf);
193         } else {
194             _tracef("tputs(%s, %d, %s) called", _nc_visbuf(string), affcnt, addrbuf);
195         }
196         TPUTS_TRACE(NULL);
197         _nc_unlock_global(tracef);
198     }
199 #endif /* TRACE */
200
201     if (!VALID_STRING(string))
202         return ERR;
203
204     if (cur_term == 0) {
205         always_delay = FALSE;
206         normal_delay = TRUE;
207     } else {
208         always_delay = (string == bell) || (string == flash_screen);
209         normal_delay =
210             !xon_xoff
211             && padding_baud_rate
212 #if NCURSES_NO_PADDING
213             && !GetNoPadding(SP_PARM)
214 #endif
215             && (_nc_baudrate(ospeed) >= padding_baud_rate);
216     }
217
218 #if BSD_TPUTS
219     /*
220      * This ugly kluge deals with the fact that some ancient BSD programs
221      * (like nethack) actually do the likes of tputs("50") to get delays.
222      */
223     trailpad = 0;
224     if (isdigit(UChar(*string))) {
225         while (isdigit(UChar(*string))) {
226             trailpad = trailpad * 10 + (*string - '0');
227             string++;
228         }
229         trailpad *= 10;
230         if (*string == '.') {
231             string++;
232             if (isdigit(UChar(*string))) {
233                 trailpad += (*string - '0');
234                 string++;
235             }
236             while (isdigit(UChar(*string)))
237                 string++;
238         }
239
240         if (*string == '*') {
241             trailpad *= affcnt;
242             string++;
243         }
244     }
245 #endif /* BSD_TPUTS */
246
247     my_outch = outc;            /* redirect delay_output() */
248     while (*string) {
249         if (*string != '$')
250             (*outc) (NCURSES_SP_ARGx *string);
251         else {
252             string++;
253             if (*string != '<') {
254                 (*outc) (NCURSES_SP_ARGx '$');
255                 if (*string)
256                     (*outc) (NCURSES_SP_ARGx *string);
257             } else {
258                 bool mandatory;
259
260                 string++;
261                 if ((!isdigit(UChar(*string)) && *string != '.')
262                     || !strchr(string, '>')) {
263                     (*outc) (NCURSES_SP_ARGx '$');
264                     (*outc) (NCURSES_SP_ARGx '<');
265                     continue;
266                 }
267
268                 number = 0;
269                 while (isdigit(UChar(*string))) {
270                     number = number * 10 + (*string - '0');
271                     string++;
272                 }
273                 number *= 10;
274                 if (*string == '.') {
275                     string++;
276                     if (isdigit(UChar(*string))) {
277                         number += (*string - '0');
278                         string++;
279                     }
280                     while (isdigit(UChar(*string)))
281                         string++;
282                 }
283
284                 mandatory = FALSE;
285                 while (*string == '*' || *string == '/') {
286                     if (*string == '*') {
287                         number *= affcnt;
288                         string++;
289                     } else {    /* if (*string == '/') */
290                         mandatory = TRUE;
291                         string++;
292                     }
293                 }
294
295                 if (number > 0
296                     && (always_delay
297                         || normal_delay
298                         || mandatory))
299                     NCURSES_SP_NAME(delay_output) (NCURSES_SP_ARGx number / 10);
300
301             }                   /* endelse (*string == '<') */
302         }                       /* endelse (*string == '$') */
303
304         if (*string == '\0')
305             break;
306
307         string++;
308     }
309
310 #if BSD_TPUTS
311     /*
312      * Emit any BSD-style prefix padding that we've accumulated now.
313      */
314     if (trailpad > 0
315         && (always_delay || normal_delay))
316         delay_output(trailpad / 10);
317 #endif /* BSD_TPUTS */
318
319     my_outch = NCURSES_SP_NAME(_nc_outch);
320     return OK;
321 }
322
323 #if NCURSES_SP_FUNCS
324 NCURSES_EXPORT(int)
325 _nc_outc_wrapper(SCREEN *sp, int c)
326 {
327     if (0 == sp) {
328         return (ERR);
329     } else {
330         return sp->jump(c);
331     }
332 }
333
334 NCURSES_EXPORT(int)
335 tputs(const char *string, int affcnt, int (*outc) (int))
336 {
337     SetSafeOutcWrapper(outc);
338     return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx string, affcnt, _nc_outc_wrapper);
339 }
340 #endif