1 /****************************************************************************
2 * Copyright (c) 1998-2013,2015 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.96 2015/01/03 23:51: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 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)
81 #define SetOutCh(func) static_outch = func
82 #define GetOutCh() static_outch
83 static NCURSES_SP_OUTC static_outch
= NCURSES_SP_NAME(_nc_outch
);
87 NCURSES_SP_NAME(delay_output
) (NCURSES_SP_DCLx
int ms
)
89 T((T_CALLED("delay_output(%p,%d)"), (void *) SP_PARM
, ms
));
91 if (!HasTInfoTerminal(SP_PARM
))
95 NCURSES_SP_NAME(_nc_flush
) (NCURSES_SP_ARG
);
98 NCURSES_SP_OUTC my_outch
= GetOutCh();
99 register int nullcount
;
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
);
115 return NCURSES_SP_NAME(delay_output
) (CURRENT_SCREEN
, ms
);
120 NCURSES_SP_NAME(_nc_flush
) (NCURSES_SP_DCL0
)
122 if (SP_PARM
!= 0 && SP_PARM
->_ofd
>= 0) {
123 if (SP_PARM
->out_inuse
) {
124 char *buf
= SP_PARM
->out_buffer
;
125 size_t amount
= SP
->out_inuse
;
130 res
= write(SP_PARM
->_ofd
, buf
, amount
);
133 /* if the write was incomplete, try again */
134 amount
-= (size_t) res
;
136 } else if (errno
== EAGAIN
) {
138 } else if (errno
== EINTR
) {
141 break; /* an error we can not recover from */
152 NCURSES_SP_NAME(_nc_flush
) (CURRENT_SCREEN
);
157 NCURSES_SP_NAME(_nc_outch
) (NCURSES_SP_DCLx
int ch
)
163 if (HasTInfoTerminal(SP_PARM
)
165 if (SP_PARM
->out_buffer
!= 0) {
166 if (SP_PARM
->out_inuse
+ 1 >= SP_PARM
->out_limit
)
167 NCURSES_SP_NAME(_nc_flush
) (NCURSES_SP_ARG
);
168 SP_PARM
->out_buffer
[SP_PARM
->out_inuse
++] = (char) ch
;
170 char tmp
= (char) ch
;
172 * POSIX says write() is safe in a signal handler, but the
173 * buffered I/O is not.
175 if (write(fileno(NC_OUTPUT(SP_PARM
)), &tmp
, (size_t) 1) == -1)
179 char tmp
= (char) ch
;
180 if (write(fileno(stdout
), &tmp
, (size_t) 1) == -1)
190 return NCURSES_SP_NAME(_nc_outch
) (CURRENT_SCREEN
, ch
);
195 * This is used for the putp special case.
198 NCURSES_SP_NAME(_nc_putchar
) (NCURSES_SP_DCLx
int ch
)
213 * putp is special - per documentation it calls tputs with putchar as the
214 * parameter for outputting characters. This means that it uses stdio, which
215 * is not signal-safe. Applications call this entrypoint; we do not call it
216 * from within the library.
219 NCURSES_SP_NAME(putp
) (NCURSES_SP_DCLx
const char *string
)
221 return NCURSES_SP_NAME(tputs
) (NCURSES_SP_ARGx
222 string
, 1, NCURSES_SP_NAME(_nc_putchar
));
227 putp(const char *string
)
229 return NCURSES_SP_NAME(putp
) (CURRENT_SCREEN
, string
);
234 * Use these entrypoints rather than "putp" within the library.
237 NCURSES_SP_NAME(_nc_putp
) (NCURSES_SP_DCLx
238 const char *name GCC_UNUSED
,
245 rc
= NCURSES_SP_NAME(tputs
) (NCURSES_SP_ARGx
246 string
, 1, NCURSES_SP_NAME(_nc_outch
));
253 _nc_putp(const char *name
, const char *string
)
255 return NCURSES_SP_NAME(_nc_putp
) (CURRENT_SCREEN
, name
, string
);
260 NCURSES_SP_NAME(tputs
) (NCURSES_SP_DCLx
263 NCURSES_SP_OUTC outc
)
265 NCURSES_SP_OUTC my_outch
= GetOutCh();
271 #endif /* BSD_TPUTS */
276 if (USE_TRACEF(TRACE_TPUTS
)) {
277 if (outc
== NCURSES_SP_NAME(_nc_outch
))
278 _nc_STRCPY(addrbuf
, "_nc_outch", sizeof(addrbuf
));
280 _nc_SPRINTF(addrbuf
, _nc_SLIMIT(sizeof(addrbuf
)) "%p", outc
);
281 if (_nc_tputs_trace
) {
282 _tracef("tputs(%s = %s, %d, %s) called", _nc_tputs_trace
,
283 _nc_visbuf(string
), affcnt
, addrbuf
);
285 _tracef("tputs(%s, %d, %s) called", _nc_visbuf(string
), affcnt
, addrbuf
);
288 _nc_unlock_global(tracef
);
292 if (SP_PARM
!= 0 && !HasTInfoTerminal(SP_PARM
))
295 if (!VALID_STRING(string
))
300 (SP_PARM
!= 0 && SP_PARM
->_term
== 0)
305 always_delay
= FALSE
;
308 always_delay
= (string
== bell
) || (string
== flash_screen
);
312 #if NCURSES_NO_PADDING
313 && !GetNoPadding(SP_PARM
)
315 && (_nc_baudrate(ospeed
) >= padding_baud_rate
);
320 * This ugly kluge deals with the fact that some ancient BSD programs
321 * (like nethack) actually do the likes of tputs("50") to get delays.
324 if (isdigit(UChar(*string
))) {
325 while (isdigit(UChar(*string
))) {
326 trailpad
= trailpad
* 10 + (*string
- '0');
330 if (*string
== '.') {
332 if (isdigit(UChar(*string
))) {
333 trailpad
+= (*string
- '0');
336 while (isdigit(UChar(*string
)))
340 if (*string
== '*') {
345 #endif /* BSD_TPUTS */
347 SetOutCh(outc
); /* redirect delay_output() */
350 (*outc
) (NCURSES_SP_ARGx
*string
);
353 if (*string
!= '<') {
354 (*outc
) (NCURSES_SP_ARGx
'$');
356 (*outc
) (NCURSES_SP_ARGx
*string
);
361 if ((!isdigit(UChar(*string
)) && *string
!= '.')
362 || !strchr(string
, '>')) {
363 (*outc
) (NCURSES_SP_ARGx
'$');
364 (*outc
) (NCURSES_SP_ARGx
'<');
369 while (isdigit(UChar(*string
))) {
370 number
= number
* 10 + (*string
- '0');
374 if (*string
== '.') {
376 if (isdigit(UChar(*string
))) {
377 number
+= (*string
- '0');
380 while (isdigit(UChar(*string
)))
385 while (*string
== '*' || *string
== '/') {
386 if (*string
== '*') {
389 } else { /* if (*string == '/') */
399 NCURSES_SP_NAME(delay_output
) (NCURSES_SP_ARGx number
/ 10);
401 } /* endelse (*string == '<') */
402 } /* endelse (*string == '$') */
412 * Emit any BSD-style prefix padding that we've accumulated now.
415 && (always_delay
|| normal_delay
))
416 delay_output(trailpad
/ 10);
417 #endif /* BSD_TPUTS */
425 _nc_outc_wrapper(SCREEN
*sp
, int c
)
435 tputs(const char *string
, int affcnt
, int (*outc
) (int))
437 SetSafeOutcWrapper(outc
);
438 return NCURSES_SP_NAME(tputs
) (NCURSES_SP_ARGx string
, affcnt
, _nc_outc_wrapper
);