xref: /freebsd/usr.sbin/rtadvd/advcap.c (revision 0546aa1b97c50f1598be549123d1dcbf8f7b230a)
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 /*
689a4365d0SYoshinobu Inoue  * termcap - routines for dealing with the terminal capability data base
699a4365d0SYoshinobu Inoue  *
709a4365d0SYoshinobu Inoue  * BUG:		Should use a "last" pointer in tbuf, so that searching
719a4365d0SYoshinobu Inoue  *		for capabilities alphabetically would not be a n**2/2
729a4365d0SYoshinobu Inoue  *		process when large numbers of capabilities are given.
739a4365d0SYoshinobu Inoue  * Note:	If we add a last pointer now we will screw up the
749a4365d0SYoshinobu Inoue  *		tc capability. We really should compile termcap.
759a4365d0SYoshinobu Inoue  *
769a4365d0SYoshinobu Inoue  * Essentially all the work here is scanning and decoding escapes
779a4365d0SYoshinobu Inoue  * in string capabilities.  We don't use stdio because the editor
789a4365d0SYoshinobu Inoue  * doesn't, and because living w/o it is not hard.
799a4365d0SYoshinobu Inoue  */
809a4365d0SYoshinobu Inoue 
819a4365d0SYoshinobu Inoue static	char *tbuf;
829a4365d0SYoshinobu Inoue static	int hopcount;	/* detect infinite loops in termcap, init 0 */
839a4365d0SYoshinobu Inoue 
84db82af41SHiroki Sato extern const char *conffile;
859a4365d0SYoshinobu Inoue 
86784bddbcSKevin Lo int tgetent(char *, char *);
87db82af41SHiroki Sato int getent(char *, char *, const char *);
88784bddbcSKevin Lo int tnchktc(void);
89784bddbcSKevin Lo int tnamatch(char *);
90784bddbcSKevin Lo static char *tskip(char *);
91784bddbcSKevin Lo int64_t tgetnum(char *);
92784bddbcSKevin Lo int tgetflag(char *);
93784bddbcSKevin Lo char *tgetstr(char *, char **);
94784bddbcSKevin Lo static char *tdecode(char *, char **);
959a4365d0SYoshinobu Inoue 
969a4365d0SYoshinobu Inoue /*
979a4365d0SYoshinobu Inoue  * Get an entry for terminal name in buffer bp,
989a4365d0SYoshinobu Inoue  * from the termcap file.  Parse is very rudimentary;
999a4365d0SYoshinobu Inoue  * we just notice escaped newlines.
1009a4365d0SYoshinobu Inoue  */
1019a4365d0SYoshinobu Inoue int
102db82af41SHiroki Sato tgetent(char *bp, char *name)
1039a4365d0SYoshinobu Inoue {
104db82af41SHiroki Sato 	return (getent(bp, name, conffile));
1059a4365d0SYoshinobu Inoue }
1069a4365d0SYoshinobu Inoue 
1079a4365d0SYoshinobu Inoue int
108db82af41SHiroki Sato getent(char *bp, char *name, const char *cfile)
1099a4365d0SYoshinobu Inoue {
11081982097SHajimu UMEMOTO 	int c;
11181982097SHajimu UMEMOTO 	int i = 0, cnt = 0;
1129a4365d0SYoshinobu Inoue 	char ibuf[BUFSIZ];
113db82af41SHiroki Sato 	char *cp;
1149a4365d0SYoshinobu Inoue 	int tf;
1159a4365d0SYoshinobu Inoue 
1169a4365d0SYoshinobu Inoue 	tbuf = bp;
1179a4365d0SYoshinobu Inoue 	tf = 0;
1189a4365d0SYoshinobu Inoue 	/*
1199a4365d0SYoshinobu Inoue 	 * TERMCAP can have one of two things in it. It can be the
1209a4365d0SYoshinobu Inoue 	 * name of a file to use instead of /etc/termcap. In this
1219a4365d0SYoshinobu Inoue 	 * case it better start with a "/". Or it can be an entry to
1229a4365d0SYoshinobu Inoue 	 * use so we don't have to read the file. In this case it
1239a4365d0SYoshinobu Inoue 	 * has to already have the newlines crunched out.
1249a4365d0SYoshinobu Inoue 	 */
125db82af41SHiroki Sato 	if (cfile && *cfile)
126db82af41SHiroki Sato 		tf = open(cfile, O_RDONLY);
127db82af41SHiroki Sato 
1289a4365d0SYoshinobu Inoue 	if (tf < 0) {
129b26e03e9SKris Kennaway 		syslog(LOG_INFO,
1301533bed0SHajimu UMEMOTO 		       "<%s> open: %s", __func__, strerror(errno));
1319a4365d0SYoshinobu Inoue 		return (-2);
1329a4365d0SYoshinobu Inoue 	}
1339a4365d0SYoshinobu Inoue 	for (;;) {
1349a4365d0SYoshinobu Inoue 		cp = bp;
1359a4365d0SYoshinobu Inoue 		for (;;) {
1369a4365d0SYoshinobu Inoue 			if (i == cnt) {
1379a4365d0SYoshinobu Inoue 				cnt = read(tf, ibuf, BUFSIZ);
1389a4365d0SYoshinobu Inoue 				if (cnt <= 0) {
1399a4365d0SYoshinobu Inoue 					close(tf);
1409a4365d0SYoshinobu Inoue 					return (0);
1419a4365d0SYoshinobu Inoue 				}
1429a4365d0SYoshinobu Inoue 				i = 0;
1439a4365d0SYoshinobu Inoue 			}
1449a4365d0SYoshinobu Inoue 			c = ibuf[i++];
1459a4365d0SYoshinobu Inoue 			if (c == '\n') {
1469a4365d0SYoshinobu Inoue 				if (cp > bp && cp[-1] == '\\') {
1479a4365d0SYoshinobu Inoue 					cp--;
1489a4365d0SYoshinobu Inoue 					continue;
1499a4365d0SYoshinobu Inoue 				}
1509a4365d0SYoshinobu Inoue 				break;
1519a4365d0SYoshinobu Inoue 			}
152*0546aa1bSHiroki Sato 			if (cp >= bp + BUFSIZ - 1) {
153e1b4d8d0SSheldon Hearn 				write(STDERR_FILENO, "Remcap entry too long\n",
154*0546aa1bSHiroki Sato 				    22);
1559a4365d0SYoshinobu Inoue 				break;
1569a4365d0SYoshinobu Inoue 			} else
1579a4365d0SYoshinobu Inoue 				*cp++ = c;
1589a4365d0SYoshinobu Inoue 		}
1599a4365d0SYoshinobu Inoue 		*cp = 0;
1609a4365d0SYoshinobu Inoue 
1619a4365d0SYoshinobu Inoue 		/*
1629a4365d0SYoshinobu Inoue 		 * The real work for the match.
1639a4365d0SYoshinobu Inoue 		 */
1649a4365d0SYoshinobu Inoue 		if (tnamatch(name)) {
1659a4365d0SYoshinobu Inoue 			close(tf);
1669a4365d0SYoshinobu Inoue 			return (tnchktc());
1679a4365d0SYoshinobu Inoue 		}
1689a4365d0SYoshinobu Inoue 	}
1699a4365d0SYoshinobu Inoue }
1709a4365d0SYoshinobu Inoue 
1719a4365d0SYoshinobu Inoue /*
1729a4365d0SYoshinobu Inoue  * tnchktc: check the last entry, see if it's tc=xxx. If so,
1739a4365d0SYoshinobu Inoue  * recursively find xxx and append that entry (minus the names)
1749a4365d0SYoshinobu Inoue  * to take the place of the tc=xxx entry. This allows termcap
1759a4365d0SYoshinobu Inoue  * entries to say "like an HP2621 but doesn't turn on the labels".
1769a4365d0SYoshinobu Inoue  * Note that this works because of the left to right scan.
1779a4365d0SYoshinobu Inoue  */
1789a4365d0SYoshinobu Inoue int
179db82af41SHiroki Sato tnchktc(void)
1809a4365d0SYoshinobu Inoue {
18181982097SHajimu UMEMOTO 	char *p, *q;
1829a4365d0SYoshinobu Inoue 	char tcname[16];	/* name of similar terminal */
1839a4365d0SYoshinobu Inoue 	char tcbuf[BUFSIZ];
1849a4365d0SYoshinobu Inoue 	char *holdtbuf = tbuf;
1859a4365d0SYoshinobu Inoue 	int l;
1869a4365d0SYoshinobu Inoue 
1879a4365d0SYoshinobu Inoue 	p = tbuf + strlen(tbuf) - 2;	/* before the last colon */
1889a4365d0SYoshinobu Inoue 	while (*--p != ':')
1899a4365d0SYoshinobu Inoue 		if (p < tbuf) {
190e1b4d8d0SSheldon Hearn 			write(STDERR_FILENO, "Bad remcap entry\n", 18);
1919a4365d0SYoshinobu Inoue 			return (0);
1929a4365d0SYoshinobu Inoue 		}
1939a4365d0SYoshinobu Inoue 	p++;
1949a4365d0SYoshinobu Inoue 	/* p now points to beginning of last field */
1959a4365d0SYoshinobu Inoue 	if (p[0] != 't' || p[1] != 'c')
1969a4365d0SYoshinobu Inoue 		return (1);
197bb58b617SHajimu UMEMOTO 	strlcpy(tcname, p + 3, sizeof tcname);
1989a4365d0SYoshinobu Inoue 	q = tcname;
1999a4365d0SYoshinobu Inoue 	while (*q && *q != ':')
2009a4365d0SYoshinobu Inoue 		q++;
2019a4365d0SYoshinobu Inoue 	*q = 0;
2029a4365d0SYoshinobu Inoue 	if (++hopcount > MAXHOP) {
203e1b4d8d0SSheldon Hearn 		write(STDERR_FILENO, "Infinite tc= loop\n", 18);
2049a4365d0SYoshinobu Inoue 		return (0);
2059a4365d0SYoshinobu Inoue 	}
2065a10f1ccSHiroki Sato 	if (getent(tcbuf, tcname, conffile) != 1) {
2079a4365d0SYoshinobu Inoue 		return (0);
2089a4365d0SYoshinobu Inoue 	}
2099a4365d0SYoshinobu Inoue 	for (q = tcbuf; *q++ != ':'; )
2109a4365d0SYoshinobu Inoue 		;
2119a4365d0SYoshinobu Inoue 	l = p - holdtbuf + strlen(q);
2129a4365d0SYoshinobu Inoue 	if (l > BUFSIZ) {
213e1b4d8d0SSheldon Hearn 		write(STDERR_FILENO, "Remcap entry too long\n", 23);
2149a4365d0SYoshinobu Inoue 		q[BUFSIZ - (p-holdtbuf)] = 0;
2159a4365d0SYoshinobu Inoue 	}
2169a4365d0SYoshinobu Inoue 	strcpy(p, q);
2179a4365d0SYoshinobu Inoue 	tbuf = holdtbuf;
2189a4365d0SYoshinobu Inoue 	return (1);
2199a4365d0SYoshinobu Inoue }
2209a4365d0SYoshinobu Inoue 
2219a4365d0SYoshinobu Inoue /*
2229a4365d0SYoshinobu Inoue  * Tnamatch deals with name matching.  The first field of the termcap
2239a4365d0SYoshinobu Inoue  * entry is a sequence of names separated by |'s, so we compare
2249a4365d0SYoshinobu Inoue  * against each such name.  The normal : terminator after the last
2259a4365d0SYoshinobu Inoue  * name (before the first field) stops us.
2269a4365d0SYoshinobu Inoue  */
2279a4365d0SYoshinobu Inoue int
228db82af41SHiroki Sato tnamatch(char *np)
2299a4365d0SYoshinobu Inoue {
23081982097SHajimu UMEMOTO 	char *Np, *Bp;
2319a4365d0SYoshinobu Inoue 
2329a4365d0SYoshinobu Inoue 	Bp = tbuf;
2339a4365d0SYoshinobu Inoue 	if (*Bp == '#')
2349a4365d0SYoshinobu Inoue 		return (0);
2359a4365d0SYoshinobu Inoue 	for (;;) {
2369a4365d0SYoshinobu Inoue 		for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
2379a4365d0SYoshinobu Inoue 			continue;
2389a4365d0SYoshinobu Inoue 		if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
2399a4365d0SYoshinobu Inoue 			return (1);
2409a4365d0SYoshinobu Inoue 		while (*Bp && *Bp != ':' && *Bp != '|')
2419a4365d0SYoshinobu Inoue 			Bp++;
2429a4365d0SYoshinobu Inoue 		if (*Bp == 0 || *Bp == ':')
2439a4365d0SYoshinobu Inoue 			return (0);
2449a4365d0SYoshinobu Inoue 		Bp++;
2459a4365d0SYoshinobu Inoue 	}
2469a4365d0SYoshinobu Inoue }
2479a4365d0SYoshinobu Inoue 
2489a4365d0SYoshinobu Inoue /*
2499a4365d0SYoshinobu Inoue  * Skip to the next field.  Notice that this is very dumb, not
2509a4365d0SYoshinobu Inoue  * knowing about \: escapes or any such.  If necessary, :'s can be put
2519a4365d0SYoshinobu Inoue  * into the termcap file in octal.
2529a4365d0SYoshinobu Inoue  */
2539a4365d0SYoshinobu Inoue static char *
254db82af41SHiroki Sato tskip(char *bp)
2559a4365d0SYoshinobu Inoue {
2569a4365d0SYoshinobu Inoue 	int dquote;
2579a4365d0SYoshinobu Inoue 
2589a4365d0SYoshinobu Inoue 	dquote = 0;
2599a4365d0SYoshinobu Inoue 	while (*bp) {
2609a4365d0SYoshinobu Inoue 		switch (*bp) {
2619a4365d0SYoshinobu Inoue 		case ':':
2629a4365d0SYoshinobu Inoue 			if (!dquote)
2639a4365d0SYoshinobu Inoue 				goto breakbreak;
2649a4365d0SYoshinobu Inoue 			else
2659a4365d0SYoshinobu Inoue 				bp++;
2669a4365d0SYoshinobu Inoue 			break;
2679a4365d0SYoshinobu Inoue 		case '\\':
2689a4365d0SYoshinobu Inoue 			bp++;
2699a4365d0SYoshinobu Inoue 			if (isdigit(*bp)) {
2709a4365d0SYoshinobu Inoue 				while (isdigit(*bp++))
2719a4365d0SYoshinobu Inoue 					;
2729a4365d0SYoshinobu Inoue 			} else
2739a4365d0SYoshinobu Inoue 				bp++;
2749a4365d0SYoshinobu Inoue 		case '"':
2759a4365d0SYoshinobu Inoue 			dquote = (dquote ? 1 : 0);
2769a4365d0SYoshinobu Inoue 			bp++;
2779a4365d0SYoshinobu Inoue 			break;
2789a4365d0SYoshinobu Inoue 		default:
2799a4365d0SYoshinobu Inoue 			bp++;
2809a4365d0SYoshinobu Inoue 			break;
2819a4365d0SYoshinobu Inoue 		}
2829a4365d0SYoshinobu Inoue 	}
2839a4365d0SYoshinobu Inoue breakbreak:
2849a4365d0SYoshinobu Inoue 	if (*bp == ':')
2859a4365d0SYoshinobu Inoue 		bp++;
2869a4365d0SYoshinobu Inoue 	return (bp);
2879a4365d0SYoshinobu Inoue }
2889a4365d0SYoshinobu Inoue 
2899a4365d0SYoshinobu Inoue /*
2909a4365d0SYoshinobu Inoue  * Return the (numeric) option id.
2919a4365d0SYoshinobu Inoue  * Numeric options look like
2929a4365d0SYoshinobu Inoue  *	li#80
2939a4365d0SYoshinobu Inoue  * i.e. the option string is separated from the numeric value by
2949a4365d0SYoshinobu Inoue  * a # character.  If the option is not found we return -1.
2959a4365d0SYoshinobu Inoue  * Note that we handle octal numbers beginning with 0.
2969a4365d0SYoshinobu Inoue  */
29747742de0SHajimu UMEMOTO int64_t
298db82af41SHiroki Sato tgetnum(char *id)
2999a4365d0SYoshinobu Inoue {
30047742de0SHajimu UMEMOTO 	int64_t i;
30181982097SHajimu UMEMOTO 	int base;
30281982097SHajimu UMEMOTO 	char *bp = tbuf;
3039a4365d0SYoshinobu Inoue 
3049a4365d0SYoshinobu Inoue 	for (;;) {
3059a4365d0SYoshinobu Inoue 		bp = tskip(bp);
3069a4365d0SYoshinobu Inoue 		if (*bp == 0)
3079a4365d0SYoshinobu Inoue 			return (-1);
3089a4365d0SYoshinobu Inoue 		if (strncmp(bp, id, strlen(id)) != 0)
3099a4365d0SYoshinobu Inoue 			continue;
3109a4365d0SYoshinobu Inoue 		bp += strlen(id);
3119a4365d0SYoshinobu Inoue 		if (*bp == '@')
3129a4365d0SYoshinobu Inoue 			return (-1);
3139a4365d0SYoshinobu Inoue 		if (*bp != '#')
3149a4365d0SYoshinobu Inoue 			continue;
3159a4365d0SYoshinobu Inoue 		bp++;
3169a4365d0SYoshinobu Inoue 		base = 10;
3179a4365d0SYoshinobu Inoue 		if (*bp == '0')
3189a4365d0SYoshinobu Inoue 			base = 8;
3199a4365d0SYoshinobu Inoue 		i = 0;
3209a4365d0SYoshinobu Inoue 		while (isdigit(*bp))
3219a4365d0SYoshinobu Inoue 			i *= base, i += *bp++ - '0';
3229a4365d0SYoshinobu Inoue 		return (i);
3239a4365d0SYoshinobu Inoue 	}
3249a4365d0SYoshinobu Inoue }
3259a4365d0SYoshinobu Inoue 
3269a4365d0SYoshinobu Inoue /*
3279a4365d0SYoshinobu Inoue  * Handle a flag option.
3289a4365d0SYoshinobu Inoue  * Flag options are given "naked", i.e. followed by a : or the end
3299a4365d0SYoshinobu Inoue  * of the buffer.  Return 1 if we find the option, or 0 if it is
3309a4365d0SYoshinobu Inoue  * not given.
3319a4365d0SYoshinobu Inoue  */
3329a4365d0SYoshinobu Inoue int
333db82af41SHiroki Sato tgetflag(char *id)
3349a4365d0SYoshinobu Inoue {
33581982097SHajimu UMEMOTO 	char *bp = tbuf;
3369a4365d0SYoshinobu Inoue 
3379a4365d0SYoshinobu Inoue 	for (;;) {
3389a4365d0SYoshinobu Inoue 		bp = tskip(bp);
3399a4365d0SYoshinobu Inoue 		if (!*bp)
3409a4365d0SYoshinobu Inoue 			return (0);
3419a4365d0SYoshinobu Inoue 		if (strncmp(bp, id, strlen(id)) == 0) {
3429a4365d0SYoshinobu Inoue 			bp += strlen(id);
3439a4365d0SYoshinobu Inoue 			if (!*bp || *bp == ':')
3449a4365d0SYoshinobu Inoue 				return (1);
3459a4365d0SYoshinobu Inoue 			else if (*bp == '@')
3469a4365d0SYoshinobu Inoue 				return (0);
3479a4365d0SYoshinobu Inoue 		}
3489a4365d0SYoshinobu Inoue 	}
3499a4365d0SYoshinobu Inoue }
3509a4365d0SYoshinobu Inoue 
3519a4365d0SYoshinobu Inoue /*
3529a4365d0SYoshinobu Inoue  * Get a string valued option.
3539a4365d0SYoshinobu Inoue  * These are given as
3549a4365d0SYoshinobu Inoue  *	cl=^Z
3559a4365d0SYoshinobu Inoue  * Much decoding is done on the strings, and the strings are
3569a4365d0SYoshinobu Inoue  * placed in area, which is a ref parameter which is updated.
3579a4365d0SYoshinobu Inoue  * No checking on area overflow.
3589a4365d0SYoshinobu Inoue  */
3599a4365d0SYoshinobu Inoue char *
360db82af41SHiroki Sato tgetstr(char *id, char **area)
3619a4365d0SYoshinobu Inoue {
36281982097SHajimu UMEMOTO 	char *bp = tbuf;
3639a4365d0SYoshinobu Inoue 
3649a4365d0SYoshinobu Inoue 	for (;;) {
3659a4365d0SYoshinobu Inoue 		bp = tskip(bp);
3669a4365d0SYoshinobu Inoue 		if (!*bp)
3679a4365d0SYoshinobu Inoue 			return (0);
3689a4365d0SYoshinobu Inoue 		if (strncmp(bp, id, strlen(id)) != 0)
3699a4365d0SYoshinobu Inoue 			continue;
3709a4365d0SYoshinobu Inoue 		bp += strlen(id);
3719a4365d0SYoshinobu Inoue 		if (*bp == '@')
3729a4365d0SYoshinobu Inoue 			return (0);
3739a4365d0SYoshinobu Inoue 		if (*bp != '=')
3749a4365d0SYoshinobu Inoue 			continue;
3759a4365d0SYoshinobu Inoue 		bp++;
3769a4365d0SYoshinobu Inoue 		return (tdecode(bp, area));
3779a4365d0SYoshinobu Inoue 	}
3789a4365d0SYoshinobu Inoue }
3799a4365d0SYoshinobu Inoue 
3809a4365d0SYoshinobu Inoue /*
3819a4365d0SYoshinobu Inoue  * Tdecode does the grung work to decode the
3829a4365d0SYoshinobu Inoue  * string capability escapes.
3839a4365d0SYoshinobu Inoue  */
3849a4365d0SYoshinobu Inoue static char *
385db82af41SHiroki Sato tdecode(char *str, char **area)
3869a4365d0SYoshinobu Inoue {
38781982097SHajimu UMEMOTO 	char *cp;
38881982097SHajimu UMEMOTO 	int c;
389db82af41SHiroki Sato 	const char *dp;
3909a4365d0SYoshinobu Inoue 	int i;
3919a4365d0SYoshinobu Inoue 	char term;
3929a4365d0SYoshinobu Inoue 
3939a4365d0SYoshinobu Inoue 	term = ':';
3949a4365d0SYoshinobu Inoue 	cp = *area;
3959a4365d0SYoshinobu Inoue again:
3969a4365d0SYoshinobu Inoue 	if (*str == '"') {
3979a4365d0SYoshinobu Inoue 		term = '"';
3989a4365d0SYoshinobu Inoue 		str++;
3999a4365d0SYoshinobu Inoue 	}
4009a4365d0SYoshinobu Inoue 	while ((c = *str++) && c != term) {
4019a4365d0SYoshinobu Inoue 		switch (c) {
4029a4365d0SYoshinobu Inoue 
4039a4365d0SYoshinobu Inoue 		case '^':
4049a4365d0SYoshinobu Inoue 			c = *str++ & 037;
4059a4365d0SYoshinobu Inoue 			break;
4069a4365d0SYoshinobu Inoue 
4079a4365d0SYoshinobu Inoue 		case '\\':
4089a4365d0SYoshinobu Inoue 			dp = "E\033^^\\\\::n\nr\rt\tb\bf\f\"\"";
4099a4365d0SYoshinobu Inoue 			c = *str++;
4109a4365d0SYoshinobu Inoue nextc:
4119a4365d0SYoshinobu Inoue 			if (*dp++ == c) {
4129a4365d0SYoshinobu Inoue 				c = *dp++;
4139a4365d0SYoshinobu Inoue 				break;
4149a4365d0SYoshinobu Inoue 			}
4159a4365d0SYoshinobu Inoue 			dp++;
4169a4365d0SYoshinobu Inoue 			if (*dp)
4179a4365d0SYoshinobu Inoue 				goto nextc;
4189a4365d0SYoshinobu Inoue 			if (isdigit(c)) {
4199a4365d0SYoshinobu Inoue 				c -= '0', i = 2;
4209a4365d0SYoshinobu Inoue 				do
4219a4365d0SYoshinobu Inoue 					c <<= 3, c |= *str++ - '0';
4229a4365d0SYoshinobu Inoue 				while (--i && isdigit(*str));
4239a4365d0SYoshinobu Inoue 			}
4249a4365d0SYoshinobu Inoue 			break;
4259a4365d0SYoshinobu Inoue 		}
4269a4365d0SYoshinobu Inoue 		*cp++ = c;
4279a4365d0SYoshinobu Inoue 	}
4289a4365d0SYoshinobu Inoue 	if (c == term && term != ':') {
4299a4365d0SYoshinobu Inoue 		term = ':';
4309a4365d0SYoshinobu Inoue 		goto again;
4319a4365d0SYoshinobu Inoue 	}
4329a4365d0SYoshinobu Inoue 	*cp++ = 0;
4339a4365d0SYoshinobu Inoue 	str = *area;
4349a4365d0SYoshinobu Inoue 	*area = cp;
4359a4365d0SYoshinobu Inoue 	return (str);
4369a4365d0SYoshinobu Inoue }
437