2 * $Id: printcap.c,v 1.12 2009-10-14 02:24:05 didg 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 */
50 #endif /* HAVE_UNISTD_H */
51 #include <sys/types.h>
55 #endif /* HAVE_FCNTL_H */
56 #include <atalk/paths.h>
63 #define MAXHOP 32 /* max number of tc= indirections */
66 * termcap - routines for dealing with the terminal capability data base
68 * BUG: Should use a "last" pointer in tbuf, so that searching
69 * for capabilities alphabetically would not be a n**2/2
70 * process when large numbers of capabilities are given.
71 * Note: If we add a last pointer now we will screw up the
72 * tc capability. We really should compile termcap.
74 * Essentially all the work here is scanning and decoding escapes
75 * in string capabilities. We don't use stdio because the editor
76 * doesn't, and because living w/o it is not hard.
82 #define tgetent pgetent
84 #define tgetstr pgetstr
85 #define tdecode pdecode
86 #define tgetnum pgetnum
87 #define tgetflag pgetflag
88 #define tdecode pdecode
89 #define tnchktc pnchktc
90 #define tnamatch pnamatch
94 static FILE *pfp = NULL; /* printcap data base file pointer */
96 static int hopcount; /* detect infinite loops in termcap, init 0 */
99 * Similar to tgetent except it returns the next entry instead of
102 * Added a "cap" parameter, so we can use these calls for printcap
105 int getprent( char *cap, char *bp, int bufsize)
107 register int c, skip = 0, i;
109 if (pfp == NULL && (pfp = fopen( cap, "r")) == NULL)
114 switch (c = getc(pfp)) {
128 if (bp[-1] == '\\') {
140 if (bp >= tbuf+BUFSIZ) {
141 write(2, "Termcap entry too long\n", 23);
146 if (++i >= bufsize) {
147 write(2, "config file too large\n", 22);
164 * Get an entry for terminal name in buffer bp,
165 * from the termcap file. Parse is very rudimentary;
166 * we just notice escaped newlines.
168 * Added a "cap" parameter, so we can use these calls for printcap
171 int tgetent(char *cap, char *bp, char *name)
175 register int i = 0, cnt = 0;
184 cp = getenv("TERMCAP");
186 * TERMCAP can have one of two things in it. It can be the
187 * name of a file to use instead of /etc/termcap. In this
188 * case it better start with a "/". Or it can be an entry to
189 * use so we don't have to read the file. In this case it
190 * has to already have the newlines crunched out.
194 cp2 = getenv("TERM");
195 if (cp2==(char *) 0 || strcmp(name,cp2)==0) {
197 return(tnchktc(cap));
216 cnt = read(tf, ibuf, BUFSIZ);
225 if (!skip && cp > bp && cp[-1] == '\\') {
235 if (c == '#' && cp == bp)
239 if (cp >= bp+BUFSIZ) {
240 write(2,"Termcap entry too long\n", 23);
248 * The real work for the match.
250 if (tnamatch(name)) {
252 return(tnchktc(cap));
258 * tnchktc: check the last entry, see if it's tc=xxx. If so,
259 * recursively find xxx and append that entry (minus the names)
260 * to take the place of the tc=xxx entry. This allows termcap
261 * entries to say "like an HP2621 but doesn't turn on the labels".
262 * Note that this works because of the left to right scan.
264 * Added a "cap" parameter, so we can use these calls for printcap
267 int tnchktc( char *cap)
269 register char *p, *q;
270 char tcname[16]; /* name of similar terminal */
272 char *holdtbuf = tbuf;
275 p = tbuf + strlen(tbuf) - 2; /* before the last colon */
278 write(2, "Bad termcap entry\n", 18);
282 /* p now points to beginning of last field */
283 if (p[0] != 't' || p[1] != 'c')
287 while (q && *q != ':')
290 if (++hopcount > MAXHOP) {
291 write(2, "Infinite tc= loop\n", 18);
294 if (tgetent( cap, tcbuf, tcname) != 1)
296 for (q=tcbuf; *q != ':'; q++)
298 l = p - holdtbuf + strlen(q);
300 write(2, "Termcap entry too long\n", 23);
301 q[BUFSIZ - (p-tbuf)] = 0;
309 * Tnamatch deals with name matching. The first field of the termcap
310 * entry is a sequence of names separated by |'s, so we compare
311 * against each such name. The normal : terminator after the last
312 * name (before the first field) stops us.
314 int tnamatch(char *np)
316 register char *Np, *Bp;
322 for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
324 if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
326 while (*Bp && *Bp != ':' && *Bp != '|')
328 if (*Bp == 0 || *Bp == ':')
335 * Skip to the next field. Notice that this is very dumb, not
336 * knowing about \: escapes or any such. If necessary, :'s can be put
337 * into the termcap file in octal.
339 static char *tskip(char *bp)
342 while (*bp && *bp != ':')
344 while (*bp && *bp == ':')
350 * Return the (numeric) option id.
351 * Numeric options look like
353 * i.e. the option string is separated from the numeric value by
354 * a # character. If the option is not found we return -1.
355 * Note that we handle octal numbers beginning with 0.
357 int tgetnum(char *id)
359 register int i, base;
360 register char *bp = tbuf;
366 if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
378 i *= base, i += *bp++ - '0';
384 * Handle a flag option.
385 * Flag options are given "naked", i.e. followed by a : or the end
386 * of the buffer. Return 1 if we find the option, or 0 if it is
389 int tgetflag(char *id)
391 register char *bp = tbuf;
397 if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
398 if (!*bp || *bp == ':')
407 * Tdecode does the grung work to decode the
408 * string capability escapes.
411 tdecode(char *str, char **area)
419 while ((c = *str++) && c != ':') {
427 dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
440 c <<= 3, c |= *str++ - '0';
441 while (--i && isdigit(*str));
454 * Get a string valued option.
457 * Much decoding is done on the strings, and the strings are
458 * placed in area, which is a ref parameter which is updated.
459 * No checking on area overflow.
462 tgetstr(char *id, char **area)
464 register char *bp = tbuf;
470 if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
477 return (tdecode(bp, area));
482 decodename(char *str, char **area, int bufsize)
490 while ((c = *str++) && --bufsize && c != ':' && c != '|' ) {
498 dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
511 c <<= 3, c |= *str++ - '0';
512 while (--i && isdigit(*str));
525 getpname(char **area, int bufsize)
527 return( decodename( tbuf, area, bufsize));