xref: /minix3/lib/libterminfo/tputs.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1 /* $NetBSD: tputs.c,v 1.3 2013/06/07 13:16:18 roy 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.3 2013/06/07 13:16:18 roy 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,int os,int pc,const char * str,int affcnt,int (* outc)(int,void *),void * args)96 _ti_puts(int dodelay, int os, int 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(str != NULL);
139 	_DIAGASSERT(outc != NULL);
140 
141 	dodelay = (str == t_bell(term) ||
142 	    str == t_flash_screen(term) ||
143 	    (t_xon_xoff(term) == 0 && t_padding_baud_rate(term) != 0));
144 
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);
151 }
152 
153 int
ti_putp(const TERMINAL * term,const char * str)154 ti_putp(const TERMINAL *term, const char *str)
155 {
156 
157 	_DIAGASSERT(term != NULL);
158 	_DIAGASSERT(str != NULL);
159 	return ti_puts(term, str, 1, (int (*)(int, void *))putchar, NULL);
160 }
161 
162 int
tputs(const char * str,int affcnt,int (* outc)(int))163 tputs(const char *str, int affcnt, int (*outc)(int))
164 {
165 
166 	_DIAGASSERT(str != NULL);
167 	_DIAGASSERT(outc != NULL);
168 	return _ti_puts(1, ospeed, PC, str, affcnt,
169 	    (int (*)(int, void *))outc, NULL);
170 }
171 
172 int
putp(const char * str)173 putp(const char *str)
174 {
175 
176 	_DIAGASSERT(str != NULL);
177 	return tputs(str, 1, putchar);
178 }
179