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