1 /* $KAME: advcap.c,v 1.11 2003/05/19 09:46:50 keiichi Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 1983 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * remcap - routines for dealing with the remote host data base
37 * derived from termcap
39 #include <sys/types.h>
49 #include "pathnames.h"
54 #define MAXHOP 32 /* max number of tc= indirections */
56 #define tgetent agetent
57 #define tnchktc anchktc
58 #define tnamatch anamatch
59 #define tgetnum agetnum
60 #define tgetflag agetflag
61 #define tgetstr agetstr
64 #define V_TERMCAP "REMOTE"
69 * termcap - routines for dealing with the terminal capability data base
71 * BUG: Should use a "last" pointer in tbuf, so that searching
72 * for capabilities alphabetically would not be a n**2/2
73 * process when large numbers of capabilities are given.
74 * Note: If we add a last pointer now we will screw up the
75 * tc capability. We really should compile termcap.
77 * Essentially all the work here is scanning and decoding escapes
78 * in string capabilities. We don't use stdio because the editor
79 * doesn't, and because living w/o it is not hard.
83 static int hopcount; /* detect infinite loops in termcap, init 0 */
85 extern const char *conffile;
87 int tgetent(char *, char *);
88 int getent(char *, char *, const char *);
91 static char *tskip(char *);
92 int64_t tgetnum(char *);
94 char *tgetstr(char *, char **);
95 static char *tdecode(char *, char **);
98 * Get an entry for terminal name in buffer bp,
99 * from the termcap file. Parse is very rudimentary;
100 * we just notice escaped newlines.
103 tgetent(char *bp, char *name)
105 return (getent(bp, name, conffile));
109 getent(char *bp, char *name, const char *cfile)
120 * TERMCAP can have one of two things in it. It can be the
121 * name of a file to use instead of /etc/termcap. In this
122 * case it better start with a "/". Or it can be an entry to
123 * use so we don't have to read the file. In this case it
124 * has to already have the newlines crunched out.
127 tf = open(cfile, O_RDONLY);
131 "<%s> open: %s", __func__, strerror(errno));
138 cnt = read(tf, ibuf, BUFSIZ);
147 if (cp > bp && cp[-1] == '\\') {
153 if (cp >= bp + BUFSIZ - 1) {
154 write(STDERR_FILENO, "Remcap entry too long\n",
163 * The real work for the match.
165 if (tnamatch(name)) {
173 * tnchktc: check the last entry, see if it's tc=xxx. If so,
174 * recursively find xxx and append that entry (minus the names)
175 * to take the place of the tc=xxx entry. This allows termcap
176 * entries to say "like an HP2621 but doesn't turn on the labels".
177 * Note that this works because of the left to right scan.
183 char tcname[16]; /* name of similar terminal */
185 char *holdtbuf = tbuf;
188 p = tbuf + strlen(tbuf) - 2; /* before the last colon */
191 write(STDERR_FILENO, "Bad remcap entry\n", 18);
195 /* p now points to beginning of last field */
196 if (p[0] != 't' || p[1] != 'c')
198 strlcpy(tcname, p + 3, sizeof tcname);
200 while (*q && *q != ':')
203 if (++hopcount > MAXHOP) {
204 write(STDERR_FILENO, "Infinite tc= loop\n", 18);
207 if (getent(tcbuf, tcname, conffile) != 1) {
210 for (q = tcbuf; *q++ != ':'; )
212 l = p - holdtbuf + strlen(q);
214 write(STDERR_FILENO, "Remcap entry too long\n", 23);
215 q[BUFSIZ - (p-holdtbuf)] = 0;
223 * Tnamatch deals with name matching. The first field of the termcap
224 * entry is a sequence of names separated by |'s, so we compare
225 * against each such name. The normal : terminator after the last
226 * name (before the first field) stops us.
237 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
239 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
241 while (*Bp && *Bp != ':' && *Bp != '|')
243 if (*Bp == 0 || *Bp == ':')
250 * Skip to the next field. Notice that this is very dumb, not
251 * knowing about \: escapes or any such. If necessary, :'s can be put
252 * into the termcap file in octal.
271 while (isdigit(*bp++))
276 dquote = (dquote ? 1 : 0);
291 * Return the (numeric) option id.
292 * Numeric options look like
294 * i.e. the option string is separated from the numeric value by
295 * a # character. If the option is not found we return -1.
296 * Note that we handle octal numbers beginning with 0.
309 if (strncmp(bp, id, strlen(id)) != 0)
322 i *= base, i += *bp++ - '0';
328 * Handle a flag option.
329 * Flag options are given "naked", i.e. followed by a : or the end
330 * of the buffer. Return 1 if we find the option, or 0 if it is
342 if (strncmp(bp, id, strlen(id)) == 0) {
344 if (!*bp || *bp == ':')
353 * Get a string valued option.
356 * Much decoding is done on the strings, and the strings are
357 * placed in area, which is a ref parameter which is updated.
358 * No checking on area overflow.
361 tgetstr(char *id, char **area)
369 if (strncmp(bp, id, strlen(id)) != 0)
377 return (tdecode(bp, area));
382 * Tdecode does the grung work to decode the
383 * string capability escapes.
386 tdecode(char *str, char **area)
401 while ((c = *str++) && c != term) {
409 dp = "E\033^^\\\\::n\nr\rt\tb\bf\f\"\"";
422 c <<= 3, c |= *str++ - '0';
423 while (--i && isdigit(*str));
429 if (c == term && term != ':') {