2 * Copyright (c) 1990,1994 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
7 * Copyright (c) 1983 Regents of the University of California.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 static char sccsid[] = "@(#)printcap.c 5.7 (Berkeley) 3/4/91";
50 #include <atalk/paths.h>
55 #define MAXHOP 32 /* max number of tc= indirections */
58 * termcap - routines for dealing with the terminal capability data base
60 * BUG: Should use a "last" pointer in tbuf, so that searching
61 * for capabilities alphabetically would not be a n**2/2
62 * process when large numbers of capabilities are given.
63 * Note: If we add a last pointer now we will screw up the
64 * tc capability. We really should compile termcap.
66 * Essentially all the work here is scanning and decoding escapes
67 * in string capabilities. We don't use stdio because the editor
68 * doesn't, and because living w/o it is not hard.
74 #define tgetent pgetent
76 #define tgetstr pgetstr
77 #define tdecode pdecode
78 #define tgetnum pgetnum
79 #define tgetflag pgetflag
80 #define tdecode pdecode
81 #define tnchktc pnchktc
82 #define tnamatch pnamatch
86 static FILE *pfp = NULL; /* printcap data base file pointer */
88 static int hopcount; /* detect infinite loops in termcap, init 0 */
91 static char *tdecode();
95 * Similar to tgetent except it returns the next entry instead of
98 * Added a "cap" parameter, so we can use these calls for printcap
105 register int c, skip = 0;
107 if (pfp == NULL && (pfp = fopen( cap, "r")) == NULL)
111 switch (c = getc(pfp)) {
121 if (bp[-1] == '\\') {
133 if (bp >= tbuf+BUFSIZ) {
134 write(2, "Termcap entry too long\n", 23);
150 * Get an entry for terminal name in buffer bp,
151 * from the termcap file. Parse is very rudimentary;
152 * we just notice escaped newlines.
154 * Added a "cap" parameter, so we can use these calls for printcap
157 tgetent( cap, bp, name)
158 char *cap, *bp, *name;
162 register int i = 0, cnt = 0;
170 cp = getenv("TERMCAP");
172 * TERMCAP can have one of two things in it. It can be the
173 * name of a file to use instead of /etc/termcap. In this
174 * case it better start with a "/". Or it can be an entry to
175 * use so we don't have to read the file. In this case it
176 * has to already have the newlines crunched out.
180 cp2 = getenv("TERM");
181 if (cp2==(char *) 0 || strcmp(name,cp2)==0) {
201 cnt = read(tf, ibuf, BUFSIZ);
210 if (cp > bp && cp[-1] == '\\'){
216 if (cp >= bp+BUFSIZ) {
217 write(2,"Termcap entry too long\n", 23);
225 * The real work for the match.
227 if (tnamatch(name)) {
235 * tnchktc: check the last entry, see if it's tc=xxx. If so,
236 * recursively find xxx and append that entry (minus the names)
237 * to take the place of the tc=xxx entry. This allows termcap
238 * entries to say "like an HP2621 but doesn't turn on the labels".
239 * Note that this works because of the left to right scan.
241 * Added a "cap" parameter, so we can use these calls for printcap
247 register char *p, *q;
248 char tcname[16]; /* name of similar terminal */
250 char *holdtbuf = tbuf;
253 p = tbuf + strlen(tbuf) - 2; /* before the last colon */
256 write(2, "Bad termcap entry\n", 18);
260 /* p now points to beginning of last field */
261 if (p[0] != 't' || p[1] != 'c')
265 while (q && *q != ':')
268 if (++hopcount > MAXHOP) {
269 write(2, "Infinite tc= loop\n", 18);
272 if (tgetent( cap, tcbuf, tcname) != 1)
274 for (q=tcbuf; *q != ':'; q++)
276 l = p - holdtbuf + strlen(q);
278 write(2, "Termcap entry too long\n", 23);
279 q[BUFSIZ - (p-tbuf)] = 0;
287 * Tnamatch deals with name matching. The first field of the termcap
288 * entry is a sequence of names separated by |'s, so we compare
289 * against each such name. The normal : terminator after the last
290 * name (before the first field) stops us.
295 register char *Np, *Bp;
301 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
303 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
305 while (*Bp && *Bp != ':' && *Bp != '|')
307 if (*Bp == 0 || *Bp == ':')
314 * Skip to the next field. Notice that this is very dumb, not
315 * knowing about \: escapes or any such. If necessary, :'s can be put
316 * into the termcap file in octal.
323 while (*bp && *bp != ':')
331 * Return the (numeric) option id.
332 * Numeric options look like
334 * i.e. the option string is separated from the numeric value by
335 * a # character. If the option is not found we return -1.
336 * Note that we handle octal numbers beginning with 0.
341 register int i, base;
342 register char *bp = tbuf;
348 if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
360 i *= base, i += *bp++ - '0';
366 * Handle a flag option.
367 * Flag options are given "naked", i.e. followed by a : or the end
368 * of the buffer. Return 1 if we find the option, or 0 if it is
374 register char *bp = tbuf;
380 if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
381 if (!*bp || *bp == ':')
390 * Get a string valued option.
393 * Much decoding is done on the strings, and the strings are
394 * placed in area, which is a ref parameter which is updated.
395 * No checking on area overflow.
401 register char *bp = tbuf;
407 if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
414 return (tdecode(bp, area));
419 * Tdecode does the grung work to decode the
420 * string capability escapes.
433 while ((c = *str++) && c != ':') {
441 dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
454 c <<= 3, c |= *str++ - '0';
455 while (--i && isdigit(*str));
468 decodename(str, area)
478 while ((c = *str++) && c != ':' && c != '|' ) {
486 dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
499 c <<= 3, c |= *str++ - '0';
500 while (--i && isdigit(*str));
516 return( decodename( tbuf, area ));