xref: /netbsd-src/lib/libcurses/tscroll.c (revision deb6f0161a9109e7de9b519dc8dfb9478668dcdd)
1 /*	$NetBSD: tscroll.c,v 1.14 2017/01/06 13:53:18 roy Exp $	*/
2 
3 /*-
4  * Copyright (c) 1992, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
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.
18  *
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.
30  */
31 
32 #include <sys/cdefs.h>
33 
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)tscroll.c	8.4 (Berkeley) 7/27/94";
37 #else
38 __RCSID("$NetBSD: tscroll.c,v 1.14 2017/01/06 13:53:18 roy Exp $");
39 #endif
40 #endif				/* not lint */
41 
42 #include <string.h>
43 #include <stdarg.h>
44 #include "curses.h"
45 #include "curses_private.h"
46 
47 #define	MAXRETURNSIZE	64
48 
49 char   *
50 __tscroll(const char *cap, int n1, int n2)
51 {
52 
53 	return __parse_cap(cap, n1, n2);
54 }
55 
56 /*
57  * Routines to parse capabilities.  Derived from tgoto.c in termcap(3)
58  * library.  Cap is a string containing printf type escapes to allow
59  * scrolling.  The following escapes are defined for substituting n:
60  *
61  *	%d	as in printf
62  *	%2	like %2d
63  *	%3	like %3d
64  *	%.	gives %c hacking special case characters
65  *	%+x	like %c but adding x first
66  *
67  *	The codes below affect the state but don't use up a value.
68  *
69  *	%>xy	if value > x add y
70  *	%i	increments n
71  *	%%	gives %
72  *	%B	BCD (2 decimal digits encoded in one byte)
73  *	%D	Delta Data (backwards bcd)
74  *
75  * all other characters are ``self-inserting''.
76  *
77  * XXX:
78  *	%r	reverse order of two parameters
79  * is also defined but we don't support it (yet).
80  */
81 char	*
82 __parse_cap (char const *cap, ...)
83 {
84 	va_list	ap;
85 	static char result[MAXRETURNSIZE];
86 	int     c, n;
87 	char   *dp;
88 	int	have_input;
89 
90 	va_start (ap, cap);
91 	n = 0;			/* XXX gcc -Wuninitialized */
92 
93 	if (cap == NULL)
94 		goto err;
95 #ifdef DEBUG
96 	{
97 		int	i;
98 
99 		__CTRACE(__CTRACE_MISC, "__parse_cap: cap = ");
100 		for (i = 0; i < strlen(cap); i++)
101 			__CTRACE(__CTRACE_MISC, "%s", unctrl(cap[i]));
102 		__CTRACE(__CTRACE_MISC, "\n");
103 	}
104 #endif
105 	have_input = 0;
106 	for (dp = result; (c = *cap++) != '\0';) {
107 		if (c != '%') {
108 			*dp++ = c;
109 			continue;
110 		}
111 		switch (c = *cap++) {
112 		case 'n':
113 			if (!have_input) {
114 				n = va_arg(ap, int);
115 				have_input = 1;
116 #ifdef DEBUG
117 				__CTRACE(__CTRACE_MISC,
118 				    "__parse_cap: %%n, val = %d\n", n);
119 #endif
120 			}
121 			n ^= 0140;
122 			continue;
123 		case 'd':
124 			if (!have_input) {
125 				n = va_arg(ap, int);
126 				have_input = 1;
127 #ifdef DEBUG
128 				__CTRACE(__CTRACE_MISC,
129 				    "__parse_cap: %%d, val = %d\n", n);
130 #endif
131 			}
132 			if (n < 10)
133 				goto one;
134 			if (n < 100)
135 				goto two;
136 			/* FALLTHROUGH */
137 		case '3':
138 			if (!have_input) {
139 				n = va_arg(ap, int);
140 				have_input = 1;
141 #ifdef DEBUG
142 				__CTRACE(__CTRACE_MISC,
143 				    "__parse_cap: %%3, val = %d\n", n);
144 #endif
145 			}
146 			*dp++ = (n / 100) | '0';
147 			n %= 100;
148 			/* FALLTHROUGH */
149 		case '2':
150 			if (!have_input) {
151 				n = va_arg(ap, int);
152 				have_input = 1;
153 #ifdef DEBUG
154 				__CTRACE(__CTRACE_MISC,
155 				    "__parse_cap: %%2, val = %d\n", n);
156 #endif
157 			}
158 	two:		*dp++ = n / 10 | '0';
159 	one:		*dp++ = n % 10 | '0';
160 			have_input = 0;
161 			continue;
162 		case '>':
163 			if (!have_input) {
164 				n = va_arg(ap, int);
165 				have_input = 1;
166 #ifdef DEBUG
167 				__CTRACE(__CTRACE_MISC,
168 				    "__parse_cap: %%>, val = %d\n", n);
169 #endif
170 			}
171 			if (n > *cap++)
172 				n += *cap++;
173 			else
174 				cap++;
175 			continue;
176 		case '+':
177 			if (!have_input) {
178 				n = va_arg(ap, int);
179 				have_input = 1;
180 #ifdef DEBUG
181 				__CTRACE(__CTRACE_MISC,
182 				    "__parse_cap: %%+, val = %d\n", n);
183 #endif
184 			}
185 			n += *cap++;
186 			/* FALLTHROUGH */
187 		case '.':
188 			if (!have_input) {
189 				n = va_arg(ap, int);
190 				have_input = 1;
191 #ifdef DEBUG
192 				__CTRACE(__CTRACE_MISC,
193 				    "__parse_cap: %%., val = %d\n", n);
194 #endif
195 			}
196 			*dp++ = n;
197 			have_input = 0;
198 			continue;
199 		case 'i':
200 			if (!have_input) {
201 				n = va_arg(ap, int);
202 				have_input = 1;
203 #ifdef DEBUG
204 				__CTRACE(__CTRACE_MISC,
205 				    "__parse_cap: %%i, val = %d\n", n);
206 #endif
207 			}
208 			n++;
209 			continue;
210 		case '%':
211 			*dp++ = c;
212 			continue;
213 		case 'B':
214 			if (!have_input) {
215 				n = va_arg(ap, int);
216 				have_input = 1;
217 #ifdef DEBUG
218 				__CTRACE(__CTRACE_MISC,
219 				    "__parse_cap: %%B, val = %d\n", n);
220 #endif
221 			}
222 			n = (n / 10 << 4) + n % 10;
223 			continue;
224 		case 'D':
225 			if (!have_input) {
226 				n = va_arg(ap, int);
227 				have_input = 1;
228 #ifdef DEBUG
229 				__CTRACE(__CTRACE_MISC,
230 				    "__parse_cap: %%D, val = %d\n", n);
231 #endif
232 			}
233 			n = n - 2 * (n % 16);
234 			continue;
235 			/*
236 			 * XXX
237 			 * System V terminfo files have lots of extra gunk.
238 			 * The only other one we've seen in capability strings
239 			 * is %pN, and it seems to work okay if we ignore it.
240 			 */
241 		case 'p':
242 			++cap;
243 			continue;
244 		default:
245 			goto err;
246 		}
247 	}
248 	*dp = '\0';
249 	va_end(ap);
250 	return result;
251 
252 err:	va_end(ap);
253 	return (char *)"\0";
254 }
255