xref: /freebsd/usr.sbin/rtadvd/advcap.c (revision 784bddbc5bca158d2fb57eed7c5e4ceebd49ff8b)
1ae326725SJun-ichiro itojun Hagino /*	$FreeBSD$	*/
2bb58b617SHajimu UMEMOTO /*	$KAME: advcap.c,v 1.11 2003/05/19 09:46:50 keiichi Exp $	*/
3b26e03e9SKris Kennaway 
49a4365d0SYoshinobu Inoue /*
59a4365d0SYoshinobu Inoue  * Copyright (c) 1983 The Regents of the University of California.
69a4365d0SYoshinobu Inoue  * All rights reserved.
79a4365d0SYoshinobu Inoue  *
89a4365d0SYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
99a4365d0SYoshinobu Inoue  * modification, are permitted provided that the following conditions
109a4365d0SYoshinobu Inoue  * are met:
119a4365d0SYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
129a4365d0SYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
139a4365d0SYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
149a4365d0SYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
159a4365d0SYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
169a4365d0SYoshinobu Inoue  * 4. Neither the name of the University nor the names of its contributors
179a4365d0SYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
189a4365d0SYoshinobu Inoue  *    without specific prior written permission.
199a4365d0SYoshinobu Inoue  *
209a4365d0SYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
219a4365d0SYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
229a4365d0SYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
239a4365d0SYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
249a4365d0SYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
259a4365d0SYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
269a4365d0SYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
279a4365d0SYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
289a4365d0SYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
299a4365d0SYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
309a4365d0SYoshinobu Inoue  * SUCH DAMAGE.
319a4365d0SYoshinobu Inoue  */
329a4365d0SYoshinobu Inoue 
339a4365d0SYoshinobu Inoue /*
349a4365d0SYoshinobu Inoue  * remcap - routines for dealing with the remote host data base
359a4365d0SYoshinobu Inoue  *
369a4365d0SYoshinobu Inoue  * derived from termcap
379a4365d0SYoshinobu Inoue  */
389a4365d0SYoshinobu Inoue #include <sys/types.h>
399a4365d0SYoshinobu Inoue #include <sys/uio.h>
409a4365d0SYoshinobu Inoue #include <unistd.h>
419a4365d0SYoshinobu Inoue #include <fcntl.h>
429a4365d0SYoshinobu Inoue #include <ctype.h>
439a4365d0SYoshinobu Inoue #include <stdlib.h>
449a4365d0SYoshinobu Inoue #include <stdio.h>
459a4365d0SYoshinobu Inoue #include <syslog.h>
469a4365d0SYoshinobu Inoue #include <errno.h>
479a4365d0SYoshinobu Inoue #include <string.h>
489a4365d0SYoshinobu Inoue #include "pathnames.h"
499a4365d0SYoshinobu Inoue 
509a4365d0SYoshinobu Inoue #ifndef BUFSIZ
519a4365d0SYoshinobu Inoue #define	BUFSIZ		1024
529a4365d0SYoshinobu Inoue #endif
539a4365d0SYoshinobu Inoue #define MAXHOP		32		/* max number of tc= indirections */
549a4365d0SYoshinobu Inoue 
559a4365d0SYoshinobu Inoue #define	tgetent		agetent
569a4365d0SYoshinobu Inoue #define	tnchktc		anchktc
579a4365d0SYoshinobu Inoue #define	tnamatch	anamatch
589a4365d0SYoshinobu Inoue #define	tgetnum		agetnum
599a4365d0SYoshinobu Inoue #define	tgetflag	agetflag
609a4365d0SYoshinobu Inoue #define	tgetstr		agetstr
619a4365d0SYoshinobu Inoue 
62b26e03e9SKris Kennaway #if 0
63b26e03e9SKris Kennaway #define V_TERMCAP	"REMOTE"
64b26e03e9SKris Kennaway #define V_TERM		"HOST"
65b26e03e9SKris Kennaway #endif
66b26e03e9SKris Kennaway 
679a4365d0SYoshinobu Inoue char	*RM;
689a4365d0SYoshinobu Inoue 
699a4365d0SYoshinobu Inoue /*
709a4365d0SYoshinobu Inoue  * termcap - routines for dealing with the terminal capability data base
719a4365d0SYoshinobu Inoue  *
729a4365d0SYoshinobu Inoue  * BUG:		Should use a "last" pointer in tbuf, so that searching
739a4365d0SYoshinobu Inoue  *		for capabilities alphabetically would not be a n**2/2
749a4365d0SYoshinobu Inoue  *		process when large numbers of capabilities are given.
759a4365d0SYoshinobu Inoue  * Note:	If we add a last pointer now we will screw up the
769a4365d0SYoshinobu Inoue  *		tc capability. We really should compile termcap.
779a4365d0SYoshinobu Inoue  *
789a4365d0SYoshinobu Inoue  * Essentially all the work here is scanning and decoding escapes
799a4365d0SYoshinobu Inoue  * in string capabilities.  We don't use stdio because the editor
809a4365d0SYoshinobu Inoue  * doesn't, and because living w/o it is not hard.
819a4365d0SYoshinobu Inoue  */
829a4365d0SYoshinobu Inoue 
839a4365d0SYoshinobu Inoue static	char *tbuf;
849a4365d0SYoshinobu Inoue static	int hopcount;	/* detect infinite loops in termcap, init 0 */
859a4365d0SYoshinobu Inoue 
869a4365d0SYoshinobu Inoue static	char *remotefile;
879a4365d0SYoshinobu Inoue 
889a4365d0SYoshinobu Inoue extern char *conffile;
899a4365d0SYoshinobu Inoue 
90784bddbcSKevin Lo int tgetent(char *, char *);
91784bddbcSKevin Lo int getent(char *, char *, char *);
92784bddbcSKevin Lo int tnchktc(void);
93784bddbcSKevin Lo int tnamatch(char *);
94784bddbcSKevin Lo static char *tskip(char *);
95784bddbcSKevin Lo int64_t tgetnum(char *);
96784bddbcSKevin Lo int tgetflag(char *);
97784bddbcSKevin Lo char *tgetstr(char *, char **);
98784bddbcSKevin Lo static char *tdecode(char *, char **);
999a4365d0SYoshinobu Inoue 
1009a4365d0SYoshinobu Inoue /*
1019a4365d0SYoshinobu Inoue  * Get an entry for terminal name in buffer bp,
1029a4365d0SYoshinobu Inoue  * from the termcap file.  Parse is very rudimentary;
1039a4365d0SYoshinobu Inoue  * we just notice escaped newlines.
1049a4365d0SYoshinobu Inoue  */
1059a4365d0SYoshinobu Inoue int
1069a4365d0SYoshinobu Inoue tgetent(bp, name)
1079a4365d0SYoshinobu Inoue 	char *bp, *name;
1089a4365d0SYoshinobu Inoue {
1099a4365d0SYoshinobu Inoue 	char *cp;
1109a4365d0SYoshinobu Inoue 
1119a4365d0SYoshinobu Inoue 	remotefile = cp = conffile ? conffile : _PATH_RTADVDCONF;
1129a4365d0SYoshinobu Inoue 	return (getent(bp, name, cp));
1139a4365d0SYoshinobu Inoue }
1149a4365d0SYoshinobu Inoue 
1159a4365d0SYoshinobu Inoue int
1169a4365d0SYoshinobu Inoue getent(bp, name, cp)
1179a4365d0SYoshinobu Inoue 	char *bp, *name, *cp;
1189a4365d0SYoshinobu Inoue {
11981982097SHajimu UMEMOTO 	int c;
12081982097SHajimu UMEMOTO 	int i = 0, cnt = 0;
1219a4365d0SYoshinobu Inoue 	char ibuf[BUFSIZ];
1229a4365d0SYoshinobu Inoue 	int tf;
1239a4365d0SYoshinobu Inoue 
1249a4365d0SYoshinobu Inoue 	tbuf = bp;
1259a4365d0SYoshinobu Inoue 	tf = 0;
1269a4365d0SYoshinobu Inoue 	/*
1279a4365d0SYoshinobu Inoue 	 * TERMCAP can have one of two things in it. It can be the
1289a4365d0SYoshinobu Inoue 	 * name of a file to use instead of /etc/termcap. In this
1299a4365d0SYoshinobu Inoue 	 * case it better start with a "/". Or it can be an entry to
1309a4365d0SYoshinobu Inoue 	 * use so we don't have to read the file. In this case it
1319a4365d0SYoshinobu Inoue 	 * has to already have the newlines crunched out.
1329a4365d0SYoshinobu Inoue 	 */
1339a4365d0SYoshinobu Inoue 	if (cp && *cp) {
1349a4365d0SYoshinobu Inoue 		tf = open(RM = cp, O_RDONLY);
1359a4365d0SYoshinobu Inoue 	}
1369a4365d0SYoshinobu Inoue 	if (tf < 0) {
137b26e03e9SKris Kennaway 		syslog(LOG_INFO,
1381533bed0SHajimu UMEMOTO 		       "<%s> open: %s", __func__, strerror(errno));
1399a4365d0SYoshinobu Inoue 		return (-2);
1409a4365d0SYoshinobu Inoue 	}
1419a4365d0SYoshinobu Inoue 	for (;;) {
1429a4365d0SYoshinobu Inoue 		cp = bp;
1439a4365d0SYoshinobu Inoue 		for (;;) {
1449a4365d0SYoshinobu Inoue 			if (i == cnt) {
1459a4365d0SYoshinobu Inoue 				cnt = read(tf, ibuf, BUFSIZ);
1469a4365d0SYoshinobu Inoue 				if (cnt <= 0) {
1479a4365d0SYoshinobu Inoue 					close(tf);
1489a4365d0SYoshinobu Inoue 					return (0);
1499a4365d0SYoshinobu Inoue 				}
1509a4365d0SYoshinobu Inoue 				i = 0;
1519a4365d0SYoshinobu Inoue 			}
1529a4365d0SYoshinobu Inoue 			c = ibuf[i++];
1539a4365d0SYoshinobu Inoue 			if (c == '\n') {
1549a4365d0SYoshinobu Inoue 				if (cp > bp && cp[-1] == '\\') {
1559a4365d0SYoshinobu Inoue 					cp--;
1569a4365d0SYoshinobu Inoue 					continue;
1579a4365d0SYoshinobu Inoue 				}
1589a4365d0SYoshinobu Inoue 				break;
1599a4365d0SYoshinobu Inoue 			}
1609a4365d0SYoshinobu Inoue 			if (cp >= bp + BUFSIZ) {
161e1b4d8d0SSheldon Hearn 				write(STDERR_FILENO, "Remcap entry too long\n",
162e1b4d8d0SSheldon Hearn 				      23);
1639a4365d0SYoshinobu Inoue 				break;
1649a4365d0SYoshinobu Inoue 			} else
1659a4365d0SYoshinobu Inoue 				*cp++ = c;
1669a4365d0SYoshinobu Inoue 		}
1679a4365d0SYoshinobu Inoue 		*cp = 0;
1689a4365d0SYoshinobu Inoue 
1699a4365d0SYoshinobu Inoue 		/*
1709a4365d0SYoshinobu Inoue 		 * The real work for the match.
1719a4365d0SYoshinobu Inoue 		 */
1729a4365d0SYoshinobu Inoue 		if (tnamatch(name)) {
1739a4365d0SYoshinobu Inoue 			close(tf);
1749a4365d0SYoshinobu Inoue 			return (tnchktc());
1759a4365d0SYoshinobu Inoue 		}
1769a4365d0SYoshinobu Inoue 	}
1779a4365d0SYoshinobu Inoue }
1789a4365d0SYoshinobu Inoue 
1799a4365d0SYoshinobu Inoue /*
1809a4365d0SYoshinobu Inoue  * tnchktc: check the last entry, see if it's tc=xxx. If so,
1819a4365d0SYoshinobu Inoue  * recursively find xxx and append that entry (minus the names)
1829a4365d0SYoshinobu Inoue  * to take the place of the tc=xxx entry. This allows termcap
1839a4365d0SYoshinobu Inoue  * entries to say "like an HP2621 but doesn't turn on the labels".
1849a4365d0SYoshinobu Inoue  * Note that this works because of the left to right scan.
1859a4365d0SYoshinobu Inoue  */
1869a4365d0SYoshinobu Inoue int
1879a4365d0SYoshinobu Inoue tnchktc()
1889a4365d0SYoshinobu Inoue {
18981982097SHajimu UMEMOTO 	char *p, *q;
1909a4365d0SYoshinobu Inoue 	char tcname[16];	/* name of similar terminal */
1919a4365d0SYoshinobu Inoue 	char tcbuf[BUFSIZ];
1929a4365d0SYoshinobu Inoue 	char *holdtbuf = tbuf;
1939a4365d0SYoshinobu Inoue 	int l;
1949a4365d0SYoshinobu Inoue 
1959a4365d0SYoshinobu Inoue 	p = tbuf + strlen(tbuf) - 2;	/* before the last colon */
1969a4365d0SYoshinobu Inoue 	while (*--p != ':')
1979a4365d0SYoshinobu Inoue 		if (p < tbuf) {
198e1b4d8d0SSheldon Hearn 			write(STDERR_FILENO, "Bad remcap entry\n", 18);
1999a4365d0SYoshinobu Inoue 			return (0);
2009a4365d0SYoshinobu Inoue 		}
2019a4365d0SYoshinobu Inoue 	p++;
2029a4365d0SYoshinobu Inoue 	/* p now points to beginning of last field */
2039a4365d0SYoshinobu Inoue 	if (p[0] != 't' || p[1] != 'c')
2049a4365d0SYoshinobu Inoue 		return (1);
205bb58b617SHajimu UMEMOTO 	strlcpy(tcname, p + 3, sizeof tcname);
2069a4365d0SYoshinobu Inoue 	q = tcname;
2079a4365d0SYoshinobu Inoue 	while (*q && *q != ':')
2089a4365d0SYoshinobu Inoue 		q++;
2099a4365d0SYoshinobu Inoue 	*q = 0;
2109a4365d0SYoshinobu Inoue 	if (++hopcount > MAXHOP) {
211e1b4d8d0SSheldon Hearn 		write(STDERR_FILENO, "Infinite tc= loop\n", 18);
2129a4365d0SYoshinobu Inoue 		return (0);
2139a4365d0SYoshinobu Inoue 	}
2149a4365d0SYoshinobu Inoue 	if (getent(tcbuf, tcname, remotefile) != 1) {
2159a4365d0SYoshinobu Inoue 		return (0);
2169a4365d0SYoshinobu Inoue 	}
2179a4365d0SYoshinobu Inoue 	for (q = tcbuf; *q++ != ':'; )
2189a4365d0SYoshinobu Inoue 		;
2199a4365d0SYoshinobu Inoue 	l = p - holdtbuf + strlen(q);
2209a4365d0SYoshinobu Inoue 	if (l > BUFSIZ) {
221e1b4d8d0SSheldon Hearn 		write(STDERR_FILENO, "Remcap entry too long\n", 23);
2229a4365d0SYoshinobu Inoue 		q[BUFSIZ - (p-holdtbuf)] = 0;
2239a4365d0SYoshinobu Inoue 	}
2249a4365d0SYoshinobu Inoue 	strcpy(p, q);
2259a4365d0SYoshinobu Inoue 	tbuf = holdtbuf;
2269a4365d0SYoshinobu Inoue 	return (1);
2279a4365d0SYoshinobu Inoue }
2289a4365d0SYoshinobu Inoue 
2299a4365d0SYoshinobu Inoue /*
2309a4365d0SYoshinobu Inoue  * Tnamatch deals with name matching.  The first field of the termcap
2319a4365d0SYoshinobu Inoue  * entry is a sequence of names separated by |'s, so we compare
2329a4365d0SYoshinobu Inoue  * against each such name.  The normal : terminator after the last
2339a4365d0SYoshinobu Inoue  * name (before the first field) stops us.
2349a4365d0SYoshinobu Inoue  */
2359a4365d0SYoshinobu Inoue int
2369a4365d0SYoshinobu Inoue tnamatch(np)
2379a4365d0SYoshinobu Inoue 	char *np;
2389a4365d0SYoshinobu Inoue {
23981982097SHajimu UMEMOTO 	char *Np, *Bp;
2409a4365d0SYoshinobu Inoue 
2419a4365d0SYoshinobu Inoue 	Bp = tbuf;
2429a4365d0SYoshinobu Inoue 	if (*Bp == '#')
2439a4365d0SYoshinobu Inoue 		return (0);
2449a4365d0SYoshinobu Inoue 	for (;;) {
2459a4365d0SYoshinobu Inoue 		for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
2469a4365d0SYoshinobu Inoue 			continue;
2479a4365d0SYoshinobu Inoue 		if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
2489a4365d0SYoshinobu Inoue 			return (1);
2499a4365d0SYoshinobu Inoue 		while (*Bp && *Bp != ':' && *Bp != '|')
2509a4365d0SYoshinobu Inoue 			Bp++;
2519a4365d0SYoshinobu Inoue 		if (*Bp == 0 || *Bp == ':')
2529a4365d0SYoshinobu Inoue 			return (0);
2539a4365d0SYoshinobu Inoue 		Bp++;
2549a4365d0SYoshinobu Inoue 	}
2559a4365d0SYoshinobu Inoue }
2569a4365d0SYoshinobu Inoue 
2579a4365d0SYoshinobu Inoue /*
2589a4365d0SYoshinobu Inoue  * Skip to the next field.  Notice that this is very dumb, not
2599a4365d0SYoshinobu Inoue  * knowing about \: escapes or any such.  If necessary, :'s can be put
2609a4365d0SYoshinobu Inoue  * into the termcap file in octal.
2619a4365d0SYoshinobu Inoue  */
2629a4365d0SYoshinobu Inoue static char *
2639a4365d0SYoshinobu Inoue tskip(bp)
26481982097SHajimu UMEMOTO 	char *bp;
2659a4365d0SYoshinobu Inoue {
2669a4365d0SYoshinobu Inoue 	int dquote;
2679a4365d0SYoshinobu Inoue 
2689a4365d0SYoshinobu Inoue 	dquote = 0;
2699a4365d0SYoshinobu Inoue 	while (*bp) {
2709a4365d0SYoshinobu Inoue 		switch (*bp) {
2719a4365d0SYoshinobu Inoue 		case ':':
2729a4365d0SYoshinobu Inoue 			if (!dquote)
2739a4365d0SYoshinobu Inoue 				goto breakbreak;
2749a4365d0SYoshinobu Inoue 			else
2759a4365d0SYoshinobu Inoue 				bp++;
2769a4365d0SYoshinobu Inoue 			break;
2779a4365d0SYoshinobu Inoue 		case '\\':
2789a4365d0SYoshinobu Inoue 			bp++;
2799a4365d0SYoshinobu Inoue 			if (isdigit(*bp)) {
2809a4365d0SYoshinobu Inoue 				while (isdigit(*bp++))
2819a4365d0SYoshinobu Inoue 					;
2829a4365d0SYoshinobu Inoue 			} else
2839a4365d0SYoshinobu Inoue 				bp++;
2849a4365d0SYoshinobu Inoue 		case '"':
2859a4365d0SYoshinobu Inoue 			dquote = (dquote ? 1 : 0);
2869a4365d0SYoshinobu Inoue 			bp++;
2879a4365d0SYoshinobu Inoue 			break;
2889a4365d0SYoshinobu Inoue 		default:
2899a4365d0SYoshinobu Inoue 			bp++;
2909a4365d0SYoshinobu Inoue 			break;
2919a4365d0SYoshinobu Inoue 		}
2929a4365d0SYoshinobu Inoue 	}
2939a4365d0SYoshinobu Inoue breakbreak:
2949a4365d0SYoshinobu Inoue 	if (*bp == ':')
2959a4365d0SYoshinobu Inoue 		bp++;
2969a4365d0SYoshinobu Inoue 	return (bp);
2979a4365d0SYoshinobu Inoue }
2989a4365d0SYoshinobu Inoue 
2999a4365d0SYoshinobu Inoue /*
3009a4365d0SYoshinobu Inoue  * Return the (numeric) option id.
3019a4365d0SYoshinobu Inoue  * Numeric options look like
3029a4365d0SYoshinobu Inoue  *	li#80
3039a4365d0SYoshinobu Inoue  * i.e. the option string is separated from the numeric value by
3049a4365d0SYoshinobu Inoue  * a # character.  If the option is not found we return -1.
3059a4365d0SYoshinobu Inoue  * Note that we handle octal numbers beginning with 0.
3069a4365d0SYoshinobu Inoue  */
30747742de0SHajimu UMEMOTO int64_t
3089a4365d0SYoshinobu Inoue tgetnum(id)
3099a4365d0SYoshinobu Inoue 	char *id;
3109a4365d0SYoshinobu Inoue {
31147742de0SHajimu UMEMOTO 	int64_t i;
31281982097SHajimu UMEMOTO 	int base;
31381982097SHajimu UMEMOTO 	char *bp = tbuf;
3149a4365d0SYoshinobu Inoue 
3159a4365d0SYoshinobu Inoue 	for (;;) {
3169a4365d0SYoshinobu Inoue 		bp = tskip(bp);
3179a4365d0SYoshinobu Inoue 		if (*bp == 0)
3189a4365d0SYoshinobu Inoue 			return (-1);
3199a4365d0SYoshinobu Inoue 		if (strncmp(bp, id, strlen(id)) != 0)
3209a4365d0SYoshinobu Inoue 			continue;
3219a4365d0SYoshinobu Inoue 		bp += strlen(id);
3229a4365d0SYoshinobu Inoue 		if (*bp == '@')
3239a4365d0SYoshinobu Inoue 			return (-1);
3249a4365d0SYoshinobu Inoue 		if (*bp != '#')
3259a4365d0SYoshinobu Inoue 			continue;
3269a4365d0SYoshinobu Inoue 		bp++;
3279a4365d0SYoshinobu Inoue 		base = 10;
3289a4365d0SYoshinobu Inoue 		if (*bp == '0')
3299a4365d0SYoshinobu Inoue 			base = 8;
3309a4365d0SYoshinobu Inoue 		i = 0;
3319a4365d0SYoshinobu Inoue 		while (isdigit(*bp))
3329a4365d0SYoshinobu Inoue 			i *= base, i += *bp++ - '0';
3339a4365d0SYoshinobu Inoue 		return (i);
3349a4365d0SYoshinobu Inoue 	}
3359a4365d0SYoshinobu Inoue }
3369a4365d0SYoshinobu Inoue 
3379a4365d0SYoshinobu Inoue /*
3389a4365d0SYoshinobu Inoue  * Handle a flag option.
3399a4365d0SYoshinobu Inoue  * Flag options are given "naked", i.e. followed by a : or the end
3409a4365d0SYoshinobu Inoue  * of the buffer.  Return 1 if we find the option, or 0 if it is
3419a4365d0SYoshinobu Inoue  * not given.
3429a4365d0SYoshinobu Inoue  */
3439a4365d0SYoshinobu Inoue int
3449a4365d0SYoshinobu Inoue tgetflag(id)
3459a4365d0SYoshinobu Inoue 	char *id;
3469a4365d0SYoshinobu Inoue {
34781982097SHajimu UMEMOTO 	char *bp = tbuf;
3489a4365d0SYoshinobu Inoue 
3499a4365d0SYoshinobu Inoue 	for (;;) {
3509a4365d0SYoshinobu Inoue 		bp = tskip(bp);
3519a4365d0SYoshinobu Inoue 		if (!*bp)
3529a4365d0SYoshinobu Inoue 			return (0);
3539a4365d0SYoshinobu Inoue 		if (strncmp(bp, id, strlen(id)) == 0) {
3549a4365d0SYoshinobu Inoue 			bp += strlen(id);
3559a4365d0SYoshinobu Inoue 			if (!*bp || *bp == ':')
3569a4365d0SYoshinobu Inoue 				return (1);
3579a4365d0SYoshinobu Inoue 			else if (*bp == '@')
3589a4365d0SYoshinobu Inoue 				return (0);
3599a4365d0SYoshinobu Inoue 		}
3609a4365d0SYoshinobu Inoue 	}
3619a4365d0SYoshinobu Inoue }
3629a4365d0SYoshinobu Inoue 
3639a4365d0SYoshinobu Inoue /*
3649a4365d0SYoshinobu Inoue  * Get a string valued option.
3659a4365d0SYoshinobu Inoue  * These are given as
3669a4365d0SYoshinobu Inoue  *	cl=^Z
3679a4365d0SYoshinobu Inoue  * Much decoding is done on the strings, and the strings are
3689a4365d0SYoshinobu Inoue  * placed in area, which is a ref parameter which is updated.
3699a4365d0SYoshinobu Inoue  * No checking on area overflow.
3709a4365d0SYoshinobu Inoue  */
3719a4365d0SYoshinobu Inoue char *
3729a4365d0SYoshinobu Inoue tgetstr(id, area)
3739a4365d0SYoshinobu Inoue 	char *id, **area;
3749a4365d0SYoshinobu Inoue {
37581982097SHajimu UMEMOTO 	char *bp = tbuf;
3769a4365d0SYoshinobu Inoue 
3779a4365d0SYoshinobu Inoue 	for (;;) {
3789a4365d0SYoshinobu Inoue 		bp = tskip(bp);
3799a4365d0SYoshinobu Inoue 		if (!*bp)
3809a4365d0SYoshinobu Inoue 			return (0);
3819a4365d0SYoshinobu Inoue 		if (strncmp(bp, id, strlen(id)) != 0)
3829a4365d0SYoshinobu Inoue 			continue;
3839a4365d0SYoshinobu Inoue 		bp += strlen(id);
3849a4365d0SYoshinobu Inoue 		if (*bp == '@')
3859a4365d0SYoshinobu Inoue 			return (0);
3869a4365d0SYoshinobu Inoue 		if (*bp != '=')
3879a4365d0SYoshinobu Inoue 			continue;
3889a4365d0SYoshinobu Inoue 		bp++;
3899a4365d0SYoshinobu Inoue 		return (tdecode(bp, area));
3909a4365d0SYoshinobu Inoue 	}
3919a4365d0SYoshinobu Inoue }
3929a4365d0SYoshinobu Inoue 
3939a4365d0SYoshinobu Inoue /*
3949a4365d0SYoshinobu Inoue  * Tdecode does the grung work to decode the
3959a4365d0SYoshinobu Inoue  * string capability escapes.
3969a4365d0SYoshinobu Inoue  */
3979a4365d0SYoshinobu Inoue static char *
3989a4365d0SYoshinobu Inoue tdecode(str, area)
39981982097SHajimu UMEMOTO 	char *str;
4009a4365d0SYoshinobu Inoue 	char **area;
4019a4365d0SYoshinobu Inoue {
40281982097SHajimu UMEMOTO 	char *cp;
40381982097SHajimu UMEMOTO 	int c;
40481982097SHajimu UMEMOTO 	char *dp;
4059a4365d0SYoshinobu Inoue 	int i;
4069a4365d0SYoshinobu Inoue 	char term;
4079a4365d0SYoshinobu Inoue 
4089a4365d0SYoshinobu Inoue 	term = ':';
4099a4365d0SYoshinobu Inoue 	cp = *area;
4109a4365d0SYoshinobu Inoue again:
4119a4365d0SYoshinobu Inoue 	if (*str == '"') {
4129a4365d0SYoshinobu Inoue 		term = '"';
4139a4365d0SYoshinobu Inoue 		str++;
4149a4365d0SYoshinobu Inoue 	}
4159a4365d0SYoshinobu Inoue 	while ((c = *str++) && c != term) {
4169a4365d0SYoshinobu Inoue 		switch (c) {
4179a4365d0SYoshinobu Inoue 
4189a4365d0SYoshinobu Inoue 		case '^':
4199a4365d0SYoshinobu Inoue 			c = *str++ & 037;
4209a4365d0SYoshinobu Inoue 			break;
4219a4365d0SYoshinobu Inoue 
4229a4365d0SYoshinobu Inoue 		case '\\':
4239a4365d0SYoshinobu Inoue 			dp = "E\033^^\\\\::n\nr\rt\tb\bf\f\"\"";
4249a4365d0SYoshinobu Inoue 			c = *str++;
4259a4365d0SYoshinobu Inoue nextc:
4269a4365d0SYoshinobu Inoue 			if (*dp++ == c) {
4279a4365d0SYoshinobu Inoue 				c = *dp++;
4289a4365d0SYoshinobu Inoue 				break;
4299a4365d0SYoshinobu Inoue 			}
4309a4365d0SYoshinobu Inoue 			dp++;
4319a4365d0SYoshinobu Inoue 			if (*dp)
4329a4365d0SYoshinobu Inoue 				goto nextc;
4339a4365d0SYoshinobu Inoue 			if (isdigit(c)) {
4349a4365d0SYoshinobu Inoue 				c -= '0', i = 2;
4359a4365d0SYoshinobu Inoue 				do
4369a4365d0SYoshinobu Inoue 					c <<= 3, c |= *str++ - '0';
4379a4365d0SYoshinobu Inoue 				while (--i && isdigit(*str));
4389a4365d0SYoshinobu Inoue 			}
4399a4365d0SYoshinobu Inoue 			break;
4409a4365d0SYoshinobu Inoue 		}
4419a4365d0SYoshinobu Inoue 		*cp++ = c;
4429a4365d0SYoshinobu Inoue 	}
4439a4365d0SYoshinobu Inoue 	if (c == term && term != ':') {
4449a4365d0SYoshinobu Inoue 		term = ':';
4459a4365d0SYoshinobu Inoue 		goto again;
4469a4365d0SYoshinobu Inoue 	}
4479a4365d0SYoshinobu Inoue 	*cp++ = 0;
4489a4365d0SYoshinobu Inoue 	str = *area;
4499a4365d0SYoshinobu Inoue 	*area = cp;
4509a4365d0SYoshinobu Inoue 	return (str);
4519a4365d0SYoshinobu Inoue }
452