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