2 /* $KAME: advcap.c,v 1.11 2003/05/19 09:46:50 keiichi Exp $ */
5 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (c) 1983 The Regents of the University of California.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * remcap - routines for dealing with the remote host data base
38 * derived from termcap
40 #include <sys/types.h>
50 #include "pathnames.h"
55 #define MAXHOP 32 /* max number of tc= indirections */
57 #define tgetent agetent
58 #define tnchktc anchktc
59 #define tnamatch anamatch
60 #define tgetnum agetnum
61 #define tgetflag agetflag
62 #define tgetstr agetstr
65 #define V_TERMCAP "REMOTE"
70 * termcap - routines for dealing with the terminal capability data base
72 * BUG: Should use a "last" pointer in tbuf, so that searching
73 * for capabilities alphabetically would not be a n**2/2
74 * process when large numbers of capabilities are given.
75 * Note: If we add a last pointer now we will screw up the
76 * tc capability. We really should compile termcap.
78 * Essentially all the work here is scanning and decoding escapes
79 * in string capabilities. We don't use stdio because the editor
80 * doesn't, and because living w/o it is not hard.
84 static int hopcount; /* detect infinite loops in termcap, init 0 */
86 extern const char *conffile;
88 int tgetent(char *, char *);
89 int getent(char *, char *, const char *);
92 static char *tskip(char *);
93 int64_t tgetnum(char *);
95 char *tgetstr(char *, char **);
96 static char *tdecode(char *, char **);
99 * Get an entry for terminal name in buffer bp,
100 * from the termcap file. Parse is very rudimentary;
101 * we just notice escaped newlines.
104 tgetent(char *bp, char *name)
106 return (getent(bp, name, conffile));
110 getent(char *bp, char *name, const char *cfile)
121 * TERMCAP can have one of two things in it. It can be the
122 * name of a file to use instead of /etc/termcap. In this
123 * case it better start with a "/". Or it can be an entry to
124 * use so we don't have to read the file. In this case it
125 * has to already have the newlines crunched out.
128 tf = open(cfile, O_RDONLY);
132 "<%s> open: %s", __func__, strerror(errno));
139 cnt = read(tf, ibuf, BUFSIZ);
148 if (cp > bp && cp[-1] == '\\') {
154 if (cp >= bp + BUFSIZ - 1) {
155 write(STDERR_FILENO, "Remcap entry too long\n",
164 * The real work for the match.
166 if (tnamatch(name)) {
174 * tnchktc: check the last entry, see if it's tc=xxx. If so,
175 * recursively find xxx and append that entry (minus the names)
176 * to take the place of the tc=xxx entry. This allows termcap
177 * entries to say "like an HP2621 but doesn't turn on the labels".
178 * Note that this works because of the left to right scan.
184 char tcname[16]; /* name of similar terminal */
186 char *holdtbuf = tbuf;
189 p = tbuf + strlen(tbuf) - 2; /* before the last colon */
192 write(STDERR_FILENO, "Bad remcap entry\n", 18);
196 /* p now points to beginning of last field */
197 if (p[0] != 't' || p[1] != 'c')
199 strlcpy(tcname, p + 3, sizeof tcname);
201 while (*q && *q != ':')
204 if (++hopcount > MAXHOP) {
205 write(STDERR_FILENO, "Infinite tc= loop\n", 18);
208 if (getent(tcbuf, tcname, conffile) != 1) {
211 for (q = tcbuf; *q++ != ':'; )
213 l = p - holdtbuf + strlen(q);
215 write(STDERR_FILENO, "Remcap entry too long\n", 23);
216 q[BUFSIZ - (p-holdtbuf)] = 0;
224 * Tnamatch deals with name matching. The first field of the termcap
225 * entry is a sequence of names separated by |'s, so we compare
226 * against each such name. The normal : terminator after the last
227 * name (before the first field) stops us.
238 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
240 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
242 while (*Bp && *Bp != ':' && *Bp != '|')
244 if (*Bp == 0 || *Bp == ':')
251 * Skip to the next field. Notice that this is very dumb, not
252 * knowing about \: escapes or any such. If necessary, :'s can be put
253 * into the termcap file in octal.
272 while (isdigit(*bp++))
277 dquote = (dquote ? 1 : 0);
292 * Return the (numeric) option id.
293 * Numeric options look like
295 * i.e. the option string is separated from the numeric value by
296 * a # character. If the option is not found we return -1.
297 * Note that we handle octal numbers beginning with 0.
310 if (strncmp(bp, id, strlen(id)) != 0)
323 i *= base, i += *bp++ - '0';
329 * Handle a flag option.
330 * Flag options are given "naked", i.e. followed by a : or the end
331 * of the buffer. Return 1 if we find the option, or 0 if it is
343 if (strncmp(bp, id, strlen(id)) == 0) {
345 if (!*bp || *bp == ':')
354 * Get a string valued option.
357 * Much decoding is done on the strings, and the strings are
358 * placed in area, which is a ref parameter which is updated.
359 * No checking on area overflow.
362 tgetstr(char *id, char **area)
370 if (strncmp(bp, id, strlen(id)) != 0)
378 return (tdecode(bp, area));
383 * Tdecode does the grung work to decode the
384 * string capability escapes.
387 tdecode(char *str, char **area)
402 while ((c = *str++) && c != term) {
410 dp = "E\033^^\\\\::n\nr\rt\tb\bf\f\"\"";
423 c <<= 3, c |= *str++ - '0';
424 while (--i && isdigit(*str));
430 if (c == term && term != ':') {