1 /****************************************************************************
2  * Copyright (c) 1998,1999,2000 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.51 2000/10/08 00:22:24 tom Exp $")
49 
50 char PC = 0;			/* used by termcap library */
51 short ospeed = 0;		/* used by termcap library */
52 
53 int _nc_nulls_sent = 0;		/* used by 'tack' program */
54 
55 static int (*my_outch) (int c) = _nc_outch;
56 
57 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)) / 10000;
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 void
79 _nc_flush(void)
80 {
81     (void) fflush(NC_OUTPUT);
82 }
83 
84 int
85 _nc_outch(int ch)
86 {
87 #ifdef TRACE
88     _nc_outchars++;
89 #endif /* TRACE */
90 
91     if (SP != 0
92 	&& SP->_cleanup) {
93 	char tmp = ch;
94 	/*
95 	 * POSIX says write() is safe in a signal handler, but the
96 	 * buffered I/O is not.
97 	 */
98 	write(fileno(NC_OUTPUT), &tmp, 1);
99     } else {
100 	putc(ch, NC_OUTPUT);
101     }
102     return OK;
103 }
104 
105 #if USE_WIDEC_SUPPORT
106 /*
107  * Reference: The Unicode Standard 2.0
108  *
109  * No surrogates supported (we're storing only one 16-bit Unicode value per
110  * cell).
111  */
112 int
113 _nc_utf8_outch(int ch)
114 {
115     static const unsigned byteMask = 0xBF;
116     static const unsigned otherMark = 0x80;
117     static const unsigned firstMark[] =
118     {0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC};
119 
120     int result[7], *ptr;
121     int count = 0;
122 
123     if ((unsigned int) ch < 0x80)
124 	count = 1;
125     else if ((unsigned int) ch < 0x800)
126 	count = 2;
127     else if ((unsigned int) ch < 0x10000)
128 	count = 3;
129     else if ((unsigned int) ch < 0x200000)
130 	count = 4;
131     else if ((unsigned int) ch < 0x4000000)
132 	count = 5;
133     else if ((unsigned int) ch <= 0x7FFFFFFF)
134 	count = 6;
135     else {
136 	count = 3;
137 	ch = 0xFFFD;
138     }
139     ptr = result + count;
140     switch (count) {
141     case 6:
142 	*--ptr = (ch | otherMark) & byteMask;
143 	ch >>= 6;
144 	/* FALLTHRU */
145     case 5:
146 	*--ptr = (ch | otherMark) & byteMask;
147 	ch >>= 6;
148 	/* FALLTHRU */
149     case 4:
150 	*--ptr = (ch | otherMark) & byteMask;
151 	ch >>= 6;
152 	/* FALLTHRU */
153     case 3:
154 	*--ptr = (ch | otherMark) & byteMask;
155 	ch >>= 6;
156 	/* FALLTHRU */
157     case 2:
158 	*--ptr = (ch | otherMark) & byteMask;
159 	ch >>= 6;
160 	/* FALLTHRU */
161     case 1:
162 	*--ptr = (ch | firstMark[count]);
163 	break;
164     }
165     while (count--)
166 	_nc_outch(*ptr++);
167     return OK;
168 }
169 #endif
170 
171 int
172 putp(const char *string)
173 {
174     return tputs(string, 1, _nc_outch);
175 }
176 
177 int
178 tputs(const char *string, int affcnt, int (*outc) (int))
179 {
180     bool always_delay;
181     bool normal_delay;
182     int number;
183 #if BSD_TPUTS
184     int trailpad;
185 #endif /* BSD_TPUTS */
186 
187 #ifdef TRACE
188     char addrbuf[32];
189 
190     if (_nc_tracing & TRACE_TPUTS) {
191 	if (outc == _nc_outch)
192 	    (void) strcpy(addrbuf, "_nc_outch");
193 	else
194 	    (void) sprintf(addrbuf, "%p", outc);
195 	if (_nc_tputs_trace) {
196 	    _tracef("tputs(%s = %s, %d, %s) called", _nc_tputs_trace,
197 		    _nc_visbuf(string), affcnt, addrbuf);
198 	} else {
199 	    _tracef("tputs(%s, %d, %s) called", _nc_visbuf(string), affcnt, addrbuf);
200 	}
201 	_nc_tputs_trace = (char *) NULL;
202     }
203 #endif /* TRACE */
204 
205     if (!VALID_STRING(string))
206 	return ERR;
207 
208     if (cur_term == 0) {
209 	always_delay = FALSE;
210 	normal_delay = TRUE;
211     } else {
212 	always_delay = (string == bell) || (string == flash_screen);
213 	normal_delay =
214 	    !xon_xoff
215 	    && padding_baud_rate
216 #if NCURSES_NO_PADDING
217 	    && (SP == 0 || !(SP->_no_padding))
218 #endif
219 	    && (_nc_baudrate(ospeed) >= padding_baud_rate);
220     }
221 
222 #if BSD_TPUTS
223     /*
224      * This ugly kluge deals with the fact that some ancient BSD programs
225      * (like nethack) actually do the likes of tputs("50") to get delays.
226      */
227     trailpad = 0;
228     if (isdigit(*string)) {
229 	while (isdigit(*string)) {
230 	    trailpad = trailpad * 10 + (*string - '0');
231 	    string++;
232 	}
233 	trailpad *= 10;
234 	if (*string == '.') {
235 	    string++;
236 	    if (isdigit(*string)) {
237 		trailpad += (*string - '0');
238 		string++;
239 	    }
240 	    while (isdigit(*string))
241 		string++;
242 	}
243 
244 	if (*string == '*') {
245 	    trailpad *= affcnt;
246 	    string++;
247 	}
248     }
249 #endif /* BSD_TPUTS */
250 
251     my_outch = outc;		/* redirect delay_output() */
252     while (*string) {
253 	if (*string != '$')
254 	    (*outc) (*string);
255 	else {
256 	    string++;
257 	    if (*string != '<') {
258 		(*outc) ('$');
259 		if (*string)
260 		    (*outc) (*string);
261 	    } else {
262 		bool mandatory;
263 
264 		string++;
265 		if ((!isdigit(*string) && *string != '.') || !strchr(string, '>')) {
266 		    (*outc) ('$');
267 		    (*outc) ('<');
268 		    continue;
269 		}
270 
271 		number = 0;
272 		while (isdigit(*string)) {
273 		    number = number * 10 + (*string - '0');
274 		    string++;
275 		}
276 		number *= 10;
277 		if (*string == '.') {
278 		    string++;
279 		    if (isdigit(*string)) {
280 			number += (*string - '0');
281 			string++;
282 		    }
283 		    while (isdigit(*string))
284 			string++;
285 		}
286 
287 		mandatory = FALSE;
288 		while (*string == '*' || *string == '/') {
289 		    if (*string == '*') {
290 			number *= affcnt;
291 			string++;
292 		    } else {	/* if (*string == '/') */
293 			mandatory = TRUE;
294 			string++;
295 		    }
296 		}
297 
298 		if (number > 0
299 		    && (always_delay
300 			|| normal_delay
301 			|| mandatory))
302 		    delay_output(number / 10);
303 
304 	    }			/* endelse (*string == '<') */
305 	}			/* endelse (*string == '$') */
306 
307 	if (*string == '\0')
308 	    break;
309 
310 	string++;
311     }
312 
313 #if BSD_TPUTS
314     /*
315      * Emit any BSD-style prefix padding that we've accumulated now.
316      */
317     if (trailpad > 0
318 	&& (always_delay || normal_delay))
319 	delay_output(trailpad / 10);
320 #endif /* BSD_TPUTS */
321 
322     my_outch = _nc_outch;
323     return OK;
324 }
325