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