xref: /freebsd/contrib/ncurses/ncurses/tinfo/lib_tputs.c (revision 38f0b757fd84d17d0fc24739a7cda160c4516d81)
1 /****************************************************************************
2  * Copyright (c) 1998-2012,2013 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 
46 #ifndef CUR
47 #define CUR SP_TERMTYPE
48 #endif
49 
50 #include <ctype.h>
51 #include <termcap.h>		/* ospeed */
52 #include <tic.h>
53 
54 MODULE_ID("$Id: lib_tputs.c,v 1.93 2013/01/12 20:57:32 tom Exp $")
55 
56 NCURSES_EXPORT_VAR(char) PC = 0;              /* used by termcap library */
57 NCURSES_EXPORT_VAR(NCURSES_OSPEED) ospeed = 0;        /* used by termcap library */
58 
59 NCURSES_EXPORT_VAR(int) _nc_nulls_sent = 0;   /* used by 'tack' program */
60 
61 #if NCURSES_NO_PADDING
62 NCURSES_EXPORT(void)
63 _nc_set_no_padding(SCREEN *sp)
64 {
65     bool no_padding = (getenv("NCURSES_NO_PADDING") != 0);
66 
67     if (sp)
68 	sp->_no_padding = no_padding;
69     else
70 	_nc_prescreen._no_padding = no_padding;
71 
72     TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used",
73 				    GetNoPadding(sp) ? " not" : ""));
74 }
75 #endif
76 
77 #if NCURSES_SP_FUNCS
78 #define SetOutCh(func) if (SP_PARM) SP_PARM->_outch = func; else _nc_prescreen._outch = func
79 #define GetOutCh()     (SP_PARM ? SP_PARM->_outch : _nc_prescreen._outch)
80 #else
81 #define SetOutCh(func) static_outch = func
82 #define GetOutCh()     static_outch
83 static NCURSES_SP_OUTC static_outch = NCURSES_SP_NAME(_nc_outch);
84 #endif
85 
86 NCURSES_EXPORT(int)
87 NCURSES_SP_NAME(delay_output) (NCURSES_SP_DCLx int ms)
88 {
89     T((T_CALLED("delay_output(%p,%d)"), (void *) SP_PARM, ms));
90 
91     if (!HasTInfoTerminal(SP_PARM))
92 	returnCode(ERR);
93 
94     if (no_pad_char) {
95 	NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
96 	napms(ms);
97     } else {
98 	NCURSES_SP_OUTC my_outch = GetOutCh();
99 	register int nullcount;
100 
101 	nullcount = (ms * _nc_baudrate(ospeed)) / (BAUDBYTE * 1000);
102 	for (_nc_nulls_sent += nullcount; nullcount > 0; nullcount--)
103 	    my_outch(NCURSES_SP_ARGx PC);
104 	if (my_outch == NCURSES_SP_NAME(_nc_outch))
105 	    NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
106     }
107 
108     returnCode(OK);
109 }
110 
111 #if NCURSES_SP_FUNCS
112 NCURSES_EXPORT(int)
113 delay_output(int ms)
114 {
115     return NCURSES_SP_NAME(delay_output) (CURRENT_SCREEN, ms);
116 }
117 #endif
118 
119 NCURSES_EXPORT(void)
120 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_DCL0)
121 {
122     if (SP_PARM != 0 && SP_PARM->_ofd >= 0) {
123 	if (SP_PARM->out_inuse) {
124 	    size_t amount = SP->out_inuse;
125 	    /*
126 	     * Help a little, if the write is interrupted, by first resetting
127 	     * our amount.
128 	     */
129 	    SP->out_inuse = 0;
130 	    IGNORE_RC(write(SP_PARM->_ofd, SP_PARM->out_buffer, amount));
131 	}
132     }
133 }
134 
135 #if NCURSES_SP_FUNCS
136 NCURSES_EXPORT(void)
137 _nc_flush(void)
138 {
139     NCURSES_SP_NAME(_nc_flush) (CURRENT_SCREEN);
140 }
141 #endif
142 
143 NCURSES_EXPORT(int)
144 NCURSES_SP_NAME(_nc_outch) (NCURSES_SP_DCLx int ch)
145 {
146     int rc = OK;
147 
148     COUNT_OUTCHARS(1);
149 
150     if (HasTInfoTerminal(SP_PARM)
151 	&& SP_PARM != 0) {
152 	if (SP_PARM->out_buffer != 0) {
153 	    if (SP_PARM->out_inuse + 1 >= SP_PARM->out_limit)
154 		NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
155 	    SP_PARM->out_buffer[SP_PARM->out_inuse++] = (char) ch;
156 	} else {
157 	    char tmp = (char) ch;
158 	    /*
159 	     * POSIX says write() is safe in a signal handler, but the
160 	     * buffered I/O is not.
161 	     */
162 	    if (write(fileno(NC_OUTPUT(SP_PARM)), &tmp, (size_t) 1) == -1)
163 		rc = ERR;
164 	}
165     } else {
166 	char tmp = (char) ch;
167 	if (write(fileno(stdout), &tmp, (size_t) 1) == -1)
168 	    rc = ERR;
169     }
170     return rc;
171 }
172 
173 #if NCURSES_SP_FUNCS
174 NCURSES_EXPORT(int)
175 _nc_outch(int ch)
176 {
177     return NCURSES_SP_NAME(_nc_outch) (CURRENT_SCREEN, ch);
178 }
179 #endif
180 
181 /*
182  * This is used for the putp special case.
183  */
184 NCURSES_EXPORT(int)
185 NCURSES_SP_NAME(_nc_putchar) (NCURSES_SP_DCLx int ch)
186 {
187     (void) SP_PARM;
188     return putchar(ch);
189 }
190 
191 #if NCURSES_SP_FUNCS
192 NCURSES_EXPORT(int)
193 _nc_putchar(int ch)
194 {
195     return putchar(ch);
196 }
197 #endif
198 
199 /*
200  * putp is special - per documentation it calls tputs with putchar as the
201  * parameter for outputting characters.  This means that it uses stdio, which
202  * is not signal-safe.  Applications call this entrypoint; we do not call it
203  * from within the library.
204  */
205 NCURSES_EXPORT(int)
206 NCURSES_SP_NAME(putp) (NCURSES_SP_DCLx const char *string)
207 {
208     return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
209 				   string, 1, NCURSES_SP_NAME(_nc_putchar));
210 }
211 
212 #if NCURSES_SP_FUNCS
213 NCURSES_EXPORT(int)
214 putp(const char *string)
215 {
216     return NCURSES_SP_NAME(putp) (CURRENT_SCREEN, string);
217 }
218 #endif
219 
220 /*
221  * Use these entrypoints rather than "putp" within the library.
222  */
223 NCURSES_EXPORT(int)
224 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_DCLx
225 			   const char *name GCC_UNUSED,
226 			   const char *string)
227 {
228     int rc = ERR;
229 
230     if (string != 0) {
231 	TPUTS_TRACE(name);
232 	rc = NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
233 				     string, 1, NCURSES_SP_NAME(_nc_outch));
234     }
235     return rc;
236 }
237 
238 #if NCURSES_SP_FUNCS
239 NCURSES_EXPORT(int)
240 _nc_putp(const char *name, const char *string)
241 {
242     return NCURSES_SP_NAME(_nc_putp) (CURRENT_SCREEN, name, string);
243 }
244 #endif
245 
246 NCURSES_EXPORT(int)
247 NCURSES_SP_NAME(tputs) (NCURSES_SP_DCLx
248 			const char *string,
249 			int affcnt,
250 			NCURSES_SP_OUTC outc)
251 {
252     NCURSES_SP_OUTC my_outch = GetOutCh();
253     bool always_delay;
254     bool normal_delay;
255     int number;
256 #if BSD_TPUTS
257     int trailpad;
258 #endif /* BSD_TPUTS */
259 
260 #ifdef TRACE
261     char addrbuf[32];
262 
263     if (USE_TRACEF(TRACE_TPUTS)) {
264 	if (outc == NCURSES_SP_NAME(_nc_outch))
265 	    _nc_STRCPY(addrbuf, "_nc_outch", sizeof(addrbuf));
266 	else
267 	    _nc_SPRINTF(addrbuf, _nc_SLIMIT(sizeof(addrbuf)) "%p", outc);
268 	if (_nc_tputs_trace) {
269 	    _tracef("tputs(%s = %s, %d, %s) called", _nc_tputs_trace,
270 		    _nc_visbuf(string), affcnt, addrbuf);
271 	} else {
272 	    _tracef("tputs(%s, %d, %s) called", _nc_visbuf(string), affcnt, addrbuf);
273 	}
274 	TPUTS_TRACE(NULL);
275 	_nc_unlock_global(tracef);
276     }
277 #endif /* TRACE */
278 
279     if (SP_PARM != 0 && !HasTInfoTerminal(SP_PARM))
280 	return ERR;
281 
282     if (!VALID_STRING(string))
283 	return ERR;
284 
285     if (
286 #if NCURSES_SP_FUNCS
287 	   (SP_PARM != 0 && SP_PARM->_term == 0)
288 #else
289 	   cur_term == 0
290 #endif
291 	) {
292 	always_delay = FALSE;
293 	normal_delay = TRUE;
294     } else {
295 	always_delay = (string == bell) || (string == flash_screen);
296 	normal_delay =
297 	    !xon_xoff
298 	    && padding_baud_rate
299 #if NCURSES_NO_PADDING
300 	    && !GetNoPadding(SP_PARM)
301 #endif
302 	    && (_nc_baudrate(ospeed) >= padding_baud_rate);
303     }
304 
305 #if BSD_TPUTS
306     /*
307      * This ugly kluge deals with the fact that some ancient BSD programs
308      * (like nethack) actually do the likes of tputs("50") to get delays.
309      */
310     trailpad = 0;
311     if (isdigit(UChar(*string))) {
312 	while (isdigit(UChar(*string))) {
313 	    trailpad = trailpad * 10 + (*string - '0');
314 	    string++;
315 	}
316 	trailpad *= 10;
317 	if (*string == '.') {
318 	    string++;
319 	    if (isdigit(UChar(*string))) {
320 		trailpad += (*string - '0');
321 		string++;
322 	    }
323 	    while (isdigit(UChar(*string)))
324 		string++;
325 	}
326 
327 	if (*string == '*') {
328 	    trailpad *= affcnt;
329 	    string++;
330 	}
331     }
332 #endif /* BSD_TPUTS */
333 
334     SetOutCh(outc);		/* redirect delay_output() */
335     while (*string) {
336 	if (*string != '$')
337 	    (*outc) (NCURSES_SP_ARGx *string);
338 	else {
339 	    string++;
340 	    if (*string != '<') {
341 		(*outc) (NCURSES_SP_ARGx '$');
342 		if (*string)
343 		    (*outc) (NCURSES_SP_ARGx *string);
344 	    } else {
345 		bool mandatory;
346 
347 		string++;
348 		if ((!isdigit(UChar(*string)) && *string != '.')
349 		    || !strchr(string, '>')) {
350 		    (*outc) (NCURSES_SP_ARGx '$');
351 		    (*outc) (NCURSES_SP_ARGx '<');
352 		    continue;
353 		}
354 
355 		number = 0;
356 		while (isdigit(UChar(*string))) {
357 		    number = number * 10 + (*string - '0');
358 		    string++;
359 		}
360 		number *= 10;
361 		if (*string == '.') {
362 		    string++;
363 		    if (isdigit(UChar(*string))) {
364 			number += (*string - '0');
365 			string++;
366 		    }
367 		    while (isdigit(UChar(*string)))
368 			string++;
369 		}
370 
371 		mandatory = FALSE;
372 		while (*string == '*' || *string == '/') {
373 		    if (*string == '*') {
374 			number *= affcnt;
375 			string++;
376 		    } else {	/* if (*string == '/') */
377 			mandatory = TRUE;
378 			string++;
379 		    }
380 		}
381 
382 		if (number > 0
383 		    && (always_delay
384 			|| normal_delay
385 			|| mandatory))
386 		    NCURSES_SP_NAME(delay_output) (NCURSES_SP_ARGx number / 10);
387 
388 	    }			/* endelse (*string == '<') */
389 	}			/* endelse (*string == '$') */
390 
391 	if (*string == '\0')
392 	    break;
393 
394 	string++;
395     }
396 
397 #if BSD_TPUTS
398     /*
399      * Emit any BSD-style prefix padding that we've accumulated now.
400      */
401     if (trailpad > 0
402 	&& (always_delay || normal_delay))
403 	delay_output(trailpad / 10);
404 #endif /* BSD_TPUTS */
405 
406     SetOutCh(my_outch);
407     return OK;
408 }
409 
410 #if NCURSES_SP_FUNCS
411 NCURSES_EXPORT(int)
412 _nc_outc_wrapper(SCREEN *sp, int c)
413 {
414     if (0 == sp) {
415 	return (ERR);
416     } else {
417 	return sp->jump(c);
418     }
419 }
420 
421 NCURSES_EXPORT(int)
422 tputs(const char *string, int affcnt, int (*outc) (int))
423 {
424     SetSafeOutcWrapper(outc);
425     return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx string, affcnt, _nc_outc_wrapper);
426 }
427 #endif
428