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