1 /****************************************************************************
2 * Copyright (c) 1998-2008,2009 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
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 ****************************************************************************/
44 #include <curses.priv.h>
47 #define CUR SP_TERMTYPE
51 #include <termcap.h> /* ospeed */
54 MODULE_ID("$Id: lib_tputs.c,v 1.74 2009/05/23 23:56:23 tom Exp $")
56 NCURSES_EXPORT_VAR(char) PC = 0; /* used by termcap library */
57 NCURSES_EXPORT_VAR(NCURSES_OSPEED) ospeed = 0; /* used by termcap library */
59 NCURSES_EXPORT_VAR(int) _nc_nulls_sent = 0; /* used by 'tack' program */
61 #if NCURSES_NO_PADDING
63 _nc_set_no_padding(SCREEN *sp)
65 bool no_padding = (getenv("NCURSES_NO_PADDING") != 0);
68 sp->_no_padding = no_padding;
70 _nc_prescreen._no_padding = no_padding;
72 TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used",
73 GetNoPadding(sp) ? " not" : ""));
78 #define my_outch SP_PARM->_outch
80 static NCURSES_SP_OUTC my_outch = NCURSES_SP_NAME(_nc_outch);
84 NCURSES_SP_NAME(delay_output) (NCURSES_SP_DCLx int ms)
86 T((T_CALLED("delay_output(%p,%d)"), SP_PARM, ms));
89 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
92 register int nullcount;
94 nullcount = (ms * _nc_baudrate(ospeed)) / (BAUDBYTE * 1000);
95 for (_nc_nulls_sent += nullcount; nullcount > 0; nullcount--)
96 my_outch(NCURSES_SP_ARGx PC);
97 if (my_outch == NCURSES_SP_NAME(_nc_outch))
98 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
108 return NCURSES_SP_NAME(delay_output) (CURRENT_SCREEN, ms);
113 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_DCL0)
115 (void) fflush(NC_OUTPUT(SP_PARM));
122 NCURSES_SP_NAME(_nc_flush) (CURRENT_SCREEN);
127 NCURSES_SP_NAME(_nc_outch) (NCURSES_SP_DCLx int ch)
132 && SP_PARM->_cleanup) {
135 * POSIX says write() is safe in a signal handler, but the
136 * buffered I/O is not.
138 write(fileno(NC_OUTPUT(SP_PARM)), &tmp, 1);
140 putc(ch, NC_OUTPUT(SP_PARM));
149 return NCURSES_SP_NAME(_nc_outch) (CURRENT_SCREEN, ch);
154 NCURSES_SP_NAME(putp) (NCURSES_SP_DCLx const char *string)
156 return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
157 string, 1, NCURSES_SP_NAME(_nc_outch));
161 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_DCLx
162 const char *name GCC_UNUSED,
169 rc = NCURSES_SP_NAME(putp) (NCURSES_SP_ARGx string);
176 putp(const char *string)
178 return NCURSES_SP_NAME(putp) (CURRENT_SCREEN, string);
182 _nc_putp(const char *name, const char *string)
184 return NCURSES_SP_NAME(_nc_putp) (CURRENT_SCREEN, name, string);
189 NCURSES_SP_NAME(tputs) (NCURSES_SP_DCLx
192 NCURSES_SP_OUTC outc)
199 #endif /* BSD_TPUTS */
204 if (USE_TRACEF(TRACE_TPUTS)) {
205 if (outc == NCURSES_SP_NAME(_nc_outch))
206 (void) strcpy(addrbuf, "_nc_outch");
208 (void) sprintf(addrbuf, "%p", outc);
209 if (_nc_tputs_trace) {
210 _tracef("tputs(%s = %s, %d, %s) called", _nc_tputs_trace,
211 _nc_visbuf(string), affcnt, addrbuf);
213 _tracef("tputs(%s, %d, %s) called", _nc_visbuf(string), affcnt, addrbuf);
216 _nc_unlock_global(tracef);
220 if (!VALID_STRING(string))
224 always_delay = FALSE;
227 always_delay = (string == bell) || (string == flash_screen);
231 #if NCURSES_NO_PADDING
232 && !GetNoPadding(SP_PARM)
234 && (_nc_baudrate(ospeed) >= padding_baud_rate);
239 * This ugly kluge deals with the fact that some ancient BSD programs
240 * (like nethack) actually do the likes of tputs("50") to get delays.
243 if (isdigit(UChar(*string))) {
244 while (isdigit(UChar(*string))) {
245 trailpad = trailpad * 10 + (*string - '0');
249 if (*string == '.') {
251 if (isdigit(UChar(*string))) {
252 trailpad += (*string - '0');
255 while (isdigit(UChar(*string)))
259 if (*string == '*') {
264 #endif /* BSD_TPUTS */
266 my_outch = outc; /* redirect delay_output() */
269 (*outc) (NCURSES_SP_ARGx *string);
272 if (*string != '<') {
273 (*outc) (NCURSES_SP_ARGx '$');
275 (*outc) (NCURSES_SP_ARGx *string);
280 if ((!isdigit(UChar(*string)) && *string != '.')
281 || !strchr(string, '>')) {
282 (*outc) (NCURSES_SP_ARGx '$');
283 (*outc) (NCURSES_SP_ARGx '<');
288 while (isdigit(UChar(*string))) {
289 number = number * 10 + (*string - '0');
293 if (*string == '.') {
295 if (isdigit(UChar(*string))) {
296 number += (*string - '0');
299 while (isdigit(UChar(*string)))
304 while (*string == '*' || *string == '/') {
305 if (*string == '*') {
308 } else { /* if (*string == '/') */
318 NCURSES_SP_NAME(delay_output) (NCURSES_SP_ARGx number / 10);
320 } /* endelse (*string == '<') */
321 } /* endelse (*string == '$') */
331 * Emit any BSD-style prefix padding that we've accumulated now.
334 && (always_delay || normal_delay))
335 delay_output(trailpad / 10);
336 #endif /* BSD_TPUTS */
338 my_outch = NCURSES_SP_NAME(_nc_outch);
344 _nc_outc_wrapper(SCREEN *sp, int c)
354 tputs(const char *string, int affcnt, int (*outc) (int))
356 SetSafeOutcWrapper(outc);
357 return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx string, affcnt, _nc_outc_wrapper);