1*06198ecaSchristos /* $NetBSD: advcap.c,v 1.19 2021/03/23 18:16:53 christos Exp $ */
28c2379fdSrpaulo /* $KAME: advcap.c,v 1.11 2003/05/19 09:46:50 keiichi Exp $ */
36a12600aSitojun
4134b5f49Sitojun /*
5134b5f49Sitojun * Copyright (c) 1983 The Regents of the University of California.
6134b5f49Sitojun * All rights reserved.
7134b5f49Sitojun *
8134b5f49Sitojun * Redistribution and use in source and binary forms, with or without
9134b5f49Sitojun * modification, are permitted provided that the following conditions
10134b5f49Sitojun * are met:
11134b5f49Sitojun * 1. Redistributions of source code must retain the above copyright
12134b5f49Sitojun * notice, this list of conditions and the following disclaimer.
13134b5f49Sitojun * 2. Redistributions in binary form must reproduce the above copyright
14134b5f49Sitojun * notice, this list of conditions and the following disclaimer in the
15134b5f49Sitojun * documentation and/or other materials provided with the distribution.
16326b2259Sagc * 3. Neither the name of the University nor the names of its contributors
17134b5f49Sitojun * may be used to endorse or promote products derived from this software
18134b5f49Sitojun * without specific prior written permission.
19134b5f49Sitojun *
20134b5f49Sitojun * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21134b5f49Sitojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22134b5f49Sitojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23134b5f49Sitojun * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24134b5f49Sitojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25134b5f49Sitojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26134b5f49Sitojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27134b5f49Sitojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28134b5f49Sitojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29134b5f49Sitojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30134b5f49Sitojun * SUCH DAMAGE.
31134b5f49Sitojun */
32134b5f49Sitojun
33134b5f49Sitojun /*
34134b5f49Sitojun * remcap - routines for dealing with the remote host data base
35134b5f49Sitojun *
36134b5f49Sitojun * derived from termcap
37134b5f49Sitojun */
38134b5f49Sitojun #include <sys/types.h>
39134b5f49Sitojun #include <sys/uio.h>
40134b5f49Sitojun #include <unistd.h>
41134b5f49Sitojun #include <fcntl.h>
42134b5f49Sitojun #include <ctype.h>
43134b5f49Sitojun #include <stdlib.h>
44134b5f49Sitojun #include <stdio.h>
45134b5f49Sitojun #include <syslog.h>
46134b5f49Sitojun #include <errno.h>
47134b5f49Sitojun #include <string.h>
48134b5f49Sitojun #include "pathnames.h"
49b108fa38Sozaki-r #include "prog_ops.h"
50134b5f49Sitojun
51869494e6Schristos #include "logit.h"
52869494e6Schristos
53707ef944Sroy #ifndef __UNCONST
54707ef944Sroy #define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
55707ef944Sroy #endif
56707ef944Sroy
57134b5f49Sitojun #ifndef BUFSIZ
58134b5f49Sitojun #define BUFSIZ 1024
59134b5f49Sitojun #endif
60134b5f49Sitojun #define MAXHOP 32 /* max number of tc= indirections */
61134b5f49Sitojun
62134b5f49Sitojun #define tgetent agetent
63134b5f49Sitojun #define tnchktc anchktc
64134b5f49Sitojun #define tnamatch anamatch
65134b5f49Sitojun #define tgetnum agetnum
66134b5f49Sitojun #define tgetflag agetflag
67134b5f49Sitojun #define tgetstr agetstr
68134b5f49Sitojun
69134b5f49Sitojun #if 0
70134b5f49Sitojun #define V_TERMCAP "REMOTE"
71134b5f49Sitojun #define V_TERM "HOST"
72134b5f49Sitojun #endif
73134b5f49Sitojun
74134b5f49Sitojun char *RM;
75134b5f49Sitojun
76134b5f49Sitojun /*
77134b5f49Sitojun * termcap - routines for dealing with the terminal capability data base
78134b5f49Sitojun *
79134b5f49Sitojun * BUG: Should use a "last" pointer in tbuf, so that searching
80134b5f49Sitojun * for capabilities alphabetically would not be a n**2/2
81134b5f49Sitojun * process when large numbers of capabilities are given.
82134b5f49Sitojun * Note: If we add a last pointer now we will screw up the
83134b5f49Sitojun * tc capability. We really should compile termcap.
84134b5f49Sitojun *
85134b5f49Sitojun * Essentially all the work here is scanning and decoding escapes
86134b5f49Sitojun * in string capabilities. We don't use stdio because the editor
87134b5f49Sitojun * doesn't, and because living w/o it is not hard.
88134b5f49Sitojun */
89134b5f49Sitojun
90134b5f49Sitojun static char *tbuf;
91134b5f49Sitojun static int hopcount; /* detect infinite loops in termcap, init 0 */
92134b5f49Sitojun
93134b5f49Sitojun static char *remotefile;
94134b5f49Sitojun
95134b5f49Sitojun extern char *conffile;
96134b5f49Sitojun
97b82d32dfSroy int tgetent(char *, char *);
98b82d32dfSroy int getent(char *, char *, char *);
99b82d32dfSroy int tnchktc(void);
100b82d32dfSroy int tnamatch(char *);
101b82d32dfSroy static char *tskip(char *);
102b82d32dfSroy int64_t tgetnum(char *);
103b82d32dfSroy int tgetflag(char *);
104b82d32dfSroy char *tgetstr(char *, char **);
105b82d32dfSroy static char *tdecode(char *, char **);
106134b5f49Sitojun
107134b5f49Sitojun /*
108134b5f49Sitojun * Get an entry for terminal name in buffer bp,
109134b5f49Sitojun * from the termcap file. Parse is very rudimentary;
110134b5f49Sitojun * we just notice escaped newlines.
111134b5f49Sitojun */
112134b5f49Sitojun int
tgetent(char * bp,char * name)113b82d32dfSroy tgetent(char *bp, char *name)
114134b5f49Sitojun {
115134b5f49Sitojun char *cp;
116134b5f49Sitojun
117b82d32dfSroy remotefile = cp = conffile ? conffile : __UNCONST(_PATH_RTADVDCONF);
118134b5f49Sitojun return (getent(bp, name, cp));
119134b5f49Sitojun }
120134b5f49Sitojun
121134b5f49Sitojun int
getent(char * bp,char * name,char * cp)122b82d32dfSroy getent(char *bp, char *name, char *cp)
123134b5f49Sitojun {
124cde8ec7dSitojun int c;
125cde8ec7dSitojun int i = 0, cnt = 0;
126134b5f49Sitojun char ibuf[BUFSIZ];
127134b5f49Sitojun int tf;
128134b5f49Sitojun
129134b5f49Sitojun tbuf = bp;
130134b5f49Sitojun tf = 0;
131134b5f49Sitojun /*
132134b5f49Sitojun * TERMCAP can have one of two things in it. It can be the
133134b5f49Sitojun * name of a file to use instead of /etc/termcap. In this
134134b5f49Sitojun * case it better start with a "/". Or it can be an entry to
135134b5f49Sitojun * use so we don't have to read the file. In this case it
136134b5f49Sitojun * has to already have the newlines crunched out.
137134b5f49Sitojun */
138134b5f49Sitojun if (cp && *cp) {
139134b5f49Sitojun tf = open(RM = cp, O_RDONLY);
140134b5f49Sitojun }
141134b5f49Sitojun if (tf < 0) {
142*06198ecaSchristos logit(LOG_INFO, "%s: open `%s': %m", __func__, cp);
143134b5f49Sitojun return (-2);
144134b5f49Sitojun }
145134b5f49Sitojun for (;;) {
146134b5f49Sitojun cp = bp;
147134b5f49Sitojun for (;;) {
148134b5f49Sitojun if (i == cnt) {
149134b5f49Sitojun cnt = read(tf, ibuf, BUFSIZ);
150134b5f49Sitojun if (cnt <= 0) {
151134b5f49Sitojun close(tf);
152134b5f49Sitojun return (0);
153134b5f49Sitojun }
154134b5f49Sitojun i = 0;
155134b5f49Sitojun }
156134b5f49Sitojun c = ibuf[i++];
157134b5f49Sitojun if (c == '\n') {
158134b5f49Sitojun if (cp > bp && cp[-1] == '\\') {
159134b5f49Sitojun cp--;
160134b5f49Sitojun continue;
161134b5f49Sitojun }
162134b5f49Sitojun break;
163134b5f49Sitojun }
164134b5f49Sitojun if (cp >= bp + BUFSIZ) {
165b108fa38Sozaki-r prog_write(2,"Remcap entry too long\n", 23);
166134b5f49Sitojun break;
167134b5f49Sitojun } else
168134b5f49Sitojun *cp++ = c;
169134b5f49Sitojun }
170134b5f49Sitojun *cp = 0;
171134b5f49Sitojun
172134b5f49Sitojun /*
173134b5f49Sitojun * The real work for the match.
174134b5f49Sitojun */
175134b5f49Sitojun if (tnamatch(name)) {
176134b5f49Sitojun close(tf);
177134b5f49Sitojun return (tnchktc());
178134b5f49Sitojun }
179134b5f49Sitojun }
180134b5f49Sitojun }
181134b5f49Sitojun
182134b5f49Sitojun /*
183134b5f49Sitojun * tnchktc: check the last entry, see if it's tc=xxx. If so,
184134b5f49Sitojun * recursively find xxx and append that entry (minus the names)
185134b5f49Sitojun * to take the place of the tc=xxx entry. This allows termcap
186134b5f49Sitojun * entries to say "like an HP2621 but doesn't turn on the labels".
187134b5f49Sitojun * Note that this works because of the left to right scan.
188134b5f49Sitojun */
189134b5f49Sitojun int
tnchktc(void)190b82d32dfSroy tnchktc(void)
191134b5f49Sitojun {
192cde8ec7dSitojun char *p, *q;
193134b5f49Sitojun char tcname[16]; /* name of similar terminal */
194134b5f49Sitojun char tcbuf[BUFSIZ];
195134b5f49Sitojun char *holdtbuf = tbuf;
196134b5f49Sitojun int l;
197134b5f49Sitojun
198134b5f49Sitojun p = tbuf + strlen(tbuf) - 2; /* before the last colon */
199134b5f49Sitojun while (*--p != ':')
200134b5f49Sitojun if (p < tbuf) {
201b108fa38Sozaki-r prog_write(2, "Bad remcap entry\n", 18);
202134b5f49Sitojun return (0);
203134b5f49Sitojun }
204134b5f49Sitojun p++;
205134b5f49Sitojun /* p now points to beginning of last field */
206134b5f49Sitojun if (p[0] != 't' || p[1] != 'c')
207134b5f49Sitojun return (1);
208cde8ec7dSitojun strlcpy(tcname, p + 3, sizeof tcname);
209134b5f49Sitojun q = tcname;
210134b5f49Sitojun while (*q && *q != ':')
211134b5f49Sitojun q++;
212134b5f49Sitojun *q = 0;
213134b5f49Sitojun if (++hopcount > MAXHOP) {
214b108fa38Sozaki-r prog_write(2, "Infinite tc= loop\n", 18);
215134b5f49Sitojun return (0);
216134b5f49Sitojun }
217134b5f49Sitojun if (getent(tcbuf, tcname, remotefile) != 1) {
218134b5f49Sitojun return (0);
219134b5f49Sitojun }
220134b5f49Sitojun for (q = tcbuf; *q++ != ':'; )
221134b5f49Sitojun ;
222134b5f49Sitojun l = p - holdtbuf + strlen(q);
223134b5f49Sitojun if (l > BUFSIZ) {
224b108fa38Sozaki-r prog_write(2, "Remcap entry too long\n", 23);
225134b5f49Sitojun q[BUFSIZ - (p-holdtbuf)] = 0;
226134b5f49Sitojun }
227134b5f49Sitojun strcpy(p, q);
228134b5f49Sitojun tbuf = holdtbuf;
229134b5f49Sitojun return (1);
230134b5f49Sitojun }
231134b5f49Sitojun
232134b5f49Sitojun /*
233134b5f49Sitojun * Tnamatch deals with name matching. The first field of the termcap
234134b5f49Sitojun * entry is a sequence of names separated by |'s, so we compare
235134b5f49Sitojun * against each such name. The normal : terminator after the last
236134b5f49Sitojun * name (before the first field) stops us.
237134b5f49Sitojun */
238134b5f49Sitojun int
tnamatch(char * np)239b82d32dfSroy tnamatch(char *np)
240134b5f49Sitojun {
241cde8ec7dSitojun char *Np, *Bp;
242134b5f49Sitojun
243134b5f49Sitojun Bp = tbuf;
244134b5f49Sitojun if (*Bp == '#')
245134b5f49Sitojun return (0);
246134b5f49Sitojun for (;;) {
247134b5f49Sitojun for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
248134b5f49Sitojun continue;
249134b5f49Sitojun if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
250134b5f49Sitojun return (1);
251134b5f49Sitojun while (*Bp && *Bp != ':' && *Bp != '|')
252134b5f49Sitojun Bp++;
253134b5f49Sitojun if (*Bp == 0 || *Bp == ':')
254134b5f49Sitojun return (0);
255134b5f49Sitojun Bp++;
256134b5f49Sitojun }
257134b5f49Sitojun }
258134b5f49Sitojun
259134b5f49Sitojun /*
260134b5f49Sitojun * Skip to the next field. Notice that this is very dumb, not
261134b5f49Sitojun * knowing about \: escapes or any such. If necessary, :'s can be put
262134b5f49Sitojun * into the termcap file in octal.
263134b5f49Sitojun */
264134b5f49Sitojun static char *
tskip(char * bp)265b82d32dfSroy tskip(char *bp)
266134b5f49Sitojun {
267134b5f49Sitojun int dquote;
268134b5f49Sitojun
269134b5f49Sitojun dquote = 0;
270134b5f49Sitojun while (*bp) {
271134b5f49Sitojun switch (*bp) {
272134b5f49Sitojun case ':':
273134b5f49Sitojun if (!dquote)
274134b5f49Sitojun goto breakbreak;
275134b5f49Sitojun else
276134b5f49Sitojun bp++;
277134b5f49Sitojun break;
278134b5f49Sitojun case '\\':
279134b5f49Sitojun bp++;
280c4670c4eSdsl if (isdigit((unsigned char)*bp)) {
281c4670c4eSdsl while (isdigit((unsigned char)*bp++))
282134b5f49Sitojun ;
283134b5f49Sitojun } else
284134b5f49Sitojun bp++;
28578876854Smrg /* FALLTHROUGH */
286134b5f49Sitojun case '"':
287bcff75ccSdan dquote = (dquote ? 0 : 1);
288134b5f49Sitojun bp++;
289134b5f49Sitojun break;
290134b5f49Sitojun default:
291134b5f49Sitojun bp++;
292134b5f49Sitojun break;
293134b5f49Sitojun }
294134b5f49Sitojun }
295134b5f49Sitojun breakbreak:
296134b5f49Sitojun if (*bp == ':')
297134b5f49Sitojun bp++;
298134b5f49Sitojun return (bp);
299134b5f49Sitojun }
300134b5f49Sitojun
301134b5f49Sitojun /*
302134b5f49Sitojun * Return the (numeric) option id.
303134b5f49Sitojun * Numeric options look like
304134b5f49Sitojun * li#80
305134b5f49Sitojun * i.e. the option string is separated from the numeric value by
306134b5f49Sitojun * a # character. If the option is not found we return -1.
307134b5f49Sitojun * Note that we handle octal numbers beginning with 0.
308134b5f49Sitojun */
309cde8ec7dSitojun int64_t
tgetnum(char * id)310b82d32dfSroy tgetnum(char *id)
311134b5f49Sitojun {
312cde8ec7dSitojun int64_t i;
313cde8ec7dSitojun int base;
314cde8ec7dSitojun char *bp = tbuf;
315134b5f49Sitojun
316134b5f49Sitojun for (;;) {
317134b5f49Sitojun bp = tskip(bp);
318134b5f49Sitojun if (*bp == 0)
319134b5f49Sitojun return (-1);
320134b5f49Sitojun if (strncmp(bp, id, strlen(id)) != 0)
321134b5f49Sitojun continue;
322134b5f49Sitojun bp += strlen(id);
323134b5f49Sitojun if (*bp == '@')
324134b5f49Sitojun return (-1);
325134b5f49Sitojun if (*bp != '#')
326134b5f49Sitojun continue;
327134b5f49Sitojun bp++;
328134b5f49Sitojun base = 10;
329134b5f49Sitojun if (*bp == '0')
330134b5f49Sitojun base = 8;
331134b5f49Sitojun i = 0;
332c4670c4eSdsl while (isdigit((unsigned char)*bp))
333134b5f49Sitojun i *= base, i += *bp++ - '0';
334134b5f49Sitojun return (i);
335134b5f49Sitojun }
336134b5f49Sitojun }
337134b5f49Sitojun
338134b5f49Sitojun /*
339134b5f49Sitojun * Handle a flag option.
340134b5f49Sitojun * Flag options are given "naked", i.e. followed by a : or the end
341134b5f49Sitojun * of the buffer. Return 1 if we find the option, or 0 if it is
342134b5f49Sitojun * not given.
343134b5f49Sitojun */
344134b5f49Sitojun int
tgetflag(char * id)345b82d32dfSroy tgetflag(char *id)
346134b5f49Sitojun {
347cde8ec7dSitojun char *bp = tbuf;
348134b5f49Sitojun
349134b5f49Sitojun for (;;) {
350134b5f49Sitojun bp = tskip(bp);
351134b5f49Sitojun if (!*bp)
352134b5f49Sitojun return (0);
353134b5f49Sitojun if (strncmp(bp, id, strlen(id)) == 0) {
354134b5f49Sitojun bp += strlen(id);
355134b5f49Sitojun if (!*bp || *bp == ':')
356134b5f49Sitojun return (1);
357134b5f49Sitojun else if (*bp == '@')
358134b5f49Sitojun return (0);
359134b5f49Sitojun }
360134b5f49Sitojun }
361134b5f49Sitojun }
362134b5f49Sitojun
363134b5f49Sitojun /*
364134b5f49Sitojun * Get a string valued option.
365134b5f49Sitojun * These are given as
366134b5f49Sitojun * cl=^Z
367134b5f49Sitojun * Much decoding is done on the strings, and the strings are
368134b5f49Sitojun * placed in area, which is a ref parameter which is updated.
369134b5f49Sitojun * No checking on area overflow.
370134b5f49Sitojun */
371134b5f49Sitojun char *
tgetstr(char * id,char ** area)372b82d32dfSroy tgetstr(char *id, char **area)
373134b5f49Sitojun {
374cde8ec7dSitojun char *bp = tbuf;
375134b5f49Sitojun
376134b5f49Sitojun for (;;) {
377134b5f49Sitojun bp = tskip(bp);
378134b5f49Sitojun if (!*bp)
379134b5f49Sitojun return (0);
380134b5f49Sitojun if (strncmp(bp, id, strlen(id)) != 0)
381134b5f49Sitojun continue;
382134b5f49Sitojun bp += strlen(id);
383134b5f49Sitojun if (*bp == '@')
384134b5f49Sitojun return (0);
385134b5f49Sitojun if (*bp != '=')
386134b5f49Sitojun continue;
387134b5f49Sitojun bp++;
388134b5f49Sitojun return (tdecode(bp, area));
389134b5f49Sitojun }
390134b5f49Sitojun }
391134b5f49Sitojun
392134b5f49Sitojun /*
393134b5f49Sitojun * Tdecode does the grung work to decode the
394134b5f49Sitojun * string capability escapes.
395134b5f49Sitojun */
396134b5f49Sitojun static char *
tdecode(char * str,char ** area)397b82d32dfSroy tdecode(char *str, char **area)
398134b5f49Sitojun {
399cde8ec7dSitojun char *cp;
400cde8ec7dSitojun int c;
401b82d32dfSroy const char *dps = "E\033^^\\\\::n\nr\rt\tb\bf\f\"\"", *dp;
402134b5f49Sitojun int i;
403134b5f49Sitojun char term;
404134b5f49Sitojun
405134b5f49Sitojun term = ':';
406134b5f49Sitojun cp = *area;
407134b5f49Sitojun again:
408134b5f49Sitojun if (*str == '"') {
409134b5f49Sitojun term = '"';
410134b5f49Sitojun str++;
411134b5f49Sitojun }
412134b5f49Sitojun while ((c = *str++) && c != term) {
413134b5f49Sitojun switch (c) {
414134b5f49Sitojun
415134b5f49Sitojun case '^':
416134b5f49Sitojun c = *str++ & 037;
417134b5f49Sitojun break;
418134b5f49Sitojun
419134b5f49Sitojun case '\\':
420b82d32dfSroy dp = dps;
421134b5f49Sitojun c = *str++;
422134b5f49Sitojun nextc:
423134b5f49Sitojun if (*dp++ == c) {
424134b5f49Sitojun c = *dp++;
425134b5f49Sitojun break;
426134b5f49Sitojun }
427134b5f49Sitojun dp++;
428134b5f49Sitojun if (*dp)
429134b5f49Sitojun goto nextc;
430c4670c4eSdsl if (isdigit((unsigned char)c)) {
431134b5f49Sitojun c -= '0', i = 2;
432134b5f49Sitojun do
433134b5f49Sitojun c <<= 3, c |= *str++ - '0';
434c4670c4eSdsl while (--i && isdigit((unsigned char)*str));
435134b5f49Sitojun }
436134b5f49Sitojun break;
437134b5f49Sitojun }
438134b5f49Sitojun *cp++ = c;
439134b5f49Sitojun }
440134b5f49Sitojun if (c == term && term != ':') {
441134b5f49Sitojun term = ':';
442134b5f49Sitojun goto again;
443134b5f49Sitojun }
444134b5f49Sitojun *cp++ = 0;
445134b5f49Sitojun str = *area;
446134b5f49Sitojun *area = cp;
447134b5f49Sitojun return (str);
448134b5f49Sitojun }
449