1 /* $NetBSD: advcap.c,v 1.15 2015/06/05 15:41:59 roy Exp $ */
2 /* $KAME: advcap.c,v 1.11 2003/05/19 09:46:50 keiichi 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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * remcap - routines for dealing with the remote host data base
36 * derived from termcap
38 #include <sys/types.h>
48 #include "pathnames.h"
51 #define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
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(char *, char *);
95 int getent(char *, char *, char *);
98 static char *tskip(char *);
99 int64_t tgetnum(char *);
100 int tgetflag(char *);
101 char *tgetstr(char *, char **);
102 static char *tdecode(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.
110 tgetent(char *bp
, char *name
)
114 remotefile
= cp
= conffile
? conffile
: __UNCONST(_PATH_RTADVDCONF
);
115 return (getent(bp
, name
, cp
));
119 getent(char *bp
, char *name
, char *cp
)
129 * TERMCAP can have one of two things in it. It can be the
130 * name of a file to use instead of /etc/termcap. In this
131 * case it better start with a "/". Or it can be an entry to
132 * use so we don't have to read the file. In this case it
133 * has to already have the newlines crunched out.
136 tf
= open(RM
= cp
, O_RDONLY
);
139 syslog(LOG_INFO
, "<%s> open: %m", __func__
);
146 cnt
= read(tf
, ibuf
, BUFSIZ
);
155 if (cp
> bp
&& cp
[-1] == '\\') {
161 if (cp
>= bp
+ BUFSIZ
) {
162 write(2,"Remcap entry too long\n", 23);
170 * The real work for the match.
172 if (tnamatch(name
)) {
180 * tnchktc: check the last entry, see if it's tc=xxx. If so,
181 * recursively find xxx and append that entry (minus the names)
182 * to take the place of the tc=xxx entry. This allows termcap
183 * entries to say "like an HP2621 but doesn't turn on the labels".
184 * Note that this works because of the left to right scan.
190 char tcname
[16]; /* name of similar terminal */
192 char *holdtbuf
= tbuf
;
195 p
= tbuf
+ strlen(tbuf
) - 2; /* before the last colon */
198 write(2, "Bad remcap entry\n", 18);
202 /* p now points to beginning of last field */
203 if (p
[0] != 't' || p
[1] != 'c')
205 strlcpy(tcname
, p
+ 3, sizeof tcname
);
207 while (*q
&& *q
!= ':')
210 if (++hopcount
> MAXHOP
) {
211 write(2, "Infinite tc= loop\n", 18);
214 if (getent(tcbuf
, tcname
, remotefile
) != 1) {
217 for (q
= tcbuf
; *q
++ != ':'; )
219 l
= p
- holdtbuf
+ strlen(q
);
221 write(2, "Remcap entry too long\n", 23);
222 q
[BUFSIZ
- (p
-holdtbuf
)] = 0;
230 * Tnamatch deals with name matching. The first field of the termcap
231 * entry is a sequence of names separated by |'s, so we compare
232 * against each such name. The normal : terminator after the last
233 * name (before the first field) stops us.
244 for (Np
= np
; *Np
&& *Bp
== *Np
; Bp
++, Np
++)
246 if (*Np
== 0 && (*Bp
== '|' || *Bp
== ':' || *Bp
== 0))
248 while (*Bp
&& *Bp
!= ':' && *Bp
!= '|')
250 if (*Bp
== 0 || *Bp
== ':')
257 * Skip to the next field. Notice that this is very dumb, not
258 * knowing about \: escapes or any such. If necessary, :'s can be put
259 * into the termcap file in octal.
277 if (isdigit((unsigned char)*bp
)) {
278 while (isdigit((unsigned char)*bp
++))
283 dquote
= (dquote
? 0 : 1);
298 * Return the (numeric) option id.
299 * Numeric options look like
301 * i.e. the option string is separated from the numeric value by
302 * a # character. If the option is not found we return -1.
303 * Note that we handle octal numbers beginning with 0.
316 if (strncmp(bp
, id
, strlen(id
)) != 0)
328 while (isdigit((unsigned char)*bp
))
329 i
*= base
, i
+= *bp
++ - '0';
335 * Handle a flag option.
336 * Flag options are given "naked", i.e. followed by a : or the end
337 * of the buffer. Return 1 if we find the option, or 0 if it is
349 if (strncmp(bp
, id
, strlen(id
)) == 0) {
351 if (!*bp
|| *bp
== ':')
360 * Get a string valued option.
363 * Much decoding is done on the strings, and the strings are
364 * placed in area, which is a ref parameter which is updated.
365 * No checking on area overflow.
368 tgetstr(char *id
, char **area
)
376 if (strncmp(bp
, id
, strlen(id
)) != 0)
384 return (tdecode(bp
, area
));
389 * Tdecode does the grung work to decode the
390 * string capability escapes.
393 tdecode(char *str
, char **area
)
397 const char *dps
= "E\033^^\\\\::n\nr\rt\tb\bf\f\"\"", *dp
;
408 while ((c
= *str
++) && c
!= term
) {
426 if (isdigit((unsigned char)c
)) {
429 c
<<= 3, c
|= *str
++ - '0';
430 while (--i
&& isdigit((unsigned char)*str
));
436 if (c
== term
&& term
!= ':') {