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