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