xref: /freebsd/contrib/ncurses/ncurses/tinfo/lib_tputs.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /****************************************************************************
2  * Copyright (c) 1998-2003,2007 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  ****************************************************************************/
33 
34 /*
35  *	tputs.c
36  *		delay_output()
37  *		_nc_outch()
38  *		tputs()
39  *
40  */
41 
42 #include <curses.priv.h>
43 #include <ctype.h>
44 #include <term.h>		/* padding_baud_rate, xon_xoff */
45 #include <termcap.h>		/* ospeed */
46 #include <tic.h>
47 
48 MODULE_ID("$Id: lib_tputs.c,v 1.64 2007/09/29 20:37:13 tom Exp $")
49 
50 NCURSES_EXPORT_VAR(char) PC = 0;              /* used by termcap library */
51 NCURSES_EXPORT_VAR(NCURSES_OSPEED) ospeed = 0;        /* used by termcap library */
52 
53 NCURSES_EXPORT_VAR(int) _nc_nulls_sent = 0;   /* used by 'tack' program */
54 
55 static int (*my_outch) (int c) = _nc_outch;
56 
57 NCURSES_EXPORT(int)
58 delay_output(int ms)
59 {
60     T((T_CALLED("delay_output(%d)"), ms));
61 
62     if (no_pad_char) {
63 	_nc_flush();
64 	napms(ms);
65     } else {
66 	register int nullcount;
67 
68 	nullcount = (ms * _nc_baudrate(ospeed)) / (BAUDBYTE * 1000);
69 	for (_nc_nulls_sent += nullcount; nullcount > 0; nullcount--)
70 	    my_outch(PC);
71 	if (my_outch == _nc_outch)
72 	    _nc_flush();
73     }
74 
75     returnCode(OK);
76 }
77 
78 NCURSES_EXPORT(void)
79 _nc_flush(void)
80 {
81     (void) fflush(NC_OUTPUT);
82 }
83 
84 NCURSES_EXPORT(int)
85 _nc_outch(int ch)
86 {
87     COUNT_OUTCHARS(1);
88 
89     if (SP != 0
90 	&& SP->_cleanup) {
91 	char tmp = ch;
92 	/*
93 	 * POSIX says write() is safe in a signal handler, but the
94 	 * buffered I/O is not.
95 	 */
96 	write(fileno(NC_OUTPUT), &tmp, 1);
97     } else {
98 	putc(ch, NC_OUTPUT);
99     }
100     return OK;
101 }
102 
103 NCURSES_EXPORT(int)
104 putp(const char *string)
105 {
106     return tputs(string, 1, _nc_outch);
107 }
108 
109 NCURSES_EXPORT(int)
110 tputs(const char *string, int affcnt, int (*outc) (int))
111 {
112     bool always_delay;
113     bool normal_delay;
114     int number;
115 #if BSD_TPUTS
116     int trailpad;
117 #endif /* BSD_TPUTS */
118 
119 #ifdef TRACE
120     char addrbuf[32];
121 
122     if (USE_TRACEF(TRACE_TPUTS)) {
123 	if (outc == _nc_outch)
124 	    (void) strcpy(addrbuf, "_nc_outch");
125 	else
126 	    (void) sprintf(addrbuf, "%p", outc);
127 	if (_nc_tputs_trace) {
128 	    _tracef("tputs(%s = %s, %d, %s) called", _nc_tputs_trace,
129 		    _nc_visbuf(string), affcnt, addrbuf);
130 	} else {
131 	    _tracef("tputs(%s, %d, %s) called", _nc_visbuf(string), affcnt, addrbuf);
132 	}
133 	TPUTS_TRACE(NULL);
134 	_nc_unlock_global(tracef);
135     }
136 #endif /* TRACE */
137 
138     if (!VALID_STRING(string))
139 	return ERR;
140 
141     if (cur_term == 0) {
142 	always_delay = FALSE;
143 	normal_delay = TRUE;
144     } else {
145 	always_delay = (string == bell) || (string == flash_screen);
146 	normal_delay =
147 	    !xon_xoff
148 	    && padding_baud_rate
149 #if NCURSES_NO_PADDING
150 	    && (SP == 0 || !(SP->_no_padding))
151 #endif
152 	    && (_nc_baudrate(ospeed) >= padding_baud_rate);
153     }
154 
155 #if BSD_TPUTS
156     /*
157      * This ugly kluge deals with the fact that some ancient BSD programs
158      * (like nethack) actually do the likes of tputs("50") to get delays.
159      */
160     trailpad = 0;
161     if (isdigit(UChar(*string))) {
162 	while (isdigit(UChar(*string))) {
163 	    trailpad = trailpad * 10 + (*string - '0');
164 	    string++;
165 	}
166 	trailpad *= 10;
167 	if (*string == '.') {
168 	    string++;
169 	    if (isdigit(UChar(*string))) {
170 		trailpad += (*string - '0');
171 		string++;
172 	    }
173 	    while (isdigit(UChar(*string)))
174 		string++;
175 	}
176 
177 	if (*string == '*') {
178 	    trailpad *= affcnt;
179 	    string++;
180 	}
181     }
182 #endif /* BSD_TPUTS */
183 
184     my_outch = outc;		/* redirect delay_output() */
185     while (*string) {
186 	if (*string != '$')
187 	    (*outc) (*string);
188 	else {
189 	    string++;
190 	    if (*string != '<') {
191 		(*outc) ('$');
192 		if (*string)
193 		    (*outc) (*string);
194 	    } else {
195 		bool mandatory;
196 
197 		string++;
198 		if ((!isdigit(UChar(*string)) && *string != '.')
199 		    || !strchr(string, '>')) {
200 		    (*outc) ('$');
201 		    (*outc) ('<');
202 		    continue;
203 		}
204 
205 		number = 0;
206 		while (isdigit(UChar(*string))) {
207 		    number = number * 10 + (*string - '0');
208 		    string++;
209 		}
210 		number *= 10;
211 		if (*string == '.') {
212 		    string++;
213 		    if (isdigit(UChar(*string))) {
214 			number += (*string - '0');
215 			string++;
216 		    }
217 		    while (isdigit(UChar(*string)))
218 			string++;
219 		}
220 
221 		mandatory = FALSE;
222 		while (*string == '*' || *string == '/') {
223 		    if (*string == '*') {
224 			number *= affcnt;
225 			string++;
226 		    } else {	/* if (*string == '/') */
227 			mandatory = TRUE;
228 			string++;
229 		    }
230 		}
231 
232 		if (number > 0
233 		    && (always_delay
234 			|| normal_delay
235 			|| mandatory))
236 		    delay_output(number / 10);
237 
238 	    }			/* endelse (*string == '<') */
239 	}			/* endelse (*string == '$') */
240 
241 	if (*string == '\0')
242 	    break;
243 
244 	string++;
245     }
246 
247 #if BSD_TPUTS
248     /*
249      * Emit any BSD-style prefix padding that we've accumulated now.
250      */
251     if (trailpad > 0
252 	&& (always_delay || normal_delay))
253 	delay_output(trailpad / 10);
254 #endif /* BSD_TPUTS */
255 
256     my_outch = _nc_outch;
257     return OK;
258 }
259