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