pkgin_all: script to auto-install all packages
[minix.git] / lib / libcurses / tscroll.c
blobfdba8b132dd2317c76b11a75501acb278b48b924
1 /* $NetBSD: tscroll.c,v 1.13 2007/01/21 13:25:36 jdc Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
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.
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.
32 #include <sys/cdefs.h>
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.13 2007/01/21 13:25:36 jdc Exp $");
39 #endif
40 #endif /* not lint */
42 #include <string.h>
43 #include <stdarg.h>
44 #include "curses.h"
45 #include "curses_private.h"
47 #define MAXRETURNSIZE 64
49 char *
50 __tscroll(const char *cap, int n1, int n2)
52 return (__parse_cap(cap, n1, n2));
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:
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
66 * The codes below affect the state but don't use up a value.
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)
74 * all other characters are ``self-inserting''.
76 * XXX:
77 * %r reverse order of two parameters
78 * is also defined but we don't support it (yet).
80 char *
81 __parse_cap (char const *cap, ...)
83 va_list ap;
84 static char result[MAXRETURNSIZE];
85 int c, n;
86 char *dp;
87 int have_input;
89 va_start (ap, cap);
90 n = 0; /* XXX gcc -Wuninitialized */
92 if (cap == NULL)
93 goto err;
94 #ifdef DEBUG
96 int i;
98 __CTRACE(__CTRACE_MISC, "__parse_cap: cap = ");
99 for (i = 0; i < strlen(cap); i++)
100 __CTRACE(__CTRACE_MISC, "%s", unctrl(cap[i]));
101 __CTRACE(__CTRACE_MISC, "\n");
103 #endif
104 have_input = 0;
105 for (dp = result; (c = *cap++) != '\0';) {
106 if (c != '%') {
107 *dp++ = c;
108 continue;
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(__CTRACE_MISC,
117 "__parse_cap: %%n, val = %d\n", n);
118 #endif
120 n ^= 0140;
121 continue;
122 case 'd':
123 if (!have_input) {
124 n = va_arg (ap, int);
125 have_input = 1;
126 #ifdef DEBUG
127 __CTRACE(__CTRACE_MISC,
128 "__parse_cap: %%d, val = %d\n", n);
129 #endif
131 if (n < 10)
132 goto one;
133 if (n < 100)
134 goto two;
135 /* FALLTHROUGH */
136 case '3':
137 if (!have_input) {
138 n = va_arg (ap, int);
139 have_input = 1;
140 #ifdef DEBUG
141 __CTRACE(__CTRACE_MISC,
142 "__parse_cap: %%3, val = %d\n", n);
143 #endif
145 *dp++ = (n / 100) | '0';
146 n %= 100;
147 /* FALLTHROUGH */
148 case '2':
149 if (!have_input) {
150 n = va_arg (ap, int);
151 have_input = 1;
152 #ifdef DEBUG
153 __CTRACE(__CTRACE_MISC,
154 "__parse_cap: %%2, val = %d\n", n);
155 #endif
157 two: *dp++ = n / 10 | '0';
158 one: *dp++ = n % 10 | '0';
159 have_input = 0;
160 continue;
161 case '>':
162 if (!have_input) {
163 n = va_arg (ap, int);
164 have_input = 1;
165 #ifdef DEBUG
166 __CTRACE(__CTRACE_MISC,
167 "__parse_cap: %%>, val = %d\n", n);
168 #endif
170 if (n > *cap++)
171 n += *cap++;
172 else
173 cap++;
174 continue;
175 case '+':
176 if (!have_input) {
177 n = va_arg (ap, int);
178 have_input = 1;
179 #ifdef DEBUG
180 __CTRACE(__CTRACE_MISC,
181 "__parse_cap: %%+, val = %d\n", n);
182 #endif
184 n += *cap++;
185 /* FALLTHROUGH */
186 case '.':
187 if (!have_input) {
188 n = va_arg (ap, int);
189 have_input = 1;
190 #ifdef DEBUG
191 __CTRACE(__CTRACE_MISC,
192 "__parse_cap: %%., val = %d\n", n);
193 #endif
195 *dp++ = n;
196 have_input = 0;
197 continue;
198 case 'i':
199 if (!have_input) {
200 n = va_arg (ap, int);
201 have_input = 1;
202 #ifdef DEBUG
203 __CTRACE(__CTRACE_MISC,
204 "__parse_cap: %%i, val = %d\n", n);
205 #endif
207 n++;
208 continue;
209 case '%':
210 *dp++ = c;
211 continue;
212 case 'B':
213 if (!have_input) {
214 n = va_arg (ap, int);
215 have_input = 1;
216 #ifdef DEBUG
217 __CTRACE(__CTRACE_MISC,
218 "__parse_cap: %%B, val = %d\n", n);
219 #endif
221 n = (n / 10 << 4) + n % 10;
222 continue;
223 case 'D':
224 if (!have_input) {
225 n = va_arg (ap, int);
226 have_input = 1;
227 #ifdef DEBUG
228 __CTRACE(__CTRACE_MISC,
229 "__parse_cap: %%D, val = %d\n", n);
230 #endif
232 n = n - 2 * (n % 16);
233 continue;
235 * XXX
236 * System V terminfo files have lots of extra gunk.
237 * The only other one we've seen in capability strings
238 * is %pN, and it seems to work okay if we ignore it.
240 case 'p':
241 ++cap;
242 continue;
243 default:
244 goto err;
247 *dp = '\0';
248 va_end (ap);
249 return (result);
251 err: va_end (ap);
252 return ((char *) "\0");