1 /*-
2  * Copyright (c) 1992, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 static char sccsid[] = "@(#)tscroll.c	8.4 (Berkeley) 7/27/94";
32 #endif /* not lint */
33 
34 #include "curses.h"
35 
36 #define	MAXRETURNSIZE	64
37 
38 /*
39  * Routine to perform scrolling.  Derived from tgoto.c in tercamp(3)
40  * library.  Cap is a string containing printf type escapes to allow
41  * scrolling.  The following escapes are defined for substituting n:
42  *
43  *	%d	as in printf
44  *	%2	like %2d
45  *	%3	like %3d
46  *	%.	gives %c hacking special case characters
47  *	%+x	like %c but adding x first
48  *
49  *	The codes below affect the state but don't use up a value.
50  *
51  *	%>xy	if value > x add y
52  *	%i	increments n
53  *	%%	gives %
54  *	%B	BCD (2 decimal digits encoded in one byte)
55  *	%D	Delta Data (backwards bcd)
56  *
57  * all other characters are ``self-inserting''.
58  */
59 char *
__tscroll(cap,n1,n2)60 __tscroll(cap, n1, n2)
61 	const char *cap;
62 	int n1, n2;
63 {
64 	static char result[MAXRETURNSIZE];
65 	int c, n;
66 	char *dp;
67 
68 	if (cap == NULL)
69 		goto err;
70 	for (n = n1, dp = result; (c = *cap++) != '\0';) {
71 		if (c != '%') {
72 			*dp++ = c;
73 			continue;
74 		}
75 		switch (c = *cap++) {
76 		case 'n':
77 			n ^= 0140;
78 			continue;
79 		case 'd':
80 			if (n < 10)
81 				goto one;
82 			if (n < 100)
83 				goto two;
84 			/* FALLTHROUGH */
85 		case '3':
86 			*dp++ = (n / 100) | '0';
87 			n %= 100;
88 			/* FALLTHROUGH */
89 		case '2':
90 two:			*dp++ = n / 10 | '0';
91 one:			*dp++ = n % 10 | '0';
92 			n = n2;
93 			continue;
94 		case '>':
95 			if (n > *cap++)
96 				n += *cap++;
97 			else
98 				cap++;
99 			continue;
100 		case '+':
101 			n += *cap++;
102 			/* FALLTHROUGH */
103 		case '.':
104 			*dp++ = n;
105 			continue;
106 		case 'i':
107 			n++;
108 			continue;
109 		case '%':
110 			*dp++ = c;
111 			continue;
112 		case 'B':
113 			n = (n / 10 << 4) + n % 10;
114 			continue;
115 		case 'D':
116 			n = n - 2 * (n % 16);
117 			continue;
118 		/*
119 		 * XXX
120 		 * System V terminfo files have lots of extra gunk.
121 		 * The only one we've seen in scrolling strings is
122 		 * %pN, and it seems to work okay if we ignore it.
123 		 */
124 		case 'p':
125 			++cap;
126 			continue;
127 		default:
128 			goto err;
129 		}
130 	}
131 	*dp = '\0';
132 	return (result);
133 
134 err:	return("curses: __tscroll failed");
135 }
136