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