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 $
39 * remcap - routines for dealing with the remote host data base
41 * derived from termcap
43 #include <sys/types.h>
53 #include "pathnames.h"
58 #define MAXHOP 32 /* max number of tc= indirections */
60 #define tgetent agetent
61 #define tnchktc anchktc
62 #define tnamatch anamatch
63 #define tgetnum agetnum
64 #define tgetflag agetflag
65 #define tgetstr agetstr
68 #define V_TERMCAP "REMOTE"
75 * termcap - routines for dealing with the terminal capability data base
77 * BUG: Should use a "last" pointer in tbuf, so that searching
78 * for capabilities alphabetically would not be a n**2/2
79 * process when large numbers of capabilities are given.
80 * Note: If we add a last pointer now we will screw up the
81 * tc capability. We really should compile termcap.
83 * Essentially all the work here is scanning and decoding escapes
84 * in string capabilities. We don't use stdio because the editor
85 * doesn't, and because living w/o it is not hard.
89 static int hopcount; /* detect infinite loops in termcap, init 0 */
91 static char *remotefile;
93 extern char *conffile;
95 int tgetent __P((char *, char *));
96 int getent __P((char *, char *, char *));
97 int tnchktc __P((void));
98 int tnamatch __P((char *));
99 static char *tskip __P((char *));
100 long long tgetnum __P((char *));
101 int tgetflag __P((char *));
102 char *tgetstr __P((char *, char **));
103 static char *tdecode __P((char *, char **));
106 * Get an entry for terminal name in buffer bp,
107 * from the termcap file. Parse is very rudimentary;
108 * we just notice escaped newlines.
116 remotefile = cp = conffile ? conffile : _PATH_RTADVDCONF;
117 return (getent(bp, name, cp));
122 char *bp, *name, *cp;
125 register int i = 0, cnt = 0;
132 * TERMCAP can have one of two things in it. It can be the
133 * name of a file to use instead of /etc/termcap. In this
134 * case it better start with a "/". Or it can be an entry to
135 * use so we don't have to read the file. In this case it
136 * has to already have the newlines crunched out.
139 tf = open(RM = cp, O_RDONLY);
143 "<%s> open: %s", __FUNCTION__, strerror(errno));
150 cnt = read(tf, ibuf, BUFSIZ);
159 if (cp > bp && cp[-1] == '\\') {
165 if (cp >= bp+BUFSIZ) {
166 write(2,"Remcap entry too long\n", 23);
174 * The real work for the match.
176 if (tnamatch(name)) {
184 * tnchktc: check the last entry, see if it's tc=xxx. If so,
185 * recursively find xxx and append that entry (minus the names)
186 * to take the place of the tc=xxx entry. This allows termcap
187 * entries to say "like an HP2621 but doesn't turn on the labels".
188 * Note that this works because of the left to right scan.
193 register char *p, *q;
194 char tcname[16]; /* name of similar terminal */
196 char *holdtbuf = tbuf;
199 p = tbuf + strlen(tbuf) - 2; /* before the last colon */
202 write(2, "Bad remcap entry\n", 18);
206 /* p now points to beginning of last field */
207 if (p[0] != 't' || p[1] != 'c')
211 while (*q && *q != ':')
214 if (++hopcount > MAXHOP) {
215 write(2, "Infinite tc= loop\n", 18);
218 if (getent(tcbuf, tcname, remotefile) != 1) {
221 for (q = tcbuf; *q++ != ':'; )
223 l = p - holdtbuf + strlen(q);
225 write(2, "Remcap entry too long\n", 23);
226 q[BUFSIZ - (p-holdtbuf)] = 0;
234 * Tnamatch deals with name matching. The first field of the termcap
235 * entry is a sequence of names separated by |'s, so we compare
236 * against each such name. The normal : terminator after the last
237 * name (before the first field) stops us.
243 register char *Np, *Bp;
249 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
251 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
253 while (*Bp && *Bp != ':' && *Bp != '|')
255 if (*Bp == 0 || *Bp == ':')
262 * Skip to the next field. Notice that this is very dumb, not
263 * knowing about \: escapes or any such. If necessary, :'s can be put
264 * into the termcap file in octal.
284 while (isdigit(*bp++))
289 dquote = (dquote ? 1 : 0);
304 * Return the (numeric) option id.
305 * Numeric options look like
307 * i.e. the option string is separated from the numeric value by
308 * a # character. If the option is not found we return -1.
309 * Note that we handle octal numbers beginning with 0.
315 register long long i;
317 register char *bp = tbuf;
323 if (strncmp(bp, id, strlen(id)) != 0)
336 i *= base, i += *bp++ - '0';
342 * Handle a flag option.
343 * Flag options are given "naked", i.e. followed by a : or the end
344 * of the buffer. Return 1 if we find the option, or 0 if it is
351 register char *bp = tbuf;
357 if (strncmp(bp, id, strlen(id)) == 0) {
359 if (!*bp || *bp == ':')
368 * Get a string valued option.
371 * Much decoding is done on the strings, and the strings are
372 * placed in area, which is a ref parameter which is updated.
373 * No checking on area overflow.
379 register char *bp = tbuf;
385 if (strncmp(bp, id, strlen(id)) != 0)
393 return (tdecode(bp, area));
398 * Tdecode does the grung work to decode the
399 * string capability escapes.
419 while ((c = *str++) && c != term) {
427 dp = "E\033^^\\\\::n\nr\rt\tb\bf\f\"\"";
440 c <<= 3, c |= *str++ - '0';
441 while (--i && isdigit(*str));
447 if (c == term && term != ':') {