1 /* $NetBSD: tputs.c,v 1.6 2021/12/15 21:07:12 blymn Exp $ */
2
3 /*
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Roy Marples.
8 *
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.
17 *
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.
28 */
29
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: tputs.c,v 1.6 2021/12/15 21:07:12 blymn Exp $");
32
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>
39
40 /*
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.
44 */
45 static const short tmspc10[] = {
46 0, 2000, 1333, 909, 743, 666, 500, 333, 166, 83, 55, 41, 20, 10, 5
47 };
48
49 short ospeed;
50 char PC;
51
52 static int
_ti_calcdelay(const char ** str,int affcnt,int * mand)53 _ti_calcdelay(const char **str, int affcnt, int *mand)
54 {
55 int i;
56
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)++;
68 }
69 if (*(*str) == '*') {
70 (*str)++;
71 i *= affcnt;
72 } else if (*(*str) == '/') {
73 (*str)++;
74 if (mand != NULL)
75 *mand = 1;
76 }
77 return i;
78 }
79
80 static void
_ti_outputdelay(int delay,short os,char pc,int (* outc)(int,void *),void * args)81 _ti_outputdelay(int delay, short os, char pc,
82 int (*outc)(int, void *), void *args)
83 {
84 int mspc10;
85
86 if (delay < 1 || os < 1 || (size_t)os >= __arraycount(tmspc10))
87 return;
88
89 mspc10 = tmspc10[os];
90 delay += mspc10 / 2;
91 for (delay /= mspc10; delay > 0; delay--)
92 outc(pc, args);
93 }
94
95 static int
_ti_puts(int dodelay,short os,char pc,const char * str,int affcnt,int (* outc)(int,void *),void * args)96 _ti_puts(int dodelay, short os, char pc,
97 const char *str, int affcnt, int (*outc)(int, void *), void *args)
98 {
99 int taildelay, delay, mand;
100
101 if (str == NULL)
102 return OK;
103
104 taildelay = _ti_calcdelay(&str, affcnt, NULL);
105
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)
112 {
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);
120 }
121 }
122
123 /* Delay if needed */
124 if (dodelay)
125 _ti_outputdelay(taildelay, os, pc, outc, args);
126
127 return OK;
128 }
129
130 int
ti_puts(const TERMINAL * term,const char * str,int affcnt,int (* outc)(int,void *),void * args)131 ti_puts(const TERMINAL *term, const char *str, int affcnt,
132 int (*outc)(int, void *), void *args)
133 {
134 int dodelay;
135 char pc;
136
137 _DIAGASSERT(term != NULL);
138 _DIAGASSERT(outc != NULL);
139
140 if (str == NULL)
141 return OK;
142
143 dodelay = (str == t_bell(term) ||
144 str == t_flash_screen(term) ||
145 (t_xon_xoff(term) == 0 && t_padding_baud_rate(term) != 0));
146
147 if (t_pad_char(term) == NULL)
148 pc = '\0';
149 else
150 pc = *t_pad_char(term);
151 return _ti_puts(dodelay, term->_ospeed, pc,
152 str, affcnt, outc, args);
153 }
154
155 int
ti_putp(const TERMINAL * term,const char * str)156 ti_putp(const TERMINAL *term, const char *str)
157 {
158
159 _DIAGASSERT(term != NULL);
160 return ti_puts(term, str, 1,
161 (int (*)(int, void *))(void *)putchar, NULL);
162 }
163
164 int
tputs(const char * str,int affcnt,int (* outc)(int))165 tputs(const char *str, int affcnt, int (*outc)(int))
166 {
167
168 _DIAGASSERT(outc != NULL);
169 return _ti_puts(1, ospeed, PC, str, affcnt,
170 (int (*)(int, void *))(void *)outc, NULL);
171 }
172
173 int
putp(const char * str)174 putp(const char *str)
175 {
176
177 return tputs(str, 1, putchar);
178 }
179