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.5 2005/02/17 14:00:10 joerg 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(char *, char *);
97 int getent(char *, char *, char *);
100 static char *tskip(char *);
101 long long tgetnum(char *);
102 int tgetflag(char *);
103 char *tgetstr(char *, char **);
104 static char *tdecode(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.
112 tgetent(char *bp, char *name)
116 remotefile = cp = conffile ? conffile : _PATH_RTADVDCONF;
117 return (getent(bp, name, cp));
121 getent(char *bp, char *name, char *cp)
131 * TERMCAP can have one of two things in it. It can be the
132 * name of a file to use instead of /etc/termcap. In this
133 * case it better start with a "/". Or it can be an entry to
134 * use so we don't have to read the file. In this case it
135 * has to already have the newlines crunched out.
138 tf = open(RM = cp, O_RDONLY);
142 "<%s> open: %s", __func__, strerror(errno));
149 cnt = read(tf, ibuf, BUFSIZ);
158 if (cp > bp && cp[-1] == '\\') {
164 if (cp >= bp+BUFSIZ) {
165 write(2,"Remcap entry too long\n", 23);
173 * The real work for the match.
175 if (tnamatch(name)) {
183 * tnchktc: check the last entry, see if it's tc=xxx. If so,
184 * recursively find xxx and append that entry (minus the names)
185 * to take the place of the tc=xxx entry. This allows termcap
186 * entries to say "like an HP2621 but doesn't turn on the labels".
187 * Note that this works because of the left to right scan.
193 char tcname[16]; /* name of similar terminal */
195 char *holdtbuf = tbuf;
198 p = tbuf + strlen(tbuf) - 2; /* before the last colon */
201 write(2, "Bad remcap entry\n", 18);
205 /* p now points to beginning of last field */
206 if (p[0] != 't' || p[1] != 'c')
210 while (*q && *q != ':')
213 if (++hopcount > MAXHOP) {
214 write(2, "Infinite tc= loop\n", 18);
217 if (getent(tcbuf, tcname, remotefile) != 1) {
220 for (q = tcbuf; *q++ != ':'; )
222 l = p - holdtbuf + strlen(q);
224 write(2, "Remcap entry too long\n", 23);
225 q[BUFSIZ - (p-holdtbuf)] = 0;
233 * Tnamatch deals with name matching. The first field of the termcap
234 * entry is a sequence of names separated by |'s, so we compare
235 * against each such name. The normal : terminator after the last
236 * name (before the first field) stops us.
247 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
249 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
251 while (*Bp && *Bp != ':' && *Bp != '|')
253 if (*Bp == 0 || *Bp == ':')
260 * Skip to the next field. Notice that this is very dumb, not
261 * knowing about \: escapes or any such. If necessary, :'s can be put
262 * into the termcap file in octal.
281 while (isdigit(*bp++))
286 dquote = (dquote ? 1 : 0);
301 * Return the (numeric) option id.
302 * Numeric options look like
304 * i.e. the option string is separated from the numeric value by
305 * a # character. If the option is not found we return -1.
306 * Note that we handle octal numbers beginning with 0.
319 if (strncmp(bp, id, strlen(id)) != 0)
332 i *= base, i += *bp++ - '0';
338 * Handle a flag option.
339 * Flag options are given "naked", i.e. followed by a : or the end
340 * of the buffer. Return 1 if we find the option, or 0 if it is
352 if (strncmp(bp, id, strlen(id)) == 0) {
354 if (!*bp || *bp == ':')
363 * Get a string valued option.
366 * Much decoding is done on the strings, and the strings are
367 * placed in area, which is a ref parameter which is updated.
368 * No checking on area overflow.
371 tgetstr(char *id, char **area)
379 if (strncmp(bp, id, strlen(id)) != 0)
387 return (tdecode(bp, area));
392 * Tdecode does the grunt work to decode the
393 * string capability escapes.
396 tdecode(char *str, char **area)
411 while ((c = *str++) && c != term) {
419 dp = "E\033^^\\\\::n\nr\rt\tb\bf\f\"\"";
432 c <<= 3, c |= *str++ - '0';
433 while (--i && isdigit(*str));
439 if (c == term && term != ':') {