1 /* $KAME: advcap.c,v 1.5 2001/02/01 09:12:08 jinmei Exp $ */
4 * Copyright (c) 1983 The Regents of the University of California.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * $FreeBSD: src/usr.sbin/rtadvd/advcap.c,v 1.1.2.2 2001/07/03 11:02:13 ume Exp $
36 * $DragonFly: src/usr.sbin/rtadvd/advcap.c,v 1.2 2003/06/17 04:30:02 dillon Exp $
40 * remcap - routines for dealing with the remote host data base
42 * derived from termcap
44 #include <sys/types.h>
54 #include "pathnames.h"
59 #define MAXHOP 32 /* max number of tc= indirections */
61 #define tgetent agetent
62 #define tnchktc anchktc
63 #define tnamatch anamatch
64 #define tgetnum agetnum
65 #define tgetflag agetflag
66 #define tgetstr agetstr
69 #define V_TERMCAP "REMOTE"
76 * termcap - routines for dealing with the terminal capability data base
78 * BUG: Should use a "last" pointer in tbuf, so that searching
79 * for capabilities alphabetically would not be a n**2/2
80 * process when large numbers of capabilities are given.
81 * Note: If we add a last pointer now we will screw up the
82 * tc capability. We really should compile termcap.
84 * Essentially all the work here is scanning and decoding escapes
85 * in string capabilities. We don't use stdio because the editor
86 * doesn't, and because living w/o it is not hard.
90 static int hopcount; /* detect infinite loops in termcap, init 0 */
92 static char *remotefile;
94 extern char *conffile;
96 int tgetent __P((char *, char *));
97 int getent __P((char *, char *, char *));
98 int tnchktc __P((void));
99 int tnamatch __P((char *));
100 static char *tskip __P((char *));
101 long long tgetnum __P((char *));
102 int tgetflag __P((char *));
103 char *tgetstr __P((char *, char **));
104 static char *tdecode __P((char *, char **));
107 * Get an entry for terminal name in buffer bp,
108 * from the termcap file. Parse is very rudimentary;
109 * we just notice escaped newlines.
117 remotefile = cp = conffile ? conffile : _PATH_RTADVDCONF;
118 return (getent(bp, name, cp));
123 char *bp, *name, *cp;
126 register int i = 0, cnt = 0;
133 * TERMCAP can have one of two things in it. It can be the
134 * name of a file to use instead of /etc/termcap. In this
135 * case it better start with a "/". Or it can be an entry to
136 * use so we don't have to read the file. In this case it
137 * has to already have the newlines crunched out.
140 tf = open(RM = cp, O_RDONLY);
144 "<%s> open: %s", __FUNCTION__, strerror(errno));
151 cnt = read(tf, ibuf, BUFSIZ);
160 if (cp > bp && cp[-1] == '\\') {
166 if (cp >= bp+BUFSIZ) {
167 write(2,"Remcap entry too long\n", 23);
175 * The real work for the match.
177 if (tnamatch(name)) {
185 * tnchktc: check the last entry, see if it's tc=xxx. If so,
186 * recursively find xxx and append that entry (minus the names)
187 * to take the place of the tc=xxx entry. This allows termcap
188 * entries to say "like an HP2621 but doesn't turn on the labels".
189 * Note that this works because of the left to right scan.
194 register char *p, *q;
195 char tcname[16]; /* name of similar terminal */
197 char *holdtbuf = tbuf;
200 p = tbuf + strlen(tbuf) - 2; /* before the last colon */
203 write(2, "Bad remcap entry\n", 18);
207 /* p now points to beginning of last field */
208 if (p[0] != 't' || p[1] != 'c')
212 while (*q && *q != ':')
215 if (++hopcount > MAXHOP) {
216 write(2, "Infinite tc= loop\n", 18);
219 if (getent(tcbuf, tcname, remotefile) != 1) {
222 for (q = tcbuf; *q++ != ':'; )
224 l = p - holdtbuf + strlen(q);
226 write(2, "Remcap entry too long\n", 23);
227 q[BUFSIZ - (p-holdtbuf)] = 0;
235 * Tnamatch deals with name matching. The first field of the termcap
236 * entry is a sequence of names separated by |'s, so we compare
237 * against each such name. The normal : terminator after the last
238 * name (before the first field) stops us.
244 register char *Np, *Bp;
250 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
252 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
254 while (*Bp && *Bp != ':' && *Bp != '|')
256 if (*Bp == 0 || *Bp == ':')
263 * Skip to the next field. Notice that this is very dumb, not
264 * knowing about \: escapes or any such. If necessary, :'s can be put
265 * into the termcap file in octal.
285 while (isdigit(*bp++))
290 dquote = (dquote ? 1 : 0);
305 * Return the (numeric) option id.
306 * Numeric options look like
308 * i.e. the option string is separated from the numeric value by
309 * a # character. If the option is not found we return -1.
310 * Note that we handle octal numbers beginning with 0.
316 register long long i;
318 register char *bp = tbuf;
324 if (strncmp(bp, id, strlen(id)) != 0)
337 i *= base, i += *bp++ - '0';
343 * Handle a flag option.
344 * Flag options are given "naked", i.e. followed by a : or the end
345 * of the buffer. Return 1 if we find the option, or 0 if it is
352 register char *bp = tbuf;
358 if (strncmp(bp, id, strlen(id)) == 0) {
360 if (!*bp || *bp == ':')
369 * Get a string valued option.
372 * Much decoding is done on the strings, and the strings are
373 * placed in area, which is a ref parameter which is updated.
374 * No checking on area overflow.
380 register char *bp = tbuf;
386 if (strncmp(bp, id, strlen(id)) != 0)
394 return (tdecode(bp, area));
399 * Tdecode does the grung work to decode the
400 * string capability escapes.
420 while ((c = *str++) && c != term) {
428 dp = "E\033^^\\\\::n\nr\rt\tb\bf\f\"\"";
441 c <<= 3, c |= *str++ - '0';
442 while (--i && isdigit(*str));
448 if (c == term && term != ':') {