122436Sdist /*
261840Sbostic * Copyright (c) 1983, 1993
361840Sbostic * The Regents of the University of California. All rights reserved.
434203Sbostic *
542801Sbostic * %sccs.include.redist.c%
622436Sdist */
722436Sdist
813954Ssam #ifndef lint
9*69060Stef static char sccsid[] = "@(#)printcap.c 8.2 (Berkeley) 04/28/95";
1034203Sbostic #endif /* not lint */
1113954Ssam
1255470Sbostic #include <sys/param.h>
1355470Sbostic
1455470Sbostic #include <fcntl.h>
1555470Sbostic #include <dirent.h>
1655470Sbostic #include <unistd.h>
1755470Sbostic #include <stdio.h>
1846978Sbostic #include <ctype.h>
1955470Sbostic #include <string.h>
2055470Sbostic #include "lp.h"
2146978Sbostic #include "pathnames.h"
2246978Sbostic
2346300Sbostic #ifndef BUFSIZ
243841Ssam #define BUFSIZ 1024
2546300Sbostic #endif
263841Ssam #define MAXHOP 32 /* max number of tc= indirections */
273678Sroot
283678Sroot /*
29*69060Stef * getcap-style interface for the old printcap routines.
30*69060Stef *
31*69060Stef * !!!USE THIS INTERFACE ONLY IF YOU DON'T HAVE THE REAL GETCAP!!!
32*69060Stef */
33*69060Stef
34*69060Stef static char *pbp; /* pointer into pbuf for pgetstr() */
35*69060Stef static char pbuf[BUFSIZ]; /* buffer for capability strings */
36*69060Stef extern char line[]; /* buffer for printcap entries */
37*69060Stef
38*69060Stef int
cgetnext(bp,db_array)39*69060Stef cgetnext(bp, db_array)
40*69060Stef register char **bp;
41*69060Stef char **db_array;
42*69060Stef {
43*69060Stef int ret;
44*69060Stef char *strdup();
45*69060Stef
46*69060Stef pbp = pbuf;
47*69060Stef ret = getprent(line);
48*69060Stef *bp = strdup(line);
49*69060Stef return (ret);
50*69060Stef }
51*69060Stef
52*69060Stef int
cgetent(bp,db_array,name)53*69060Stef cgetent(bp, db_array, name)
54*69060Stef char **bp, **db_array, *name;
55*69060Stef {
56*69060Stef int i;
57*69060Stef
58*69060Stef *bp = line;
59*69060Stef pbp = pbuf;
60*69060Stef i = pgetent(*bp, name);
61*69060Stef if (i < 0)
62*69060Stef return (-2);
63*69060Stef else if (i == 0)
64*69060Stef return (-1);
65*69060Stef else
66*69060Stef return (0);
67*69060Stef }
68*69060Stef
69*69060Stef char *
cgetcap(buf,cap,type)70*69060Stef cgetcap(buf, cap, type)
71*69060Stef char *buf, *cap;
72*69060Stef int type;
73*69060Stef {
74*69060Stef return ((char *) pgetflag(cap));
75*69060Stef }
76*69060Stef
77*69060Stef int
cgetstr(buf,cap,str)78*69060Stef cgetstr(buf, cap, str)
79*69060Stef char *buf, *cap;
80*69060Stef char **str;
81*69060Stef {
82*69060Stef char *pgetstr __P((char *, char **));
83*69060Stef
84*69060Stef if (pbp >= pbuf+BUFSIZ) {
85*69060Stef write(2, "Capability string buffer overflow\n", 34);
86*69060Stef return (-1);
87*69060Stef }
88*69060Stef return ((*str = pgetstr(cap, &pbp)) == NULL ? -1 : 0);
89*69060Stef }
90*69060Stef
91*69060Stef int
cgetnum(buf,cap,num)92*69060Stef cgetnum(buf, cap, num)
93*69060Stef char *buf, *cap;
94*69060Stef long *num;
95*69060Stef {
96*69060Stef return ((*num = pgetnum(cap)) < 0 ? -1 : 0);
97*69060Stef }
98*69060Stef
99*69060Stef int
cgetclose()100*69060Stef cgetclose()
101*69060Stef {
102*69060Stef void endprent __P((void));
103*69060Stef
104*69060Stef endprent();
105*69060Stef return (0);
106*69060Stef }
107*69060Stef
108*69060Stef
109*69060Stef /*
1103841Ssam * termcap - routines for dealing with the terminal capability data base
1113678Sroot *
1123678Sroot * BUG: Should use a "last" pointer in tbuf, so that searching
1133678Sroot * for capabilities alphabetically would not be a n**2/2
1143678Sroot * process when large numbers of capabilities are given.
1153841Ssam * Note: If we add a last pointer now we will screw up the
1163841Ssam * tc capability. We really should compile termcap.
1173678Sroot *
1183678Sroot * Essentially all the work here is scanning and decoding escapes
1193678Sroot * in string capabilities. We don't use stdio because the editor
1203678Sroot * doesn't, and because living w/o it is not hard.
1213678Sroot */
1223678Sroot
1233841Ssam #define PRINTCAP
1243678Sroot
1253841Ssam #ifdef PRINTCAP
1263841Ssam #define tgetent pgetent
1273841Ssam #define tskip pskip
1283841Ssam #define tgetstr pgetstr
1293841Ssam #define tdecode pdecode
1303841Ssam #define tgetnum pgetnum
1313841Ssam #define tgetflag pgetflag
1323841Ssam #define tdecode pdecode
1333841Ssam #define tnchktc pnchktc
1343841Ssam #define tnamatch pnamatch
1353841Ssam #define V6
1363841Ssam #endif
1373841Ssam
13812429Sralph static FILE *pfp = NULL; /* printcap data base file pointer */
1393841Ssam static char *tbuf;
14012429Sralph static int hopcount; /* detect infinite loops in termcap, init 0 */
141*69060Stef static int tf;
1423841Ssam
143*69060Stef char *tgetstr __P((char *, char **));
14455470Sbostic static char *tskip __P((char *));
14555470Sbostic static char *tdecode __P((char *, char **));
14655470Sbostic
1473678Sroot /*
14812429Sralph * Similar to tgetent except it returns the next enrty instead of
14912429Sralph * doing a lookup.
15012429Sralph */
15155470Sbostic int
getprent(bp)15212429Sralph getprent(bp)
15312429Sralph register char *bp;
15412429Sralph {
15512429Sralph register int c, skip = 0;
15612429Sralph
15737968Sbostic if (pfp == NULL && (pfp = fopen(_PATH_PRINTCAP, "r")) == NULL)
15812429Sralph return(-1);
15912429Sralph tbuf = bp;
16012429Sralph for (;;) {
16112429Sralph switch (c = getc(pfp)) {
16212429Sralph case EOF:
16312429Sralph fclose(pfp);
16412429Sralph pfp = NULL;
16512429Sralph return(0);
16612429Sralph case '\n':
16712429Sralph if (bp == tbuf) {
16812429Sralph skip = 0;
16912429Sralph continue;
17012429Sralph }
17112429Sralph if (bp[-1] == '\\') {
17212429Sralph bp--;
17312429Sralph continue;
17412429Sralph }
17512429Sralph *bp = '\0';
17612429Sralph return(1);
17712429Sralph case '#':
17812429Sralph if (bp == tbuf)
17912429Sralph skip++;
18012429Sralph default:
18112429Sralph if (skip)
18212429Sralph continue;
18312429Sralph if (bp >= tbuf+BUFSIZ) {
18412429Sralph write(2, "Termcap entry too long\n", 23);
18512429Sralph *bp = '\0';
18612429Sralph return(1);
18712429Sralph }
18812429Sralph *bp++ = c;
18912429Sralph }
19012429Sralph }
19112429Sralph }
19212429Sralph
19355470Sbostic void
endprent()19412429Sralph endprent()
19512429Sralph {
196*69060Stef if (pfp != NULL) {
197*69060Stef /*
198*69060Stef * Can't use fclose here because on POSIX-compliant
199*69060Stef * systems, fclose() causes the file pointer of the
200*69060Stef * underlying file descriptor (which is possibly shared
201*69060Stef * with a parent process) to be adjusted, and this
202*69060Stef * reeks havoc in the parent because it doesn't know
203*69060Stef * the file pointer has changed.
204*69060Stef */
205*69060Stef (void) close(fileno(pfp));
206*69060Stef pfp = NULL;
207*69060Stef }
20812429Sralph }
20912429Sralph
21012429Sralph /*
2113841Ssam * Get an entry for terminal name in buffer bp,
2123841Ssam * from the termcap file. Parse is very rudimentary;
2133678Sroot * we just notice escaped newlines.
2143678Sroot */
21555470Sbostic int
tgetent(bp,name)2163841Ssam tgetent(bp, name)
2173678Sroot char *bp, *name;
2183678Sroot {
2193678Sroot register char *cp;
2203678Sroot register int c;
2213678Sroot register int i = 0, cnt = 0;
2223678Sroot char ibuf[BUFSIZ];
2233678Sroot
2243841Ssam tbuf = bp;
2253841Ssam #ifndef V6
2263841Ssam cp = getenv("TERMCAP");
2273841Ssam /*
2283841Ssam * TERMCAP can have one of two things in it. It can be the
2293841Ssam * name of a file to use instead of /etc/termcap. In this
2303841Ssam * case it better start with a "/". Or it can be an entry to
2313841Ssam * use so we don't have to read the file. In this case it
2323841Ssam * has to already have the newlines crunched out.
2333841Ssam */
2343841Ssam if (cp && *cp) {
2353841Ssam if (*cp!='/') {
2363841Ssam cp2 = getenv("TERM");
2373841Ssam if (cp2==(char *) 0 || strcmp(name,cp2)==0) {
2383841Ssam strcpy(bp,cp);
2393841Ssam return(tnchktc());
2403841Ssam } else {
24137968Sbostic tf = open(_PATH_PRINTCAP, 0);
2423841Ssam }
2433841Ssam } else
2443841Ssam tf = open(cp, 0);
2453841Ssam }
246*69060Stef #endif
2473841Ssam if (tf==0)
24837968Sbostic tf = open(_PATH_PRINTCAP, 0);
2493678Sroot if (tf < 0)
2503678Sroot return (-1);
2513678Sroot for (;;) {
2523678Sroot cp = bp;
2533678Sroot for (;;) {
2543678Sroot if (i == cnt) {
2553678Sroot cnt = read(tf, ibuf, BUFSIZ);
2563678Sroot if (cnt <= 0) {
2573678Sroot close(tf);
258*69060Stef tf = 0;
2593678Sroot return (0);
2603678Sroot }
2613678Sroot i = 0;
2623678Sroot }
2633678Sroot c = ibuf[i++];
2643678Sroot if (c == '\n') {
2653678Sroot if (cp > bp && cp[-1] == '\\'){
2663678Sroot cp--;
2673678Sroot continue;
2683678Sroot }
2693678Sroot break;
2703678Sroot }
2713841Ssam if (cp >= bp+BUFSIZ) {
2723841Ssam write(2,"Termcap entry too long\n", 23);
2733841Ssam break;
2743841Ssam } else
2753841Ssam *cp++ = c;
2763678Sroot }
2773678Sroot *cp = 0;
2783678Sroot
2793678Sroot /*
2803678Sroot * The real work for the match.
2813678Sroot */
2823841Ssam if (tnamatch(name)) {
283*69060Stef lseek(tf, 0L, 0);
284*69060Stef i = tnchktc();
285*69060Stef if (tf) {
286*69060Stef close(tf);
287*69060Stef tf = 0;
288*69060Stef }
289*69060Stef return(i);
2903678Sroot }
2913678Sroot }
2923678Sroot }
2933678Sroot
2943678Sroot /*
2953841Ssam * tnchktc: check the last entry, see if it's tc=xxx. If so,
2963841Ssam * recursively find xxx and append that entry (minus the names)
2973841Ssam * to take the place of the tc=xxx entry. This allows termcap
2983841Ssam * entries to say "like an HP2621 but doesn't turn on the labels".
2993841Ssam * Note that this works because of the left to right scan.
3003841Ssam */
30155470Sbostic int
tnchktc()3023841Ssam tnchktc()
3033841Ssam {
3043841Ssam register char *p, *q;
3053841Ssam char tcname[16]; /* name of similar terminal */
3063841Ssam char tcbuf[BUFSIZ];
3073841Ssam char *holdtbuf = tbuf;
3083841Ssam int l;
3093841Ssam
3103841Ssam p = tbuf + strlen(tbuf) - 2; /* before the last colon */
3113841Ssam while (*--p != ':')
3123841Ssam if (p<tbuf) {
3133841Ssam write(2, "Bad termcap entry\n", 18);
3143841Ssam return (0);
3153841Ssam }
3163841Ssam p++;
3173841Ssam /* p now points to beginning of last field */
3183841Ssam if (p[0] != 't' || p[1] != 'c')
3193841Ssam return(1);
3203841Ssam strcpy(tcname,p+3);
3213841Ssam q = tcname;
3223841Ssam while (q && *q != ':')
3233841Ssam q++;
3243841Ssam *q = 0;
3253841Ssam if (++hopcount > MAXHOP) {
3263841Ssam write(2, "Infinite tc= loop\n", 18);
3273841Ssam return (0);
3283841Ssam }
3293841Ssam if (tgetent(tcbuf, tcname) != 1)
3303841Ssam return(0);
3313841Ssam for (q=tcbuf; *q != ':'; q++)
3323841Ssam ;
3333841Ssam l = p - holdtbuf + strlen(q);
3343841Ssam if (l > BUFSIZ) {
3353841Ssam write(2, "Termcap entry too long\n", 23);
3363841Ssam q[BUFSIZ - (p-tbuf)] = 0;
3373841Ssam }
3383841Ssam strcpy(p, q+1);
3393841Ssam tbuf = holdtbuf;
3403841Ssam return(1);
3413841Ssam }
3423841Ssam
3433841Ssam /*
3443841Ssam * Tnamatch deals with name matching. The first field of the termcap
3453678Sroot * entry is a sequence of names separated by |'s, so we compare
3463678Sroot * against each such name. The normal : terminator after the last
3473678Sroot * name (before the first field) stops us.
3483678Sroot */
34955470Sbostic int
tnamatch(np)3503841Ssam tnamatch(np)
3513678Sroot char *np;
3523678Sroot {
3533678Sroot register char *Np, *Bp;
3543678Sroot
3553841Ssam Bp = tbuf;
3563841Ssam if (*Bp == '#')
3573841Ssam return(0);
3583678Sroot for (;;) {
3593678Sroot for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
3603678Sroot continue;
3613678Sroot if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
3623678Sroot return (1);
3633678Sroot while (*Bp && *Bp != ':' && *Bp != '|')
3643678Sroot Bp++;
3653678Sroot if (*Bp == 0 || *Bp == ':')
3663678Sroot return (0);
3673678Sroot Bp++;
3683678Sroot }
3693678Sroot }
3703678Sroot
3713678Sroot /*
3723678Sroot * Skip to the next field. Notice that this is very dumb, not
3733678Sroot * knowing about \: escapes or any such. If necessary, :'s can be put
3743841Ssam * into the termcap file in octal.
3753678Sroot */
3763678Sroot static char *
tskip(bp)3773841Ssam tskip(bp)
3783678Sroot register char *bp;
3793678Sroot {
3803678Sroot
3813678Sroot while (*bp && *bp != ':')
3823678Sroot bp++;
3833678Sroot if (*bp == ':')
3843678Sroot bp++;
3853678Sroot return (bp);
3863678Sroot }
3873678Sroot
3883678Sroot /*
3893678Sroot * Return the (numeric) option id.
3903678Sroot * Numeric options look like
3913678Sroot * li#80
3923678Sroot * i.e. the option string is separated from the numeric value by
3933678Sroot * a # character. If the option is not found we return -1.
3943678Sroot * Note that we handle octal numbers beginning with 0.
3953678Sroot */
39655470Sbostic int
tgetnum(id)3973841Ssam tgetnum(id)
3983678Sroot char *id;
3993678Sroot {
4003678Sroot register int i, base;
4013841Ssam register char *bp = tbuf;
4023678Sroot
4033678Sroot for (;;) {
4043841Ssam bp = tskip(bp);
4053678Sroot if (*bp == 0)
4063678Sroot return (-1);
4073678Sroot if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
4083678Sroot continue;
4093841Ssam if (*bp == '@')
4103841Ssam return(-1);
4113678Sroot if (*bp != '#')
4123678Sroot continue;
4133678Sroot bp++;
4143678Sroot base = 10;
4153678Sroot if (*bp == '0')
4163678Sroot base = 8;
4173678Sroot i = 0;
4183678Sroot while (isdigit(*bp))
4193678Sroot i *= base, i += *bp++ - '0';
4203678Sroot return (i);
4213678Sroot }
4223678Sroot }
4233678Sroot
4243678Sroot /*
4253678Sroot * Handle a flag option.
4263678Sroot * Flag options are given "naked", i.e. followed by a : or the end
4273678Sroot * of the buffer. Return 1 if we find the option, or 0 if it is
4283678Sroot * not given.
4293678Sroot */
43055470Sbostic int
tgetflag(id)4313841Ssam tgetflag(id)
4323678Sroot char *id;
4333678Sroot {
4343841Ssam register char *bp = tbuf;
4353678Sroot
4363678Sroot for (;;) {
4373841Ssam bp = tskip(bp);
4383678Sroot if (!*bp)
4393678Sroot return (0);
4403841Ssam if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
4413841Ssam if (!*bp || *bp == ':')
4423841Ssam return (1);
4433841Ssam else if (*bp == '@')
4443841Ssam return(0);
4453841Ssam }
4463678Sroot }
4473678Sroot }
4483678Sroot
4493678Sroot /*
4503678Sroot * Get a string valued option.
4513678Sroot * These are given as
4523678Sroot * cl=^Z
4533678Sroot * Much decoding is done on the strings, and the strings are
4543678Sroot * placed in area, which is a ref parameter which is updated.
4553678Sroot * No checking on area overflow.
4563678Sroot */
4573678Sroot char *
tgetstr(id,area)4583841Ssam tgetstr(id, area)
4593678Sroot char *id, **area;
4603678Sroot {
4613841Ssam register char *bp = tbuf;
4623678Sroot
4633678Sroot for (;;) {
4643841Ssam bp = tskip(bp);
4653678Sroot if (!*bp)
4663678Sroot return (0);
4673678Sroot if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
4683678Sroot continue;
4693841Ssam if (*bp == '@')
4703841Ssam return(0);
4713678Sroot if (*bp != '=')
4723678Sroot continue;
4733678Sroot bp++;
4743841Ssam return (tdecode(bp, area));
4753678Sroot }
4763678Sroot }
4773678Sroot
4783678Sroot /*
4793678Sroot * Tdecode does the grung work to decode the
4803678Sroot * string capability escapes.
4813678Sroot */
4823678Sroot static char *
tdecode(str,area)4833841Ssam tdecode(str, area)
4843678Sroot register char *str;
4853678Sroot char **area;
4863678Sroot {
4873678Sroot register char *cp;
4883678Sroot register int c;
4893678Sroot register char *dp;
4903678Sroot int i;
4913678Sroot
4923678Sroot cp = *area;
4933678Sroot while ((c = *str++) && c != ':') {
4943678Sroot switch (c) {
4953678Sroot
4963678Sroot case '^':
4973678Sroot c = *str++ & 037;
4983678Sroot break;
4993678Sroot
5003678Sroot case '\\':
5013678Sroot dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
5023678Sroot c = *str++;
5033678Sroot nextc:
5043678Sroot if (*dp++ == c) {
5053678Sroot c = *dp++;
5063678Sroot break;
5073678Sroot }
5083678Sroot dp++;
5093678Sroot if (*dp)
5103678Sroot goto nextc;
5113678Sroot if (isdigit(c)) {
5123678Sroot c -= '0', i = 2;
5133678Sroot do
5143678Sroot c <<= 3, c |= *str++ - '0';
5153678Sroot while (--i && isdigit(*str));
5163678Sroot }
5173678Sroot break;
5183678Sroot }
5193678Sroot *cp++ = c;
5203678Sroot }
5213678Sroot *cp++ = 0;
5223678Sroot str = *area;
5233678Sroot *area = cp;
5243678Sroot return (str);
5253678Sroot }
526