Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.sbin / rtadvd / advcap.c
blob892b7f1d8186fb54e1f5df1bfcd65a36817cd010
1 /* $NetBSD: advcap.c,v 1.11 2006/03/05 23:47:08 rpaulo Exp $ */
2 /* $KAME: advcap.c,v 1.11 2003/05/19 09:46:50 keiichi Exp $ */
4 /*
5 * Copyright (c) 1983 The Regents of the University of California.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
30 * SUCH DAMAGE.
34 * remcap - routines for dealing with the remote host data base
36 * derived from termcap
38 #include <sys/types.h>
39 #include <sys/uio.h>
40 #include <unistd.h>
41 #include <fcntl.h>
42 #include <ctype.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <syslog.h>
46 #include <errno.h>
47 #include <string.h>
48 #include "pathnames.h"
50 #ifndef BUFSIZ
51 #define BUFSIZ 1024
52 #endif
53 #define MAXHOP 32 /* max number of tc= indirections */
55 #define tgetent agetent
56 #define tnchktc anchktc
57 #define tnamatch anamatch
58 #define tgetnum agetnum
59 #define tgetflag agetflag
60 #define tgetstr agetstr
62 #if 0
63 #define V_TERMCAP "REMOTE"
64 #define V_TERM "HOST"
65 #endif
67 char *RM;
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.
83 static char *tbuf;
84 static int hopcount; /* detect infinite loops in termcap, init 0 */
86 static char *remotefile;
88 extern char *conffile;
90 int tgetent __P((char *, char *));
91 int getent __P((char *, char *, char *));
92 int tnchktc __P((void));
93 int tnamatch __P((char *));
94 static char *tskip __P((char *));
95 int64_t tgetnum __P((char *));
96 int tgetflag __P((char *));
97 char *tgetstr __P((char *, char **));
98 static char *tdecode __P((char *, char **));
101 * Get an entry for terminal name in buffer bp,
102 * from the termcap file. Parse is very rudimentary;
103 * we just notice escaped newlines.
106 tgetent(bp, name)
107 char *bp, *name;
109 char *cp;
111 remotefile = cp = conffile ? conffile : _PATH_RTADVDCONF;
112 return (getent(bp, name, cp));
116 getent(bp, name, cp)
117 char *bp, *name, *cp;
119 int c;
120 int i = 0, cnt = 0;
121 char ibuf[BUFSIZ];
122 int tf;
124 tbuf = bp;
125 tf = 0;
127 * TERMCAP can have one of two things in it. It can be the
128 * name of a file to use instead of /etc/termcap. In this
129 * case it better start with a "/". Or it can be an entry to
130 * use so we don't have to read the file. In this case it
131 * has to already have the newlines crunched out.
133 if (cp && *cp) {
134 tf = open(RM = cp, O_RDONLY);
136 if (tf < 0) {
137 syslog(LOG_INFO,
138 "<%s> open: %s", __func__, strerror(errno));
139 return (-2);
141 for (;;) {
142 cp = bp;
143 for (;;) {
144 if (i == cnt) {
145 cnt = read(tf, ibuf, BUFSIZ);
146 if (cnt <= 0) {
147 close(tf);
148 return (0);
150 i = 0;
152 c = ibuf[i++];
153 if (c == '\n') {
154 if (cp > bp && cp[-1] == '\\') {
155 cp--;
156 continue;
158 break;
160 if (cp >= bp + BUFSIZ) {
161 write(2,"Remcap entry too long\n", 23);
162 break;
163 } else
164 *cp++ = c;
166 *cp = 0;
169 * The real work for the match.
171 if (tnamatch(name)) {
172 close(tf);
173 return (tnchktc());
179 * tnchktc: check the last entry, see if it's tc=xxx. If so,
180 * recursively find xxx and append that entry (minus the names)
181 * to take the place of the tc=xxx entry. This allows termcap
182 * entries to say "like an HP2621 but doesn't turn on the labels".
183 * Note that this works because of the left to right scan.
186 tnchktc()
188 char *p, *q;
189 char tcname[16]; /* name of similar terminal */
190 char tcbuf[BUFSIZ];
191 char *holdtbuf = tbuf;
192 int l;
194 p = tbuf + strlen(tbuf) - 2; /* before the last colon */
195 while (*--p != ':')
196 if (p < tbuf) {
197 write(2, "Bad remcap entry\n", 18);
198 return (0);
200 p++;
201 /* p now points to beginning of last field */
202 if (p[0] != 't' || p[1] != 'c')
203 return (1);
204 strlcpy(tcname, p + 3, sizeof tcname);
205 q = tcname;
206 while (*q && *q != ':')
207 q++;
208 *q = 0;
209 if (++hopcount > MAXHOP) {
210 write(2, "Infinite tc= loop\n", 18);
211 return (0);
213 if (getent(tcbuf, tcname, remotefile) != 1) {
214 return (0);
216 for (q = tcbuf; *q++ != ':'; )
218 l = p - holdtbuf + strlen(q);
219 if (l > BUFSIZ) {
220 write(2, "Remcap entry too long\n", 23);
221 q[BUFSIZ - (p-holdtbuf)] = 0;
223 strcpy(p, q);
224 tbuf = holdtbuf;
225 return (1);
229 * Tnamatch deals with name matching. The first field of the termcap
230 * entry is a sequence of names separated by |'s, so we compare
231 * against each such name. The normal : terminator after the last
232 * name (before the first field) stops us.
235 tnamatch(np)
236 char *np;
238 char *Np, *Bp;
240 Bp = tbuf;
241 if (*Bp == '#')
242 return (0);
243 for (;;) {
244 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
245 continue;
246 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
247 return (1);
248 while (*Bp && *Bp != ':' && *Bp != '|')
249 Bp++;
250 if (*Bp == 0 || *Bp == ':')
251 return (0);
252 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.
261 static char *
262 tskip(bp)
263 char *bp;
265 int dquote;
267 dquote = 0;
268 while (*bp) {
269 switch (*bp) {
270 case ':':
271 if (!dquote)
272 goto breakbreak;
273 else
274 bp++;
275 break;
276 case '\\':
277 bp++;
278 if (isdigit((unsigned char)*bp)) {
279 while (isdigit((unsigned char)*bp++))
281 } else
282 bp++;
283 case '"':
284 dquote = (dquote ? 0 : 1);
285 bp++;
286 break;
287 default:
288 bp++;
289 break;
292 breakbreak:
293 if (*bp == ':')
294 bp++;
295 return (bp);
299 * Return the (numeric) option id.
300 * Numeric options look like
301 * li#80
302 * i.e. the option string is separated from the numeric value by
303 * a # character. If the option is not found we return -1.
304 * Note that we handle octal numbers beginning with 0.
306 int64_t
307 tgetnum(id)
308 char *id;
310 int64_t i;
311 int base;
312 char *bp = tbuf;
314 for (;;) {
315 bp = tskip(bp);
316 if (*bp == 0)
317 return (-1);
318 if (strncmp(bp, id, strlen(id)) != 0)
319 continue;
320 bp += strlen(id);
321 if (*bp == '@')
322 return (-1);
323 if (*bp != '#')
324 continue;
325 bp++;
326 base = 10;
327 if (*bp == '0')
328 base = 8;
329 i = 0;
330 while (isdigit((unsigned char)*bp))
331 i *= base, i += *bp++ - '0';
332 return (i);
337 * Handle a flag option.
338 * Flag options are given "naked", i.e. followed by a : or the end
339 * of the buffer. Return 1 if we find the option, or 0 if it is
340 * not given.
343 tgetflag(id)
344 char *id;
346 char *bp = tbuf;
348 for (;;) {
349 bp = tskip(bp);
350 if (!*bp)
351 return (0);
352 if (strncmp(bp, id, strlen(id)) == 0) {
353 bp += strlen(id);
354 if (!*bp || *bp == ':')
355 return (1);
356 else if (*bp == '@')
357 return (0);
363 * Get a string valued option.
364 * These are given as
365 * cl=^Z
366 * Much decoding is done on the strings, and the strings are
367 * placed in area, which is a ref parameter which is updated.
368 * No checking on area overflow.
370 char *
371 tgetstr(id, area)
372 char *id, **area;
374 char *bp = tbuf;
376 for (;;) {
377 bp = tskip(bp);
378 if (!*bp)
379 return (0);
380 if (strncmp(bp, id, strlen(id)) != 0)
381 continue;
382 bp += strlen(id);
383 if (*bp == '@')
384 return (0);
385 if (*bp != '=')
386 continue;
387 bp++;
388 return (tdecode(bp, area));
393 * Tdecode does the grung work to decode the
394 * string capability escapes.
396 static char *
397 tdecode(str, area)
398 char *str;
399 char **area;
401 char *cp;
402 int c;
403 char *dp;
404 int i;
405 char term;
407 term = ':';
408 cp = *area;
409 again:
410 if (*str == '"') {
411 term = '"';
412 str++;
414 while ((c = *str++) && c != term) {
415 switch (c) {
417 case '^':
418 c = *str++ & 037;
419 break;
421 case '\\':
422 dp = "E\033^^\\\\::n\nr\rt\tb\bf\f\"\"";
423 c = *str++;
424 nextc:
425 if (*dp++ == c) {
426 c = *dp++;
427 break;
429 dp++;
430 if (*dp)
431 goto nextc;
432 if (isdigit((unsigned char)c)) {
433 c -= '0', i = 2;
435 c <<= 3, c |= *str++ - '0';
436 while (--i && isdigit((unsigned char)*str));
438 break;
440 *cp++ = c;
442 if (c == term && term != ':') {
443 term = ':';
444 goto again;
446 *cp++ = 0;
447 str = *area;
448 *area = cp;
449 return (str);