]> git.cameronkatri.com Git - apple_cmds.git/blob - network_cmds/rtadvd.tproj/advcap.c
download.sh: Properly update
[apple_cmds.git] / network_cmds / rtadvd.tproj / advcap.c
1 /*
2 * Copyright (c) 2009-2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /* $KAME: advcap.c,v 1.11 2003/05/19 09:46:50 keiichi Exp $ */
30
31 /*
32 * Copyright (c) 1983 The Regents of the University of California.
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 */
63
64 /*
65 * remcap - routines for dealing with the remote host data base
66 *
67 * derived from termcap
68 */
69 #include <sys/types.h>
70 #include <sys/uio.h>
71 #include <unistd.h>
72 #include <fcntl.h>
73 #include <ctype.h>
74 #include <stdlib.h>
75 #include <stdio.h>
76 #include <errno.h>
77 #include <string.h>
78 #include "pathnames.h"
79 #include "rtadvd_logging.h"
80
81 #ifndef BUFSIZ
82 #define BUFSIZ 1024
83 #endif
84 #define MAXHOP 32 /* max number of tc= indirections */
85
86 #define tgetent agetent
87 #define tnchktc anchktc
88 #define tnamatch anamatch
89 #define tgetnum agetnum
90 #define tgetflag agetflag
91 #define tgetstr agetstr
92
93 #if 0
94 #define V_TERMCAP "REMOTE"
95 #define V_TERM "HOST"
96 #endif
97
98 char *RM;
99
100 /*
101 * termcap - routines for dealing with the terminal capability data base
102 *
103 * BUG: Should use a "last" pointer in tbuf, so that searching
104 * for capabilities alphabetically would not be a n**2/2
105 * process when large numbers of capabilities are given.
106 * Note: If we add a last pointer now we will screw up the
107 * tc capability. We really should compile termcap.
108 *
109 * Essentially all the work here is scanning and decoding escapes
110 * in string capabilities. We don't use stdio because the editor
111 * doesn't, and because living w/o it is not hard.
112 */
113
114 static char *tbuf;
115 static int hopcount; /* detect infinite loops in termcap, init 0 */
116
117 static char *remotefile;
118
119 extern char *conffile;
120
121 int tgetent(char *, char *);
122 int getent(char *, char *, char *);
123 int tnchktc(void);
124 int tnamatch(char *);
125 static char *tskip(char *);
126 int64_t tgetnum(char *);
127 int tgetflag(char *);
128 char *tgetstr(char *, char **);
129 static char *tdecode(char *, char **);
130
131 /*
132 * Get an entry for terminal name in buffer bp,
133 * from the termcap file. Parse is very rudimentary;
134 * we just notice escaped newlines.
135 */
136 int
137 tgetent(bp, name)
138 char *bp, *name;
139 {
140 char *cp;
141
142 remotefile = cp = conffile ? conffile : _PATH_RTADVDCONF;
143 return (getent(bp, name, cp));
144 }
145
146 int
147 getent(bp, name, cp)
148 char *bp, *name, *cp;
149 {
150 int c;
151 int i = 0, cnt = 0;
152 char ibuf[BUFSIZ];
153 int tf;
154
155 tbuf = bp;
156 tf = 0;
157 /*
158 * TERMCAP can have one of two things in it. It can be the
159 * name of a file to use instead of /etc/termcap. In this
160 * case it better start with a "/". Or it can be an entry to
161 * use so we don't have to read the file. In this case it
162 * has to already have the newlines crunched out.
163 */
164 if (cp && *cp) {
165 tf = open(RM = cp, O_RDONLY);
166 }
167 if (tf < 0) {
168 infolog("<%s> open: %s", __func__, strerror(errno));
169 return (-2);
170 }
171 for (;;) {
172 cp = bp;
173 for (;;) {
174 if (i == cnt) {
175 cnt = read(tf, ibuf, BUFSIZ);
176 if (cnt <= 0) {
177 close(tf);
178 return (0);
179 }
180 i = 0;
181 }
182 c = ibuf[i++];
183 if (c == '\n') {
184 if (cp > bp && cp[-1] == '\\') {
185 cp--;
186 continue;
187 }
188 break;
189 }
190 if (cp >= bp + BUFSIZ - 1) {
191 write(STDERR_FILENO, "Remcap entry too long\n",
192 22);
193 break;
194 } else
195 *cp++ = c;
196 }
197 *cp = 0;
198
199 /*
200 * The real work for the match.
201 */
202 if (tnamatch(name)) {
203 close(tf);
204 return (tnchktc());
205 }
206 }
207 }
208
209 /*
210 * tnchktc: check the last entry, see if it's tc=xxx. If so,
211 * recursively find xxx and append that entry (minus the names)
212 * to take the place of the tc=xxx entry. This allows termcap
213 * entries to say "like an HP2621 but doesn't turn on the labels".
214 * Note that this works because of the left to right scan.
215 */
216 int
217 tnchktc()
218 {
219 char *p, *q;
220 char tcname[16]; /* name of similar terminal */
221 char tcbuf[BUFSIZ];
222 char *holdtbuf = tbuf;
223 int l;
224
225 p = tbuf + strlen(tbuf) - 2; /* before the last colon */
226 while (*--p != ':')
227 if (p < tbuf) {
228 write(STDERR_FILENO, "Bad remcap entry\n", 18);
229 return (0);
230 }
231 p++;
232 /* p now points to beginning of last field */
233 if (p[0] != 't' || p[1] != 'c')
234 return (1);
235 strlcpy(tcname, p + 3, sizeof tcname);
236 q = tcname;
237 while (*q && *q != ':')
238 q++;
239 *q = 0;
240 if (++hopcount > MAXHOP) {
241 write(STDERR_FILENO, "Infinite tc= loop\n", 18);
242 return (0);
243 }
244 if (getent(tcbuf, tcname, remotefile) != 1) {
245 return (0);
246 }
247 for (q = tcbuf; *q++ != ':'; )
248 ;
249 l = p - holdtbuf + strlen(q);
250
251 /* check length before copying string below */
252 if (l > BUFSIZ) {
253 write(STDERR_FILENO, "Remcap entry too long\n", 23);
254 q[BUFSIZ - (p-holdtbuf)] = 0;
255 }
256 strlcpy(p, q, p-tbuf);
257 tbuf = holdtbuf;
258 return (1);
259 }
260
261 /*
262 * Tnamatch deals with name matching. The first field of the termcap
263 * entry is a sequence of names separated by |'s, so we compare
264 * against each such name. The normal : terminator after the last
265 * name (before the first field) stops us.
266 */
267 int
268 tnamatch(np)
269 char *np;
270 {
271 char *Np, *Bp;
272
273 Bp = tbuf;
274 if (*Bp == '#')
275 return (0);
276 for (;;) {
277 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
278 continue;
279 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
280 return (1);
281 while (*Bp && *Bp != ':' && *Bp != '|')
282 Bp++;
283 if (*Bp == 0 || *Bp == ':')
284 return (0);
285 Bp++;
286 }
287 }
288
289 /*
290 * Skip to the next field. Notice that this is very dumb, not
291 * knowing about \: escapes or any such. If necessary, :'s can be put
292 * into the termcap file in octal.
293 */
294 static char *
295 tskip(bp)
296 char *bp;
297 {
298 int dquote;
299
300 dquote = 0;
301 while (*bp) {
302 switch (*bp) {
303 case ':':
304 if (!dquote)
305 goto breakbreak;
306 else
307 bp++;
308 break;
309 case '\\':
310 bp++;
311 if (isdigit(*bp)) {
312 while (isdigit(*bp++))
313 ;
314 } else
315 bp++;
316 case '"':
317 dquote = (dquote ? 1 : 0);
318 bp++;
319 break;
320 default:
321 bp++;
322 break;
323 }
324 }
325 breakbreak:
326 if (*bp == ':')
327 bp++;
328 return (bp);
329 }
330
331 /*
332 * Return the (numeric) option id.
333 * Numeric options look like
334 * li#80
335 * i.e. the option string is separated from the numeric value by
336 * a # character. If the option is not found we return -1.
337 * Note that we handle octal numbers beginning with 0.
338 */
339 int64_t
340 tgetnum(id)
341 char *id;
342 {
343 int64_t i;
344 int base;
345 char *bp = tbuf;
346
347 for (;;) {
348 bp = tskip(bp);
349 if (*bp == 0)
350 return (-1);
351 if (strncmp(bp, id, strlen(id)) != 0)
352 continue;
353 bp += strlen(id);
354 if (*bp == '@')
355 return (-1);
356 if (*bp != '#')
357 continue;
358 bp++;
359 base = 10;
360 if (*bp == '0')
361 base = 8;
362 i = 0;
363 while (isdigit(*bp))
364 i *= base, i += *bp++ - '0';
365 return (i);
366 }
367 }
368
369 /*
370 * Handle a flag option.
371 * Flag options are given "naked", i.e. followed by a : or the end
372 * of the buffer. Return 1 if we find the option, or 0 if it is
373 * not given.
374 */
375 int
376 tgetflag(id)
377 char *id;
378 {
379 char *bp = tbuf;
380
381 for (;;) {
382 bp = tskip(bp);
383 if (!*bp)
384 return (0);
385 if (strncmp(bp, id, strlen(id)) == 0) {
386 bp += strlen(id);
387 if (!*bp || *bp == ':')
388 return (1);
389 else if (*bp == '@')
390 return (0);
391 }
392 }
393 }
394
395 /*
396 * Get a string valued option.
397 * These are given as
398 * cl=^Z
399 * Much decoding is done on the strings, and the strings are
400 * placed in area, which is a ref parameter which is updated.
401 * No checking on area overflow.
402 */
403 char *
404 tgetstr(id, area)
405 char *id, **area;
406 {
407 char *bp = tbuf;
408
409 for (;;) {
410 bp = tskip(bp);
411 if (!*bp)
412 return (0);
413 if (strncmp(bp, id, strlen(id)) != 0)
414 continue;
415 bp += strlen(id);
416 if (*bp == '@')
417 return (0);
418 if (*bp != '=')
419 continue;
420 bp++;
421 return (tdecode(bp, area));
422 }
423 }
424
425 /*
426 * Tdecode does the grung work to decode the
427 * string capability escapes.
428 */
429 static char *
430 tdecode(str, area)
431 char *str;
432 char **area;
433 {
434 char *cp;
435 int c;
436 char *dp;
437 int i;
438 char term;
439
440 term = ':';
441 cp = *area;
442 again:
443 if (*str == '"') {
444 term = '"';
445 str++;
446 }
447 while ((c = *str++) && c != term) {
448 switch (c) {
449
450 case '^':
451 c = *str++ & 037;
452 break;
453
454 case '\\':
455 dp = "E\033^^\\\\::n\nr\rt\tb\bf\f\"\"";
456 c = *str++;
457 nextc:
458 if (*dp++ == c) {
459 c = *dp++;
460 break;
461 }
462 dp++;
463 if (*dp)
464 goto nextc;
465 if (isdigit(c)) {
466 c -= '0', i = 2;
467 do
468 c <<= 3, c |= *str++ - '0';
469 while (--i && isdigit(*str));
470 }
471 break;
472 }
473 *cp++ = c;
474 }
475 if (c == term && term != ':') {
476 term = ':';
477 goto again;
478 }
479 *cp++ = 0;
480 str = *area;
481 *area = cp;
482 return (str);
483 }