Sync usage with man page.
[netbsd-mini2440.git] / lib / libterm / tputs.c
blobfff731a1eea8ce4785cb818f336c32454bd7278f
1 /* $NetBSD: tputs.c,v 1.23 2005/05/15 21:11:13 christos 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.23 2005/05/15 21:11:13 christos Exp $");
38 #endif
39 #endif /* not lint */
41 #include <assert.h>
42 #include <ctype.h>
43 #include <termcap.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include "termcap_private.h"
47 #undef ospeed
49 /* internal functions */
50 int _tputs_convert(const char **, int);
53 * The following array gives the number of tens of milliseconds per
54 * character for each speed as returned by gtty. Thus since 300
55 * baud returns a 7, there are 33.3 milliseconds per char at 300 baud.
57 const short __tmspc10[TMSPC10SIZE] = {
58 0, 2000, 1333, 909, 743, 666, 500, 333, 166, 83, 55, 41, 20, 10, 5
61 short ospeed;
62 char PC;
64 int
65 _tputs_convert(const char **ptr, int affcnt)
67 int i = 0;
69 _DIAGASSERT(ptr != NULL);
70 _DIAGASSERT(*ptr != NULL);
73 * Convert the number representing the delay.
75 if (isdigit(*(const unsigned char *)(*ptr))) {
77 i = i * 10 + *(*ptr)++ - '0';
78 while (isdigit(*(const unsigned char *)(*ptr)));
80 i *= 10;
81 if (*(*ptr) == '.') {
82 (*ptr)++;
83 if (isdigit(*(const unsigned char *)(*ptr)))
84 i += *(*ptr) - '0';
86 * Only one digit to the right of the decimal point.
88 while (isdigit(*(const unsigned char *)(*ptr)))
89 (*ptr)++;
93 * If the delay is followed by a `*', then
94 * multiply by the affected lines count.
96 if (*(*ptr) == '*')
97 (*ptr)++, i *= affcnt;
99 return i;
103 * Put the character string cp out, with padding.
104 * The number of affected lines is affcnt, and the routine
105 * used to output one character is outc.
108 tputs(const char *cp, int affcnt, int (*outc)(int))
110 int i = 0;
111 int mspc10;
113 _DIAGASSERT(outc != 0);
115 if (cp == 0)
116 return -1;
118 /* scan and convert delay digits (if any) */
119 i = _tputs_convert(&cp, affcnt);
122 * The guts of the string.
124 while (*cp)
125 (void)(*outc)(*cp++);
128 * If no delay needed, or output speed is
129 * not comprehensible, then don't try to delay.
131 if (i == 0)
132 return 0;
133 if (ospeed <= 0 || ospeed >= TMSPC10SIZE)
134 return 0;
137 * Round up by a half a character frame,
138 * and then do the delay.
139 * Too bad there are no user program accessible programmed delays.
140 * Transmitting pad characters slows many
141 * terminals down and also loads the system.
143 mspc10 = __tmspc10[ospeed];
144 i += mspc10 / 2;
145 for (i /= mspc10; i > 0; i--)
146 (void)(*outc)(PC);
147 return 0;
152 t_puts(struct tinfo *info, const char *cp, int affcnt,
153 void (*outc)(char, void *), void *args)
155 int i = 0;
156 size_t limit;
157 int mspc10;
158 char pad[2], *pptr;
159 char *pc;
161 /* XXX: info may be NULL ? */
162 /* cp is handled below */
163 _DIAGASSERT(outc != NULL);
164 _DIAGASSERT(args != NULL);
166 if (info != NULL) {
168 * if we have info then get the pad char from the
169 * termcap entry if it exists, otherwise use the
170 * default NUL char.
172 pptr = pad;
173 limit = sizeof(pad);
174 pc = t_getstr(info, "pc", &pptr, &limit);
175 if (pc == NULL)
176 pad[0] = '\0';
177 else
178 free(pc);
181 if (cp == 0)
182 return -1;
184 /* scan and convert delay digits (if any) */
185 i = _tputs_convert(&cp, affcnt);
188 * The guts of the string.
190 while (*cp)
191 (*outc)(*cp++, args);
194 * If no delay needed, or output speed is
195 * not comprehensible, then don't try to delay.
197 if (i == 0)
198 return 0;
199 if (ospeed <= 0 || ospeed >= TMSPC10SIZE)
200 return 0;
203 * Round up by a half a character frame,
204 * and then do the delay.
205 * Too bad there are no user program accessible programmed delays.
206 * Transmitting pad characters slows many
207 * terminals down and also loads the system.
209 mspc10 = __tmspc10[ospeed];
210 i += mspc10 / 2;
211 for (i /= mspc10; i > 0; i--)
212 (*outc)(pad[0], args);
214 return 0;