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