xref: /titanic_50/usr/src/cmd/tip/remcap.c (revision 8d489c7a815fcac696803219572e95aa01532b0f)
17c478bd9Sstevel@tonic-gate /*
2*8d489c7aSmuffin  * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
3*8d489c7aSmuffin  * 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  */
11*8d489c7aSmuffin 
127c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate /*
157c478bd9Sstevel@tonic-gate  * remcap - routines for dealing with the remote host data base
167c478bd9Sstevel@tonic-gate  *
177c478bd9Sstevel@tonic-gate  * derived from termcap
187c478bd9Sstevel@tonic-gate  */
197c478bd9Sstevel@tonic-gate #ifdef USG
207c478bd9Sstevel@tonic-gate #include <sys/types.h>
217c478bd9Sstevel@tonic-gate #include <fcntl.h>	/* for O_RDONLY */
227c478bd9Sstevel@tonic-gate #else
237c478bd9Sstevel@tonic-gate #include <sys/file.h>	/* for O_RDONLY */
247c478bd9Sstevel@tonic-gate #include <ctype.h>
257c478bd9Sstevel@tonic-gate #endif
267c478bd9Sstevel@tonic-gate 
27*8d489c7aSmuffin #include <stdlib.h>
28*8d489c7aSmuffin #include <string.h>
29*8d489c7aSmuffin #include <unistd.h>
30*8d489c7aSmuffin #include <ctype.h>
31*8d489c7aSmuffin 
327c478bd9Sstevel@tonic-gate #ifndef BUFSIZ
337c478bd9Sstevel@tonic-gate #define	BUFSIZ		1024
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate #define	MAXHOP		32		/* max number of tc= indirections */
367c478bd9Sstevel@tonic-gate #define	SYSREMOTE	"/etc/remote"	/* system remote file */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #define	tgetent		rgetent
397c478bd9Sstevel@tonic-gate #define	tnchktc		rnchktc
407c478bd9Sstevel@tonic-gate #define	tnamatch	rnamatch
417c478bd9Sstevel@tonic-gate #define	tgetnum		rgetnum
427c478bd9Sstevel@tonic-gate #define	tgetflag	rgetflag
437c478bd9Sstevel@tonic-gate #define	tgetstr		rgetstr
447c478bd9Sstevel@tonic-gate #define	E_TERMCAP	RM = SYSREMOTE
457c478bd9Sstevel@tonic-gate #define	V_TERMCAP	"REMOTE"
467c478bd9Sstevel@tonic-gate #define	V_TERM		"HOST"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate char	*RM;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * termcap - routines for dealing with the terminal capability data base
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * BUG:		Should use a "last" pointer in tbuf, so that searching
547c478bd9Sstevel@tonic-gate  *		for capabilities alphabetically would not be a n**2/2
557c478bd9Sstevel@tonic-gate  *		process when large numbers of capabilities are given.
567c478bd9Sstevel@tonic-gate  * Note:	If we add a last pointer now we will screw up the
577c478bd9Sstevel@tonic-gate  *		tc capability. We really should compile termcap.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * Essentially all the work here is scanning and decoding escapes
607c478bd9Sstevel@tonic-gate  * in string capabilities.  We don't use stdio because the editor
617c478bd9Sstevel@tonic-gate  * doesn't, and because living w/o it is not hard.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static char *tbuf;
657c478bd9Sstevel@tonic-gate static int hopcount;	/* detect infinite loops in termcap, init 0 */
667c478bd9Sstevel@tonic-gate static char *remotefile;
677c478bd9Sstevel@tonic-gate 
68*8d489c7aSmuffin static char	*tskip(char *);
69*8d489c7aSmuffin static char	*tdecode(char *, char **);
70*8d489c7aSmuffin 
71*8d489c7aSmuffin char	*tgetstr(char *, char **);
72*8d489c7aSmuffin int	getent(char *, char *, char *, int);
73*8d489c7aSmuffin int	tnchktc(void);
74*8d489c7aSmuffin int	tnamatch(char *);
75*8d489c7aSmuffin 
76*8d489c7aSmuffin extern void	myperm(void);
77*8d489c7aSmuffin extern void	userperm(void);
78*8d489c7aSmuffin 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * If we use a user specified entry to get the device name,
817c478bd9Sstevel@tonic-gate  * we need to open the device as the user.
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate int trusted_device = 0;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * Get an entry for terminal name in buffer bp,
877c478bd9Sstevel@tonic-gate  * from the termcap file.  Parse is very rudimentary;
887c478bd9Sstevel@tonic-gate  * we just notice escaped newlines.
897c478bd9Sstevel@tonic-gate  */
90*8d489c7aSmuffin int
tgetent(char * bp,char * name,int len)91*8d489c7aSmuffin tgetent(char *bp, char *name, int len)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	char lbuf[BUFSIZ], *cp, *p;
947c478bd9Sstevel@tonic-gate 	int rc1, rc2;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	trusted_device = 1;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	remotefile = cp = getenv(V_TERMCAP);
997c478bd9Sstevel@tonic-gate 	if (cp == (char *)0 || strcmp(cp, SYSREMOTE) == 0) {
1007c478bd9Sstevel@tonic-gate 		remotefile = cp = SYSREMOTE;
1017c478bd9Sstevel@tonic-gate 		return (getent(bp, name, cp, len));
1027c478bd9Sstevel@tonic-gate 	} else {
1037c478bd9Sstevel@tonic-gate 		if ((rc1 = getent(bp, name, cp, len)) != 1)
1047c478bd9Sstevel@tonic-gate 			*bp = '\0';
1057c478bd9Sstevel@tonic-gate 		remotefile = cp = SYSREMOTE;
1067c478bd9Sstevel@tonic-gate 		rc2 = getent(lbuf, name, cp, sizeof (lbuf));
1077c478bd9Sstevel@tonic-gate 		if (rc1 != 1 && rc2 != 1)
1087c478bd9Sstevel@tonic-gate 			return (rc2);
1097c478bd9Sstevel@tonic-gate 		if (rc2 == 1) {
1107c478bd9Sstevel@tonic-gate 			p = lbuf;
1117c478bd9Sstevel@tonic-gate 			if (rc1 == 1)
1127c478bd9Sstevel@tonic-gate 				while (*p++ != ':')
1137c478bd9Sstevel@tonic-gate 					;
1147c478bd9Sstevel@tonic-gate 			if (strlen(bp) + strlen(p) >= len) {
115*8d489c7aSmuffin 				(void) write(2, "Remcap entry too long\n", 23);
1167c478bd9Sstevel@tonic-gate 				return (-1);
1177c478bd9Sstevel@tonic-gate 			}
118*8d489c7aSmuffin 			(void) strcat(bp, p);
1197c478bd9Sstevel@tonic-gate 		}
1207c478bd9Sstevel@tonic-gate 		tbuf = bp;
1217c478bd9Sstevel@tonic-gate 		return (1);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
125*8d489c7aSmuffin int
getent(char * bp,char * name,char * cp,int len)126*8d489c7aSmuffin getent(char *bp, char *name, char *cp, int len)
1277c478bd9Sstevel@tonic-gate {
128*8d489c7aSmuffin 	int c;
129*8d489c7aSmuffin 	int i = 0, cnt = 0;
1307c478bd9Sstevel@tonic-gate 	char ibuf[BUFSIZ], *cp2;
1317c478bd9Sstevel@tonic-gate 	int tf;
1327c478bd9Sstevel@tonic-gate 	int safe = 1; /* reset only when we open the user's $REMOTE */
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	tbuf = bp;
1357c478bd9Sstevel@tonic-gate 	tf = 0;
1367c478bd9Sstevel@tonic-gate 	/*
1377c478bd9Sstevel@tonic-gate 	 * TERMCAP can have one of two things in it. It can be the
1387c478bd9Sstevel@tonic-gate 	 * name of a file to use instead of /etc/termcap. In this
1397c478bd9Sstevel@tonic-gate 	 * case it better start with a "/". Or it can be an entry to
1407c478bd9Sstevel@tonic-gate 	 * use so we don't have to read the file. In this case it
1417c478bd9Sstevel@tonic-gate 	 * has to already have the newlines crunched out.
1427c478bd9Sstevel@tonic-gate 	 */
1437c478bd9Sstevel@tonic-gate 	if (cp && *cp) {
1447c478bd9Sstevel@tonic-gate 		if (*cp != '/') {
1457c478bd9Sstevel@tonic-gate 			cp2 = getenv(V_TERM);
1467c478bd9Sstevel@tonic-gate 			if (cp2 == (char *)0 || strcmp(name, cp2) == 0) {
1477c478bd9Sstevel@tonic-gate 				if (strstr(cp, "dv=") != 0)
1487c478bd9Sstevel@tonic-gate 					trusted_device = 0;
149*8d489c7aSmuffin 				(void) strncpy(bp, cp, len-1);
1507c478bd9Sstevel@tonic-gate 				bp[len-1] = '\0';
1517c478bd9Sstevel@tonic-gate 				return (tnchktc());
1527c478bd9Sstevel@tonic-gate 			} else
1537c478bd9Sstevel@tonic-gate 				tf = open(E_TERMCAP, O_RDONLY);
1547c478bd9Sstevel@tonic-gate 		} else {
1557c478bd9Sstevel@tonic-gate 			/* open SYSREMOTE as uucp, other files as user */
1567c478bd9Sstevel@tonic-gate 			safe = strcmp(cp, SYSREMOTE) == 0;
1577c478bd9Sstevel@tonic-gate 			if (!safe)
1587c478bd9Sstevel@tonic-gate 				userperm();
1597c478bd9Sstevel@tonic-gate 			tf = open(RM = cp, O_RDONLY);
1607c478bd9Sstevel@tonic-gate 			if (!safe)
1617c478bd9Sstevel@tonic-gate 				myperm();
1627c478bd9Sstevel@tonic-gate 		}
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 	if (tf == 0)
1657c478bd9Sstevel@tonic-gate 		tf = open(E_TERMCAP, O_RDONLY);
1667c478bd9Sstevel@tonic-gate 	if (tf < 0)
1677c478bd9Sstevel@tonic-gate 		return (-1);
1687c478bd9Sstevel@tonic-gate 	for (;;) {
1697c478bd9Sstevel@tonic-gate 		cp = bp;
1707c478bd9Sstevel@tonic-gate 		for (;;) {
1717c478bd9Sstevel@tonic-gate 			if (i == cnt) {
1727c478bd9Sstevel@tonic-gate 				cnt = read(tf, ibuf, BUFSIZ);
1737c478bd9Sstevel@tonic-gate 				if (cnt <= 0) {
174*8d489c7aSmuffin 					(void) close(tf);
1757c478bd9Sstevel@tonic-gate 					return (0);
1767c478bd9Sstevel@tonic-gate 				}
1777c478bd9Sstevel@tonic-gate 				i = 0;
1787c478bd9Sstevel@tonic-gate 			}
1797c478bd9Sstevel@tonic-gate 			c = ibuf[i++];
1807c478bd9Sstevel@tonic-gate 			if (c == '\n') {
1817c478bd9Sstevel@tonic-gate 				if (cp > bp && cp[-1] == '\\') {
1827c478bd9Sstevel@tonic-gate 					cp--;
1837c478bd9Sstevel@tonic-gate 					continue;
1847c478bd9Sstevel@tonic-gate 				}
1857c478bd9Sstevel@tonic-gate 				break;
1867c478bd9Sstevel@tonic-gate 			}
1877c478bd9Sstevel@tonic-gate 			if (cp >= bp+len) {
188*8d489c7aSmuffin 				(void) write(2, "Remcap entry too long\n", 23);
1897c478bd9Sstevel@tonic-gate 				break;
1907c478bd9Sstevel@tonic-gate 			} else
1917c478bd9Sstevel@tonic-gate 				*cp++ = c;
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 		*cp = 0;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 		/*
1967c478bd9Sstevel@tonic-gate 		 * The real work for the match.
1977c478bd9Sstevel@tonic-gate 		 */
1987c478bd9Sstevel@tonic-gate 		if (tnamatch(name)) {
1997c478bd9Sstevel@tonic-gate 			/*
2007c478bd9Sstevel@tonic-gate 			 * if a dv= entry is obtained from $REMOTE,
2017c478bd9Sstevel@tonic-gate 			 * switch off trusted_device status
2027c478bd9Sstevel@tonic-gate 			 */
2037c478bd9Sstevel@tonic-gate 			if (!safe && strstr(bp, "dv=") != 0)
2047c478bd9Sstevel@tonic-gate 				trusted_device = 0;
205*8d489c7aSmuffin 			(void) close(tf);
2067c478bd9Sstevel@tonic-gate 			return (tnchktc());
2077c478bd9Sstevel@tonic-gate 		}
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate  * tnchktc: check the last entry, see if it's tc=xxx. If so,
2137c478bd9Sstevel@tonic-gate  * recursively find xxx and append that entry (minus the names)
2147c478bd9Sstevel@tonic-gate  * to take the place of the tc=xxx entry. This allows termcap
2157c478bd9Sstevel@tonic-gate  * entries to say "like an HP2621 but doesn't turn on the labels".
2167c478bd9Sstevel@tonic-gate  * Note that this works because of the left to right scan.
2177c478bd9Sstevel@tonic-gate  */
218*8d489c7aSmuffin int
tnchktc(void)219*8d489c7aSmuffin tnchktc(void)
2207c478bd9Sstevel@tonic-gate {
221*8d489c7aSmuffin 	char *p, *q;
2227c478bd9Sstevel@tonic-gate 	char tcname[64];	/* name of similar terminal */
2237c478bd9Sstevel@tonic-gate 	char tcbuf[BUFSIZ];
2247c478bd9Sstevel@tonic-gate 	char *holdtbuf = tbuf;
2257c478bd9Sstevel@tonic-gate 	int l;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	p = tbuf + strlen(tbuf) - 2;	/* before the last colon */
2287c478bd9Sstevel@tonic-gate 	while (*--p != ':')
2297c478bd9Sstevel@tonic-gate 		if (p < tbuf) {
230*8d489c7aSmuffin 			(void) write(2, "Bad remcap entry\n", 18);
2317c478bd9Sstevel@tonic-gate 			return (0);
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 	p++;
2347c478bd9Sstevel@tonic-gate 	/* p now points to beginning of last field */
2357c478bd9Sstevel@tonic-gate 	if (p[0] != 't' || p[1] != 'c')
2367c478bd9Sstevel@tonic-gate 		return (1);
237*8d489c7aSmuffin 	(void) strlcpy(tcname, p+3, sizeof (tcname));
2387c478bd9Sstevel@tonic-gate 	q = tcname;
2397c478bd9Sstevel@tonic-gate 	while (*q && *q != ':')
2407c478bd9Sstevel@tonic-gate 		q++;
2417c478bd9Sstevel@tonic-gate 	*q = 0;
2427c478bd9Sstevel@tonic-gate 	if (++hopcount > MAXHOP) {
243*8d489c7aSmuffin 		(void) write(2, "Infinite tc= loop\n", 18);
2447c478bd9Sstevel@tonic-gate 		return (0);
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 	if (getent(tcbuf, tcname, remotefile, sizeof (tcbuf)) != 1) {
2477c478bd9Sstevel@tonic-gate 		if (strcmp(remotefile, SYSREMOTE) == 0)
2487c478bd9Sstevel@tonic-gate 			return (0);
2497c478bd9Sstevel@tonic-gate 		else if (getent(tcbuf, tcname, SYSREMOTE, sizeof (tcbuf)) != 1)
2507c478bd9Sstevel@tonic-gate 			return (0);
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 	for (q = tcbuf; *q++ != ':'; )
2537c478bd9Sstevel@tonic-gate 		;
2547c478bd9Sstevel@tonic-gate 	l = p - holdtbuf + strlen(q);
2557c478bd9Sstevel@tonic-gate 	if (l > BUFSIZ) {
256*8d489c7aSmuffin 		(void) write(2, "Remcap entry too long\n", 23);
2577c478bd9Sstevel@tonic-gate 		q[BUFSIZ - (p-holdtbuf)] = 0;
2587c478bd9Sstevel@tonic-gate 	}
259*8d489c7aSmuffin 	(void) strcpy(p, q);
2607c478bd9Sstevel@tonic-gate 	tbuf = holdtbuf;
2617c478bd9Sstevel@tonic-gate 	return (1);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate /*
2657c478bd9Sstevel@tonic-gate  * Tnamatch deals with name matching.  The first field of the termcap
2667c478bd9Sstevel@tonic-gate  * entry is a sequence of names separated by |'s, so we compare
2677c478bd9Sstevel@tonic-gate  * against each such name.  The normal : terminator after the last
2687c478bd9Sstevel@tonic-gate  * name (before the first field) stops us.
2697c478bd9Sstevel@tonic-gate  */
270*8d489c7aSmuffin int
tnamatch(char * np)271*8d489c7aSmuffin tnamatch(char *np)
2727c478bd9Sstevel@tonic-gate {
273*8d489c7aSmuffin 	char *Np, *Bp;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	Bp = tbuf;
2767c478bd9Sstevel@tonic-gate 	if (*Bp == '#')
2777c478bd9Sstevel@tonic-gate 		return (0);
2787c478bd9Sstevel@tonic-gate 	for (;;) {
2797c478bd9Sstevel@tonic-gate 		for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
2807c478bd9Sstevel@tonic-gate 			continue;
2817c478bd9Sstevel@tonic-gate 		if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
2827c478bd9Sstevel@tonic-gate 			return (1);
2837c478bd9Sstevel@tonic-gate 		while (*Bp && *Bp != ':' && *Bp != '|')
2847c478bd9Sstevel@tonic-gate 			Bp++;
2857c478bd9Sstevel@tonic-gate 		if (*Bp == 0 || *Bp == ':')
2867c478bd9Sstevel@tonic-gate 			return (0);
2877c478bd9Sstevel@tonic-gate 		Bp++;
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate /*
2927c478bd9Sstevel@tonic-gate  * Skip to the next field.  Notice that this is very dumb, not
2937c478bd9Sstevel@tonic-gate  * knowing about \: escapes or any such.  If necessary, :'s can be put
2947c478bd9Sstevel@tonic-gate  * into the termcap file in octal.
2957c478bd9Sstevel@tonic-gate  */
2967c478bd9Sstevel@tonic-gate static char *
tskip(char * bp)297*8d489c7aSmuffin tskip(char *bp)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	while (*bp && *bp != ':')
3017c478bd9Sstevel@tonic-gate 		bp++;
3027c478bd9Sstevel@tonic-gate 	if (*bp == ':') {
3037c478bd9Sstevel@tonic-gate 		do {
3047c478bd9Sstevel@tonic-gate 			bp++;
3057c478bd9Sstevel@tonic-gate 			while (isspace(*bp))
3067c478bd9Sstevel@tonic-gate 				bp++;
3077c478bd9Sstevel@tonic-gate 		} while (*bp == ':');
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 	return (bp);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate  * Return the (numeric) option id.
3147c478bd9Sstevel@tonic-gate  * Numeric options look like
3157c478bd9Sstevel@tonic-gate  *	li#80
3167c478bd9Sstevel@tonic-gate  * i.e. the option string is separated from the numeric value by
3177c478bd9Sstevel@tonic-gate  * a # character.  If the option is not found we return -1.
3187c478bd9Sstevel@tonic-gate  * Note that we handle octal numbers beginning with 0.
3197c478bd9Sstevel@tonic-gate  */
320*8d489c7aSmuffin int
tgetnum(char * id)321*8d489c7aSmuffin tgetnum(char *id)
3227c478bd9Sstevel@tonic-gate {
323*8d489c7aSmuffin 	int i, base;
324*8d489c7aSmuffin 	char *bp = tbuf;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	for (;;) {
3277c478bd9Sstevel@tonic-gate 		bp = tskip(bp);
3287c478bd9Sstevel@tonic-gate 		if (*bp == 0)
3297c478bd9Sstevel@tonic-gate 			return (-1);
3307c478bd9Sstevel@tonic-gate 		if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
3317c478bd9Sstevel@tonic-gate 			continue;
3327c478bd9Sstevel@tonic-gate 		if (*bp == '@')
3337c478bd9Sstevel@tonic-gate 			return (-1);
3347c478bd9Sstevel@tonic-gate 		if (*bp != '#')
3357c478bd9Sstevel@tonic-gate 			continue;
3367c478bd9Sstevel@tonic-gate 		bp++;
3377c478bd9Sstevel@tonic-gate 		base = 10;
3387c478bd9Sstevel@tonic-gate 		if (*bp == '0')
3397c478bd9Sstevel@tonic-gate 			base = 8;
3407c478bd9Sstevel@tonic-gate 		i = 0;
3417c478bd9Sstevel@tonic-gate 		while (isdigit(*bp))
3427c478bd9Sstevel@tonic-gate 			i *= base, i += *bp++ - '0';
3437c478bd9Sstevel@tonic-gate 		return (i);
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /*
3487c478bd9Sstevel@tonic-gate  * Handle a flag option.
3497c478bd9Sstevel@tonic-gate  * Flag options are given "naked", i.e. followed by a : or the end
3507c478bd9Sstevel@tonic-gate  * of the buffer.  Return 1 if we find the option, or 0 if it is
3517c478bd9Sstevel@tonic-gate  * not given.
3527c478bd9Sstevel@tonic-gate  */
353*8d489c7aSmuffin int
tgetflag(char * id)354*8d489c7aSmuffin tgetflag(char *id)
3557c478bd9Sstevel@tonic-gate {
356*8d489c7aSmuffin 	char *bp = tbuf;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	for (;;) {
3597c478bd9Sstevel@tonic-gate 		bp = tskip(bp);
3607c478bd9Sstevel@tonic-gate 		if (!*bp)
3617c478bd9Sstevel@tonic-gate 			return (0);
3627c478bd9Sstevel@tonic-gate 		if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
3637c478bd9Sstevel@tonic-gate 			if (!*bp || *bp == ':')
3647c478bd9Sstevel@tonic-gate 				return (1);
3657c478bd9Sstevel@tonic-gate 			else if (*bp == '@')
3667c478bd9Sstevel@tonic-gate 				return (0);
3677c478bd9Sstevel@tonic-gate 		}
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate /*
3727c478bd9Sstevel@tonic-gate  * Get a string valued option.
3737c478bd9Sstevel@tonic-gate  * These are given as
3747c478bd9Sstevel@tonic-gate  *	cl=^Z
3757c478bd9Sstevel@tonic-gate  * Much decoding is done on the strings, and the strings are
3767c478bd9Sstevel@tonic-gate  * placed in area, which is a ref parameter which is updated.
3777c478bd9Sstevel@tonic-gate  * No checking on area overflow.
3787c478bd9Sstevel@tonic-gate  */
3797c478bd9Sstevel@tonic-gate char *
tgetstr(char * id,char ** area)380*8d489c7aSmuffin tgetstr(char *id, char **area)
3817c478bd9Sstevel@tonic-gate {
382*8d489c7aSmuffin 	char *bp = tbuf;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	for (;;) {
3857c478bd9Sstevel@tonic-gate 		bp = tskip(bp);
3867c478bd9Sstevel@tonic-gate 		if (!*bp)
3877c478bd9Sstevel@tonic-gate 			return (0);
3887c478bd9Sstevel@tonic-gate 		if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
3897c478bd9Sstevel@tonic-gate 			continue;
3907c478bd9Sstevel@tonic-gate 		if (*bp == '@')
3917c478bd9Sstevel@tonic-gate 			return (0);
3927c478bd9Sstevel@tonic-gate 		if (*bp != '=')
3937c478bd9Sstevel@tonic-gate 			continue;
3947c478bd9Sstevel@tonic-gate 		bp++;
3957c478bd9Sstevel@tonic-gate 		return (tdecode(bp, area));
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate /*
4007c478bd9Sstevel@tonic-gate  * Tdecode does the grung work to decode the
4017c478bd9Sstevel@tonic-gate  * string capability escapes.
4027c478bd9Sstevel@tonic-gate  */
4037c478bd9Sstevel@tonic-gate static char *
tdecode(char * str,char ** area)404*8d489c7aSmuffin tdecode(char *str, char **area)
4057c478bd9Sstevel@tonic-gate {
406*8d489c7aSmuffin 	char *cp;
407*8d489c7aSmuffin 	int c;
408*8d489c7aSmuffin 	char *dp;
4097c478bd9Sstevel@tonic-gate 	int i;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	cp = *area;
412*8d489c7aSmuffin 	while ((c = *str++) != 0 && c != ':') {
4137c478bd9Sstevel@tonic-gate 		switch (c) {
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 		case '^':
4167c478bd9Sstevel@tonic-gate 			c = *str++ & 037;
4177c478bd9Sstevel@tonic-gate 			break;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 		case '\\':
4207c478bd9Sstevel@tonic-gate 			dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
4217c478bd9Sstevel@tonic-gate 			c = *str++;
4227c478bd9Sstevel@tonic-gate nextc:
4237c478bd9Sstevel@tonic-gate 			if (*dp++ == c) {
4247c478bd9Sstevel@tonic-gate 				c = *dp++;
4257c478bd9Sstevel@tonic-gate 				break;
4267c478bd9Sstevel@tonic-gate 			}
4277c478bd9Sstevel@tonic-gate 			dp++;
4287c478bd9Sstevel@tonic-gate 			if (*dp)
4297c478bd9Sstevel@tonic-gate 				goto nextc;
4307c478bd9Sstevel@tonic-gate 			if (isdigit(c)) {
4317c478bd9Sstevel@tonic-gate 				c -= '0', i = 2;
4327c478bd9Sstevel@tonic-gate 				do
4337c478bd9Sstevel@tonic-gate 					c <<= 3, c |= *str++ - '0';
434*8d489c7aSmuffin 				while (--i && isdigit(*str))
435*8d489c7aSmuffin 					;
4367c478bd9Sstevel@tonic-gate 			}
4377c478bd9Sstevel@tonic-gate 			break;
4387c478bd9Sstevel@tonic-gate 		}
4397c478bd9Sstevel@tonic-gate 		*cp++ = c;
4407c478bd9Sstevel@tonic-gate 	}
4417c478bd9Sstevel@tonic-gate 	*cp++ = 0;
4427c478bd9Sstevel@tonic-gate 	str = *area;
4437c478bd9Sstevel@tonic-gate 	*area = cp;
4447c478bd9Sstevel@tonic-gate 	return (str);
4457c478bd9Sstevel@tonic-gate }
446