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