drm/radeon: Reduce differences with Linux 3.18
[dragonfly.git] / usr.sbin / rtadvd / advcap.c
blobefeec4baf6437c0b380cc7e1543d38a4a7df1a5f
1 /* $FreeBSD: stable/10/usr.sbin/rtadvd/advcap.c 222824 2011-06-07 15:40:17Z hrs $ */
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
68 * termcap - routines for dealing with the terminal capability data base
70 * BUG: Should use a "last" pointer in tbuf, so that searching
71 * for capabilities alphabetically would not be a n**2/2
72 * process when large numbers of capabilities are given.
73 * Note: If we add a last pointer now we will screw up the
74 * tc capability. We really should compile termcap.
76 * Essentially all the work here is scanning and decoding escapes
77 * in string capabilities. We don't use stdio because the editor
78 * doesn't, and because living w/o it is not hard.
81 static char *tbuf;
82 static int hopcount; /* detect infinite loops in termcap, init 0 */
84 extern const char *conffile;
86 int tgetent(char *, char *);
87 int getent(char *, char *, const char *);
88 int tnchktc(void);
89 int tnamatch(char *);
90 static char *tskip(char *);
91 int64_t tgetnum(char *);
92 int tgetflag(char *);
93 char *tgetstr(char *, char **);
94 static char *tdecode(char *, char **);
97 * Get an entry for terminal name in buffer bp,
98 * from the termcap file. Parse is very rudimentary;
99 * we just notice escaped newlines.
102 tgetent(char *bp, char *name)
104 return (getent(bp, name, conffile));
108 getent(char *bp, char *name, const char *cfile)
110 int c;
111 int i = 0, cnt = 0;
112 char ibuf[BUFSIZ];
113 char *cp;
114 int tf;
116 tbuf = bp;
117 tf = 0;
119 * TERMCAP can have one of two things in it. It can be the
120 * name of a file to use instead of /etc/termcap. In this
121 * case it better start with a "/". Or it can be an entry to
122 * use so we don't have to read the file. In this case it
123 * has to already have the newlines crunched out.
125 if (cfile && *cfile)
126 tf = open(cfile, O_RDONLY);
128 if (tf < 0) {
129 syslog(LOG_INFO,
130 "<%s> open: %s", __func__, strerror(errno));
131 return (-2);
133 for (;;) {
134 cp = bp;
135 for (;;) {
136 if (i == cnt) {
137 cnt = read(tf, ibuf, BUFSIZ);
138 if (cnt <= 0) {
139 close(tf);
140 return (0);
142 i = 0;
144 c = ibuf[i++];
145 if (c == '\n') {
146 if (cp > bp && cp[-1] == '\\') {
147 cp--;
148 continue;
150 break;
152 if (cp >= bp + BUFSIZ) {
153 write(STDERR_FILENO, "Remcap entry too long\n",
154 23);
155 break;
156 } else
157 *cp++ = c;
159 *cp = 0;
162 * The real work for the match.
164 if (tnamatch(name)) {
165 close(tf);
166 return (tnchktc());
172 * tnchktc: check the last entry, see if it's tc=xxx. If so,
173 * recursively find xxx and append that entry (minus the names)
174 * to take the place of the tc=xxx entry. This allows termcap
175 * entries to say "like an HP2621 but doesn't turn on the labels".
176 * Note that this works because of the left to right scan.
179 tnchktc(void)
181 char *p, *q;
182 char tcname[16]; /* name of similar terminal */
183 char tcbuf[BUFSIZ];
184 char *holdtbuf = tbuf;
185 int l;
187 p = tbuf + strlen(tbuf) - 2; /* before the last colon */
188 while (*--p != ':')
189 if (p < tbuf) {
190 write(STDERR_FILENO, "Bad remcap entry\n", 18);
191 return (0);
193 p++;
194 /* p now points to beginning of last field */
195 if (p[0] != 't' || p[1] != 'c')
196 return (1);
197 strlcpy(tcname, p + 3, sizeof tcname);
198 q = tcname;
199 while (*q && *q != ':')
200 q++;
201 *q = 0;
202 if (++hopcount > MAXHOP) {
203 write(STDERR_FILENO, "Infinite tc= loop\n", 18);
204 return (0);
206 if (getent(tcbuf, tcname, conffile) != 1) {
207 return (0);
209 for (q = tcbuf; *q++ != ':'; )
211 l = p - holdtbuf + strlen(q);
212 if (l > BUFSIZ) {
213 write(STDERR_FILENO, "Remcap entry too long\n", 23);
214 q[BUFSIZ - (p-holdtbuf)] = 0;
216 strcpy(p, q);
217 tbuf = holdtbuf;
218 return (1);
222 * Tnamatch deals with name matching. The first field of the termcap
223 * entry is a sequence of names separated by |'s, so we compare
224 * against each such name. The normal : terminator after the last
225 * name (before the first field) stops us.
228 tnamatch(char *np)
230 char *Np, *Bp;
232 Bp = tbuf;
233 if (*Bp == '#')
234 return (0);
235 for (;;) {
236 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
237 continue;
238 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
239 return (1);
240 while (*Bp && *Bp != ':' && *Bp != '|')
241 Bp++;
242 if (*Bp == 0 || *Bp == ':')
243 return (0);
244 Bp++;
249 * Skip to the next field. Notice that this is very dumb, not
250 * knowing about \: escapes or any such. If necessary, :'s can be put
251 * into the termcap file in octal.
253 static char *
254 tskip(char *bp)
256 int dquote;
258 dquote = 0;
259 while (*bp) {
260 switch (*bp) {
261 case ':':
262 if (!dquote)
263 goto breakbreak;
264 else
265 bp++;
266 break;
267 case '\\':
268 bp++;
269 if (isdigit(*bp)) {
270 while (isdigit(*bp++))
272 } else
273 bp++;
274 /* FALLTHROUGH */
275 case '"':
276 dquote = (dquote ? 1 : 0);
277 bp++;
278 break;
279 default:
280 bp++;
281 break;
284 breakbreak:
285 if (*bp == ':')
286 bp++;
287 return (bp);
291 * Return the (numeric) option id.
292 * Numeric options look like
293 * li#80
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.
298 int64_t
299 tgetnum(char *id)
301 int64_t i;
302 int base;
303 char *bp = tbuf;
305 for (;;) {
306 bp = tskip(bp);
307 if (*bp == 0)
308 return (-1);
309 if (strncmp(bp, id, strlen(id)) != 0)
310 continue;
311 bp += strlen(id);
312 if (*bp == '@')
313 return (-1);
314 if (*bp != '#')
315 continue;
316 bp++;
317 base = 10;
318 if (*bp == '0')
319 base = 8;
320 i = 0;
321 while (isdigit(*bp))
322 i *= base, i += *bp++ - '0';
323 return (i);
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
331 * not given.
334 tgetflag(char *id)
336 char *bp = tbuf;
338 for (;;) {
339 bp = tskip(bp);
340 if (!*bp)
341 return (0);
342 if (strncmp(bp, id, strlen(id)) == 0) {
343 bp += strlen(id);
344 if (!*bp || *bp == ':')
345 return (1);
346 else if (*bp == '@')
347 return (0);
353 * Get a string valued option.
354 * These are given as
355 * cl=^Z
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.
360 char *
361 tgetstr(char *id, char **area)
363 char *bp = tbuf;
365 for (;;) {
366 bp = tskip(bp);
367 if (!*bp)
368 return (0);
369 if (strncmp(bp, id, strlen(id)) != 0)
370 continue;
371 bp += strlen(id);
372 if (*bp == '@')
373 return (0);
374 if (*bp != '=')
375 continue;
376 bp++;
377 return (tdecode(bp, area));
382 * Tdecode does the grung work to decode the
383 * string capability escapes.
385 static char *
386 tdecode(char *str, char **area)
388 char *cp;
389 int c;
390 const char *dp;
391 int i;
392 char term;
394 term = ':';
395 cp = *area;
396 again:
397 if (*str == '"') {
398 term = '"';
399 str++;
401 while ((c = *str++) && c != term) {
402 switch (c) {
404 case '^':
405 c = *str++ & 037;
406 break;
408 case '\\':
409 dp = "E\033^^\\\\::n\nr\rt\tb\bf\f\"\"";
410 c = *str++;
411 nextc:
412 if (*dp++ == c) {
413 c = *dp++;
414 break;
416 dp++;
417 if (*dp)
418 goto nextc;
419 if (isdigit(c)) {
420 c -= '0', i = 2;
422 c <<= 3, c |= *str++ - '0';
423 while (--i && isdigit(*str));
425 break;
427 *cp++ = c;
429 if (c == term && term != ':') {
430 term = ':';
431 goto again;
433 *cp++ = 0;
434 str = *area;
435 *area = cp;
436 return (str);