mkfs, mkproto: minor improvements
[minix.git] / lib / libterminfo / tputs.c
blob28ec67678fdfc0517cf5b568c6d67db485d133bb
1 /* $NetBSD: tputs.c,v 1.2 2010/02/12 10:36:07 martin Exp $ */
3 /*
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Roy Marples.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: tputs.c,v 1.2 2010/02/12 10:36:07 martin Exp $");
33 #include <assert.h>
34 #include <ctype.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <term_private.h>
38 #include <term.h>
41 * The following array gives the number of tens of milliseconds per
42 * character for each speed as returned by gtty. Thus since 300
43 * baud returns a 7, there are 33.3 milliseconds per char at 300 baud.
45 static const short tmspc10[] = {
46 0, 2000, 1333, 909, 743, 666, 500, 333, 166, 83, 55, 41, 20, 10, 5
49 short ospeed;
50 char PC;
52 static int
53 _ti_calcdelay(const char **str, int affcnt, int *mand)
55 int i;
57 i = 0;
58 /* Convert the delay */
59 while (isdigit(*(const unsigned char *)*str))
60 i = i * 10 + *(*str)++ - '0';
61 i *= 10;
62 if (*(*str) == '.') {
63 (*str)++;
64 if (isdigit(*(const unsigned char *)*str))
65 i += *(*str) - '0';
66 while (isdigit(*(const unsigned char *)*str))
67 (*str)++;
69 if (*(*str) == '*') {
70 (*str)++;
71 i *= affcnt;
72 } else if (*(*str) == '/') {
73 (*str)++;
74 if (mand != NULL)
75 *mand = 1;
77 return i;
80 static void
81 _ti_outputdelay(int delay, short os, char pc,
82 int (*outc)(int, void *), void *args)
84 int mspc10;
86 if (delay < 1 || os < 1 || (size_t)os >= __arraycount(tmspc10))
87 return;
89 mspc10 = tmspc10[os];
90 delay += mspc10 / 2;
91 for (delay /= mspc10; delay > 0; delay--)
92 outc(pc, args);
95 static int
96 _ti_puts(int dodelay, int os, int pc,
97 const char *str, int affcnt, int (*outc)(int, void *), void *args)
99 int taildelay, delay, mand;
101 if (str == NULL)
102 return OK;
104 taildelay = _ti_calcdelay(&str, affcnt, NULL);
106 /* Output the string with embedded delays */
107 for (; *str != '\0'; str++) {
108 if (str[0] != '$' ||
109 str[1] != '<' ||
110 !(isdigit((const unsigned char)str[2]) || str[2] == '.') ||
111 strchr(str + 3, '>') == NULL)
113 outc(*str, args);
114 } else {
115 str += 2;
116 mand = 0;
117 delay = _ti_calcdelay(&str, affcnt, &mand);
118 if (dodelay != 0 || mand != 0)
119 _ti_outputdelay(delay, os, pc, outc, args);
123 /* Delay if needed */
124 if (dodelay)
125 _ti_outputdelay(taildelay, os, pc, outc, args);
127 return OK;
131 ti_puts(const TERMINAL *term, const char *str, int affcnt,
132 int (*outc)(int, void *), void *args)
134 int dodelay;
135 char pc;
137 _DIAGASSERT(term != NULL);
138 _DIAGASSERT(str != NULL);
139 _DIAGASSERT(outc != NULL);
141 dodelay = (str == t_bell(term) ||
142 str == t_flash_screen(term) ||
143 (t_xon_xoff(term) == 0 && t_padding_baud_rate(term) != 0));
145 if (t_pad_char(term) == NULL)
146 pc = '\0';
147 else
148 pc = *t_pad_char(term);
149 return _ti_puts(dodelay, term->_ospeed, pc,
150 str, affcnt, outc, args);
154 ti_putp(const TERMINAL *term, const char *str)
157 _DIAGASSERT(term != NULL);
158 _DIAGASSERT(str != NULL);
159 return ti_puts(term, str, 1, (int (*)(int, void *))putchar, NULL);
163 tputs(const char *str, int affcnt, int (*outc)(int))
166 _DIAGASSERT(str != NULL);
167 _DIAGASSERT(outc != NULL);
168 return _ti_puts(1, ospeed, PC, str, affcnt,
169 (int (*)(int, void *))outc, NULL);
173 putp(const char *str)
176 _DIAGASSERT(str != NULL);
177 return tputs(str, 1, putchar);