xref: /illumos-gate/usr/src/cmd/tip/remcap.c (revision 1e094e1b)
17c478bd9Sstevel@tonic-gate /*
28d489c7aSmuffin  * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
38d489c7aSmuffin  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
87c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
97c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
107c478bd9Sstevel@tonic-gate  */
118d489c7aSmuffin 
127c478bd9Sstevel@tonic-gate /*
137c478bd9Sstevel@tonic-gate  * remcap - routines for dealing with the remote host data base
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  * derived from termcap
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate #ifdef USG
187c478bd9Sstevel@tonic-gate #include <sys/types.h>
197c478bd9Sstevel@tonic-gate #include <fcntl.h>	/* for O_RDONLY */
207c478bd9Sstevel@tonic-gate #else
217c478bd9Sstevel@tonic-gate #include <sys/file.h>	/* for O_RDONLY */
227c478bd9Sstevel@tonic-gate #include <ctype.h>
237c478bd9Sstevel@tonic-gate #endif
247c478bd9Sstevel@tonic-gate 
258d489c7aSmuffin #include <stdlib.h>
268d489c7aSmuffin #include <string.h>
278d489c7aSmuffin #include <unistd.h>
288d489c7aSmuffin #include <ctype.h>
29*1e094e1bSToomas Soome #include "tip.h"
308d489c7aSmuffin 
317c478bd9Sstevel@tonic-gate #ifndef BUFSIZ
327c478bd9Sstevel@tonic-gate #define	BUFSIZ		1024
337c478bd9Sstevel@tonic-gate #endif
347c478bd9Sstevel@tonic-gate #define	MAXHOP		32		/* max number of tc= indirections */
357c478bd9Sstevel@tonic-gate #define	SYSREMOTE	"/etc/remote"	/* system remote file */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #define	tgetent		rgetent
387c478bd9Sstevel@tonic-gate #define	tnchktc		rnchktc
397c478bd9Sstevel@tonic-gate #define	tnamatch	rnamatch
407c478bd9Sstevel@tonic-gate #define	tgetnum		rgetnum
417c478bd9Sstevel@tonic-gate #define	tgetflag	rgetflag
427c478bd9Sstevel@tonic-gate #define	tgetstr		rgetstr
437c478bd9Sstevel@tonic-gate #define	E_TERMCAP	RM = SYSREMOTE
447c478bd9Sstevel@tonic-gate #define	V_TERMCAP	"REMOTE"
457c478bd9Sstevel@tonic-gate #define	V_TERM		"HOST"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * termcap - routines for dealing with the terminal capability data base
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * BUG:		Should use a "last" pointer in tbuf, so that searching
517c478bd9Sstevel@tonic-gate  *		for capabilities alphabetically would not be a n**2/2
527c478bd9Sstevel@tonic-gate  *		process when large numbers of capabilities are given.
537c478bd9Sstevel@tonic-gate  * Note:	If we add a last pointer now we will screw up the
547c478bd9Sstevel@tonic-gate  *		tc capability. We really should compile termcap.
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * Essentially all the work here is scanning and decoding escapes
577c478bd9Sstevel@tonic-gate  * in string capabilities.  We don't use stdio because the editor
587c478bd9Sstevel@tonic-gate  * doesn't, and because living w/o it is not hard.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate 
618d489c7aSmuffin static char *tbuf;
628d489c7aSmuffin static int hopcount;	/* detect infinite loops in termcap, init 0 */
638d489c7aSmuffin static char *remotefile;
648d489c7aSmuffin 
658d489c7aSmuffin static char	*tskip(char *);
668d489c7aSmuffin static char	*tdecode(char *, char **);
678d489c7aSmuffin 
688d489c7aSmuffin char	*tgetstr(char *, char **);
698d489c7aSmuffin int	getent(char *, char *, char *, int);
708d489c7aSmuffin int	tnchktc(void);
718d489c7aSmuffin int	tnamatch(char *);
728d489c7aSmuffin 
738d489c7aSmuffin extern void	myperm(void);
748d489c7aSmuffin extern void	userperm(void);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * If we use a user specified entry to get the device name,
787c478bd9Sstevel@tonic-gate  * we need to open the device as the user.
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate int trusted_device = 0;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * Get an entry for terminal name in buffer bp,
847c478bd9Sstevel@tonic-gate  * from the termcap file.  Parse is very rudimentary;
857c478bd9Sstevel@tonic-gate  * we just notice escaped newlines.
867c478bd9Sstevel@tonic-gate  */
878d489c7aSmuffin int
tgetent(char * bp,char * name,int len)888d489c7aSmuffin tgetent(char *bp, char *name, int len)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	char lbuf[BUFSIZ], *cp, *p;
917c478bd9Sstevel@tonic-gate 	int rc1, rc2;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	trusted_device = 1;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	remotefile = cp = getenv(V_TERMCAP);
967c478bd9Sstevel@tonic-gate 	if (cp == (char *)0 || strcmp(cp, SYSREMOTE) == 0) {
977c478bd9Sstevel@tonic-gate 		remotefile = cp = SYSREMOTE;
987c478bd9Sstevel@tonic-gate 		return (getent(bp, name, cp, len));
997c478bd9Sstevel@tonic-gate 	} else {
1007c478bd9Sstevel@tonic-gate 		if ((rc1 = getent(bp, name, cp, len)) != 1)
1017c478bd9Sstevel@tonic-gate 			*bp = '\0';
1027c478bd9Sstevel@tonic-gate 		remotefile = cp = SYSREMOTE;
1037c478bd9Sstevel@tonic-gate 		rc2 = getent(lbuf, name, cp, sizeof (lbuf));
1047c478bd9Sstevel@tonic-gate 		if (rc1 != 1 && rc2 != 1)
1057c478bd9Sstevel@tonic-gate 			return (rc2);
1067c478bd9Sstevel@tonic-gate 		if (rc2 == 1) {
1077c478bd9Sstevel@tonic-gate 			p = lbuf;
1087c478bd9Sstevel@tonic-gate 			if (rc1 == 1)
1097c478bd9Sstevel@tonic-gate 				while (*p++ != ':')
1107c478bd9Sstevel@tonic-gate 					;
1117c478bd9Sstevel@tonic-gate 			if (strlen(bp) + strlen(p) >= len) {
1128d489c7aSmuffin 				(void) write(2, "Remcap entry too long\n", 23);
1137c478bd9Sstevel@tonic-gate 				return (-1);
1147c478bd9Sstevel@tonic-gate 			}
1158d489c7aSmuffin 			(void) strcat(bp, p);
1167c478bd9Sstevel@tonic-gate 		}
1177c478bd9Sstevel@tonic-gate 		tbuf = bp;
1187c478bd9Sstevel@tonic-gate 		return (1);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1228d489c7aSmuffin int
getent(char * bp,char * name,char * cp,int len)1238d489c7aSmuffin getent(char *bp, char *name, char *cp, int len)
1247c478bd9Sstevel@tonic-gate {
1258d489c7aSmuffin 	int c;
1268d489c7aSmuffin 	int i = 0, cnt = 0;
1277c478bd9Sstevel@tonic-gate 	char ibuf[BUFSIZ], *cp2;
1287c478bd9Sstevel@tonic-gate 	int tf;
1297c478bd9Sstevel@tonic-gate 	int safe = 1; /* reset only when we open the user's $REMOTE */
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	tbuf = bp;
1327c478bd9Sstevel@tonic-gate 	tf = 0;
1337c478bd9Sstevel@tonic-gate 	/*
1347c478bd9Sstevel@tonic-gate 	 * TERMCAP can have one of two things in it. It can be the
1357c478bd9Sstevel@tonic-gate 	 * name of a file to use instead of /etc/termcap. In this
1367c478bd9Sstevel@tonic-gate 	 * case it better start with a "/". Or it can be an entry to
1377c478bd9Sstevel@tonic-gate 	 * use so we don't have to read the file. In this case it
1387c478bd9Sstevel@tonic-gate 	 * has to already have the newlines crunched out.
1397c478bd9Sstevel@tonic-gate 	 */
1407c478bd9Sstevel@tonic-gate 	if (cp && *cp) {
1417c478bd9Sstevel@tonic-gate 		if (*cp != '/') {
1427c478bd9Sstevel@tonic-gate 			cp2 = getenv(V_TERM);
1437c478bd9Sstevel@tonic-gate 			if (cp2 == (char *)0 || strcmp(name, cp2) == 0) {
1447c478bd9Sstevel@tonic-gate 				if (strstr(cp, "dv=") != 0)
1457c478bd9Sstevel@tonic-gate 					trusted_device = 0;
1468d489c7aSmuffin 				(void) strncpy(bp, cp, len-1);
1477c478bd9Sstevel@tonic-gate 				bp[len-1] = '\0';
1487c478bd9Sstevel@tonic-gate 				return (tnchktc());
1497c478bd9Sstevel@tonic-gate 			} else
1507c478bd9Sstevel@tonic-gate 				tf = open(E_TERMCAP, O_RDONLY);
1517c478bd9Sstevel@tonic-gate 		} else {
1527c478bd9Sstevel@tonic-gate 			/* open SYSREMOTE as uucp, other files as user */
1537c478bd9Sstevel@tonic-gate 			safe = strcmp(cp, SYSREMOTE) == 0;
1547c478bd9Sstevel@tonic-gate 			if (!safe)
1557c478bd9Sstevel@tonic-gate 				userperm();
1567c478bd9Sstevel@tonic-gate 			tf = open(RM = cp, O_RDONLY);
1577c478bd9Sstevel@tonic-gate 			if (!safe)
1587c478bd9Sstevel@tonic-gate 				myperm();
1597c478bd9Sstevel@tonic-gate 		}
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 	if (tf == 0)
1627c478bd9Sstevel@tonic-gate 		tf = open(E_TERMCAP, O_RDONLY);
1637c478bd9Sstevel@tonic-gate 	if (tf < 0)
1647c478bd9Sstevel@tonic-gate 		return (-1);
1657c478bd9Sstevel@tonic-gate 	for (;;) {
1667c478bd9Sstevel@tonic-gate 		cp = bp;
1677c478bd9Sstevel@tonic-gate 		for (;;) {
1687c478bd9Sstevel@tonic-gate 			if (i == cnt) {
1697c478bd9Sstevel@tonic-gate 				cnt = read(tf, ibuf, BUFSIZ);
1707c478bd9Sstevel@tonic-gate 				if (cnt <= 0) {
1718d489c7aSmuffin 					(void) close(tf);
1727c478bd9Sstevel@tonic-gate 					return (0);
1737c478bd9Sstevel@tonic-gate 				}
1747c478bd9Sstevel@tonic-gate 				i = 0;
1757c478bd9Sstevel@tonic-gate 			}
1767c478bd9Sstevel@tonic-gate 			c = ibuf[i++];
1777c478bd9Sstevel@tonic-gate 			if (c == '\n') {
1787c478bd9Sstevel@tonic-gate 				if (cp > bp && cp[-1] == '\\') {
1797c478bd9Sstevel@tonic-gate 					cp--;
1807c478bd9Sstevel@tonic-gate 					continue;
1817c478bd9Sstevel@tonic-gate 				}
1827c478bd9Sstevel@tonic-gate 				break;
1837c478bd9Sstevel@tonic-gate 			}
1847c478bd9Sstevel@tonic-gate 			if (cp >= bp+len) {
1858d489c7aSmuffin 				(void) write(2, "Remcap entry too long\n", 23);
1867c478bd9Sstevel@tonic-gate 				break;
1877c478bd9Sstevel@tonic-gate 			} else
1887c478bd9Sstevel@tonic-gate 				*cp++ = c;
1897c478bd9Sstevel@tonic-gate 		}
1907c478bd9Sstevel@tonic-gate 		*cp = 0;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		/*
1937c478bd9Sstevel@tonic-gate 		 * The real work for the match.
1947c478bd9Sstevel@tonic-gate 		 */
1957c478bd9Sstevel@tonic-gate 		if (tnamatch(name)) {
1967c478bd9Sstevel@tonic-gate 			/*
1977c478bd9Sstevel@tonic-gate 			 * if a dv= entry is obtained from $REMOTE,
1987c478bd9Sstevel@tonic-gate 			 * switch off trusted_device status
1997c478bd9Sstevel@tonic-gate 			 */
2007c478bd9Sstevel@tonic-gate 			if (!safe && strstr(bp, "dv=") != 0)
2017c478bd9Sstevel@tonic-gate 				trusted_device = 0;
2028d489c7aSmuffin 			(void) close(tf);
2037c478bd9Sstevel@tonic-gate 			return (tnchktc());
2047c478bd9Sstevel@tonic-gate 		}
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * tnchktc: check the last entry, see if it's tc=xxx. If so,
2107c478bd9Sstevel@tonic-gate  * recursively find xxx and append that entry (minus the names)
2117c478bd9Sstevel@tonic-gate  * to take the place of the tc=xxx entry. This allows termcap
2127c478bd9Sstevel@tonic-gate  * entries to say "like an HP2621 but doesn't turn on the labels".
2137c478bd9Sstevel@tonic-gate  * Note that this works because of the left to right scan.
2147c478bd9Sstevel@tonic-gate  */
2158d489c7aSmuffin int
tnchktc(void)2168d489c7aSmuffin tnchktc(void)
2177c478bd9Sstevel@tonic-gate {
2188d489c7aSmuffin 	char *p, *q;
2197c478bd9Sstevel@tonic-gate 	char tcname[64];	/* name of similar terminal */
2207c478bd9Sstevel@tonic-gate 	char tcbuf[BUFSIZ];
2217c478bd9Sstevel@tonic-gate 	char *holdtbuf = tbuf;
2227c478bd9Sstevel@tonic-gate 	int l;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	p = tbuf + strlen(tbuf) - 2;	/* before the last colon */
2257c478bd9Sstevel@tonic-gate 	while (*--p != ':')
2267c478bd9Sstevel@tonic-gate 		if (p < tbuf) {
2278d489c7aSmuffin 			(void) write(2, "Bad remcap entry\n", 18);
2287c478bd9Sstevel@tonic-gate 			return (0);
2297c478bd9Sstevel@tonic-gate 		}
2307c478bd9Sstevel@tonic-gate 	p++;
2317c478bd9Sstevel@tonic-gate 	/* p now points to beginning of last field */
2327c478bd9Sstevel@tonic-gate 	if (p[0] != 't' || p[1] != 'c')
2337c478bd9Sstevel@tonic-gate 		return (1);
2348d489c7aSmuffin 	(void) strlcpy(tcname, p+3, sizeof (tcname));
2357c478bd9Sstevel@tonic-gate 	q = tcname;
2367c478bd9Sstevel@tonic-gate 	while (*q && *q != ':')
2377c478bd9Sstevel@tonic-gate 		q++;
2387c478bd9Sstevel@tonic-gate 	*q = 0;
2397c478bd9Sstevel@tonic-gate 	if (++hopcount > MAXHOP) {
2408d489c7aSmuffin 		(void) write(2, "Infinite tc= loop\n", 18);
2417c478bd9Sstevel@tonic-gate 		return (0);
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 	if (getent(tcbuf, tcname, remotefile, sizeof (tcbuf)) != 1) {
2447c478bd9Sstevel@tonic-gate 		if (strcmp(remotefile, SYSREMOTE) == 0)
2457c478bd9Sstevel@tonic-gate 			return (0);
2467c478bd9Sstevel@tonic-gate 		else if (getent(tcbuf, tcname, SYSREMOTE, sizeof (tcbuf)) != 1)
2477c478bd9Sstevel@tonic-gate 			return (0);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 	for (q = tcbuf; *q++ != ':'; )
2507c478bd9Sstevel@tonic-gate 		;
2517c478bd9Sstevel@tonic-gate 	l = p - holdtbuf + strlen(q);
2527c478bd9Sstevel@tonic-gate 	if (l > BUFSIZ) {
2538d489c7aSmuffin 		(void) write(2, "Remcap entry too long\n", 23);
2547c478bd9Sstevel@tonic-gate 		q[BUFSIZ - (p-holdtbuf)] = 0;
2557c478bd9Sstevel@tonic-gate 	}
2568d489c7aSmuffin 	(void) strcpy(p, q);
2577c478bd9Sstevel@tonic-gate 	tbuf = holdtbuf;
2587c478bd9Sstevel@tonic-gate 	return (1);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate /*
2627c478bd9Sstevel@tonic-gate  * Tnamatch deals with name matching.  The first field of the termcap
2637c478bd9Sstevel@tonic-gate  * entry is a sequence of names separated by |'s, so we compare
2647c478bd9Sstevel@tonic-gate  * against each such name.  The normal : terminator after the last
2657c478bd9Sstevel@tonic-gate  * name (before the first field) stops us.
2667c478bd9Sstevel@tonic-gate  */
2678d489c7aSmuffin int
tnamatch(char * np)2688d489c7aSmuffin tnamatch(char *np)
2697c478bd9Sstevel@tonic-gate {
2708d489c7aSmuffin 	char *Np, *Bp;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	Bp = tbuf;
2737c478bd9Sstevel@tonic-gate 	if (*Bp == '#')
2747c478bd9Sstevel@tonic-gate 		return (0);
2757c478bd9Sstevel@tonic-gate 	for (;;) {
2767c478bd9Sstevel@tonic-gate 		for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
2777c478bd9Sstevel@tonic-gate 			continue;
2787c478bd9Sstevel@tonic-gate 		if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
2797c478bd9Sstevel@tonic-gate 			return (1);
2807c478bd9Sstevel@tonic-gate 		while (*Bp && *Bp != ':' && *Bp != '|')
2817c478bd9Sstevel@tonic-gate 			Bp++;
2827c478bd9Sstevel@tonic-gate 		if (*Bp == 0 || *Bp == ':')
2837c478bd9Sstevel@tonic-gate 			return (0);
2847c478bd9Sstevel@tonic-gate 		Bp++;
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate  * Skip to the next field.  Notice that this is very dumb, not
2907c478bd9Sstevel@tonic-gate  * knowing about \: escapes or any such.  If necessary, :'s can be put
2917c478bd9Sstevel@tonic-gate  * into the termcap file in octal.
2927c478bd9Sstevel@tonic-gate  */
2937c478bd9Sstevel@tonic-gate static char *
tskip(char * bp)2948d489c7aSmuffin tskip(char *bp)
2957c478bd9Sstevel@tonic-gate {
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	while (*bp && *bp != ':')
2987c478bd9Sstevel@tonic-gate 		bp++;
2997c478bd9Sstevel@tonic-gate 	if (*bp == ':') {
3007c478bd9Sstevel@tonic-gate 		do {
3017c478bd9Sstevel@tonic-gate 			bp++;
3027c478bd9Sstevel@tonic-gate 			while (isspace(*bp))
3037c478bd9Sstevel@tonic-gate 				bp++;
3047c478bd9Sstevel@tonic-gate 		} while (*bp == ':');
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 	return (bp);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate /*
3107c478bd9Sstevel@tonic-gate  * Return the (numeric) option id.
3117c478bd9Sstevel@tonic-gate  * Numeric options look like
3127c478bd9Sstevel@tonic-gate  *	li#80
3137c478bd9Sstevel@tonic-gate  * i.e. the option string is separated from the numeric value by
3147c478bd9Sstevel@tonic-gate  * a # character.  If the option is not found we return -1.
3157c478bd9Sstevel@tonic-gate  * Note that we handle octal numbers beginning with 0.
3167c478bd9Sstevel@tonic-gate  */
3178d489c7aSmuffin int
tgetnum(char * id)3188d489c7aSmuffin tgetnum(char *id)
3197c478bd9Sstevel@tonic-gate {
3208d489c7aSmuffin 	int i, base;
3218d489c7aSmuffin 	char *bp = tbuf;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	for (;;) {
3247c478bd9Sstevel@tonic-gate 		bp = tskip(bp);
3257c478bd9Sstevel@tonic-gate 		if (*bp == 0)
3267c478bd9Sstevel@tonic-gate 			return (-1);
3277c478bd9Sstevel@tonic-gate 		if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
3287c478bd9Sstevel@tonic-gate 			continue;
3297c478bd9Sstevel@tonic-gate 		if (*bp == '@')
3307c478bd9Sstevel@tonic-gate 			return (-1);
3317c478bd9Sstevel@tonic-gate 		if (*bp != '#')
3327c478bd9Sstevel@tonic-gate 			continue;
3337c478bd9Sstevel@tonic-gate 		bp++;
3347c478bd9Sstevel@tonic-gate 		base = 10;
3357c478bd9Sstevel@tonic-gate 		if (*bp == '0')
3367c478bd9Sstevel@tonic-gate 			base = 8;
3377c478bd9Sstevel@tonic-gate 		i = 0;
3387c478bd9Sstevel@tonic-gate 		while (isdigit(*bp))
3397c478bd9Sstevel@tonic-gate 			i *= base, i += *bp++ - '0';
3407c478bd9Sstevel@tonic-gate 		return (i);
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate  * Handle a flag option.
3467c478bd9Sstevel@tonic-gate  * Flag options are given "naked", i.e. followed by a : or the end
3477c478bd9Sstevel@tonic-gate  * of the buffer.  Return 1 if we find the option, or 0 if it is
3487c478bd9Sstevel@tonic-gate  * not given.
3497c478bd9Sstevel@tonic-gate  */
3508d489c7aSmuffin int
tgetflag(char * id)3518d489c7aSmuffin tgetflag(char *id)
3527c478bd9Sstevel@tonic-gate {
3538d489c7aSmuffin 	char *bp = tbuf;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	for (;;) {
3567c478bd9Sstevel@tonic-gate 		bp = tskip(bp);
3577c478bd9Sstevel@tonic-gate 		if (!*bp)
3587c478bd9Sstevel@tonic-gate 			return (0);
3597c478bd9Sstevel@tonic-gate 		if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
3607c478bd9Sstevel@tonic-gate 			if (!*bp || *bp == ':')
3617c478bd9Sstevel@tonic-gate 				return (1);
3627c478bd9Sstevel@tonic-gate 			else if (*bp == '@')
3637c478bd9Sstevel@tonic-gate 				return (0);
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate /*
3697c478bd9Sstevel@tonic-gate  * Get a string valued option.
3707c478bd9Sstevel@tonic-gate  * These are given as
3717c478bd9Sstevel@tonic-gate  *	cl=^Z
3727c478bd9Sstevel@tonic-gate  * Much decoding is done on the strings, and the strings are
3737c478bd9Sstevel@tonic-gate  * placed in area, which is a ref parameter which is updated.
3747c478bd9Sstevel@tonic-gate  * No checking on area overflow.
3757c478bd9Sstevel@tonic-gate  */
3767c478bd9Sstevel@tonic-gate char *
tgetstr(char * id,char ** area)3778d489c7aSmuffin tgetstr(char *id, char **area)
3787c478bd9Sstevel@tonic-gate {
3798d489c7aSmuffin 	char *bp = tbuf;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	for (;;) {
3827c478bd9Sstevel@tonic-gate 		bp = tskip(bp);
3837c478bd9Sstevel@tonic-gate 		if (!*bp)
3847c478bd9Sstevel@tonic-gate 			return (0);
3857c478bd9Sstevel@tonic-gate 		if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
3867c478bd9Sstevel@tonic-gate 			continue;
3877c478bd9Sstevel@tonic-gate 		if (*bp == '@')
3887c478bd9Sstevel@tonic-gate 			return (0);
3897c478bd9Sstevel@tonic-gate 		if (*bp != '=')
3907c478bd9Sstevel@tonic-gate 			continue;
3917c478bd9Sstevel@tonic-gate 		bp++;
3927c478bd9Sstevel@tonic-gate 		return (tdecode(bp, area));
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate /*
3977c478bd9Sstevel@tonic-gate  * Tdecode does the grung work to decode the
3987c478bd9Sstevel@tonic-gate  * string capability escapes.
3997c478bd9Sstevel@tonic-gate  */
4007c478bd9Sstevel@tonic-gate static char *
tdecode(char * str,char ** area)4018d489c7aSmuffin tdecode(char *str, char **area)
4027c478bd9Sstevel@tonic-gate {
4038d489c7aSmuffin 	char *cp;
4048d489c7aSmuffin 	int c;
4058d489c7aSmuffin 	char *dp;
4067c478bd9Sstevel@tonic-gate 	int i;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	cp = *area;
4098d489c7aSmuffin 	while ((c = *str++) != 0 && c != ':') {
4107c478bd9Sstevel@tonic-gate 		switch (c) {
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 		case '^':
4137c478bd9Sstevel@tonic-gate 			c = *str++ & 037;
4147c478bd9Sstevel@tonic-gate 			break;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 		case '\\':
4177c478bd9Sstevel@tonic-gate 			dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
4187c478bd9Sstevel@tonic-gate 			c = *str++;
4197c478bd9Sstevel@tonic-gate nextc:
4207c478bd9Sstevel@tonic-gate 			if (*dp++ == c) {
4217c478bd9Sstevel@tonic-gate 				c = *dp++;
4227c478bd9Sstevel@tonic-gate 				break;
4237c478bd9Sstevel@tonic-gate 			}
4247c478bd9Sstevel@tonic-gate 			dp++;
4257c478bd9Sstevel@tonic-gate 			if (*dp)
4267c478bd9Sstevel@tonic-gate 				goto nextc;
4277c478bd9Sstevel@tonic-gate 			if (isdigit(c)) {
4287c478bd9Sstevel@tonic-gate 				c -= '0', i = 2;
4297c478bd9Sstevel@tonic-gate 				do
4307c478bd9Sstevel@tonic-gate 					c <<= 3, c |= *str++ - '0';
4318d489c7aSmuffin 				while (--i && isdigit(*str))
4328d489c7aSmuffin 					;
4337c478bd9Sstevel@tonic-gate 			}
4347c478bd9Sstevel@tonic-gate 			break;
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 		*cp++ = c;
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 	*cp++ = 0;
4397c478bd9Sstevel@tonic-gate 	str = *area;
4407c478bd9Sstevel@tonic-gate 	*area = cp;
4417c478bd9Sstevel@tonic-gate 	return (str);
4427c478bd9Sstevel@tonic-gate }
443