xref: /openbsd/lib/libcurses/tinfo/lib_tputs.c (revision 73471bf0)
1 /* $OpenBSD: lib_tputs.c,v 1.12 2010/01/12 23:22:06 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  *     and: Thomas E. Dickey                        1996-on                 *
35  ****************************************************************************/
36 
37 /*
38  *	tputs.c
39  *		delay_output()
40  *		_nc_outch()
41  *		tputs()
42  *
43  */
44 
45 #include <curses.priv.h>
46 #include <ctype.h>
47 #include <term.h>		/* padding_baud_rate, xon_xoff */
48 #include <termcap.h>		/* ospeed */
49 #include <tic.h>
50 
51 MODULE_ID("$Id: lib_tputs.c,v 1.12 2010/01/12 23:22:06 nicm Exp $")
52 
53 NCURSES_EXPORT_VAR(char) PC = 0;              /* used by termcap library */
54 NCURSES_EXPORT_VAR(NCURSES_OSPEED) ospeed = 0;        /* used by termcap library */
55 
56 NCURSES_EXPORT_VAR(int) _nc_nulls_sent = 0;   /* used by 'tack' program */
57 
58 #if NCURSES_NO_PADDING
59 NCURSES_EXPORT(void)
60 _nc_set_no_padding(SCREEN *sp)
61 {
62     bool no_padding = (getenv("NCURSES_NO_PADDING") != 0);
63 
64     if (sp)
65 	sp->_no_padding = no_padding;
66     else
67 	_nc_prescreen._no_padding = no_padding;
68 
69     TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used",
70 				    GetNoPadding(sp) ? " not" : ""));
71 }
72 #endif
73 
74 static int (*my_outch) (int c) = _nc_outch;
75 
76 NCURSES_EXPORT(int)
77 delay_output(int ms)
78 {
79     T((T_CALLED("delay_output(%d)"), ms));
80 
81     if (no_pad_char) {
82 	_nc_flush();
83 	napms(ms);
84     } else {
85 	register int nullcount;
86 
87 	nullcount = (ms * _nc_baudrate(ospeed)) / (BAUDBYTE * 1000);
88 	for (_nc_nulls_sent += nullcount; nullcount > 0; nullcount--)
89 	    my_outch(PC);
90 	if (my_outch == _nc_outch)
91 	    _nc_flush();
92     }
93 
94     returnCode(OK);
95 }
96 
97 NCURSES_EXPORT(void)
98 _nc_flush(void)
99 {
100     (void) fflush(NC_OUTPUT);
101 }
102 
103 NCURSES_EXPORT(int)
104 _nc_outch(int ch)
105 {
106     COUNT_OUTCHARS(1);
107 
108     if (SP != 0
109 	&& SP->_cleanup) {
110 	char tmp = ch;
111 	/*
112 	 * POSIX says write() is safe in a signal handler, but the
113 	 * buffered I/O is not.
114 	 */
115 	write(fileno(NC_OUTPUT), &tmp, 1);
116     } else {
117 	putc(ch, NC_OUTPUT);
118     }
119     return OK;
120 }
121 
122 NCURSES_EXPORT(int)
123 putp(const char *string)
124 {
125     return tputs(string, 1, _nc_outch);
126 }
127 
128 NCURSES_EXPORT(int)
129 tputs(const char *string, int affcnt, int (*outc) (int))
130 {
131     bool always_delay;
132     bool normal_delay;
133     int number;
134 #if BSD_TPUTS
135     int trailpad;
136 #endif /* BSD_TPUTS */
137 
138 #ifdef TRACE
139     char addrbuf[32];
140 
141     if (USE_TRACEF(TRACE_TPUTS)) {
142 	if (outc == _nc_outch)
143 	    (void) strlcpy(addrbuf, "_nc_outch", sizeof(addrbuf));
144 	else
145 	    (void) snprintf(addrbuf, sizeof(addrbuf), "%p", outc);
146 	if (_nc_tputs_trace) {
147 	    _tracef("tputs(%s = %s, %d, %s) called", _nc_tputs_trace,
148 		    _nc_visbuf(string), affcnt, addrbuf);
149 	} else {
150 	    _tracef("tputs(%s, %d, %s) called", _nc_visbuf(string), affcnt, addrbuf);
151 	}
152 	TPUTS_TRACE(NULL);
153 	_nc_unlock_global(tracef);
154     }
155 #endif /* TRACE */
156 
157     if (!VALID_STRING(string))
158 	return ERR;
159 
160     if (cur_term == 0) {
161 	always_delay = FALSE;
162 	normal_delay = TRUE;
163     } else {
164 	always_delay = (string == bell) || (string == flash_screen);
165 	normal_delay =
166 	    !xon_xoff
167 	    && padding_baud_rate
168 #if NCURSES_NO_PADDING
169 	    && !GetNoPadding(SP)
170 #endif
171 	    && (_nc_baudrate(ospeed) >= padding_baud_rate);
172     }
173 
174 #if BSD_TPUTS
175     /*
176      * This ugly kluge deals with the fact that some ancient BSD programs
177      * (like nethack) actually do the likes of tputs("50") to get delays.
178      */
179     trailpad = 0;
180     if (isdigit(UChar(*string))) {
181 	while (isdigit(UChar(*string))) {
182 	    trailpad = trailpad * 10 + (*string - '0');
183 	    string++;
184 	}
185 	trailpad *= 10;
186 	if (*string == '.') {
187 	    string++;
188 	    if (isdigit(UChar(*string))) {
189 		trailpad += (*string - '0');
190 		string++;
191 	    }
192 	    while (isdigit(UChar(*string)))
193 		string++;
194 	}
195 
196 	if (*string == '*') {
197 	    trailpad *= affcnt;
198 	    string++;
199 	}
200     }
201 #endif /* BSD_TPUTS */
202 
203     my_outch = outc;		/* redirect delay_output() */
204     while (*string) {
205 	if (*string != '$')
206 	    (*outc) (*string);
207 	else {
208 	    string++;
209 	    if (*string != '<') {
210 		(*outc) ('$');
211 		if (*string)
212 		    (*outc) (*string);
213 	    } else {
214 		bool mandatory;
215 
216 		string++;
217 		if ((!isdigit(UChar(*string)) && *string != '.')
218 		    || !strchr(string, '>')) {
219 		    (*outc) ('$');
220 		    (*outc) ('<');
221 		    continue;
222 		}
223 
224 		number = 0;
225 		while (isdigit(UChar(*string))) {
226 		    number = number * 10 + (*string - '0');
227 		    string++;
228 		}
229 		number *= 10;
230 		if (*string == '.') {
231 		    string++;
232 		    if (isdigit(UChar(*string))) {
233 			number += (*string - '0');
234 			string++;
235 		    }
236 		    while (isdigit(UChar(*string)))
237 			string++;
238 		}
239 
240 		mandatory = FALSE;
241 		while (*string == '*' || *string == '/') {
242 		    if (*string == '*') {
243 			number *= affcnt;
244 			string++;
245 		    } else {	/* if (*string == '/') */
246 			mandatory = TRUE;
247 			string++;
248 		    }
249 		}
250 
251 		if (number > 0
252 		    && (always_delay
253 			|| normal_delay
254 			|| mandatory))
255 		    delay_output(number / 10);
256 
257 	    }			/* endelse (*string == '<') */
258 	}			/* endelse (*string == '$') */
259 
260 	if (*string == '\0')
261 	    break;
262 
263 	string++;
264     }
265 
266 #if BSD_TPUTS
267     /*
268      * Emit any BSD-style prefix padding that we've accumulated now.
269      */
270     if (trailpad > 0
271 	&& (always_delay || normal_delay))
272 	delay_output(trailpad / 10);
273 #endif /* BSD_TPUTS */
274 
275     my_outch = _nc_outch;
276     return OK;
277 }
278