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 <term.h> /* padding_baud_rate, xon_xoff */
52 #include <termcap.h> /* ospeed */
55 MODULE_ID("$Id: lib_tputs.c,v 1.73 2009/05/10 00:54:17 tom Exp $")
57 NCURSES_EXPORT_VAR(char) PC = 0; /* used by termcap library */
58 NCURSES_EXPORT_VAR(NCURSES_OSPEED) ospeed = 0; /* used by termcap library */
60 NCURSES_EXPORT_VAR(int) _nc_nulls_sent = 0; /* used by 'tack' program */
62 #if NCURSES_NO_PADDING
64 _nc_set_no_padding(SCREEN *sp)
66 bool no_padding = (getenv("NCURSES_NO_PADDING") != 0);
69 sp->_no_padding = no_padding;
71 _nc_prescreen._no_padding = no_padding;
73 TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used",
74 GetNoPadding(sp) ? " not" : ""));
79 #define my_outch SP_PARM->_outch
81 static NCURSES_SP_OUTC my_outch = NCURSES_SP_NAME(_nc_outch);
85 NCURSES_SP_NAME(delay_output) (NCURSES_SP_DCLx int ms)
87 T((T_CALLED("delay_output(%p,%d)"), SP_PARM, ms));
90 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
93 register int nullcount;
95 nullcount = (ms * _nc_baudrate(ospeed)) / (BAUDBYTE * 1000);
96 for (_nc_nulls_sent += nullcount; nullcount > 0; nullcount--)
97 my_outch(NCURSES_SP_ARGx PC);
98 if (my_outch == NCURSES_SP_NAME(_nc_outch))
99 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
109 return NCURSES_SP_NAME(delay_output) (CURRENT_SCREEN, ms);
114 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_DCL0)
116 (void) fflush(NC_OUTPUT(SP_PARM));
123 NCURSES_SP_NAME(_nc_flush) (CURRENT_SCREEN);
128 NCURSES_SP_NAME(_nc_outch) (NCURSES_SP_DCLx int ch)
133 && SP_PARM->_cleanup) {
136 * POSIX says write() is safe in a signal handler, but the
137 * buffered I/O is not.
139 write(fileno(NC_OUTPUT(SP_PARM)), &tmp, 1);
141 putc(ch, NC_OUTPUT(SP_PARM));
150 return NCURSES_SP_NAME(_nc_outch) (CURRENT_SCREEN, ch);
155 NCURSES_SP_NAME(putp) (NCURSES_SP_DCLx const char *string)
157 return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
158 string, 1, NCURSES_SP_NAME(_nc_outch));
163 putp(const char *string)
165 return NCURSES_SP_NAME(putp) (CURRENT_SCREEN, string);
170 NCURSES_SP_NAME(tputs) (NCURSES_SP_DCLx
173 NCURSES_SP_OUTC outc)
180 #endif /* BSD_TPUTS */
185 if (USE_TRACEF(TRACE_TPUTS)) {
186 if (outc == NCURSES_SP_NAME(_nc_outch))
187 (void) strcpy(addrbuf, "_nc_outch");
189 (void) sprintf(addrbuf, "%p", outc);
190 if (_nc_tputs_trace) {
191 _tracef("tputs(%s = %s, %d, %s) called", _nc_tputs_trace,
192 _nc_visbuf(string), affcnt, addrbuf);
194 _tracef("tputs(%s, %d, %s) called", _nc_visbuf(string), affcnt, addrbuf);
197 _nc_unlock_global(tracef);
201 if (!VALID_STRING(string))
205 always_delay = FALSE;
208 always_delay = (string == bell) || (string == flash_screen);
212 #if NCURSES_NO_PADDING
213 && !GetNoPadding(SP_PARM)
215 && (_nc_baudrate(ospeed) >= padding_baud_rate);
220 * This ugly kluge deals with the fact that some ancient BSD programs
221 * (like nethack) actually do the likes of tputs("50") to get delays.
224 if (isdigit(UChar(*string))) {
225 while (isdigit(UChar(*string))) {
226 trailpad = trailpad * 10 + (*string - '0');
230 if (*string == '.') {
232 if (isdigit(UChar(*string))) {
233 trailpad += (*string - '0');
236 while (isdigit(UChar(*string)))
240 if (*string == '*') {
245 #endif /* BSD_TPUTS */
247 my_outch = outc; /* redirect delay_output() */
250 (*outc) (NCURSES_SP_ARGx *string);
253 if (*string != '<') {
254 (*outc) (NCURSES_SP_ARGx '$');
256 (*outc) (NCURSES_SP_ARGx *string);
261 if ((!isdigit(UChar(*string)) && *string != '.')
262 || !strchr(string, '>')) {
263 (*outc) (NCURSES_SP_ARGx '$');
264 (*outc) (NCURSES_SP_ARGx '<');
269 while (isdigit(UChar(*string))) {
270 number = number * 10 + (*string - '0');
274 if (*string == '.') {
276 if (isdigit(UChar(*string))) {
277 number += (*string - '0');
280 while (isdigit(UChar(*string)))
285 while (*string == '*' || *string == '/') {
286 if (*string == '*') {
289 } else { /* if (*string == '/') */
299 NCURSES_SP_NAME(delay_output) (NCURSES_SP_ARGx number / 10);
301 } /* endelse (*string == '<') */
302 } /* endelse (*string == '$') */
312 * Emit any BSD-style prefix padding that we've accumulated now.
315 && (always_delay || normal_delay))
316 delay_output(trailpad / 10);
317 #endif /* BSD_TPUTS */
319 my_outch = NCURSES_SP_NAME(_nc_outch);
325 _nc_outc_wrapper(SCREEN *sp, int c)
335 tputs(const char *string, int affcnt, int (*outc) (int))
337 SetSafeOutcWrapper(outc);
338 return NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx string, affcnt, _nc_outc_wrapper);