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