- if (cp && *cp && *cp != '/') {
- tbuf = cp;
- c = tnamatch(name);
- tbuf = bp;
- if (c) {
- strcpy(bp,cp);
- return (tnchktc());
- }
- }
- return (tfindent(bp, name)); /* find terminal entry in path */
-}
-
-/*
- * tfindent - reads through the list of files in pathvec as if they were one
- * continuous file searching for terminal entries along the way. It will
- * participate in indirect recursion if the call to tnchktc() finds a tc=
- * field, which is only searched for in the current file and files ocurring
- * after it in pathvec. The usable part of this vector is kept in the global
- * variable pvec. Terminal entries may not be broken across files. Parse is
- * very rudimentary; we just notice escaped newlines.
- */
-tfindent(bp, name)
- char *bp, *name;
-{
- register char *cp;
- register int c;
- register int i, cnt;
- char ibuf[BUFSIZ];
- int opencnt = 0;
- int tf;
-
- tbuf = bp;
-nextfile:
- i = cnt = 0;
- while (*pvec && (tf = open(*pvec, 0)) < 0)
- pvec++;
- if (!*pvec)
- return (opencnt ? 0 : -1);
- opencnt++;
- for (;;) {
- cp = bp;
- for (;;) {
- if (i == cnt) {
- cnt = read(tf, ibuf, BUFSIZ);
- if (cnt <= 0) {
- close(tf);
- pvec++;
- goto nextfile;
- }
- i = 0;
- }
- c = ibuf[i++];
- if (c == '\n') {
- if (cp > bp && cp[-1] == '\\'){
- cp--;
- continue;
- }
- break;
- }
- if (cp >= bp+BUFSIZ) {
- write(2,"Termcap entry too long\n", 23);
- break;
- } else
- *cp++ = c;
- }
- *cp = 0;
-
- /*
- * The real work for the match.
- */
- if (tnamatch(name)) {
- close(tf);
- return(tnchktc());
- }
- }
-}
-
-/*
- * tnchktc: check the last entry, see if it's tc=xxx. If so,
- * recursively find xxx and append that entry (minus the names)
- * to take the place of the tc=xxx entry. This allows termcap
- * entries to say "like an HP2621 but doesn't turn on the labels".
- * Note that this works because of the left to right scan.
- */
-tnchktc()
-{
- register char *p, *q;
- char tcname[16]; /* name of similar terminal */
- char tcbuf[BUFSIZ];
- char *holdtbuf = tbuf;
- int l;
-
- p = tbuf + strlen(tbuf) - 2; /* before the last colon */
- while (*--p != ':')
- if (p<tbuf) {
- write(2, "Bad termcap entry\n", 18);
- return (0);
- }
- p++;
- /* p now points to beginning of last field */
- if (p[0] != 't' || p[1] != 'c')
- return(1);
- strcpy(tcname,p+3);
- q = tcname;
- while (*q && *q != ':')
- q++;
- *q = 0;
- if (++hopcount > MAXHOP) {
- write(2, "Infinite tc= loop\n", 18);
- return (0);
- }
- if (tfindent(tcbuf, tcname) != 1) {
- hopcount = 0; /* unwind recursion */
- return(0);
- }
- for (q=tcbuf; *q != ':'; q++)
- ;
- l = p - holdtbuf + strlen(q);
- if (l > BUFSIZ) {
- write(2, "Termcap entry too long\n", 23);
- q[BUFSIZ - (p-tbuf)] = 0;
- }
- strcpy(p, q+1);
- tbuf = holdtbuf;
- hopcount = 0; /* unwind recursion */
- return(1);
-}
-
-/*
- * Tnamatch deals with name matching. The first field of the termcap
- * entry is a sequence of names separated by |'s, so we compare
- * against each such name. The normal : terminator after the last
- * name (before the first field) stops us.
- */
-tnamatch(np)
- char *np;
-{
- register char *Np, *Bp;
-
- Bp = tbuf;
- if (*Bp == '#')
- return(0);
- for (;;) {
- for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
- continue;
- if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
- return (1);
- while (*Bp && *Bp != ':' && *Bp != '|')
- Bp++;
- if (*Bp == 0 || *Bp == ':')
- return (0);
- Bp++;
- }
-}
-
-/*
- * Skip to the next field. Notice that this is very dumb, not
- * knowing about \: escapes or any such. If necessary, :'s can be put
- * into the termcap file in octal.
- */
-static char *
-tskip(bp)
- register char *bp;
-{
-
- while (*bp && *bp != ':')
- bp++;
- if (*bp == ':')
- bp++;
- return (bp);