xref: /original-bsd/lib/libcurses/tscroll.c (revision 4a4c8f34)
1 /*-
2  * Copyright (c) 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)tscroll.c	5.5 (Berkeley) 05/30/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 	register char *dp;
44 	register int c;
45 	char *cp, result[MAXRETURNSIZE];
46 
47 	if (cap == NULL) {
48 toohard:
49 		/*
50 		 * ``We don't do that under BOZO's big top''
51 		 */
52 		return ("OOPS");
53 	}
54 
55 	cp = (char *) cap;
56 	dp = result;
57 	while (c = *cp++) {
58 		if (c != '%') {
59 			*dp++ = c;
60 			continue;
61 		}
62 		switch (c = *cp++) {
63 		case 'n':
64 			n ^= 0140;
65 			continue;
66 		case 'd':
67 			if (n < 10)
68 				goto one;
69 			if (n < 100)
70 				goto two;
71 			/* fall into... */
72 		case '3':
73 			*dp++ = (n / 100) | '0';
74 			n %= 100;
75 			/* fall into... */
76 		case '2':
77 two:
78 			*dp++ = n / 10 | '0';
79 one:
80 			*dp++ = n % 10 | '0';
81 			continue;
82 		case '>':
83 			if (n > *cp++)
84 				n += *cp++;
85 			else
86 				cp++;
87 			continue;
88 		case '+':
89 			n += *cp++;
90 			/* fall into... */
91 		case '.':
92 			*dp++ = n;
93 			continue;
94 		case 'i':
95 			n++;
96 			continue;
97 		case '%':
98 			*dp++ = c;
99 			continue;
100 
101 		case 'B':
102 			n = (n / 10 << 4) + n % 10;
103 			continue;
104 		case 'D':
105 			n = n - 2 * (n % 16);
106 			continue;
107 		default:
108 			goto toohard;
109 		}
110 	}
111 	*dp = '\0';
112 	return (result);
113 }
114