]>
git.saurik.com Git - apple/network_cmds.git/blob - rtadvd.tproj/advcap.c
2 * Copyright (c) 2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* $KAME: advcap.c,v 1.5 2001/02/01 09:12:08 jinmei Exp $ */
32 * Copyright (c) 1983 The Regents of the University of California.
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
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.
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
63 * $FreeBSD: src/usr.sbin/rtadvd/advcap.c,v 1.1.2.2 2001/07/03 11:02:13 ume Exp $
67 * remcap - routines for dealing with the remote host data base
69 * derived from termcap
71 #include <sys/types.h>
81 #include "pathnames.h"
86 #define MAXHOP 32 /* max number of tc= indirections */
88 #define tgetent agetent
89 #define tnchktc anchktc
90 #define tnamatch anamatch
91 #define tgetnum agetnum
92 #define tgetflag agetflag
93 #define tgetstr agetstr
96 #define V_TERMCAP "REMOTE"
103 * termcap - routines for dealing with the terminal capability data base
105 * BUG: Should use a "last" pointer in tbuf, so that searching
106 * for capabilities alphabetically would not be a n**2/2
107 * process when large numbers of capabilities are given.
108 * Note: If we add a last pointer now we will screw up the
109 * tc capability. We really should compile termcap.
111 * Essentially all the work here is scanning and decoding escapes
112 * in string capabilities. We don't use stdio because the editor
113 * doesn't, and because living w/o it is not hard.
117 static int hopcount
; /* detect infinite loops in termcap, init 0 */
119 static char *remotefile
;
121 extern char *conffile
;
123 int tgetent
__P((char *, char *));
124 int getent
__P((char *, char *, char *));
125 int tnchktc
__P((void));
126 int tnamatch
__P((char *));
127 static char *tskip
__P((char *));
128 long long tgetnum
__P((char *));
129 int tgetflag
__P((char *));
130 char *tgetstr
__P((char *, char **));
131 static char *tdecode
__P((char *, char **));
134 * Get an entry for terminal name in buffer bp,
135 * from the termcap file. Parse is very rudimentary;
136 * we just notice escaped newlines.
144 remotefile
= cp
= conffile
? conffile
: _PATH_RTADVDCONF
;
145 return (getent(bp
, name
, cp
));
150 char *bp
, *name
, *cp
;
153 register int i
= 0, cnt
= 0;
160 * TERMCAP can have one of two things in it. It can be the
161 * name of a file to use instead of /etc/termcap. In this
162 * case it better start with a "/". Or it can be an entry to
163 * use so we don't have to read the file. In this case it
164 * has to already have the newlines crunched out.
167 tf
= open(RM
= cp
, O_RDONLY
);
171 "<%s> open: %s", __FUNCTION__
, strerror(errno
));
178 cnt
= read(tf
, ibuf
, BUFSIZ
);
187 if (cp
> bp
&& cp
[-1] == '\\') {
193 if (cp
>= bp
+BUFSIZ
) {
194 write(2,"Remcap entry too long\n", 23);
202 * The real work for the match.
204 if (tnamatch(name
)) {
212 * tnchktc: check the last entry, see if it's tc=xxx. If so,
213 * recursively find xxx and append that entry (minus the names)
214 * to take the place of the tc=xxx entry. This allows termcap
215 * entries to say "like an HP2621 but doesn't turn on the labels".
216 * Note that this works because of the left to right scan.
221 register char *p
, *q
;
222 char tcname
[16]; /* name of similar terminal */
224 char *holdtbuf
= tbuf
;
227 p
= tbuf
+ strlen(tbuf
) - 2; /* before the last colon */
230 write(2, "Bad remcap entry\n", 18);
234 /* p now points to beginning of last field */
235 if (p
[0] != 't' || p
[1] != 'c')
237 strlcpy(tcname
, p
+3, sizeof(tcname
));
239 while (*q
&& *q
!= ':')
242 if (++hopcount
> MAXHOP
) {
243 write(2, "Infinite tc= loop\n", 18);
246 if (getent(tcbuf
, tcname
, remotefile
) != 1) {
249 for (q
= tcbuf
; *q
++ != ':'; )
251 l
= p
- holdtbuf
+ strlen(q
);
253 /* check length before copying string below */
255 write(2, "Remcap entry too long\n", 23);
256 q
[BUFSIZ
- (p
-holdtbuf
)] = 0;
258 strlcpy(p
, q
, p
-tbuf
);
264 * Tnamatch deals with name matching. The first field of the termcap
265 * entry is a sequence of names separated by |'s, so we compare
266 * against each such name. The normal : terminator after the last
267 * name (before the first field) stops us.
273 register char *Np
, *Bp
;
279 for (Np
= np
; *Np
&& *Bp
== *Np
; Bp
++, Np
++)
281 if (*Np
== 0 && (*Bp
== '|' || *Bp
== ':' || *Bp
== 0))
283 while (*Bp
&& *Bp
!= ':' && *Bp
!= '|')
285 if (*Bp
== 0 || *Bp
== ':')
292 * Skip to the next field. Notice that this is very dumb, not
293 * knowing about \: escapes or any such. If necessary, :'s can be put
294 * into the termcap file in octal.
314 while (isdigit(*bp
++))
319 dquote
= (dquote
? 1 : 0);
334 * Return the (numeric) option id.
335 * Numeric options look like
337 * i.e. the option string is separated from the numeric value by
338 * a # character. If the option is not found we return -1.
339 * Note that we handle octal numbers beginning with 0.
345 register long long i
;
347 register char *bp
= tbuf
;
353 if (strncmp(bp
, id
, strlen(id
)) != 0)
366 i
*= base
, i
+= *bp
++ - '0';
372 * Handle a flag option.
373 * Flag options are given "naked", i.e. followed by a : or the end
374 * of the buffer. Return 1 if we find the option, or 0 if it is
381 register char *bp
= tbuf
;
387 if (strncmp(bp
, id
, strlen(id
)) == 0) {
389 if (!*bp
|| *bp
== ':')
398 * Get a string valued option.
401 * Much decoding is done on the strings, and the strings are
402 * placed in area, which is a ref parameter which is updated.
403 * No checking on area overflow.
409 register char *bp
= tbuf
;
415 if (strncmp(bp
, id
, strlen(id
)) != 0)
423 return (tdecode(bp
, area
));
428 * Tdecode does the grung work to decode the
429 * string capability escapes.
449 while ((c
= *str
++) && c
!= term
) {
457 dp
= "E\033^^\\\\::n\nr\rt\tb\bf\f\"\"";
470 c
<<= 3, c
|= *str
++ - '0';
471 while (--i
&& isdigit(*str
));
477 if (c
== term
&& term
!= ':') {