]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_tputs.c
ncurses 5.7 - patch 20090214
[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.67 2009/02/15 00:49:44 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 _nc_flush(void)
106 {
107     (void) fflush(NC_OUTPUT);
108 }
109
110 NCURSES_EXPORT(int)
111 _nc_outch(int ch)
112 {
113     COUNT_OUTCHARS(1);
114
115     if (SP != 0
116         && SP->_cleanup) {
117         char tmp = ch;
118         /*
119          * POSIX says write() is safe in a signal handler, but the
120          * buffered I/O is not.
121          */
122         write(fileno(NC_OUTPUT), &tmp, 1);
123     } else {
124         putc(ch, NC_OUTPUT);
125     }
126     return OK;
127 }
128
129 NCURSES_EXPORT(int)
130 putp(const char *string)
131 {
132     return tputs(string, 1, _nc_outch);
133 }
134
135 NCURSES_EXPORT(int)
136 tputs(const char *string, int affcnt, int (*outc) (int))
137 {
138     bool always_delay;
139     bool normal_delay;
140     int number;
141 #if BSD_TPUTS
142     int trailpad;
143 #endif /* BSD_TPUTS */
144
145 #ifdef TRACE
146     char addrbuf[32];
147
148     if (USE_TRACEF(TRACE_TPUTS)) {
149         if (outc == _nc_outch)
150             (void) strcpy(addrbuf, "_nc_outch");
151         else
152             (void) sprintf(addrbuf, "%p", outc);
153         if (_nc_tputs_trace) {
154             _tracef("tputs(%s = %s, %d, %s) called", _nc_tputs_trace,
155                     _nc_visbuf(string), affcnt, addrbuf);
156         } else {
157             _tracef("tputs(%s, %d, %s) called", _nc_visbuf(string), affcnt, addrbuf);
158         }
159         TPUTS_TRACE(NULL);
160         _nc_unlock_global(tracef);
161     }
162 #endif /* TRACE */
163
164     if (!VALID_STRING(string))
165         return ERR;
166
167     if (cur_term == 0) {
168         always_delay = FALSE;
169         normal_delay = TRUE;
170     } else {
171         always_delay = (string == bell) || (string == flash_screen);
172         normal_delay =
173             !xon_xoff
174             && padding_baud_rate
175 #if NCURSES_NO_PADDING
176             && !GetNoPadding(SP)
177 #endif
178             && (_nc_baudrate(ospeed) >= padding_baud_rate);
179     }
180
181 #if BSD_TPUTS
182     /*
183      * This ugly kluge deals with the fact that some ancient BSD programs
184      * (like nethack) actually do the likes of tputs("50") to get delays.
185      */
186     trailpad = 0;
187     if (isdigit(UChar(*string))) {
188         while (isdigit(UChar(*string))) {
189             trailpad = trailpad * 10 + (*string - '0');
190             string++;
191         }
192         trailpad *= 10;
193         if (*string == '.') {
194             string++;
195             if (isdigit(UChar(*string))) {
196                 trailpad += (*string - '0');
197                 string++;
198             }
199             while (isdigit(UChar(*string)))
200                 string++;
201         }
202
203         if (*string == '*') {
204             trailpad *= affcnt;
205             string++;
206         }
207     }
208 #endif /* BSD_TPUTS */
209
210     my_outch = outc;            /* redirect delay_output() */
211     while (*string) {
212         if (*string != '$')
213             (*outc) (*string);
214         else {
215             string++;
216             if (*string != '<') {
217                 (*outc) ('$');
218                 if (*string)
219                     (*outc) (*string);
220             } else {
221                 bool mandatory;
222
223                 string++;
224                 if ((!isdigit(UChar(*string)) && *string != '.')
225                     || !strchr(string, '>')) {
226                     (*outc) ('$');
227                     (*outc) ('<');
228                     continue;
229                 }
230
231                 number = 0;
232                 while (isdigit(UChar(*string))) {
233                     number = number * 10 + (*string - '0');
234                     string++;
235                 }
236                 number *= 10;
237                 if (*string == '.') {
238                     string++;
239                     if (isdigit(UChar(*string))) {
240                         number += (*string - '0');
241                         string++;
242                     }
243                     while (isdigit(UChar(*string)))
244                         string++;
245                 }
246
247                 mandatory = FALSE;
248                 while (*string == '*' || *string == '/') {
249                     if (*string == '*') {
250                         number *= affcnt;
251                         string++;
252                     } else {    /* if (*string == '/') */
253                         mandatory = TRUE;
254                         string++;
255                     }
256                 }
257
258                 if (number > 0
259                     && (always_delay
260                         || normal_delay
261                         || mandatory))
262                     delay_output(number / 10);
263
264             }                   /* endelse (*string == '<') */
265         }                       /* endelse (*string == '$') */
266
267         if (*string == '\0')
268             break;
269
270         string++;
271     }
272
273 #if BSD_TPUTS
274     /*
275      * Emit any BSD-style prefix padding that we've accumulated now.
276      */
277     if (trailpad > 0
278         && (always_delay || normal_delay))
279         delay_output(trailpad / 10);
280 #endif /* BSD_TPUTS */
281
282     my_outch = _nc_outch;
283     return OK;
284 }