]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_tputs.c
f17d8382e921e6f0fcc60a8714917d6b8917214f
[ncurses.git] / ncurses / tinfo / lib_tputs.c
1 /****************************************************************************
2  * Copyright (c) 1998,1999,2000 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.47 2000/05/27 23:08:41 tom Exp $")
49
50 char PC = 0;                    /* used by termcap library */
51 speed_t ospeed = 0;             /* used by termcap library */
52
53 int _nc_nulls_sent = 0;         /* used by 'tack' program */
54
55 static int (*my_outch) (int c) = _nc_outch;
56
57 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 void
79 _nc_flush(void)
80 {
81     (void)fflush(NC_OUTPUT);
82 }
83
84 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 #ifdef USE_WIDEC_SUPPORT
106 /*
107  * Reference: The Unicode Standard 2.0
108  *
109  * No surrogates supported (we're storing only one 16-bit Unicode value per
110  * cell).
111  */
112 int
113 _nc_utf8_outch(int ch)
114 {
115     static const unsigned byteMask = 0xBF;
116     static const unsigned otherMark = 0x80;
117     static const unsigned firstMark[] =
118     {0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC};
119
120     int result[7], *ptr;
121     int count = 0;
122
123     if (ch < 0x80)
124         count = 1;
125     else if (ch < 0x800)
126         count = 2;
127     else if (ch < 0x10000)
128         count = 3;
129     else if (ch < 0x200000)
130         count = 4;
131     else if (ch < 0x4000000)
132         count = 5;
133     else if (ch <= 0x7FFFFFFF)
134         count = 6;
135     else {
136         count = 2;
137         ch = 0xFFFD;
138     }
139     ptr = result + count;
140     switch (count) {
141     case 6:
142         *--ptr = (ch | otherMark) & byteMask;
143         ch >>= 6;
144     case 5:
145         *--ptr = (ch | otherMark) & byteMask;
146         ch >>= 6;
147     case 4:
148         *--ptr = (ch | otherMark) & byteMask;
149         ch >>= 6;
150     case 3:
151         *--ptr = (ch | otherMark) & byteMask;
152         ch >>= 6;
153     case 2:
154         *--ptr = (ch | otherMark) & byteMask;
155         ch >>= 6;
156     case 1:
157         *--ptr = (ch | firstMark[count]);
158     }
159     while (count--)
160         _nc_outch(*ptr++);
161     return OK;
162 }
163 #endif
164
165 int
166 putp(const char *string)
167 {
168     return tputs(string, 1, _nc_outch);
169 }
170
171 int
172 tputs(const char *string, int affcnt, int (*outc) (int))
173 {
174     bool always_delay;
175     bool normal_delay;
176     int number;
177 #ifdef BSD_TPUTS
178     int trailpad;
179 #endif /* BSD_TPUTS */
180
181 #ifdef TRACE
182     char addrbuf[32];
183
184     if (_nc_tracing & TRACE_TPUTS) {
185         if (outc == _nc_outch)
186             (void) strcpy(addrbuf, "_nc_outch");
187         else
188             (void) sprintf(addrbuf, "%p", outc);
189         if (_nc_tputs_trace) {
190             _tracef("tputs(%s = %s, %d, %s) called", _nc_tputs_trace,
191                 _nc_visbuf(string), affcnt, addrbuf);
192         } else {
193             _tracef("tputs(%s, %d, %s) called", _nc_visbuf(string), affcnt, addrbuf);
194         }
195         _nc_tputs_trace = (char *) NULL;
196     }
197 #endif /* TRACE */
198
199     if (!VALID_STRING(string))
200         return ERR;
201
202     if (cur_term == 0) {
203         always_delay = FALSE;
204         normal_delay = TRUE;
205     } else {
206         always_delay = (string == bell) || (string == flash_screen);
207         normal_delay =
208             !xon_xoff
209             && padding_baud_rate
210 #ifdef NCURSES_NO_PADDING
211             && (SP == 0 || !(SP->_no_padding))
212 #endif
213             && (_nc_baudrate(ospeed) >= padding_baud_rate);
214     }
215
216 #ifdef BSD_TPUTS
217     /*
218      * This ugly kluge deals with the fact that some ancient BSD programs
219      * (like nethack) actually do the likes of tputs("50") to get delays.
220      */
221     trailpad = 0;
222     if (isdigit(*string)) {
223         while (isdigit(*string)) {
224             trailpad = trailpad * 10 + (*string - '0');
225             string++;
226         }
227         trailpad *= 10;
228         if (*string == '.') {
229             string++;
230             if (isdigit(*string)) {
231                 trailpad += (*string - '0');
232                 string++;
233             }
234             while (isdigit(*string))
235                 string++;
236         }
237
238         if (*string == '*') {
239             trailpad *= affcnt;
240             string++;
241         }
242     }
243 #endif /* BSD_TPUTS */
244
245     my_outch = outc;            /* redirect delay_output() */
246     while (*string) {
247         if (*string != '$')
248             (*outc) (*string);
249         else {
250             string++;
251             if (*string != '<') {
252                 (*outc) ('$');
253                 if (*string)
254                     (*outc) (*string);
255             } else {
256                 bool mandatory;
257
258                 string++;
259                 if ((!isdigit(*string) && *string != '.') || !strchr(string, '>')) {
260                     (*outc) ('$');
261                     (*outc) ('<');
262                     continue;
263                 }
264
265                 number = 0;
266                 while (isdigit(*string)) {
267                     number = number * 10 + (*string - '0');
268                     string++;
269                 }
270                 number *= 10;
271                 if (*string == '.') {
272                     string++;
273                     if (isdigit(*string)) {
274                         number += (*string - '0');
275                         string++;
276                     }
277                     while (isdigit(*string))
278                         string++;
279                 }
280
281                 mandatory = FALSE;
282                 while (*string == '*' || *string == '/') {
283                     if (*string == '*') {
284                         number *= affcnt;
285                         string++;
286                     } else {    /* if (*string == '/') */
287                         mandatory = TRUE;
288                         string++;
289                     }
290                 }
291
292                 if (number > 0
293                     && (always_delay
294                         || normal_delay
295                         || mandatory))
296                     delay_output(number / 10);
297
298             }                   /* endelse (*string == '<') */
299         }                       /* endelse (*string == '$') */
300
301         if (*string == '\0')
302             break;
303
304         string++;
305     }
306
307 #ifdef BSD_TPUTS
308     /*
309      * Emit any BSD-style prefix padding that we've accumulated now.
310      */
311     if (trailpad > 0
312         && (always_delay || normal_delay))
313         delay_output(trailpad / 10);
314 #endif /* BSD_TPUTS */
315
316     my_outch = _nc_outch;
317     return OK;
318 }