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