xref: /original-bsd/lib/libcurses/tscroll.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)tscroll.c	8.1 (Berkeley) 06/04/93";
10 #endif /* not lint */
11 
12 #include <curses.h>
13 
14 #define	MAXRETURNSIZE	64
15 
16 /*
17  * Routine to perform scrolling.  Derived from tgoto.c in tercamp(3) library.
18  * Cap is a string containing printf type escapes to allow
19  * scrolling.
20  * The following escapes are defined for substituting n:
21  *
22  *	%d	as in printf
23  *	%2	like %2d
24  *	%3	like %3d
25  *	%.	gives %c hacking special case characters
26  *	%+x	like %c but adding x first
27  *
28  *	The codes below affect the state but don't use up a value.
29  *
30  *	%>xy	if value > x add y
31  *	%i	increments n
32  *	%%	gives %
33  *	%B	BCD (2 decimal digits encoded in one byte)
34  *	%D	Delta Data (backwards bcd)
35  *
36  * all other characters are ``self-inserting''.
37  */
38 char *
39 __tscroll(cap, n)
40 	const char *cap;
41 	int n;
42 {
43 	static char result[MAXRETURNSIZE];
44 	register char *dp;
45 	register int c;
46 	char *cp;
47 
48 	if (cap == NULL) {
49 toohard:
50 		/*
51 		 * ``We don't do that under BOZO's big top''
52 		 */
53 		return ("OOPS");
54 	}
55 
56 	cp = (char *) cap;
57 	dp = result;
58 	while (c = *cp++) {
59 		if (c != '%') {
60 			*dp++ = c;
61 			continue;
62 		}
63 		switch (c = *cp++) {
64 		case 'n':
65 			n ^= 0140;
66 			continue;
67 		case 'd':
68 			if (n < 10)
69 				goto one;
70 			if (n < 100)
71 				goto two;
72 			/* fall into... */
73 		case '3':
74 			*dp++ = (n / 100) | '0';
75 			n %= 100;
76 			/* fall into... */
77 		case '2':
78 two:
79 			*dp++ = n / 10 | '0';
80 one:
81 			*dp++ = n % 10 | '0';
82 			continue;
83 		case '>':
84 			if (n > *cp++)
85 				n += *cp++;
86 			else
87 				cp++;
88 			continue;
89 		case '+':
90 			n += *cp++;
91 			/* fall into... */
92 		case '.':
93 			*dp++ = n;
94 			continue;
95 		case 'i':
96 			n++;
97 			continue;
98 		case '%':
99 			*dp++ = c;
100 			continue;
101 
102 		case 'B':
103 			n = (n / 10 << 4) + n % 10;
104 			continue;
105 		case 'D':
106 			n = n - 2 * (n % 16);
107 			continue;
108 		default:
109 			goto toohard;
110 		}
111 	}
112 	*dp = '\0';
113 	return (result);
114 }
115