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