]>
git.cameronkatri.com Git - apple_cmds.git/blob - network_cmds/rtadvd.tproj/advcap.c
2 * Copyright (c) 2009-2011 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.11 2003/05/19 09:46:50 keiichi 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
65 * remcap - routines for dealing with the remote host data base
67 * derived from termcap
69 #include <sys/types.h>
78 #include "pathnames.h"
79 #include "rtadvd_logging.h"
84 #define MAXHOP 32 /* max number of tc= indirections */
86 #define tgetent agetent
87 #define tnchktc anchktc
88 #define tnamatch anamatch
89 #define tgetnum agetnum
90 #define tgetflag agetflag
91 #define tgetstr agetstr
94 #define V_TERMCAP "REMOTE"
101 * termcap - routines for dealing with the terminal capability data base
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.
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.
115 static int hopcount
; /* detect infinite loops in termcap, init 0 */
117 static char *remotefile
;
119 extern char *conffile
;
121 int tgetent(char *, char *);
122 int getent(char *, char *, char *);
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 **);
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.
142 remotefile
= cp
= conffile
? conffile
: _PATH_RTADVDCONF
;
143 return (getent(bp
, name
, cp
));
148 char *bp
, *name
, *cp
;
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.
165 tf
= open(RM
= cp
, O_RDONLY
);
168 infolog("<%s> open: %s", __func__
, strerror(errno
));
175 cnt
= read(tf
, ibuf
, BUFSIZ
);
184 if (cp
> bp
&& cp
[-1] == '\\') {
190 if (cp
>= bp
+ BUFSIZ
- 1) {
191 write(STDERR_FILENO
, "Remcap entry too long\n",
200 * The real work for the match.
202 if (tnamatch(name
)) {
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.
220 char tcname
[16]; /* name of similar terminal */
222 char *holdtbuf
= tbuf
;
225 p
= tbuf
+ strlen(tbuf
) - 2; /* before the last colon */
228 write(STDERR_FILENO
, "Bad remcap entry\n", 18);
232 /* p now points to beginning of last field */
233 if (p
[0] != 't' || p
[1] != 'c')
235 strlcpy(tcname
, p
+ 3, sizeof tcname
);
237 while (*q
&& *q
!= ':')
240 if (++hopcount
> MAXHOP
) {
241 write(STDERR_FILENO
, "Infinite tc= loop\n", 18);
244 if (getent(tcbuf
, tcname
, remotefile
) != 1) {
247 for (q
= tcbuf
; *q
++ != ':'; )
249 l
= p
- holdtbuf
+ strlen(q
);
251 /* check length before copying string below */
253 write(STDERR_FILENO
, "Remcap entry too long\n", 23);
254 q
[BUFSIZ
- (p
-holdtbuf
)] = 0;
256 strlcpy(p
, q
, p
-tbuf
);
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.
277 for (Np
= np
; *Np
&& *Bp
== *Np
; Bp
++, Np
++)
279 if (*Np
== 0 && (*Bp
== '|' || *Bp
== ':' || *Bp
== 0))
281 while (*Bp
&& *Bp
!= ':' && *Bp
!= '|')
283 if (*Bp
== 0 || *Bp
== ':')
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.
312 while (isdigit(*bp
++))
317 dquote
= (dquote
? 1 : 0);
332 * Return the (numeric) option id.
333 * Numeric options look like
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.
351 if (strncmp(bp
, id
, strlen(id
)) != 0)
364 i
*= base
, i
+= *bp
++ - '0';
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
385 if (strncmp(bp
, id
, strlen(id
)) == 0) {
387 if (!*bp
|| *bp
== ':')
396 * Get a string valued option.
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.
413 if (strncmp(bp
, id
, strlen(id
)) != 0)
421 return (tdecode(bp
, area
));
426 * Tdecode does the grung work to decode the
427 * string capability escapes.
447 while ((c
= *str
++) && c
!= term
) {
455 dp
= "E\033^^\\\\::n\nr\rt\tb\bf\f\"\"";
468 c
<<= 3, c
|= *str
++ - '0';
469 while (--i
&& isdigit(*str
));
475 if (c
== term
&& term
!= ':') {