xref: /386bsd/usr/src/lib/libcurses/tscroll.c (revision a2142627)
1 /*-
2  * Copyright (c) 1992, 1993
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.1 (Berkeley) 6/4/93";
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) library.
44  * Cap is a string containing printf type escapes to allow
45  * scrolling.
46  * The following escapes are defined for substituting n:
47  *
48  *	%d	as in printf
49  *	%2	like %2d
50  *	%3	like %3d
51  *	%.	gives %c hacking special case characters
52  *	%+x	like %c but adding x first
53  *
54  *	The codes below affect the state but don't use up a value.
55  *
56  *	%>xy	if value > x add y
57  *	%i	increments n
58  *	%%	gives %
59  *	%B	BCD (2 decimal digits encoded in one byte)
60  *	%D	Delta Data (backwards bcd)
61  *
62  * all other characters are ``self-inserting''.
63  */
64 char *
__tscroll(cap,n)65 __tscroll(cap, n)
66 	const char *cap;
67 	int n;
68 {
69 	static char result[MAXRETURNSIZE];
70 	register char *dp;
71 	register int c;
72 	char *cp;
73 
74 	if (cap == NULL) {
75 toohard:
76 		/*
77 		 * ``We don't do that under BOZO's big top''
78 		 */
79 		return ("OOPS");
80 	}
81 
82 	cp = (char *) cap;
83 	dp = result;
84 	while (c = *cp++) {
85 		if (c != '%') {
86 			*dp++ = c;
87 			continue;
88 		}
89 		switch (c = *cp++) {
90 		case 'n':
91 			n ^= 0140;
92 			continue;
93 		case 'd':
94 			if (n < 10)
95 				goto one;
96 			if (n < 100)
97 				goto two;
98 			/* fall into... */
99 		case '3':
100 			*dp++ = (n / 100) | '0';
101 			n %= 100;
102 			/* fall into... */
103 		case '2':
104 two:
105 			*dp++ = n / 10 | '0';
106 one:
107 			*dp++ = n % 10 | '0';
108 			continue;
109 		case '>':
110 			if (n > *cp++)
111 				n += *cp++;
112 			else
113 				cp++;
114 			continue;
115 		case '+':
116 			n += *cp++;
117 			/* fall into... */
118 		case '.':
119 			*dp++ = n;
120 			continue;
121 		case 'i':
122 			n++;
123 			continue;
124 		case '%':
125 			*dp++ = c;
126 			continue;
127 
128 		case 'B':
129 			n = (n / 10 << 4) + n % 10;
130 			continue;
131 		case 'D':
132 			n = n - 2 * (n % 16);
133 			continue;
134 		default:
135 			goto toohard;
136 		}
137 	}
138 	*dp = '\0';
139 	return (result);
140 }
141