xref: /illumos-gate/usr/src/cmd/vgrind/vgrindefs.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
37c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
47c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
57c478bd9Sstevel@tonic-gate  */
67c478bd9Sstevel@tonic-gate 
77c478bd9Sstevel@tonic-gate #define	BUFSIZ	1024
87c478bd9Sstevel@tonic-gate #define MAXHOP	32	/* max number of tc= indirections */
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate #include <ctype.h>
117c478bd9Sstevel@tonic-gate #include <locale.h>
127c478bd9Sstevel@tonic-gate #include <string.h>
137c478bd9Sstevel@tonic-gate /*
147c478bd9Sstevel@tonic-gate  * grindcap - routines for dealing with the language definitions data base
157c478bd9Sstevel@tonic-gate  *	(code stolen almost totally from termcap)
167c478bd9Sstevel@tonic-gate  *
177c478bd9Sstevel@tonic-gate  * BUG:		Should use a "last" pointer in tbuf, so that searching
187c478bd9Sstevel@tonic-gate  *		for capabilities alphabetically would not be a n**2/2
197c478bd9Sstevel@tonic-gate  *		process when large numbers of capabilities are given.
207c478bd9Sstevel@tonic-gate  * Note:	If we add a last pointer now we will screw up the
217c478bd9Sstevel@tonic-gate  *		tc capability. We really should compile termcap.
227c478bd9Sstevel@tonic-gate  *
237c478bd9Sstevel@tonic-gate  * Essentially all the work here is scanning and decoding escapes
247c478bd9Sstevel@tonic-gate  * in string capabilities.  We don't use stdio because the editor
257c478bd9Sstevel@tonic-gate  * doesn't, and because living w/o it is not hard.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate static	char *tbuf;
297c478bd9Sstevel@tonic-gate static	char *filename;
307c478bd9Sstevel@tonic-gate static	int hopcount;	/* detect infinite loops in termcap, init 0 */
317c478bd9Sstevel@tonic-gate char	*tgetstr();
327c478bd9Sstevel@tonic-gate char	*getenv();
337c478bd9Sstevel@tonic-gate 
34*e5af7cceScraigm static char *tdecode(char *str, char **area);
35*e5af7cceScraigm static char *tskip(char *bp);
36*e5af7cceScraigm static int tnchktc(void);
37*e5af7cceScraigm static int tnamatch(char *np);
38*e5af7cceScraigm 
397c478bd9Sstevel@tonic-gate static char	*vgrind_msg;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * Get an entry for terminal name in buffer bp,
437c478bd9Sstevel@tonic-gate  * from the termcap file.  Parse is very rudimentary;
447c478bd9Sstevel@tonic-gate  * we just notice escaped newlines.
457c478bd9Sstevel@tonic-gate  */
46*e5af7cceScraigm int
tgetent(char * bp,char * name,char * file)47*e5af7cceScraigm tgetent(char *bp, char *name, char *file)
487c478bd9Sstevel@tonic-gate {
49*e5af7cceScraigm 	char *cp;
50*e5af7cceScraigm 	int c;
51*e5af7cceScraigm 	int i = 0, cnt = 0;
527c478bd9Sstevel@tonic-gate 	char ibuf[BUFSIZ];
537c478bd9Sstevel@tonic-gate 	char *cp2;
547c478bd9Sstevel@tonic-gate 	int tf;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	tbuf = bp;
577c478bd9Sstevel@tonic-gate 	tf = 0;
587c478bd9Sstevel@tonic-gate 	filename = file;
597c478bd9Sstevel@tonic-gate 	tf = open(filename, 0);
607c478bd9Sstevel@tonic-gate 	if (tf < 0)
617c478bd9Sstevel@tonic-gate 		return (-1);
627c478bd9Sstevel@tonic-gate 	for (;;) {
637c478bd9Sstevel@tonic-gate 		cp = bp;
647c478bd9Sstevel@tonic-gate 		for (;;) {
657c478bd9Sstevel@tonic-gate 			if (i == cnt) {
667c478bd9Sstevel@tonic-gate 				cnt = read(tf, ibuf, BUFSIZ);
677c478bd9Sstevel@tonic-gate 				if (cnt <= 0) {
687c478bd9Sstevel@tonic-gate 					close(tf);
697c478bd9Sstevel@tonic-gate 					return (0);
707c478bd9Sstevel@tonic-gate 				}
717c478bd9Sstevel@tonic-gate 				i = 0;
727c478bd9Sstevel@tonic-gate 			}
737c478bd9Sstevel@tonic-gate 			c = ibuf[i++];
747c478bd9Sstevel@tonic-gate 			if (c == '\n') {
757c478bd9Sstevel@tonic-gate 				if (cp > bp && cp[-1] == '\\'){
767c478bd9Sstevel@tonic-gate 					cp--;
777c478bd9Sstevel@tonic-gate 					continue;
787c478bd9Sstevel@tonic-gate 				}
797c478bd9Sstevel@tonic-gate 				break;
807c478bd9Sstevel@tonic-gate 			}
817c478bd9Sstevel@tonic-gate 			if (cp >= bp+BUFSIZ) {
827c478bd9Sstevel@tonic-gate 				vgrind_msg = gettext("Vgrind entry too long\n");
837c478bd9Sstevel@tonic-gate 				write(2, vgrind_msg, strlen(vgrind_msg));
847c478bd9Sstevel@tonic-gate 				break;
857c478bd9Sstevel@tonic-gate 			} else
867c478bd9Sstevel@tonic-gate 				*cp++ = c;
877c478bd9Sstevel@tonic-gate 		}
887c478bd9Sstevel@tonic-gate 		*cp = 0;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 		/*
917c478bd9Sstevel@tonic-gate 		 * The real work for the match.
927c478bd9Sstevel@tonic-gate 		 */
937c478bd9Sstevel@tonic-gate 		if (tnamatch(name)) {
947c478bd9Sstevel@tonic-gate 			close(tf);
957c478bd9Sstevel@tonic-gate 			return(tnchktc());
967c478bd9Sstevel@tonic-gate 		}
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  * tnchktc: check the last entry, see if it's tc=xxx. If so,
1027c478bd9Sstevel@tonic-gate  * recursively find xxx and append that entry (minus the names)
1037c478bd9Sstevel@tonic-gate  * to take the place of the tc=xxx entry. This allows termcap
1047c478bd9Sstevel@tonic-gate  * entries to say "like an HP2621 but doesn't turn on the labels".
1057c478bd9Sstevel@tonic-gate  * Note that this works because of the left to right scan.
1067c478bd9Sstevel@tonic-gate  */
107*e5af7cceScraigm static int
tnchktc(void)108*e5af7cceScraigm tnchktc(void)
1097c478bd9Sstevel@tonic-gate {
110*e5af7cceScraigm 	char *p, *q;
1117c478bd9Sstevel@tonic-gate 	char tcname[16];	/* name of similar terminal */
1127c478bd9Sstevel@tonic-gate 	char tcbuf[BUFSIZ];
1137c478bd9Sstevel@tonic-gate 	char *holdtbuf = tbuf;
1147c478bd9Sstevel@tonic-gate 	int l;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	p = tbuf + strlen(tbuf) - 2;	/* before the last colon */
1177c478bd9Sstevel@tonic-gate 	while (*--p != ':')
1187c478bd9Sstevel@tonic-gate 		if (p<tbuf) {
1197c478bd9Sstevel@tonic-gate 			vgrind_msg = gettext("Bad vgrind entry\n");
1207c478bd9Sstevel@tonic-gate 			write(2, vgrind_msg, strlen(vgrind_msg));
1217c478bd9Sstevel@tonic-gate 			return (0);
1227c478bd9Sstevel@tonic-gate 		}
1237c478bd9Sstevel@tonic-gate 	p++;
1247c478bd9Sstevel@tonic-gate 	/* p now points to beginning of last field */
1257c478bd9Sstevel@tonic-gate 	if (p[0] != 't' || p[1] != 'c')
1267c478bd9Sstevel@tonic-gate 		return(1);
1277c478bd9Sstevel@tonic-gate 	strcpy(tcname,p+3);
1287c478bd9Sstevel@tonic-gate 	q = tcname;
1297c478bd9Sstevel@tonic-gate 	while (q && *q != ':')
1307c478bd9Sstevel@tonic-gate 		q++;
1317c478bd9Sstevel@tonic-gate 	*q = 0;
1327c478bd9Sstevel@tonic-gate 	if (++hopcount > MAXHOP) {
1337c478bd9Sstevel@tonic-gate 		vgrind_msg = gettext("Infinite tc= loop\n");
1347c478bd9Sstevel@tonic-gate 		write(2, vgrind_msg, strlen(vgrind_msg));
1357c478bd9Sstevel@tonic-gate 		return (0);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 	if (tgetent(tcbuf, tcname, filename) != 1)
1387c478bd9Sstevel@tonic-gate 		return(0);
1397c478bd9Sstevel@tonic-gate 	for (q=tcbuf; *q != ':'; q++)
1407c478bd9Sstevel@tonic-gate 		;
1417c478bd9Sstevel@tonic-gate 	l = p - holdtbuf + strlen(q);
1427c478bd9Sstevel@tonic-gate 	if (l > BUFSIZ) {
1437c478bd9Sstevel@tonic-gate 		vgrind_msg = gettext("Vgrind entry too long\n");
1447c478bd9Sstevel@tonic-gate 		write(2, vgrind_msg, strlen(vgrind_msg));
1457c478bd9Sstevel@tonic-gate 		q[BUFSIZ - (p-tbuf)] = 0;
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 	strcpy(p, q+1);
1487c478bd9Sstevel@tonic-gate 	tbuf = holdtbuf;
1497c478bd9Sstevel@tonic-gate 	return(1);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * Tnamatch deals with name matching.  The first field of the termcap
1547c478bd9Sstevel@tonic-gate  * entry is a sequence of names separated by |'s, so we compare
1557c478bd9Sstevel@tonic-gate  * against each such name.  The normal : terminator after the last
1567c478bd9Sstevel@tonic-gate  * name (before the first field) stops us.
1577c478bd9Sstevel@tonic-gate  */
158*e5af7cceScraigm static int
tnamatch(char * np)159*e5af7cceScraigm tnamatch(char *np)
1607c478bd9Sstevel@tonic-gate {
161*e5af7cceScraigm 	char *Np, *Bp;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	Bp = tbuf;
1647c478bd9Sstevel@tonic-gate 	if (*Bp == '#')
1657c478bd9Sstevel@tonic-gate 		return(0);
1667c478bd9Sstevel@tonic-gate 	for (;;) {
1677c478bd9Sstevel@tonic-gate 		for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
1687c478bd9Sstevel@tonic-gate 			continue;
1697c478bd9Sstevel@tonic-gate 		if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
1707c478bd9Sstevel@tonic-gate 			return (1);
1717c478bd9Sstevel@tonic-gate 		while (*Bp && *Bp != ':' && *Bp != '|')
1727c478bd9Sstevel@tonic-gate 			Bp++;
1737c478bd9Sstevel@tonic-gate 		if (*Bp == 0 || *Bp == ':')
1747c478bd9Sstevel@tonic-gate 			return (0);
1757c478bd9Sstevel@tonic-gate 		Bp++;
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate  * Skip to the next field.  Notice that this is very dumb, not
1817c478bd9Sstevel@tonic-gate  * knowing about \: escapes or any such.  If necessary, :'s can be put
1827c478bd9Sstevel@tonic-gate  * into the termcap file in octal.
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate static char *
tskip(char * bp)185*e5af7cceScraigm tskip(char *bp)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	while (*bp && *bp != ':')
1897c478bd9Sstevel@tonic-gate 		bp++;
1907c478bd9Sstevel@tonic-gate 	if (*bp == ':')
1917c478bd9Sstevel@tonic-gate 		bp++;
1927c478bd9Sstevel@tonic-gate 	return (bp);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate /*
1967c478bd9Sstevel@tonic-gate  * Return the (numeric) option id.
1977c478bd9Sstevel@tonic-gate  * Numeric options look like
1987c478bd9Sstevel@tonic-gate  *	li#80
1997c478bd9Sstevel@tonic-gate  * i.e. the option string is separated from the numeric value by
2007c478bd9Sstevel@tonic-gate  * a # character.  If the option is not found we return -1.
2017c478bd9Sstevel@tonic-gate  * Note that we handle octal numbers beginning with 0.
2027c478bd9Sstevel@tonic-gate  */
203*e5af7cceScraigm static int
tgetnum(char * id)204*e5af7cceScraigm tgetnum(char *id)
2057c478bd9Sstevel@tonic-gate {
206*e5af7cceScraigm 	int i, base;
207*e5af7cceScraigm 	char *bp = tbuf;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	for (;;) {
2107c478bd9Sstevel@tonic-gate 		bp = tskip(bp);
2117c478bd9Sstevel@tonic-gate 		if (*bp == 0)
2127c478bd9Sstevel@tonic-gate 			return (-1);
2137c478bd9Sstevel@tonic-gate 		if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
2147c478bd9Sstevel@tonic-gate 			continue;
2157c478bd9Sstevel@tonic-gate 		if (*bp == '@')
2167c478bd9Sstevel@tonic-gate 			return(-1);
2177c478bd9Sstevel@tonic-gate 		if (*bp != '#')
2187c478bd9Sstevel@tonic-gate 			continue;
2197c478bd9Sstevel@tonic-gate 		bp++;
2207c478bd9Sstevel@tonic-gate 		base = 10;
2217c478bd9Sstevel@tonic-gate 		if (*bp == '0')
2227c478bd9Sstevel@tonic-gate 			base = 8;
2237c478bd9Sstevel@tonic-gate 		i = 0;
2247c478bd9Sstevel@tonic-gate 		while (isdigit(*bp))
2257c478bd9Sstevel@tonic-gate 			i *= base, i += *bp++ - '0';
2267c478bd9Sstevel@tonic-gate 		return (i);
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate /*
2317c478bd9Sstevel@tonic-gate  * Handle a flag option.
2327c478bd9Sstevel@tonic-gate  * Flag options are given "naked", i.e. followed by a : or the end
2337c478bd9Sstevel@tonic-gate  * of the buffer.  Return 1 if we find the option, or 0 if it is
2347c478bd9Sstevel@tonic-gate  * not given.
2357c478bd9Sstevel@tonic-gate  */
236*e5af7cceScraigm int
tgetflag(char * id)237*e5af7cceScraigm tgetflag(char *id)
2387c478bd9Sstevel@tonic-gate {
239*e5af7cceScraigm 	char *bp = tbuf;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	for (;;) {
2427c478bd9Sstevel@tonic-gate 		bp = tskip(bp);
2437c478bd9Sstevel@tonic-gate 		if (!*bp)
2447c478bd9Sstevel@tonic-gate 			return (0);
2457c478bd9Sstevel@tonic-gate 		if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
2467c478bd9Sstevel@tonic-gate 			if (!*bp || *bp == ':')
2477c478bd9Sstevel@tonic-gate 				return (1);
2487c478bd9Sstevel@tonic-gate 			else if (*bp == '@')
2497c478bd9Sstevel@tonic-gate 				return(0);
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate  * Get a string valued option.
2567c478bd9Sstevel@tonic-gate  * These are given as
2577c478bd9Sstevel@tonic-gate  *	cl=^Z
2587c478bd9Sstevel@tonic-gate  * Much decoding is done on the strings, and the strings are
2597c478bd9Sstevel@tonic-gate  * placed in area, which is a ref parameter which is updated.
2607c478bd9Sstevel@tonic-gate  * No checking on area overflow.
2617c478bd9Sstevel@tonic-gate  */
2627c478bd9Sstevel@tonic-gate char *
tgetstr(char * id,char ** area)263*e5af7cceScraigm tgetstr(char *id, char **area)
2647c478bd9Sstevel@tonic-gate {
265*e5af7cceScraigm 	char *bp = tbuf;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	for (;;) {
2687c478bd9Sstevel@tonic-gate 		bp = tskip(bp);
2697c478bd9Sstevel@tonic-gate 		if (!*bp)
2707c478bd9Sstevel@tonic-gate 			return (0);
2717c478bd9Sstevel@tonic-gate 		if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
2727c478bd9Sstevel@tonic-gate 			continue;
2737c478bd9Sstevel@tonic-gate 		if (*bp == '@')
2747c478bd9Sstevel@tonic-gate 			return(0);
2757c478bd9Sstevel@tonic-gate 		if (*bp != '=')
2767c478bd9Sstevel@tonic-gate 			continue;
2777c478bd9Sstevel@tonic-gate 		bp++;
2787c478bd9Sstevel@tonic-gate 		return (tdecode(bp, area));
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate /*
2837c478bd9Sstevel@tonic-gate  * Tdecode does the grung work to decode the
2847c478bd9Sstevel@tonic-gate  * string capability escapes.
2857c478bd9Sstevel@tonic-gate  */
2867c478bd9Sstevel@tonic-gate static char *
tdecode(char * str,char ** area)287*e5af7cceScraigm tdecode(char *str, char **area)
2887c478bd9Sstevel@tonic-gate {
289*e5af7cceScraigm 	char *cp;
290*e5af7cceScraigm 	int c;
2917c478bd9Sstevel@tonic-gate 	int i;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	cp = *area;
2947c478bd9Sstevel@tonic-gate 	while (c = *str++) {
2957c478bd9Sstevel@tonic-gate 	    if (c == ':' && *(cp-1) != '\\')
2967c478bd9Sstevel@tonic-gate 		break;
2977c478bd9Sstevel@tonic-gate 	    *cp++ = c;
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 	*cp++ = 0;
3007c478bd9Sstevel@tonic-gate 	str = *area;
3017c478bd9Sstevel@tonic-gate 	*area = cp;
3027c478bd9Sstevel@tonic-gate 	return (str);
3037c478bd9Sstevel@tonic-gate }
304