1 /* $NetBSD: tscroll.c,v 1.15 2021/09/06 07:03:50 rin 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.15 2021/09/06 07:03:50 rin 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 *
__tscroll(const char * cap,int n1,int n2)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 *
__parse_cap(char const * cap,...)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 __CTRACE(__CTRACE_MISC,
117 "__parse_cap: %%n, val = %d\n", n);
118 }
119 n ^= 0140;
120 continue;
121 case 'd':
122 if (!have_input) {
123 n = va_arg(ap, int);
124 have_input = 1;
125 __CTRACE(__CTRACE_MISC,
126 "__parse_cap: %%d, val = %d\n", n);
127 }
128 if (n < 10)
129 goto one;
130 if (n < 100)
131 goto two;
132 /* FALLTHROUGH */
133 case '3':
134 if (!have_input) {
135 n = va_arg(ap, int);
136 have_input = 1;
137 __CTRACE(__CTRACE_MISC,
138 "__parse_cap: %%3, val = %d\n", n);
139 }
140 *dp++ = (n / 100) | '0';
141 n %= 100;
142 /* FALLTHROUGH */
143 case '2':
144 if (!have_input) {
145 n = va_arg(ap, int);
146 have_input = 1;
147 __CTRACE(__CTRACE_MISC,
148 "__parse_cap: %%2, val = %d\n", n);
149 }
150 two: *dp++ = n / 10 | '0';
151 one: *dp++ = n % 10 | '0';
152 have_input = 0;
153 continue;
154 case '>':
155 if (!have_input) {
156 n = va_arg(ap, int);
157 have_input = 1;
158 __CTRACE(__CTRACE_MISC,
159 "__parse_cap: %%>, val = %d\n", n);
160 }
161 if (n > *cap++)
162 n += *cap++;
163 else
164 cap++;
165 continue;
166 case '+':
167 if (!have_input) {
168 n = va_arg(ap, int);
169 have_input = 1;
170 __CTRACE(__CTRACE_MISC,
171 "__parse_cap: %%+, val = %d\n", n);
172 }
173 n += *cap++;
174 /* FALLTHROUGH */
175 case '.':
176 if (!have_input) {
177 n = va_arg(ap, int);
178 have_input = 1;
179 __CTRACE(__CTRACE_MISC,
180 "__parse_cap: %%., val = %d\n", n);
181 }
182 *dp++ = n;
183 have_input = 0;
184 continue;
185 case 'i':
186 if (!have_input) {
187 n = va_arg(ap, int);
188 have_input = 1;
189 __CTRACE(__CTRACE_MISC,
190 "__parse_cap: %%i, val = %d\n", n);
191 }
192 n++;
193 continue;
194 case '%':
195 *dp++ = c;
196 continue;
197 case 'B':
198 if (!have_input) {
199 n = va_arg(ap, int);
200 have_input = 1;
201 __CTRACE(__CTRACE_MISC,
202 "__parse_cap: %%B, val = %d\n", n);
203 }
204 n = (n / 10 << 4) + n % 10;
205 continue;
206 case 'D':
207 if (!have_input) {
208 n = va_arg(ap, int);
209 have_input = 1;
210 __CTRACE(__CTRACE_MISC,
211 "__parse_cap: %%D, val = %d\n", n);
212 }
213 n = n - 2 * (n % 16);
214 continue;
215 /*
216 * XXX
217 * System V terminfo files have lots of extra gunk.
218 * The only other one we've seen in capability strings
219 * is %pN, and it seems to work okay if we ignore it.
220 */
221 case 'p':
222 ++cap;
223 continue;
224 default:
225 goto err;
226 }
227 }
228 *dp = '\0';
229 va_end(ap);
230 return result;
231
232 err: va_end(ap);
233 return (char *)"\0";
234 }
235