]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/lib_tputs.c
e5b4a6bcaa14e4389df1d93e8de18866e7a49fd4
[ncurses.git] / ncurses / lib_tputs.c
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 <tic.h>
47
48 MODULE_ID("$Id: lib_tputs.c,v 1.30 1998/02/11 12:14:00 tom Exp $")
49
50 #define OUTPUT ((SP != 0) ? SP->_ofp : stdout)
51
52 int _nc_nulls_sent;     /* used by 'tack' program */
53
54 static int (*my_outch)(int c) = _nc_outch;
55
56 int delay_output(int ms)
57 {
58         T((T_CALLED("delay_output(%d)"), ms));
59
60         if (cur_term == 0 || cur_term->_baudrate <= 0) {
61                 (void) fflush(OUTPUT);
62                 _nc_timed_wait(0, ms, (int *)0);
63         }
64 #ifdef no_pad_char
65         else if (no_pad_char)
66                 napms(ms);
67 #endif /* no_pad_char */
68         else {
69                 register int    nullcount;
70                 char    null = '\0';
71
72 #ifdef pad_char
73                 if (pad_char)
74                         null = pad_char[0];
75 #endif /* pad_char */
76
77                 nullcount = ms * cur_term->_baudrate / 10000;
78                 for (_nc_nulls_sent += nullcount; nullcount > 0; nullcount--)
79                         my_outch(null);
80                 if (my_outch == _nc_outch)
81                         (void) fflush(OUTPUT);
82         }
83
84         returnCode(OK);
85 }
86
87 int _nc_outch(int ch)
88 {
89 #ifdef TRACE
90         _nc_outchars++;
91 #endif /* TRACE */
92
93         putc(ch, OUTPUT);
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[17];
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                 else
123                         TR(TRACE_MAXIMUM, ("tputs(%s, %d, %s) called", _nc_visbuf(string), affcnt, addrbuf));
124                 _nc_tputs_trace = (char *)NULL;
125         }
126 #endif /* TRACE */
127         
128         if (string == ABSENT_STRING || string == CANCELLED_STRING)
129                 return ERR;
130
131         if (cur_term == 0) {
132                 always_delay = FALSE;
133                 normal_delay = TRUE;
134         } else {
135                 always_delay = (string == bell) || (string == flash_screen);
136                 normal_delay =
137                  !xon_xoff
138 #ifdef padding_baud_rate
139                  && padding_baud_rate
140                  && (!cur_term || cur_term->_baudrate >= padding_baud_rate)
141 #endif
142                  ;
143         }
144
145 #ifdef BSD_TPUTS
146         /*
147          * This ugly kluge deals with the fact that some ancient BSD programs
148          * (like nethack) actually do the likes of tputs("50") to get delays.
149          */
150         trailpad = 0;
151         while (isdigit(*string)) {
152                 trailpad = trailpad * 10 + (*string - '0');
153                 string++;
154         }
155         trailpad *= 10;
156         if (*string == '.') {
157                 string++;
158                 if (isdigit(*string)) {
159                         trailpad += (*string - '0');
160                         string++;
161                 }
162                 while (isdigit(*string))
163                         string++;
164         }
165
166         if (*string == '*') {
167                 trailpad *= affcnt;
168                 string++;
169         }
170 #endif /* BSD_TPUTS */
171
172         my_outch = outc;        /* redirect delay_output() */
173         while (*string) {
174                 if (*string != '$')
175                         (*outc)(*string);
176                 else {
177                         string++;
178                         if (*string != '<') {
179                                 (*outc)('$');
180                                 if (*string)
181                                     (*outc)(*string);
182                         } else {
183                                 bool mandatory;
184
185                                 string++;
186                                 if ((!isdigit(*string) && *string != '.') || !strchr(string, '>')) {
187                                         (*outc)('$');
188                                         (*outc)('<');
189                                         continue;
190                                 }
191
192                                 number = 0;
193                                 while (isdigit(*string)) {
194                                         number = number * 10 + (*string - '0');
195                                         string++;
196                                 }
197                                 number *= 10;
198                                 if (*string == '.') {
199                                         string++;
200                                         if (isdigit(*string)) {
201                                                 number += (*string - '0');
202                                                 string++;
203                                         }
204                                         while (isdigit(*string))
205                                                 string++;
206                                 }
207
208                                 mandatory = FALSE;
209                                 while (*string == '*' || *string == '/')
210                                 {
211                                         if (*string == '*') {
212                                                 number *= affcnt;
213                                                 string++;
214                                         }
215                                         else /* if (*string == '/') */ {
216                                                 mandatory = TRUE;
217                                                 string++;
218                                         }
219                                 }
220
221                                 if (number > 0
222                                  && (always_delay
223                                   || normal_delay
224                                   || mandatory))
225                                         delay_output(number/10);
226
227                         } /* endelse (*string == '<') */
228                 } /* endelse (*string == '$') */
229
230                 if (*string == '\0')
231                         break;
232
233                 string++;
234         }
235
236 #ifdef BSD_TPUTS
237         /*
238          * Emit any BSD-style prefix padding that we've accumulated now.
239          */
240         if (trailpad > 0
241          && (always_delay || normal_delay))
242                 delay_output(trailpad/10);
243 #endif /* BSD_TPUTS */
244
245         my_outch = _nc_outch;
246         return OK;
247 }