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