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