2 * $Id: printcap.c,v 1.4 2001-06-25 20:13:45 rufustfirefly Exp $
4 * Copyright (c) 1990,1994 Regents of The University of Michigan.
5 * 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
41 #endif /* HAVE_CONFIG_H */
44 static char sccsid[] = "@(#)printcap.c 5.7 (Berkeley) 3/4/91";
52 #endif /* HAVE_UNISTD_H */
53 #include <sys/types.h>
57 #endif /* HAVE_FCNTL_H */
58 #include <atalk/paths.h>
65 #define MAXHOP 32 /* max number of tc= indirections */
68 * termcap - routines for dealing with the terminal capability data base
70 * BUG: Should use a "last" pointer in tbuf, so that searching
71 * for capabilities alphabetically would not be a n**2/2
72 * process when large numbers of capabilities are given.
73 * Note: If we add a last pointer now we will screw up the
74 * tc capability. We really should compile termcap.
76 * Essentially all the work here is scanning and decoding escapes
77 * in string capabilities. We don't use stdio because the editor
78 * doesn't, and because living w/o it is not hard.
84 #define tgetent pgetent
86 #define tgetstr pgetstr
87 #define tdecode pdecode
88 #define tgetnum pgetnum
89 #define tgetflag pgetflag
90 #define tdecode pdecode
91 #define tnchktc pnchktc
92 #define tnamatch pnamatch
96 static FILE *pfp = NULL; /* printcap data base file pointer */
98 static int hopcount; /* detect infinite loops in termcap, init 0 */
101 static char *tdecode();
105 * Similar to tgetent except it returns the next entry instead of
108 * Added a "cap" parameter, so we can use these calls for printcap
111 int getprent( cap, bp)
115 register int c, skip = 0;
117 if (pfp == NULL && (pfp = fopen( cap, "r")) == NULL)
121 switch (c = getc(pfp)) {
131 if (bp[-1] == '\\') {
143 if (bp >= tbuf+BUFSIZ) {
144 write(2, "Termcap entry too long\n", 23);
160 * Get an entry for terminal name in buffer bp,
161 * from the termcap file. Parse is very rudimentary;
162 * we just notice escaped newlines.
164 * Added a "cap" parameter, so we can use these calls for printcap
167 int tgetent( cap, bp, name)
168 char *cap, *bp, *name;
172 register int i = 0, cnt = 0;
180 cp = getenv("TERMCAP");
182 * TERMCAP can have one of two things in it. It can be the
183 * name of a file to use instead of /etc/termcap. In this
184 * case it better start with a "/". Or it can be an entry to
185 * use so we don't have to read the file. In this case it
186 * has to already have the newlines crunched out.
190 cp2 = getenv("TERM");
191 if (cp2==(char *) 0 || strcmp(name,cp2)==0) {
211 cnt = read(tf, ibuf, BUFSIZ);
220 if (cp > bp && cp[-1] == '\\'){
226 if (cp >= bp+BUFSIZ) {
227 write(2,"Termcap entry too long\n", 23);
235 * The real work for the match.
237 if (tnamatch(name)) {
239 return(tnchktc(cap));
245 * tnchktc: check the last entry, see if it's tc=xxx. If so,
246 * recursively find xxx and append that entry (minus the names)
247 * to take the place of the tc=xxx entry. This allows termcap
248 * entries to say "like an HP2621 but doesn't turn on the labels".
249 * Note that this works because of the left to right scan.
251 * Added a "cap" parameter, so we can use these calls for printcap
257 register char *p, *q;
258 char tcname[16]; /* name of similar terminal */
260 char *holdtbuf = tbuf;
263 p = tbuf + strlen(tbuf) - 2; /* before the last colon */
266 write(2, "Bad termcap entry\n", 18);
270 /* p now points to beginning of last field */
271 if (p[0] != 't' || p[1] != 'c')
275 while (q && *q != ':')
278 if (++hopcount > MAXHOP) {
279 write(2, "Infinite tc= loop\n", 18);
282 if (tgetent( cap, tcbuf, tcname) != 1)
284 for (q=tcbuf; *q != ':'; q++)
286 l = p - holdtbuf + strlen(q);
288 write(2, "Termcap entry too long\n", 23);
289 q[BUFSIZ - (p-tbuf)] = 0;
297 * Tnamatch deals with name matching. The first field of the termcap
298 * entry is a sequence of names separated by |'s, so we compare
299 * against each such name. The normal : terminator after the last
300 * name (before the first field) stops us.
305 register char *Np, *Bp;
311 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
313 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
315 while (*Bp && *Bp != ':' && *Bp != '|')
317 if (*Bp == 0 || *Bp == ':')
324 * Skip to the next field. Notice that this is very dumb, not
325 * knowing about \: escapes or any such. If necessary, :'s can be put
326 * into the termcap file in octal.
333 while (*bp && *bp != ':')
341 * Return the (numeric) option id.
342 * Numeric options look like
344 * i.e. the option string is separated from the numeric value by
345 * a # character. If the option is not found we return -1.
346 * Note that we handle octal numbers beginning with 0.
351 register int i, base;
352 register char *bp = tbuf;
358 if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
370 i *= base, i += *bp++ - '0';
376 * Handle a flag option.
377 * Flag options are given "naked", i.e. followed by a : or the end
378 * of the buffer. Return 1 if we find the option, or 0 if it is
384 register char *bp = tbuf;
390 if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
391 if (!*bp || *bp == ':')
400 * Get a string valued option.
403 * Much decoding is done on the strings, and the strings are
404 * placed in area, which is a ref parameter which is updated.
405 * No checking on area overflow.
411 register char *bp = tbuf;
417 if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
424 return (tdecode(bp, area));
429 * Tdecode does the grung work to decode the
430 * string capability escapes.
443 while ((c = *str++) && c != ':') {
451 dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
464 c <<= 3, c |= *str++ - '0';
465 while (--i && isdigit(*str));
478 decodename(str, area)
488 while ((c = *str++) && c != ':' && c != '|' ) {
496 dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
509 c <<= 3, c |= *str++ - '0';
510 while (--i && isdigit(*str));
526 return( decodename( tbuf, area ));