Expand PMF_FN_* macros.
[netbsd-mini2440.git] / lib / libterm / tputws.c
blob4082db287dc581d2f05ebec6c0a6a5653510e4de
1 /* $NetBSD: tputs.c,v 1.22 2005/02/04 15:52:08 perry Exp $ */
3 /*
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)tputs.c 8.1 (Berkeley) 6/4/93";
36 #else
37 __RCSID("$NetBSD: tputs.c,v 1.22 2005/02/04 15:52:08 perry Exp $");
38 #endif
39 #endif /* not lint */
41 #include <assert.h>
42 #include <wctype.h>
43 #include <wchar.h>
44 #include <termcap.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include "termcap_private.h"
48 #undef ospeed
50 /* internal functions */
51 int _tputws_convert(const wchar_t **, int);
54 * The following array gives the number of tens of milliseconds per
55 * character for each speed as returned by gtty. Thus since 300
56 * baud returns a 7, there are 33.3 milliseconds per char at 300 baud.
58 extern short ospeed;
59 extern char PC;
61 int
62 _tputws_convert(const wchar_t **ptr, int affcnt)
64 int i = 0;
66 _DIAGASSERT(ptr != NULL);
67 _DIAGASSERT(*ptr != NULL);
70 * Convert the number representing the delay.
72 if (iswdigit(**ptr)) {
74 i = i * 10 + *(*ptr)++ - '0';
75 while (iswdigit(**ptr));
77 i *= 10;
78 if (*(*ptr) == '.') {
79 (*ptr)++;
80 if (iswdigit(**ptr))
81 i += *(*ptr) - '0';
83 * Only one digit to the right of the decimal point.
85 while (iswdigit(**ptr))
86 (*ptr)++;
90 * If the delay is followed by a `*', then
91 * multiply by the affected lines count.
93 if (*(*ptr) == '*')
94 (*ptr)++, i *= affcnt;
96 return i;
99 int
100 t_putws(struct tinfo *info, const wchar_t *cp, int affcnt,
101 void (*outc)(wchar_t, void *), void *args)
103 int i = 0;
104 size_t limit;
105 int mspc10;
106 char pad[2], *pptr;
107 char *pc;
109 /* XXX: info may be NULL ? */
110 /* cp is handled below */
111 _DIAGASSERT(outc != NULL);
112 _DIAGASSERT(args != NULL);
114 if (info != NULL) {
116 * if we have info then get the pad char from the
117 * termcap entry if it exists, otherwise use the
118 * default NUL char.
120 pptr = pad;
121 limit = sizeof(pad);
122 pc = t_getstr(info, "pc", &pptr, &limit);
123 if (pc == NULL)
124 pad[0] = '\0';
125 else
126 free(pc);
129 if (cp == 0)
130 return -1;
132 /* scan and convert delay digits (if any) */
133 i = _tputws_convert(&cp, affcnt);
136 * The guts of the string.
138 while (*cp)
139 (*outc)(*cp++, args);
142 * If no delay needed, or output speed is
143 * not comprehensible, then don't try to delay.
145 if (i == 0)
146 return 0;
147 if (ospeed <= 0 || ospeed >= TMSPC10SIZE)
148 return 0;
151 * Round up by a half a character frame,
152 * and then do the delay.
153 * Too bad there are no user program accessible programmed delays.
154 * Transmitting pad characters slows many
155 * terminals down and also loads the system.
157 mspc10 = __tmspc10[ospeed];
158 i += mspc10 / 2;
159 for (i /= mspc10; i > 0; i--)
160 (*outc)(pad[0], args);
162 return 0;