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