Home | History | Annotate | Line # | Download | only in libcurses
tscroll.c revision 1.6
      1 /*	$NetBSD: tscroll.c,v 1.6 2000/04/12 21:37:15 jdc Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1992, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #include <stdarg.h>
     38 
     39 #ifndef lint
     40 #if 0
     41 static char sccsid[] = "@(#)tscroll.c	8.4 (Berkeley) 7/27/94";
     42 #else
     43 __RCSID("$NetBSD: tscroll.c,v 1.6 2000/04/12 21:37:15 jdc Exp $");
     44 #endif
     45 #endif				/* not lint */
     46 
     47 #include "curses.h"
     48 #include "curses_private.h"
     49 
     50 #define	MAXRETURNSIZE	64
     51 
     52 char   *
     53 __tscroll(cap, n1, n2)
     54 	const char *cap;
     55 	int     n1, n2;
     56 {
     57 	return (__parse_cap(cap, n1, n2));
     58 }
     59 
     60 /*
     61  * Routines to parse capabilities.  Derived from tgoto.c in termcap(3)
     62  * library.  Cap is a string containing printf type escapes to allow
     63  * scrolling.  The following escapes are defined for substituting n:
     64  *
     65  *	%d	as in printf
     66  *	%2	like %2d
     67  *	%3	like %3d
     68  *	%.	gives %c hacking special case characters
     69  *	%+x	like %c but adding x first
     70  *
     71  *	The codes below affect the state but don't use up a value.
     72  *
     73  *	%>xy	if value > x add y
     74  *	%i	increments n
     75  *	%%	gives %
     76  *	%B	BCD (2 decimal digits encoded in one byte)
     77  *	%D	Delta Data (backwards bcd)
     78  *
     79  * all other characters are ``self-inserting''.
     80  *
     81  * XXX:
     82  *	%r	reverse order of two parameters
     83  * is also defined but we don't support it (yet).
     84  */
     85 char	*
     86 #if __STDC__
     87 __parse_cap (char const *cap, ...)
     88 #else
     89 __parse_cap (cap, va_alist)
     90 	const char	*cap;
     91 	va_dcl
     92 #endif
     93 {
     94 	va_list	ap;
     95 	static char result[MAXRETURNSIZE];
     96 	int     c, n;
     97 	char   *dp;
     98 	int	have_input;
     99 
    100 #if __STDC__
    101 	va_start (ap, cap);
    102 #else
    103 	va_start(ap);
    104 #endif
    105 	if (cap == NULL)
    106 		goto err;
    107 #ifdef DEBUG
    108 	__CTRACE ("__parse_cap: cap = %s\n", cap);
    109 #endif
    110 	have_input = 0;
    111 	for (dp = result; (c = *cap++) != '\0';) {
    112 		if (c != '%') {
    113 			*dp++ = c;
    114 			continue;
    115 		}
    116 		switch (c = *cap++) {
    117 		case 'n':
    118 			if (!have_input) {
    119 				n = va_arg (ap, int);
    120 				have_input = 1;
    121 #ifdef DEBUG
    122 				__CTRACE ("__parse_cap: %%n, val = %d\n", n);
    123 #endif
    124 			}
    125 			n ^= 0140;
    126 			continue;
    127 		case 'd':
    128 			if (!have_input) {
    129 				n = va_arg (ap, int);
    130 				have_input = 1;
    131 #ifdef DEBUG
    132 				__CTRACE ("__parse_cap: %%d, val = %d\n", n);
    133 #endif
    134 			}
    135 			if (n < 10)
    136 				goto one;
    137 			if (n < 100)
    138 				goto two;
    139 			/* FALLTHROUGH */
    140 		case '3':
    141 			if (!have_input) {
    142 				n = va_arg (ap, int);
    143 				have_input = 1;
    144 #ifdef DEBUG
    145 				__CTRACE ("__parse_cap: %%3, val = %d\n", n);
    146 #endif
    147 			}
    148 			*dp++ = (n / 100) | '0';
    149 			n %= 100;
    150 			/* FALLTHROUGH */
    151 		case '2':
    152 			if (!have_input) {
    153 				n = va_arg (ap, int);
    154 				have_input = 1;
    155 #ifdef DEBUG
    156 				__CTRACE ("__parse_cap: %%2, val = %d\n", n);
    157 #endif
    158 			}
    159 	two:		*dp++ = n / 10 | '0';
    160 	one:		*dp++ = n % 10 | '0';
    161 			have_input = 0;
    162 			continue;
    163 		case '>':
    164 			if (!have_input) {
    165 				n = va_arg (ap, int);
    166 				have_input = 1;
    167 #ifdef DEBUG
    168 				__CTRACE ("__parse_cap: %%>, val = %d\n", n);
    169 #endif
    170 			}
    171 			if (n > *cap++)
    172 				n += *cap++;
    173 			else
    174 				cap++;
    175 			continue;
    176 		case '+':
    177 			if (!have_input) {
    178 				n = va_arg (ap, int);
    179 				have_input = 1;
    180 #ifdef DEBUG
    181 				__CTRACE ("__parse_cap: %%+, val = %d\n", n);
    182 #endif
    183 			}
    184 			n += *cap++;
    185 			/* FALLTHROUGH */
    186 		case '.':
    187 			if (!have_input) {
    188 				n = va_arg (ap, int);
    189 				have_input = 1;
    190 #ifdef DEBUG
    191 				__CTRACE ("__parse_cap: %%., val = %d\n", n);
    192 #endif
    193 			}
    194 			*dp++ = n;
    195 			have_input = 0;
    196 			continue;
    197 		case 'i':
    198 			if (!have_input) {
    199 				n = va_arg (ap, int);
    200 				have_input = 1;
    201 #ifdef DEBUG
    202 				__CTRACE ("__parse_cap: %%i, val = %d\n", n);
    203 #endif
    204 			}
    205 			n++;
    206 			continue;
    207 		case '%':
    208 			*dp++ = c;
    209 			continue;
    210 		case 'B':
    211 			if (!have_input) {
    212 				n = va_arg (ap, int);
    213 				have_input = 1;
    214 #ifdef DEBUG
    215 				__CTRACE ("__parse_cap: %%B, val = %d\n", n);
    216 #endif
    217 			}
    218 			n = (n / 10 << 4) + n % 10;
    219 			continue;
    220 		case 'D':
    221 			if (!have_input) {
    222 				n = va_arg (ap, int);
    223 				have_input = 1;
    224 #ifdef DEBUG
    225 				__CTRACE ("__parse_cap: %%D, val = %d\n", n);
    226 #endif
    227 			}
    228 			n = n - 2 * (n % 16);
    229 			continue;
    230 			/*
    231 			 * XXX
    232 			 * System V terminfo files have lots of extra gunk.
    233 			 * The only other one we've seen in capability strings
    234 			 * is %pN, and it seems to work okay if we ignore it.
    235 			 */
    236 		case 'p':
    237 			++cap;
    238 			continue;
    239 		default:
    240 			goto err;
    241 		}
    242 	}
    243 	*dp = '\0';
    244 	va_end (ap);
    245 	return (result);
    246 
    247 err:	va_end (ap);
    248 	return ("\0");
    249 }
    250