2 /* $KAME: advcap.c,v 1.3 2000/05/16 13:34:13 itojun Exp $ */
5 * Copyright (c) 1983 The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * remcap - routines for dealing with the remote host data base
40 * derived from termcap
42 #include <sys/types.h>
52 #include "pathnames.h"
57 #define MAXHOP 32 /* max number of tc= indirections */
59 #define tgetent agetent
60 #define tnchktc anchktc
61 #define tnamatch anamatch
62 #define tgetnum agetnum
63 #define tgetflag agetflag
64 #define tgetstr agetstr
67 #define V_TERMCAP "REMOTE"
74 * termcap - routines for dealing with the terminal capability data base
76 * BUG: Should use a "last" pointer in tbuf, so that searching
77 * for capabilities alphabetically would not be a n**2/2
78 * process when large numbers of capabilities are given.
79 * Note: If we add a last pointer now we will screw up the
80 * tc capability. We really should compile termcap.
82 * Essentially all the work here is scanning and decoding escapes
83 * in string capabilities. We don't use stdio because the editor
84 * doesn't, and because living w/o it is not hard.
88 static int hopcount; /* detect infinite loops in termcap, init 0 */
90 static char *remotefile;
92 extern char *conffile;
94 int tgetent __P((char *, char *));
95 int getent __P((char *, char *, char *));
96 int tnchktc __P((void));
97 int tnamatch __P((char *));
98 static char *tskip __P((char *));
99 int tgetnum __P((char *));
100 int tgetflag __P((char *));
101 char *tgetstr __P((char *, char **));
102 static char *tdecode __P((char *, char **));
105 * Get an entry for terminal name in buffer bp,
106 * from the termcap file. Parse is very rudimentary;
107 * we just notice escaped newlines.
115 remotefile = cp = conffile ? conffile : _PATH_RTADVDCONF;
116 return (getent(bp, name, cp));
121 char *bp, *name, *cp;
124 register int i = 0, cnt = 0;
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", __FUNCTION__, 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.
192 register char *p, *q;
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.
242 register char *Np, *Bp;
248 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
250 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
252 while (*Bp && *Bp != ':' && *Bp != '|')
254 if (*Bp == 0 || *Bp == ':')
261 * Skip to the next field. Notice that this is very dumb, not
262 * knowing about \: escapes or any such. If necessary, :'s can be put
263 * into the termcap file in octal.
283 while (isdigit(*bp++))
288 dquote = (dquote ? 1 : 0);
303 * Return the (numeric) option id.
304 * Numeric options look like
306 * i.e. the option string is separated from the numeric value by
307 * a # character. If the option is not found we return -1.
308 * Note that we handle octal numbers beginning with 0.
316 register char *bp = tbuf;
322 if (strncmp(bp, id, strlen(id)) != 0)
335 i *= base, i += *bp++ - '0';
341 * Handle a flag option.
342 * Flag options are given "naked", i.e. followed by a : or the end
343 * of the buffer. Return 1 if we find the option, or 0 if it is
350 register char *bp = tbuf;
356 if (strncmp(bp, id, strlen(id)) == 0) {
358 if (!*bp || *bp == ':')
367 * Get a string valued option.
370 * Much decoding is done on the strings, and the strings are
371 * placed in area, which is a ref parameter which is updated.
372 * No checking on area overflow.
378 register char *bp = tbuf;
384 if (strncmp(bp, id, strlen(id)) != 0)
392 return (tdecode(bp, area));
397 * Tdecode does the grung work to decode the
398 * string capability escapes.
418 while ((c = *str++) && c != term) {
426 dp = "E\033^^\\\\::n\nr\rt\tb\bf\f\"\"";
439 c <<= 3, c |= *str++ - '0';
440 while (--i && isdigit(*str));
446 if (c == term && term != ':') {