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