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
33 * @(#)remcap.c 8.1 (Berkeley) 6/6/93
37 * remcap - routines for dealing with the remote host data base
39 * derived from termcap
41 #include <sys/types.h>
45 #include "pathnames.h"
50 #define MAXHOP 32 /* max number of tc= indirections */
52 #define tgetent rgetent
53 #define tnchktc rnchktc
54 #define tnamatch rnamatch
55 #define tgetnum rgetnum
56 #define tgetflag rgetflag
57 #define tgetstr rgetstr
58 #define E_TERMCAP RM = _PATH_REMOTE
59 #define V_TERMCAP "REMOTE"
65 * termcap - routines for dealing with the terminal capability data base
67 * BUG: Should use a "last" pointer in tbuf, so that searching
68 * for capabilities alphabetically would not be a n**2/2
69 * process when large numbers of capabilities are given.
70 * Note: If we add a last pointer now we will screw up the
71 * tc capability. We really should compile termcap.
73 * Essentially all the work here is scanning and decoding escapes
74 * in string capabilities. We don't use stdio because the editor
75 * doesn't, and because living w/o it is not hard.
79 static int hopcount
; /* detect infinite loops in termcap, init 0 */
82 static char *tdecode();
83 static char *remotefile
;
86 * Get an entry for terminal name in buffer bp,
87 * from the termcap file. Parse is very rudimentary;
88 * we just notice escaped newlines.
93 char lbuf
[BUFSIZ
], *cp
, *p
;
96 remotefile
= cp
= getenv(V_TERMCAP
);
97 if (cp
== NULL
|| strcmp(cp
, _PATH_REMOTE
) == 0) {
98 remotefile
= cp
= _PATH_REMOTE
;
99 return (getent(bp
, name
, cp
));
101 if ((rc1
= getent(bp
, name
, cp
)) != 1)
103 remotefile
= cp
= _PATH_REMOTE
;
104 rc2
= getent(lbuf
, name
, cp
);
105 if (rc1
!= 1 && rc2
!= 1)
112 if (strlen(bp
) + strlen(p
) > BUFSIZ
) {
113 write(2, "Remcap entry too long\n", 23);
124 char *bp
, *name
, *cp
;
127 register int i
= 0, cnt
= 0;
128 char ibuf
[BUFSIZ
], *cp2
;
134 * TERMCAP can have one of two things in it. It can be the
135 * name of a file to use instead of /etc/termcap. In this
136 * case it better start with a "/". Or it can be an entry to
137 * use so we don't have to read the file. In this case it
138 * has to already have the newlines crunched out.
142 cp2
= getenv(V_TERM
);
143 if (cp2
== NULL
|| strcmp(name
,cp2
) == 0) {
147 tf
= open(E_TERMCAP
, O_RDONLY
);
149 tf
= open(RM
= cp
, O_RDONLY
);
152 tf
= open(E_TERMCAP
, O_RDONLY
);
159 cnt
= read(tf
, ibuf
, BUFSIZ
);
168 if (cp
> bp
&& cp
[-1] == '\\') {
174 if (cp
>= bp
+BUFSIZ
) {
175 write(2,"Remcap entry too long\n", 23);
183 * The real work for the match.
185 if (tnamatch(name
)) {
193 * tnchktc: check the last entry, see if it's tc=xxx. If so,
194 * recursively find xxx and append that entry (minus the names)
195 * to take the place of the tc=xxx entry. This allows termcap
196 * entries to say "like an HP2621 but doesn't turn on the labels".
197 * Note that this works because of the left to right scan.
201 register char *p
, *q
;
202 char tcname
[16]; /* name of similar terminal */
204 char *holdtbuf
= tbuf
;
208 p
= tbuf
+ strlen(tbuf
) - 2; /* before the last colon */
211 write(2, "Bad remcap entry\n", 18);
215 /* p now points to beginning of last field */
216 if (p
[0] != 't' || p
[1] != 'c')
220 while (*q
&& *q
!= ':')
223 if (++hopcount
> MAXHOP
) {
224 write(2, "Infinite tc= loop\n", 18);
227 if (getent(tcbuf
, tcname
, remotefile
) != 1) {
228 if (strcmp(remotefile
, _PATH_REMOTE
) == 0)
230 else if (getent(tcbuf
, tcname
, _PATH_REMOTE
) != 1)
233 for (q
= tcbuf
; *q
++ != ':'; )
235 l
= p
- holdtbuf
+ strlen(q
);
237 write(2, "Remcap entry too long\n", 23);
238 q
[BUFSIZ
- (p
-holdtbuf
)] = 0;
246 * Tnamatch deals with name matching. The first field of the termcap
247 * entry is a sequence of names separated by |'s, so we compare
248 * against each such name. The normal : terminator after the last
249 * name (before the first field) stops us.
254 register char *Np
, *Bp
;
260 for (Np
= np
; *Np
&& *Bp
== *Np
; Bp
++, Np
++)
262 if (*Np
== 0 && (*Bp
== '|' || *Bp
== ':' || *Bp
== 0))
264 while (*Bp
&& *Bp
!= ':' && *Bp
!= '|')
266 if (*Bp
== 0 || *Bp
== ':')
273 * Skip to the next field. Notice that this is very dumb, not
274 * knowing about \: escapes or any such. If necessary, :'s can be put
275 * into the termcap file in octal.
282 while (*bp
&& *bp
!= ':')
290 * Return the (numeric) option id.
291 * Numeric options look like
293 * i.e. the option string is separated from the numeric value by
294 * a # character. If the option is not found we return -1.
295 * Note that we handle octal numbers beginning with 0.
300 register int i
, base
;
301 register char *bp
= tbuf
;
307 if (*bp
++ != id
[0] || *bp
== 0 || *bp
++ != id
[1])
319 i
*= base
, i
+= *bp
++ - '0';
325 * Handle a flag option.
326 * Flag options are given "naked", i.e. followed by a : or the end
327 * of the buffer. Return 1 if we find the option, or 0 if it is
333 register char *bp
= tbuf
;
339 if (*bp
++ == id
[0] && *bp
!= 0 && *bp
++ == id
[1]) {
340 if (!*bp
|| *bp
== ':')
349 * Get a string valued option.
352 * Much decoding is done on the strings, and the strings are
353 * placed in area, which is a ref parameter which is updated.
354 * No checking on area overflow.
360 register char *bp
= tbuf
;
366 if (*bp
++ != id
[0] || *bp
== 0 || *bp
++ != id
[1])
373 return (tdecode(bp
, area
));
378 * Tdecode does the grung work to decode the
379 * string capability escapes.
392 while ((c
= *str
++) && c
!= ':') {
400 dp
= "E\033^^\\\\::n\nr\rt\tb\bf\f";
413 c
<<= 3, c
|= *str
++ - '0';
414 while (--i
&& isdigit(*str
));