advcap.c revision 1.17 1 /* $NetBSD: advcap.c,v 1.17 2017/11/06 15:15:04 christos Exp $ */
2 /* $KAME: advcap.c,v 1.11 2003/05/19 09:46:50 keiichi Exp $ */
3
4 /*
5 * Copyright (c) 1983 The Regents of the University of California.
6 * All rights reserved.
7 *
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.
19 *
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.
31 */
32
33 /*
34 * remcap - routines for dealing with the remote host data base
35 *
36 * derived from termcap
37 */
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"
49 #include "prog_ops.h"
50
51 #include "logit.h"
52
53 #ifndef __UNCONST
54 #define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
55 #endif
56
57 #ifndef BUFSIZ
58 #define BUFSIZ 1024
59 #endif
60 #define MAXHOP 32 /* max number of tc= indirections */
61
62 #define tgetent agetent
63 #define tnchktc anchktc
64 #define tnamatch anamatch
65 #define tgetnum agetnum
66 #define tgetflag agetflag
67 #define tgetstr agetstr
68
69 #if 0
70 #define V_TERMCAP "REMOTE"
71 #define V_TERM "HOST"
72 #endif
73
74 char *RM;
75
76 /*
77 * termcap - routines for dealing with the terminal capability data base
78 *
79 * BUG: Should use a "last" pointer in tbuf, so that searching
80 * for capabilities alphabetically would not be a n**2/2
81 * process when large numbers of capabilities are given.
82 * Note: If we add a last pointer now we will screw up the
83 * tc capability. We really should compile termcap.
84 *
85 * Essentially all the work here is scanning and decoding escapes
86 * in string capabilities. We don't use stdio because the editor
87 * doesn't, and because living w/o it is not hard.
88 */
89
90 static char *tbuf;
91 static int hopcount; /* detect infinite loops in termcap, init 0 */
92
93 static char *remotefile;
94
95 extern char *conffile;
96
97 int tgetent(char *, char *);
98 int getent(char *, char *, char *);
99 int tnchktc(void);
100 int tnamatch(char *);
101 static char *tskip(char *);
102 int64_t tgetnum(char *);
103 int tgetflag(char *);
104 char *tgetstr(char *, char **);
105 static char *tdecode(char *, char **);
106
107 /*
108 * Get an entry for terminal name in buffer bp,
109 * from the termcap file. Parse is very rudimentary;
110 * we just notice escaped newlines.
111 */
112 int
113 tgetent(char *bp, char *name)
114 {
115 char *cp;
116
117 remotefile = cp = conffile ? conffile : __UNCONST(_PATH_RTADVDCONF);
118 return (getent(bp, name, cp));
119 }
120
121 int
122 getent(char *bp, char *name, char *cp)
123 {
124 int c;
125 int i = 0, cnt = 0;
126 char ibuf[BUFSIZ];
127 int tf;
128
129 tbuf = bp;
130 tf = 0;
131 /*
132 * TERMCAP can have one of two things in it. It can be the
133 * name of a file to use instead of /etc/termcap. In this
134 * case it better start with a "/". Or it can be an entry to
135 * use so we don't have to read the file. In this case it
136 * has to already have the newlines crunched out.
137 */
138 if (cp && *cp) {
139 tf = open(RM = cp, O_RDONLY);
140 }
141 if (tf < 0) {
142 logit(LOG_INFO, "<%s> open: %m", __func__);
143 return (-2);
144 }
145 for (;;) {
146 cp = bp;
147 for (;;) {
148 if (i == cnt) {
149 cnt = read(tf, ibuf, BUFSIZ);
150 if (cnt <= 0) {
151 close(tf);
152 return (0);
153 }
154 i = 0;
155 }
156 c = ibuf[i++];
157 if (c == '\n') {
158 if (cp > bp && cp[-1] == '\\') {
159 cp--;
160 continue;
161 }
162 break;
163 }
164 if (cp >= bp + BUFSIZ) {
165 prog_write(2,"Remcap entry too long\n", 23);
166 break;
167 } else
168 *cp++ = c;
169 }
170 *cp = 0;
171
172 /*
173 * The real work for the match.
174 */
175 if (tnamatch(name)) {
176 close(tf);
177 return (tnchktc());
178 }
179 }
180 }
181
182 /*
183 * tnchktc: check the last entry, see if it's tc=xxx. If so,
184 * recursively find xxx and append that entry (minus the names)
185 * to take the place of the tc=xxx entry. This allows termcap
186 * entries to say "like an HP2621 but doesn't turn on the labels".
187 * Note that this works because of the left to right scan.
188 */
189 int
190 tnchktc(void)
191 {
192 char *p, *q;
193 char tcname[16]; /* name of similar terminal */
194 char tcbuf[BUFSIZ];
195 char *holdtbuf = tbuf;
196 int l;
197
198 p = tbuf + strlen(tbuf) - 2; /* before the last colon */
199 while (*--p != ':')
200 if (p < tbuf) {
201 prog_write(2, "Bad remcap entry\n", 18);
202 return (0);
203 }
204 p++;
205 /* p now points to beginning of last field */
206 if (p[0] != 't' || p[1] != 'c')
207 return (1);
208 strlcpy(tcname, p + 3, sizeof tcname);
209 q = tcname;
210 while (*q && *q != ':')
211 q++;
212 *q = 0;
213 if (++hopcount > MAXHOP) {
214 prog_write(2, "Infinite tc= loop\n", 18);
215 return (0);
216 }
217 if (getent(tcbuf, tcname, remotefile) != 1) {
218 return (0);
219 }
220 for (q = tcbuf; *q++ != ':'; )
221 ;
222 l = p - holdtbuf + strlen(q);
223 if (l > BUFSIZ) {
224 prog_write(2, "Remcap entry too long\n", 23);
225 q[BUFSIZ - (p-holdtbuf)] = 0;
226 }
227 strcpy(p, q);
228 tbuf = holdtbuf;
229 return (1);
230 }
231
232 /*
233 * Tnamatch deals with name matching. The first field of the termcap
234 * entry is a sequence of names separated by |'s, so we compare
235 * against each such name. The normal : terminator after the last
236 * name (before the first field) stops us.
237 */
238 int
239 tnamatch(char *np)
240 {
241 char *Np, *Bp;
242
243 Bp = tbuf;
244 if (*Bp == '#')
245 return (0);
246 for (;;) {
247 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
248 continue;
249 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
250 return (1);
251 while (*Bp && *Bp != ':' && *Bp != '|')
252 Bp++;
253 if (*Bp == 0 || *Bp == ':')
254 return (0);
255 Bp++;
256 }
257 }
258
259 /*
260 * Skip to the next field. Notice that this is very dumb, not
261 * knowing about \: escapes or any such. If necessary, :'s can be put
262 * into the termcap file in octal.
263 */
264 static char *
265 tskip(char *bp)
266 {
267 int dquote;
268
269 dquote = 0;
270 while (*bp) {
271 switch (*bp) {
272 case ':':
273 if (!dquote)
274 goto breakbreak;
275 else
276 bp++;
277 break;
278 case '\\':
279 bp++;
280 if (isdigit((unsigned char)*bp)) {
281 while (isdigit((unsigned char)*bp++))
282 ;
283 } else
284 bp++;
285 case '"':
286 dquote = (dquote ? 0 : 1);
287 bp++;
288 break;
289 default:
290 bp++;
291 break;
292 }
293 }
294 breakbreak:
295 if (*bp == ':')
296 bp++;
297 return (bp);
298 }
299
300 /*
301 * Return the (numeric) option id.
302 * Numeric options look like
303 * li#80
304 * i.e. the option string is separated from the numeric value by
305 * a # character. If the option is not found we return -1.
306 * Note that we handle octal numbers beginning with 0.
307 */
308 int64_t
309 tgetnum(char *id)
310 {
311 int64_t i;
312 int base;
313 char *bp = tbuf;
314
315 for (;;) {
316 bp = tskip(bp);
317 if (*bp == 0)
318 return (-1);
319 if (strncmp(bp, id, strlen(id)) != 0)
320 continue;
321 bp += strlen(id);
322 if (*bp == '@')
323 return (-1);
324 if (*bp != '#')
325 continue;
326 bp++;
327 base = 10;
328 if (*bp == '0')
329 base = 8;
330 i = 0;
331 while (isdigit((unsigned char)*bp))
332 i *= base, i += *bp++ - '0';
333 return (i);
334 }
335 }
336
337 /*
338 * Handle a flag option.
339 * Flag options are given "naked", i.e. followed by a : or the end
340 * of the buffer. Return 1 if we find the option, or 0 if it is
341 * not given.
342 */
343 int
344 tgetflag(char *id)
345 {
346 char *bp = tbuf;
347
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);
358 }
359 }
360 }
361
362 /*
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.
369 */
370 char *
371 tgetstr(char *id, char **area)
372 {
373 char *bp = tbuf;
374
375 for (;;) {
376 bp = tskip(bp);
377 if (!*bp)
378 return (0);
379 if (strncmp(bp, id, strlen(id)) != 0)
380 continue;
381 bp += strlen(id);
382 if (*bp == '@')
383 return (0);
384 if (*bp != '=')
385 continue;
386 bp++;
387 return (tdecode(bp, area));
388 }
389 }
390
391 /*
392 * Tdecode does the grung work to decode the
393 * string capability escapes.
394 */
395 static char *
396 tdecode(char *str, char **area)
397 {
398 char *cp;
399 int c;
400 const char *dps = "E\033^^\\\\::n\nr\rt\tb\bf\f\"\"", *dp;
401 int i;
402 char term;
403
404 term = ':';
405 cp = *area;
406 again:
407 if (*str == '"') {
408 term = '"';
409 str++;
410 }
411 while ((c = *str++) && c != term) {
412 switch (c) {
413
414 case '^':
415 c = *str++ & 037;
416 break;
417
418 case '\\':
419 dp = dps;
420 c = *str++;
421 nextc:
422 if (*dp++ == c) {
423 c = *dp++;
424 break;
425 }
426 dp++;
427 if (*dp)
428 goto nextc;
429 if (isdigit((unsigned char)c)) {
430 c -= '0', i = 2;
431 do
432 c <<= 3, c |= *str++ - '0';
433 while (--i && isdigit((unsigned char)*str));
434 }
435 break;
436 }
437 *cp++ = c;
438 }
439 if (c == term && term != ':') {
440 term = ':';
441 goto again;
442 }
443 *cp++ = 0;
444 str = *area;
445 *area = cp;
446 return (str);
447 }
448