]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_tputs.c
ncurses 5.7 - patch 20090419
[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 #include <ctype.h>
46 #include <term.h>               /* padding_baud_rate, xon_xoff */
47 #include <termcap.h>            /* ospeed */
48 #include <tic.h>
49
50 MODULE_ID("$Id: lib_tputs.c,v 1.68 2009/02/28 21:08:18 tom Exp $")
51
52 NCURSES_EXPORT_VAR(char) PC = 0;              /* used by termcap library */
53 NCURSES_EXPORT_VAR(NCURSES_OSPEED) ospeed = 0;        /* used by termcap library */
54
55 NCURSES_EXPORT_VAR(int) _nc_nulls_sent = 0;   /* used by 'tack' program */
56
57 #if NCURSES_NO_PADDING
58 NCURSES_EXPORT(void)
59 _nc_set_no_padding(SCREEN *sp)
60 {
61     bool no_padding = (getenv("NCURSES_NO_PADDING") != 0);
62
63     if (sp)
64         sp->_no_padding = no_padding;
65     else
66         _nc_prescreen._no_padding = no_padding;
67
68     TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used",
69                                     GetNoPadding(sp) ? " not" : ""));
70 }
71 #endif
72
73 static int (*my_outch) (int c) = _nc_outch;
74
75 NCURSES_EXPORT(int)
76 NCURSES_SP_NAME(delay_output) (NCURSES_SP_DCLx int ms)
77 {
78     T((T_CALLED("delay_output(%d)"), ms));
79
80     if (no_pad_char) {
81         _nc_flush();
82         napms(ms);
83     } else {
84         register int nullcount;
85
86         nullcount = (ms * _nc_baudrate(ospeed)) / (BAUDBYTE * 1000);
87         for (_nc_nulls_sent += nullcount; nullcount > 0; nullcount--)
88             my_outch(PC);
89         if (my_outch == _nc_outch)
90             _nc_flush();
91     }
92
93     returnCode(OK);
94 }
95
96 #if NCURSES_SP_FUNCS
97 NCURSES_EXPORT(int)
98 delay_output(int ms)
99 {
100     return NCURSES_SP_NAME(delay_output) (CURRENT_SCREEN, ms);
101 }
102 #endif
103
104 NCURSES_EXPORT(void)
105 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_DCL0)
106 {
107     (void) fflush(NC_OUTPUT);
108 }
109
110 #if NCURSES_SP_FUNCS
111 NCURSES_EXPORT(void)
112 _nc_flush(void)
113 {
114     NCURSES_SP_NAME(_nc_flush) (CURRENT_SCREEN);
115 }
116 #endif
117
118 NCURSES_EXPORT(int)
119 NCURSES_SP_NAME(_nc_outch) (NCURSES_SP_DCLx int ch)
120 {
121     COUNT_OUTCHARS(1);
122
123     if (SP_PARM != 0
124         && SP_PARM->_cleanup) {
125         char tmp = ch;
126         /*
127          * POSIX says write() is safe in a signal handler, but the
128          * buffered I/O is not.
129          */
130         write(fileno(NC_OUTPUT), &tmp, 1);
131     } else {
132         putc(ch, NC_OUTPUT);
133     }
134     return OK;
135 }
136
137 #if NCURSES_SP_FUNCS
138 NCURSES_EXPORT(int)
139 _nc_outch(int ch)
140 {
141     return NCURSES_SP_NAME(_nc_outch) (CURRENT_SCREEN, ch);
142 }
143 #endif
144
145 NCURSES_EXPORT(int)
146 putp(const char *string)
147 {
148     return tputs(string, 1, _nc_outch);
149 }
150
151 NCURSES_EXPORT(int)
152 tputs(const char *string, int affcnt, int (*outc) (int))
153 {
154     bool always_delay;
155     bool normal_delay;
156     int number;
157 #if BSD_TPUTS
158     int trailpad;
159 #endif /* BSD_TPUTS */
160
161 #ifdef TRACE
162     char addrbuf[32];
163
164     if (USE_TRACEF(TRACE_TPUTS)) {
165         if (outc == _nc_outch)
166             (void) strcpy(addrbuf, "_nc_outch");
167         else
168             (void) sprintf(addrbuf, "%p", outc);
169         if (_nc_tputs_trace) {
170             _tracef("tputs(%s = %s, %d, %s) called", _nc_tputs_trace,
171                     _nc_visbuf(string), affcnt, addrbuf);
172         } else {
173             _tracef("tputs(%s, %d, %s) called", _nc_visbuf(string), affcnt, addrbuf);
174         }
175         TPUTS_TRACE(NULL);
176         _nc_unlock_global(tracef);
177     }
178 #endif /* TRACE */
179
180     if (!VALID_STRING(string))
181         return ERR;
182
183     if (cur_term == 0) {
184         always_delay = FALSE;
185         normal_delay = TRUE;
186     } else {
187         always_delay = (string == bell) || (string == flash_screen);
188         normal_delay =
189             !xon_xoff
190             && padding_baud_rate
191 #if NCURSES_NO_PADDING
192             && !GetNoPadding(SP)
193 #endif
194             && (_nc_baudrate(ospeed) >= padding_baud_rate);
195     }
196
197 #if BSD_TPUTS
198     /*
199      * This ugly kluge deals with the fact that some ancient BSD programs
200      * (like nethack) actually do the likes of tputs("50") to get delays.
201      */
202     trailpad = 0;
203     if (isdigit(UChar(*string))) {
204         while (isdigit(UChar(*string))) {
205             trailpad = trailpad * 10 + (*string - '0');
206             string++;
207         }
208         trailpad *= 10;
209         if (*string == '.') {
210             string++;
211             if (isdigit(UChar(*string))) {
212                 trailpad += (*string - '0');
213                 string++;
214             }
215             while (isdigit(UChar(*string)))
216                 string++;
217         }
218
219         if (*string == '*') {
220             trailpad *= affcnt;
221             string++;
222         }
223     }
224 #endif /* BSD_TPUTS */
225
226     my_outch = outc;            /* redirect delay_output() */
227     while (*string) {
228         if (*string != '$')
229             (*outc) (*string);
230         else {
231             string++;
232             if (*string != '<') {
233                 (*outc) ('$');
234                 if (*string)
235                     (*outc) (*string);
236             } else {
237                 bool mandatory;
238
239                 string++;
240                 if ((!isdigit(UChar(*string)) && *string != '.')
241                     || !strchr(string, '>')) {
242                     (*outc) ('$');
243                     (*outc) ('<');
244                     continue;
245                 }
246
247                 number = 0;
248                 while (isdigit(UChar(*string))) {
249                     number = number * 10 + (*string - '0');
250                     string++;
251                 }
252                 number *= 10;
253                 if (*string == '.') {
254                     string++;
255                     if (isdigit(UChar(*string))) {
256                         number += (*string - '0');
257                         string++;
258                     }
259                     while (isdigit(UChar(*string)))
260                         string++;
261                 }
262
263                 mandatory = FALSE;
264                 while (*string == '*' || *string == '/') {
265                     if (*string == '*') {
266                         number *= affcnt;
267                         string++;
268                     } else {    /* if (*string == '/') */
269                         mandatory = TRUE;
270                         string++;
271                     }
272                 }
273
274                 if (number > 0
275                     && (always_delay
276                         || normal_delay
277                         || mandatory))
278                     delay_output(number / 10);
279
280             }                   /* endelse (*string == '<') */
281         }                       /* endelse (*string == '$') */
282
283         if (*string == '\0')
284             break;
285
286         string++;
287     }
288
289 #if BSD_TPUTS
290     /*
291      * Emit any BSD-style prefix padding that we've accumulated now.
292      */
293     if (trailpad > 0
294         && (always_delay || normal_delay))
295         delay_output(trailpad / 10);
296 #endif /* BSD_TPUTS */
297
298     my_outch = _nc_outch;
299     return OK;
300 }