2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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 static char sccsid[] = "@(#)remcap.c 8.1 (Berkeley) 6/6/93";
39 * remcap - routines for dealing with the remote host data base
41 * derived from termcap
43 #include <sys/types.h>
47 #include "pathnames.h"
52 #define MAXHOP 32 /* max number of tc= indirections */
54 #define tgetent rgetent
55 #define tnchktc rnchktc
56 #define tnamatch rnamatch
57 #define tgetnum rgetnum
58 #define tgetflag rgetflag
59 #define tgetstr rgetstr
60 #define E_TERMCAP RM = _PATH_REMOTE
61 #define V_TERMCAP "REMOTE"
67 * termcap - routines for dealing with the terminal capability data base
69 * BUG: Should use a "last" pointer in tbuf, so that searching
70 * for capabilities alphabetically would not be a n**2/2
71 * process when large numbers of capabilities are given.
72 * Note: If we add a last pointer now we will screw up the
73 * tc capability. We really should compile termcap.
75 * Essentially all the work here is scanning and decoding escapes
76 * in string capabilities. We don't use stdio because the editor
77 * doesn't, and because living w/o it is not hard.
81 static int hopcount; /* detect infinite loops in termcap, init 0 */
84 static char *tdecode();
85 static char *remotefile;
88 * Get an entry for terminal name in buffer bp,
89 * from the termcap file. Parse is very rudimentary;
90 * we just notice escaped newlines.
95 char lbuf[BUFSIZ], *cp, *p;
98 remotefile = cp = getenv(V_TERMCAP);
99 if (cp == (char *)0 || strcmp(cp, _PATH_REMOTE) == 0) {
100 remotefile = cp = _PATH_REMOTE;
101 return (getent(bp, name, cp));
103 if ((rc1 = getent(bp, name, cp)) != 1)
105 remotefile = cp = _PATH_REMOTE;
106 rc2 = getent(lbuf, name, cp);
107 if (rc1 != 1 && rc2 != 1)
114 if (strlen(bp) + strlen(p) > BUFSIZ) {
115 write(2, "Remcap entry too long\n", 23);
126 char *bp, *name, *cp;
129 register int i = 0, cnt = 0;
130 char ibuf[BUFSIZ], *cp2;
136 * TERMCAP can have one of two things in it. It can be the
137 * name of a file to use instead of /etc/termcap. In this
138 * case it better start with a "/". Or it can be an entry to
139 * use so we don't have to read the file. In this case it
140 * has to already have the newlines crunched out.
144 cp2 = getenv(V_TERM);
145 if (cp2 == (char *)0 || strcmp(name,cp2) == 0) {
149 tf = open(E_TERMCAP, O_RDONLY);
151 tf = open(RM = cp, O_RDONLY);
154 tf = open(E_TERMCAP, O_RDONLY);
161 cnt = read(tf, ibuf, BUFSIZ);
170 if (cp > bp && cp[-1] == '\\') {
176 if (cp >= bp+BUFSIZ) {
177 write(2,"Remcap entry too long\n", 23);
185 * The real work for the match.
187 if (tnamatch(name)) {
195 * tnchktc: check the last entry, see if it's tc=xxx. If so,
196 * recursively find xxx and append that entry (minus the names)
197 * to take the place of the tc=xxx entry. This allows termcap
198 * entries to say "like an HP2621 but doesn't turn on the labels".
199 * Note that this works because of the left to right scan.
203 register char *p, *q;
204 char tcname[16]; /* name of similar terminal */
206 char *holdtbuf = tbuf;
210 p = tbuf + strlen(tbuf) - 2; /* before the last colon */
213 write(2, "Bad remcap entry\n", 18);
217 /* p now points to beginning of last field */
218 if (p[0] != 't' || p[1] != 'c')
222 while (*q && *q != ':')
225 if (++hopcount > MAXHOP) {
226 write(2, "Infinite tc= loop\n", 18);
229 if (getent(tcbuf, tcname, remotefile) != 1) {
230 if (strcmp(remotefile, _PATH_REMOTE) == 0)
232 else if (getent(tcbuf, tcname, _PATH_REMOTE) != 1)
235 for (q = tcbuf; *q++ != ':'; )
237 l = p - holdtbuf + strlen(q);
239 write(2, "Remcap entry too long\n", 23);
240 q[BUFSIZ - (p-holdtbuf)] = 0;
248 * Tnamatch deals with name matching. The first field of the termcap
249 * entry is a sequence of names separated by |'s, so we compare
250 * against each such name. The normal : terminator after the last
251 * name (before the first field) stops us.
256 register char *Np, *Bp;
262 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
264 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
266 while (*Bp && *Bp != ':' && *Bp != '|')
268 if (*Bp == 0 || *Bp == ':')
275 * Skip to the next field. Notice that this is very dumb, not
276 * knowing about \: escapes or any such. If necessary, :'s can be put
277 * into the termcap file in octal.
284 while (*bp && *bp != ':')
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.
302 register int i, base;
303 register char *bp = tbuf;
309 if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
321 i *= base, i += *bp++ - '0';
327 * Handle a flag option.
328 * Flag options are given "naked", i.e. followed by a : or the end
329 * of the buffer. Return 1 if we find the option, or 0 if it is
335 register char *bp = tbuf;
341 if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
342 if (!*bp || *bp == ':')
351 * Get a string valued option.
354 * Much decoding is done on the strings, and the strings are
355 * placed in area, which is a ref parameter which is updated.
356 * No checking on area overflow.
362 register char *bp = tbuf;
368 if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
375 return (tdecode(bp, area));
380 * Tdecode does the grung work to decode the
381 * string capability escapes.
394 while ((c = *str++) && c != ':') {
402 dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
415 c <<= 3, c |= *str++ - '0';
416 while (--i && isdigit(*str));