| 1 | /* |
| 2 | * Copyright (c) 1980 The Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. All advertising materials mentioning features or use of this software |
| 14 | * must display the following acknowledgement: |
| 15 | * This product includes software developed by the University of |
| 16 | * California, Berkeley and its contributors. |
| 17 | * 4. Neither the name of the University nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #ifndef lint |
| 35 | static char sccsid[] = "@(#)termcap.c 5.5 (Berkeley) 6/1/90"; |
| 36 | #endif /* not lint */ |
| 37 | |
| 38 | #define BUFSIZ 1024 |
| 39 | #define MAXHOP 32 /* max number of tc= indirections */ |
| 40 | #define PBUFSIZ 512 /* max length of filename path */ |
| 41 | #define PVECSIZ 32 /* max number of names in path */ |
| 42 | |
| 43 | #include <ctype.h> |
| 44 | #include "pathnames.h" |
| 45 | |
| 46 | /* |
| 47 | * termcap - routines for dealing with the terminal capability data base |
| 48 | * |
| 49 | * BUG: Should use a "last" pointer in tbuf, so that searching |
| 50 | * for capabilities alphabetically would not be a n**2/2 |
| 51 | * process when large numbers of capabilities are given. |
| 52 | * Note: If we add a last pointer now we will screw up the |
| 53 | * tc capability. We really should compile termcap. |
| 54 | * |
| 55 | * Essentially all the work here is scanning and decoding escapes |
| 56 | * in string capabilities. We don't use stdio because the editor |
| 57 | * doesn't, and because living w/o it is not hard. |
| 58 | */ |
| 59 | |
| 60 | static char *tbuf; |
| 61 | static int hopcount; /* detect infinite loops in termcap, init 0 */ |
| 62 | static char pathbuf[PBUFSIZ]; /* holds raw path of filenames */ |
| 63 | static char *pathvec[PVECSIZ]; /* to point to names in pathbuf */ |
| 64 | static char **pvec; /* holds usable tail of path vector */ |
| 65 | static char *tskip(); |
| 66 | static char *tdecode(); |
| 67 | char *tgetstr(); |
| 68 | char *getenv(); |
| 69 | |
| 70 | /* |
| 71 | * Get an entry for terminal name in buffer bp from the termcap file. |
| 72 | */ |
| 73 | tgetent(bp, name) |
| 74 | char *bp, *name; |
| 75 | { |
| 76 | register char *p; |
| 77 | register char *cp; |
| 78 | register int c; |
| 79 | char *term, *home, *termpath; |
| 80 | char **fname = pathvec; |
| 81 | |
| 82 | pvec = pathvec; |
| 83 | tbuf = bp; |
| 84 | p = pathbuf; |
| 85 | cp = getenv("TERMCAP"); |
| 86 | /* |
| 87 | * TERMCAP can have one of two things in it. It can be the |
| 88 | * name of a file to use instead of /etc/termcap. In this |
| 89 | * case it better start with a "/". Or it can be an entry to |
| 90 | * use so we don't have to read the file. In this case it |
| 91 | * has to already have the newlines crunched out. If TERMCAP |
| 92 | * does not hold a file name then a path of names is searched |
| 93 | * instead. The path is found in the TERMPATH variable, or |
| 94 | * becomes "$HOME/.termcap /etc/termcap" if no TERMPATH exists. |
| 95 | */ |
| 96 | if (!cp || *cp != '/') { /* no TERMCAP or it holds an entry */ |
| 97 | if (termpath = getenv("TERMPATH")) |
| 98 | strncpy(pathbuf, termpath, PBUFSIZ); |
| 99 | else { |
| 100 | if (home = getenv("HOME")) { /* set up default */ |
| 101 | p += strlen(home); /* path, looking in */ |
| 102 | strcpy(pathbuf, home); /* $HOME first */ |
| 103 | *p++ = '/'; |
| 104 | } /* if no $HOME look in current directory */ |
| 105 | strncpy(p, _PATH_DEF, PBUFSIZ - (p - pathbuf)); |
| 106 | } |
| 107 | } |
| 108 | else /* user-defined name in TERMCAP */ |
| 109 | strncpy(pathbuf, cp, PBUFSIZ); /* still can be tokenized */ |
| 110 | |
| 111 | *fname++ = pathbuf; /* tokenize path into vector of names */ |
| 112 | while (*++p) |
| 113 | if (*p == ' ' || *p == ':') { |
| 114 | *p = '\0'; |
| 115 | while (*++p) |
| 116 | if (*p != ' ' && *p != ':') |
| 117 | break; |
| 118 | if (*p == '\0') |
| 119 | break; |
| 120 | *fname++ = p; |
| 121 | if (fname >= pathvec + PVECSIZ) { |
| 122 | fname--; |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | *fname = (char *) 0; /* mark end of vector */ |
| 127 | if (cp && *cp && *cp != '/') { |
| 128 | tbuf = cp; |
| 129 | c = tnamatch(name); |
| 130 | tbuf = bp; |
| 131 | if (c) { |
| 132 | strcpy(bp,cp); |
| 133 | return (tnchktc()); |
| 134 | } |
| 135 | } |
| 136 | return (tfindent(bp, name)); /* find terminal entry in path */ |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * tfindent - reads through the list of files in pathvec as if they were one |
| 141 | * continuous file searching for terminal entries along the way. It will |
| 142 | * participate in indirect recursion if the call to tnchktc() finds a tc= |
| 143 | * field, which is only searched for in the current file and files ocurring |
| 144 | * after it in pathvec. The usable part of this vector is kept in the global |
| 145 | * variable pvec. Terminal entries may not be broken across files. Parse is |
| 146 | * very rudimentary; we just notice escaped newlines. |
| 147 | */ |
| 148 | tfindent(bp, name) |
| 149 | char *bp, *name; |
| 150 | { |
| 151 | register char *cp; |
| 152 | register int c; |
| 153 | register int i, cnt; |
| 154 | char ibuf[BUFSIZ]; |
| 155 | int opencnt = 0; |
| 156 | int tf; |
| 157 | |
| 158 | tbuf = bp; |
| 159 | nextfile: |
| 160 | i = cnt = 0; |
| 161 | while (*pvec && (tf = open(*pvec, 0)) < 0) |
| 162 | pvec++; |
| 163 | if (!*pvec) |
| 164 | return (opencnt ? 0 : -1); |
| 165 | opencnt++; |
| 166 | for (;;) { |
| 167 | cp = bp; |
| 168 | for (;;) { |
| 169 | if (i == cnt) { |
| 170 | cnt = read(tf, ibuf, BUFSIZ); |
| 171 | if (cnt <= 0) { |
| 172 | close(tf); |
| 173 | pvec++; |
| 174 | goto nextfile; |
| 175 | } |
| 176 | i = 0; |
| 177 | } |
| 178 | c = ibuf[i++]; |
| 179 | if (c == '\n') { |
| 180 | if (cp > bp && cp[-1] == '\\'){ |
| 181 | cp--; |
| 182 | continue; |
| 183 | } |
| 184 | break; |
| 185 | } |
| 186 | if (cp >= bp+BUFSIZ) { |
| 187 | write(2,"Termcap entry too long\n", 23); |
| 188 | break; |
| 189 | } else |
| 190 | *cp++ = c; |
| 191 | } |
| 192 | *cp = 0; |
| 193 | |
| 194 | /* |
| 195 | * The real work for the match. |
| 196 | */ |
| 197 | if (tnamatch(name)) { |
| 198 | close(tf); |
| 199 | return(tnchktc()); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * tnchktc: check the last entry, see if it's tc=xxx. If so, |
| 206 | * recursively find xxx and append that entry (minus the names) |
| 207 | * to take the place of the tc=xxx entry. This allows termcap |
| 208 | * entries to say "like an HP2621 but doesn't turn on the labels". |
| 209 | * Note that this works because of the left to right scan. |
| 210 | */ |
| 211 | tnchktc() |
| 212 | { |
| 213 | register char *p, *q; |
| 214 | char tcname[16]; /* name of similar terminal */ |
| 215 | char tcbuf[BUFSIZ]; |
| 216 | char *holdtbuf = tbuf; |
| 217 | int l; |
| 218 | |
| 219 | p = tbuf + strlen(tbuf) - 2; /* before the last colon */ |
| 220 | while (*--p != ':') |
| 221 | if (p<tbuf) { |
| 222 | write(2, "Bad termcap entry\n", 18); |
| 223 | return (0); |
| 224 | } |
| 225 | p++; |
| 226 | /* p now points to beginning of last field */ |
| 227 | if (p[0] != 't' || p[1] != 'c') |
| 228 | return(1); |
| 229 | strcpy(tcname,p+3); |
| 230 | q = tcname; |
| 231 | while (*q && *q != ':') |
| 232 | q++; |
| 233 | *q = 0; |
| 234 | if (++hopcount > MAXHOP) { |
| 235 | write(2, "Infinite tc= loop\n", 18); |
| 236 | return (0); |
| 237 | } |
| 238 | if (tfindent(tcbuf, tcname) != 1) { |
| 239 | hopcount = 0; /* unwind recursion */ |
| 240 | return(0); |
| 241 | } |
| 242 | for (q=tcbuf; *q != ':'; q++) |
| 243 | ; |
| 244 | l = p - holdtbuf + strlen(q); |
| 245 | if (l > BUFSIZ) { |
| 246 | write(2, "Termcap entry too long\n", 23); |
| 247 | q[BUFSIZ - (p-tbuf)] = 0; |
| 248 | } |
| 249 | strcpy(p, q+1); |
| 250 | tbuf = holdtbuf; |
| 251 | hopcount = 0; /* unwind recursion */ |
| 252 | return(1); |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Tnamatch deals with name matching. The first field of the termcap |
| 257 | * entry is a sequence of names separated by |'s, so we compare |
| 258 | * against each such name. The normal : terminator after the last |
| 259 | * name (before the first field) stops us. |
| 260 | */ |
| 261 | tnamatch(np) |
| 262 | char *np; |
| 263 | { |
| 264 | register char *Np, *Bp; |
| 265 | |
| 266 | Bp = tbuf; |
| 267 | if (*Bp == '#') |
| 268 | return(0); |
| 269 | for (;;) { |
| 270 | for (Np = np; *Np && *Bp == *Np; Bp++, Np++) |
| 271 | continue; |
| 272 | if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0)) |
| 273 | return (1); |
| 274 | while (*Bp && *Bp != ':' && *Bp != '|') |
| 275 | Bp++; |
| 276 | if (*Bp == 0 || *Bp == ':') |
| 277 | return (0); |
| 278 | Bp++; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * Skip to the next field. Notice that this is very dumb, not |
| 284 | * knowing about \: escapes or any such. If necessary, :'s can be put |
| 285 | * into the termcap file in octal. |
| 286 | */ |
| 287 | static char * |
| 288 | tskip(bp) |
| 289 | register char *bp; |
| 290 | { |
| 291 | |
| 292 | while (*bp && *bp != ':') |
| 293 | bp++; |
| 294 | if (*bp == ':') |
| 295 | bp++; |
| 296 | return (bp); |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Return the (numeric) option id. |
| 301 | * Numeric options look like |
| 302 | * li#80 |
| 303 | * i.e. the option string is separated from the numeric value by |
| 304 | * a # character. If the option is not found we return -1. |
| 305 | * Note that we handle octal numbers beginning with 0. |
| 306 | */ |
| 307 | tgetnum(id) |
| 308 | char *id; |
| 309 | { |
| 310 | register int i, base; |
| 311 | register char *bp = tbuf; |
| 312 | |
| 313 | for (;;) { |
| 314 | bp = tskip(bp); |
| 315 | if (*bp == 0) |
| 316 | return (-1); |
| 317 | if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1]) |
| 318 | continue; |
| 319 | if (*bp == '@') |
| 320 | return(-1); |
| 321 | if (*bp != '#') |
| 322 | continue; |
| 323 | bp++; |
| 324 | base = 10; |
| 325 | if (*bp == '0') |
| 326 | base = 8; |
| 327 | i = 0; |
| 328 | while (isdigit(*bp)) |
| 329 | i *= base, i += *bp++ - '0'; |
| 330 | return (i); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * Handle a flag option. |
| 336 | * Flag options are given "naked", i.e. followed by a : or the end |
| 337 | * of the buffer. Return 1 if we find the option, or 0 if it is |
| 338 | * not given. |
| 339 | */ |
| 340 | tgetflag(id) |
| 341 | char *id; |
| 342 | { |
| 343 | register char *bp = tbuf; |
| 344 | |
| 345 | for (;;) { |
| 346 | bp = tskip(bp); |
| 347 | if (!*bp) |
| 348 | return (0); |
| 349 | if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) { |
| 350 | if (!*bp || *bp == ':') |
| 351 | return (1); |
| 352 | else if (*bp == '@') |
| 353 | return(0); |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Get a string valued option. |
| 360 | * These are given as |
| 361 | * cl=^Z |
| 362 | * Much decoding is done on the strings, and the strings are |
| 363 | * placed in area, which is a ref parameter which is updated. |
| 364 | * No checking on area overflow. |
| 365 | */ |
| 366 | char * |
| 367 | tgetstr(id, area) |
| 368 | char *id, **area; |
| 369 | { |
| 370 | register char *bp = tbuf; |
| 371 | |
| 372 | for (;;) { |
| 373 | bp = tskip(bp); |
| 374 | if (!*bp) |
| 375 | return (0); |
| 376 | if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1]) |
| 377 | continue; |
| 378 | if (*bp == '@') |
| 379 | return(0); |
| 380 | if (*bp != '=') |
| 381 | continue; |
| 382 | bp++; |
| 383 | return (tdecode(bp, area)); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * Tdecode does the grung work to decode the |
| 389 | * string capability escapes. |
| 390 | */ |
| 391 | static char * |
| 392 | tdecode(str, area) |
| 393 | register char *str; |
| 394 | char **area; |
| 395 | { |
| 396 | register char *cp; |
| 397 | register int c; |
| 398 | register char *dp; |
| 399 | int i; |
| 400 | |
| 401 | cp = *area; |
| 402 | while ((c = *str++) && c != ':') { |
| 403 | switch (c) { |
| 404 | |
| 405 | case '^': |
| 406 | c = *str++ & 037; |
| 407 | break; |
| 408 | |
| 409 | case '\\': |
| 410 | dp = "E\033^^\\\\::n\nr\rt\tb\bf\f"; |
| 411 | c = *str++; |
| 412 | nextc: |
| 413 | if (*dp++ == c) { |
| 414 | c = *dp++; |
| 415 | break; |
| 416 | } |
| 417 | dp++; |
| 418 | if (*dp) |
| 419 | goto nextc; |
| 420 | if (isdigit(c)) { |
| 421 | c -= '0', i = 2; |
| 422 | do |
| 423 | c <<= 3, c |= *str++ - '0'; |
| 424 | while (--i && isdigit(*str)); |
| 425 | } |
| 426 | break; |
| 427 | } |
| 428 | *cp++ = c; |
| 429 | } |
| 430 | *cp++ = 0; |
| 431 | str = *area; |
| 432 | *area = cp; |
| 433 | return (str); |
| 434 | } |