1 /**************************************************************************** 2 * Copyright (c) 1998-2009,2010 Free Software Foundation, Inc. * 3 * * 4 * Permission is hereby granted, free of charge, to any person obtaining a * 5 * copy of this software and associated documentation files (the * 6 * "Software"), to deal in the Software without restriction, including * 7 * without limitation the rights to use, copy, modify, merge, publish, * 8 * distribute, distribute with modifications, sublicense, and/or sell * 9 * copies of the Software, and to permit persons to whom the Software is * 10 * furnished to do so, subject to the following conditions: * 11 * * 12 * The above copyright notice and this permission notice shall be included * 13 * in all copies or substantial portions of the Software. * 14 * * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 22 * * 23 * Except as contained in this notice, the name(s) of the above copyright * 24 * holders shall not be used in advertising or otherwise to promote the * 25 * sale, use or other dealings in this Software without prior written * 26 * authorization. * 27 ****************************************************************************/ 28 29 /**************************************************************************** 30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 31 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 32 * and: Thomas E. Dickey 1996-on * 33 ****************************************************************************/ 34 35 /* 36 * Termcap compatibility support 37 * 38 * If your OS integrator didn't install a terminfo database, you can call 39 * _nc_read_termcap_entry() to support reading and translating capabilities 40 * from the system termcap file. This is a kludge; it will bulk up and slow 41 * down every program that uses ncurses, and translated termcap entries cannot 42 * use full terminfo capabilities. Don't use it unless you absolutely have to; 43 * instead, get your system people to run tic(1) from root on the terminfo 44 * master included with ncurses to translate it into a terminfo database. 45 * 46 * If USE_GETCAP is enabled, we use what is effectively a copy of the 4.4BSD 47 * getcap code to fetch entries. There are disadvantages to this; mainly that 48 * getcap(3) does its own resolution, meaning that entries read in in this way 49 * can't reference the terminfo tree. The only thing it buys is faster startup 50 * time, getcap(3) is much faster than our tic parser. 51 */ 52 53 #include <curses.priv.h> 54 55 #include <ctype.h> 56 #include <sys/types.h> 57 #include <sys/stat.h> 58 #include <tic.h> 59 60 MODULE_ID("$Id: read_termcap.c,v 1.74 2010/01/23 17:57:43 tom Exp $") 61 62 #if !PURE_TERMINFO 63 64 #define TC_SUCCESS 0 65 #define TC_NOT_FOUND -1 66 #define TC_SYS_ERR -2 67 #define TC_REF_LOOP -3 68 #define TC_UNRESOLVED -4 /* this is not returned by BSD cgetent */ 69 70 static NCURSES_CONST char * 71 get_termpath(void) 72 { 73 NCURSES_CONST char *result; 74 75 if (!use_terminfo_vars() || (result = getenv("TERMPATH")) == 0) 76 result = TERMPATH; 77 T(("TERMPATH is %s", result)); 78 return result; 79 } 80 81 /* 82 * Note: 83 * getcap(), cgetent(), etc., are BSD functions. A copy of those was added to 84 * this file in November 1995, derived from the BSD4.4 Lite sources. 85 * 86 * The initial adaptation uses 518 lines from that source. 87 * The current source (in 2009) uses 183 lines of BSD4.4 Lite (441 ignoring 88 * whitespace). 89 */ 90 #if USE_GETCAP 91 92 #if HAVE_BSD_CGETENT 93 #define _nc_cgetcap cgetcap 94 #define _nc_cgetent(buf, oline, db_array, name) cgetent(buf, db_array, name) 95 #define _nc_cgetmatch cgetmatch 96 #define _nc_cgetset cgetset 97 #else 98 static int _nc_cgetmatch(char *, const char *); 99 static int _nc_getent(char **, unsigned *, int *, int, char **, int, const char 100 *, int, char *); 101 static int _nc_nfcmp(const char *, char *); 102 103 /*- 104 * Copyright (c) 1992, 1993 105 * The Regents of the University of California. All rights reserved. 106 * 107 * This code is derived from software contributed to Berkeley by 108 * Casey Leedom of Lawrence Livermore National Laboratory. 109 * 110 * Redistribution and use in source and binary forms, with or without 111 * modification, are permitted provided that the following conditions 112 * are met: 113 * 1. Redistributions of source code must retain the above copyright 114 * notice, this list of conditions and the following disclaimer. 115 * 2. Redistributions in binary form must reproduce the above copyright 116 * notice, this list of conditions and the following disclaimer in the 117 * documentation and/or other materials provided with the distribution. 118 * 3. Neither the name of the University nor the names of its contributors 119 * may be used to endorse or promote products derived from this software 120 * without specific prior written permission. 121 * 122 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 123 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 124 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 125 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 126 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 127 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 128 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 129 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 130 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 131 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 132 * SUCH DAMAGE. 133 */ 134 135 /* static char sccsid[] = "@(#)getcap.c 8.3 (Berkeley) 3/25/94"; */ 136 137 #define BFRAG 1024 138 #define BSIZE 1024 139 #define MAX_RECURSION 32 /* maximum getent recursion */ 140 141 static size_t topreclen; /* toprec length */ 142 static char *toprec; /* Additional record specified by cgetset() */ 143 static int gottoprec; /* Flag indicating retrieval of toprecord */ 144 145 /* 146 * Cgetset() allows the addition of a user specified buffer to be added to the 147 * database array, in effect "pushing" the buffer on top of the virtual 148 * database. 0 is returned on success, -1 on failure. 149 */ 150 static int 151 _nc_cgetset(const char *ent) 152 { 153 if (ent == 0) { 154 FreeIfNeeded(toprec); 155 toprec = 0; 156 topreclen = 0; 157 return (0); 158 } 159 topreclen = strlen(ent); 160 if ((toprec = typeMalloc(char, topreclen + 1)) == 0) { 161 errno = ENOMEM; 162 return (-1); 163 } 164 gottoprec = 0; 165 (void) strcpy(toprec, ent); 166 return (0); 167 } 168 169 /* 170 * Cgetcap searches the capability record buf for the capability cap with type 171 * `type'. A pointer to the value of cap is returned on success, 0 if the 172 * requested capability couldn't be found. 173 * 174 * Specifying a type of ':' means that nothing should follow cap (:cap:). In 175 * this case a pointer to the terminating ':' or NUL will be returned if cap is 176 * found. 177 * 178 * If (cap, '@') or (cap, terminator, '@') is found before (cap, terminator) 179 * return 0. 180 */ 181 static char * 182 _nc_cgetcap(char *buf, const char *cap, int type) 183 { 184 register const char *cp; 185 register char *bp; 186 187 bp = buf; 188 for (;;) { 189 /* 190 * Skip past the current capability field - it's either the 191 * name field if this is the first time through the loop, or 192 * the remainder of a field whose name failed to match cap. 193 */ 194 for (;;) { 195 if (*bp == '\0') 196 return (0); 197 else if (*bp++ == ':') 198 break; 199 } 200 201 /* 202 * Try to match (cap, type) in buf. 203 */ 204 for (cp = cap; *cp == *bp && *bp != '\0'; cp++, bp++) 205 continue; 206 if (*cp != '\0') 207 continue; 208 if (*bp == '@') 209 return (0); 210 if (type == ':') { 211 if (*bp != '\0' && *bp != ':') 212 continue; 213 return (bp); 214 } 215 if (*bp != type) 216 continue; 217 bp++; 218 return (*bp == '@' ? 0 : bp); 219 } 220 /* NOTREACHED */ 221 } 222 223 /* 224 * Cgetent extracts the capability record name from the NULL terminated file 225 * array db_array and returns a pointer to a malloc'd copy of it in buf. Buf 226 * must be retained through all subsequent calls to cgetcap, cgetnum, cgetflag, 227 * and cgetstr, but may then be freed. 228 * 229 * Returns: 230 * 231 * positive # on success (i.e., the index in db_array) 232 * TC_NOT_FOUND if the requested record couldn't be found 233 * TC_SYS_ERR if a system error was encountered (e.g.,couldn't open a file) 234 * TC_REF_LOOP if a potential reference loop is detected 235 * TC_UNRESOLVED if we had too many recurrences to resolve 236 */ 237 static int 238 _nc_cgetent(char **buf, int *oline, char **db_array, const char *name) 239 { 240 unsigned dummy; 241 242 return (_nc_getent(buf, &dummy, oline, 0, db_array, -1, name, 0, 0)); 243 } 244 245 /* 246 * Getent implements the functions of cgetent. If fd is non-negative, 247 * *db_array has already been opened and fd is the open file descriptor. We 248 * do this to save time and avoid using up file descriptors for tc= 249 * recursions. 250 * 251 * Getent returns the same success/failure codes as cgetent. On success, a 252 * pointer to a malloc'd capability record with all tc= capabilities fully 253 * expanded and its length (not including trailing ASCII NUL) are left in 254 * *cap and *len. 255 * 256 * Basic algorithm: 257 * + Allocate memory incrementally as needed in chunks of size BFRAG 258 * for capability buffer. 259 * + Recurse for each tc=name and interpolate result. Stop when all 260 * names interpolated, a name can't be found, or depth exceeds 261 * MAX_RECURSION. 262 */ 263 #define DOALLOC(size) typeRealloc(char, size, record) 264 static int 265 _nc_getent( 266 char **cap, /* termcap-content */ 267 unsigned *len, /* length, needed for recursion */ 268 int *beginning, /* line-number at match */ 269 int in_array, /* index in 'db_array[] */ 270 char **db_array, /* list of files to search */ 271 int fd, 272 const char *name, 273 int depth, 274 char *nfield) 275 { 276 register char *r_end, *rp; 277 int myfd = FALSE; 278 char *record = 0; 279 int tc_not_resolved; 280 int current; 281 int lineno; 282 283 /* 284 * Return with ``loop detected'' error if we've recurred more than 285 * MAX_RECURSION times. 286 */ 287 if (depth > MAX_RECURSION) 288 return (TC_REF_LOOP); 289 290 /* 291 * Check if we have a top record from cgetset(). 292 */ 293 if (depth == 0 && toprec != 0 && _nc_cgetmatch(toprec, name) == 0) { 294 if ((record = DOALLOC(topreclen + BFRAG)) == 0) { 295 errno = ENOMEM; 296 return (TC_SYS_ERR); 297 } 298 (void) strcpy(record, toprec); 299 rp = record + topreclen + 1; 300 r_end = rp + BFRAG; 301 current = in_array; 302 } else { 303 int foundit; 304 305 /* 306 * Allocate first chunk of memory. 307 */ 308 if ((record = DOALLOC(BFRAG)) == 0) { 309 errno = ENOMEM; 310 return (TC_SYS_ERR); 311 } 312 rp = r_end = record + BFRAG; 313 foundit = FALSE; 314 315 /* 316 * Loop through database array until finding the record. 317 */ 318 for (current = in_array; db_array[current] != 0; current++) { 319 int eof = FALSE; 320 321 /* 322 * Open database if not already open. 323 */ 324 if (fd >= 0) { 325 (void) lseek(fd, (off_t) 0, SEEK_SET); 326 } else if ((_nc_access(db_array[current], R_OK) < 0) 327 || (fd = open(db_array[current], O_RDONLY, 0)) < 0) { 328 /* No error on unfound file. */ 329 if (errno == ENOENT) 330 continue; 331 free(record); 332 return (TC_SYS_ERR); 333 } else { 334 myfd = TRUE; 335 } 336 lineno = 0; 337 338 /* 339 * Find the requested capability record ... 340 */ 341 { 342 char buf[2048]; 343 register char *b_end = buf; 344 register char *bp = buf; 345 register int c; 346 347 /* 348 * Loop invariants: 349 * There is always room for one more character in record. 350 * R_end always points just past end of record. 351 * Rp always points just past last character in record. 352 * B_end always points just past last character in buf. 353 * Bp always points at next character in buf. 354 */ 355 356 for (;;) { 357 int first = lineno + 1; 358 359 /* 360 * Read in a line implementing (\, newline) 361 * line continuation. 362 */ 363 rp = record; 364 for (;;) { 365 if (bp >= b_end) { 366 int n; 367 368 n = read(fd, buf, sizeof(buf)); 369 if (n <= 0) { 370 if (myfd) 371 (void) close(fd); 372 if (n < 0) { 373 free(record); 374 return (TC_SYS_ERR); 375 } 376 fd = -1; 377 eof = TRUE; 378 break; 379 } 380 b_end = buf + n; 381 bp = buf; 382 } 383 384 c = *bp++; 385 if (c == '\n') { 386 lineno++; 387 if (rp == record || *(rp - 1) != '\\') 388 break; 389 } 390 *rp++ = c; 391 392 /* 393 * Enforce loop invariant: if no room 394 * left in record buffer, try to get 395 * some more. 396 */ 397 if (rp >= r_end) { 398 unsigned pos; 399 size_t newsize; 400 401 pos = rp - record; 402 newsize = r_end - record + BFRAG; 403 record = DOALLOC(newsize); 404 if (record == 0) { 405 if (myfd) 406 (void) close(fd); 407 errno = ENOMEM; 408 return (TC_SYS_ERR); 409 } 410 r_end = record + newsize; 411 rp = record + pos; 412 } 413 } 414 /* loop invariant lets us do this */ 415 *rp++ = '\0'; 416 417 /* 418 * If encountered eof check next file. 419 */ 420 if (eof) 421 break; 422 423 /* 424 * Toss blank lines and comments. 425 */ 426 if (*record == '\0' || *record == '#') 427 continue; 428 429 /* 430 * See if this is the record we want ... 431 */ 432 if (_nc_cgetmatch(record, name) == 0 433 && (nfield == 0 434 || !_nc_nfcmp(nfield, record))) { 435 foundit = TRUE; 436 *beginning = first; 437 break; /* found it! */ 438 } 439 } 440 } 441 if (foundit) 442 break; 443 } 444 445 if (!foundit) 446 return (TC_NOT_FOUND); 447 } 448 449 /* 450 * Got the capability record, but now we have to expand all tc=name 451 * references in it ... 452 */ 453 { 454 register char *newicap, *s; 455 register int newilen; 456 unsigned ilen; 457 int diff, iret, tclen, oline; 458 char *icap, *scan, *tc, *tcstart, *tcend; 459 460 /* 461 * Loop invariants: 462 * There is room for one more character in record. 463 * R_end points just past end of record. 464 * Rp points just past last character in record. 465 * Scan points at remainder of record that needs to be 466 * scanned for tc=name constructs. 467 */ 468 scan = record; 469 tc_not_resolved = FALSE; 470 for (;;) { 471 if ((tc = _nc_cgetcap(scan, "tc", '=')) == 0) 472 break; 473 474 /* 475 * Find end of tc=name and stomp on the trailing `:' 476 * (if present) so we can use it to call ourselves. 477 */ 478 s = tc; 479 while (*s != '\0') { 480 if (*s++ == ':') { 481 *(s - 1) = '\0'; 482 break; 483 } 484 } 485 tcstart = tc - 3; 486 tclen = s - tcstart; 487 tcend = s; 488 489 iret = _nc_getent(&icap, &ilen, &oline, current, db_array, fd, 490 tc, depth + 1, 0); 491 newicap = icap; /* Put into a register. */ 492 newilen = ilen; 493 if (iret != TC_SUCCESS) { 494 /* an error */ 495 if (iret < TC_NOT_FOUND) { 496 if (myfd) 497 (void) close(fd); 498 free(record); 499 return (iret); 500 } 501 if (iret == TC_UNRESOLVED) 502 tc_not_resolved = TRUE; 503 /* couldn't resolve tc */ 504 if (iret == TC_NOT_FOUND) { 505 *(s - 1) = ':'; 506 scan = s - 1; 507 tc_not_resolved = TRUE; 508 continue; 509 } 510 } 511 512 /* not interested in name field of tc'ed record */ 513 s = newicap; 514 while (*s != '\0' && *s++ != ':') ; 515 newilen -= s - newicap; 516 newicap = s; 517 518 /* make sure interpolated record is `:'-terminated */ 519 s += newilen; 520 if (*(s - 1) != ':') { 521 *s = ':'; /* overwrite NUL with : */ 522 newilen++; 523 } 524 525 /* 526 * Make sure there's enough room to insert the 527 * new record. 528 */ 529 diff = newilen - tclen; 530 if (diff >= r_end - rp) { 531 unsigned pos, tcpos, tcposend; 532 size_t newsize; 533 534 pos = rp - record; 535 newsize = r_end - record + diff + BFRAG; 536 tcpos = tcstart - record; 537 tcposend = tcend - record; 538 record = DOALLOC(newsize); 539 if (record == 0) { 540 if (myfd) 541 (void) close(fd); 542 free(icap); 543 errno = ENOMEM; 544 return (TC_SYS_ERR); 545 } 546 r_end = record + newsize; 547 rp = record + pos; 548 tcstart = record + tcpos; 549 tcend = record + tcposend; 550 } 551 552 /* 553 * Insert tc'ed record into our record. 554 */ 555 s = tcstart + newilen; 556 memmove(s, tcend, (size_t) (rp - tcend)); 557 memmove(tcstart, newicap, (size_t) newilen); 558 rp += diff; 559 free(icap); 560 561 /* 562 * Start scan on `:' so next cgetcap works properly 563 * (cgetcap always skips first field). 564 */ 565 scan = s - 1; 566 } 567 } 568 569 /* 570 * Close file (if we opened it), give back any extra memory, and 571 * return capability, length and success. 572 */ 573 if (myfd) 574 (void) close(fd); 575 *len = rp - record - 1; /* don't count NUL */ 576 if (r_end > rp) { 577 if ((record = DOALLOC((size_t) (rp - record))) == 0) { 578 errno = ENOMEM; 579 return (TC_SYS_ERR); 580 } 581 } 582 583 *cap = record; 584 if (tc_not_resolved) 585 return (TC_UNRESOLVED); 586 return (current); 587 } 588 589 /* 590 * Cgetmatch will return 0 if name is one of the names of the capability 591 * record buf, -1 if not. 592 */ 593 static int 594 _nc_cgetmatch(char *buf, const char *name) 595 { 596 register const char *np; 597 register char *bp; 598 599 /* 600 * Start search at beginning of record. 601 */ 602 bp = buf; 603 for (;;) { 604 /* 605 * Try to match a record name. 606 */ 607 np = name; 608 for (;;) { 609 if (*np == '\0') { 610 if (*bp == '|' || *bp == ':' || *bp == '\0') 611 return (0); 612 else 613 break; 614 } else if (*bp++ != *np++) { 615 break; 616 } 617 } 618 619 /* 620 * Match failed, skip to next name in record. 621 */ 622 bp--; /* a '|' or ':' may have stopped the match */ 623 for (;;) { 624 if (*bp == '\0' || *bp == ':') 625 return (-1); /* match failed totally */ 626 else if (*bp++ == '|') 627 break; /* found next name */ 628 } 629 } 630 } 631 632 /* 633 * Compare name field of record. 634 */ 635 static int 636 _nc_nfcmp(const char *nf, char *rec) 637 { 638 char *cp, tmp; 639 int ret; 640 641 for (cp = rec; *cp != ':'; cp++) ; 642 643 tmp = *(cp + 1); 644 *(cp + 1) = '\0'; 645 ret = strcmp(nf, rec); 646 *(cp + 1) = tmp; 647 648 return (ret); 649 } 650 #endif /* HAVE_BSD_CGETENT */ 651 652 /* 653 * Since ncurses provides its own 'tgetent()', we cannot use the native one. 654 * So we reproduce the logic to get down to cgetent() -- or our cut-down 655 * version of that -- to circumvent the problem of configuring against the 656 * termcap library. 657 */ 658 #define USE_BSD_TGETENT 1 659 660 #if USE_BSD_TGETENT 661 /* 662 * Copyright (c) 1980, 1993 663 * The Regents of the University of California. All rights reserved. 664 * 665 * Redistribution and use in source and binary forms, with or without 666 * modification, are permitted provided that the following conditions 667 * are met: 668 * 1. Redistributions of source code must retain the above copyright 669 * notice, this list of conditions and the following disclaimer. 670 * 2. Redistributions in binary form must reproduce the above copyright 671 * notice, this list of conditions and the following disclaimer in the 672 * documentation and/or other materials provided with the distribution. 673 * 3. All advertising materials mentioning features or use of this software 674 * must display the following acknowledgment: 675 * This product includes software developed by the University of 676 * California, Berkeley and its contributors. 677 * 4. Neither the name of the University nor the names of its contributors 678 * may be used to endorse or promote products derived from this software 679 * without specific prior written permission. 680 * 681 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 682 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 683 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 684 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 685 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 686 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 687 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 688 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 689 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 690 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 691 * SUCH DAMAGE. 692 */ 693 694 /* static char sccsid[] = "@(#)termcap.c 8.1 (Berkeley) 6/4/93" */ 695 696 #define PBUFSIZ 512 /* max length of filename path */ 697 #define PVECSIZ 32 /* max number of names in path */ 698 #define TBUFSIZ (2048*2) 699 700 static char *tbuf; 701 702 /* 703 * On entry, srcp points to a non ':' character which is the beginning of the 704 * token, if any. We'll try to return a string that doesn't end with a ':'. 705 */ 706 static char * 707 get_tc_token(char **srcp, int *endp) 708 { 709 int ch; 710 bool found = FALSE; 711 char *s, *base; 712 char *tok = 0; 713 714 *endp = TRUE; 715 for (s = base = *srcp; *s != '\0';) { 716 ch = *s++; 717 if (ch == '\\') { 718 if (*s == '\0') { 719 break; 720 } else if (*s++ == '\n') { 721 while (isspace(UChar(*s))) 722 s++; 723 } else { 724 found = TRUE; 725 } 726 } else if (ch == ':') { 727 if (found) { 728 tok = base; 729 s[-1] = '\0'; 730 *srcp = s; 731 *endp = FALSE; 732 break; 733 } 734 base = s; 735 } else if (isgraph(UChar(ch))) { 736 found = TRUE; 737 } 738 } 739 740 /* malformed entry may end without a ':' */ 741 if (tok == 0 && found) { 742 tok = base; 743 } 744 745 return tok; 746 } 747 748 static char * 749 copy_tc_token(char *dst, const char *src, size_t len) 750 { 751 int ch; 752 753 while ((ch = *src++) != '\0') { 754 if (ch == '\\' && *src == '\n') { 755 while (isspace(UChar(*src))) 756 src++; 757 continue; 758 } 759 if (--len == 0) { 760 dst = 0; 761 break; 762 } 763 *dst++ = ch; 764 } 765 return dst; 766 } 767 768 /* 769 * Get an entry for terminal name in buffer bp from the termcap file. 770 */ 771 static int 772 _nc_tgetent(char *bp, char **sourcename, int *lineno, const char *name) 773 { 774 static char *the_source; 775 776 register char *p; 777 register char *cp; 778 char *dummy = NULL; 779 char **fname; 780 char *home; 781 int i; 782 char pathbuf[PBUFSIZ]; /* holds raw path of filenames */ 783 char *pathvec[PVECSIZ]; /* to point to names in pathbuf */ 784 char **pvec; /* holds usable tail of path vector */ 785 NCURSES_CONST char *termpath; 786 string_desc desc; 787 788 fname = pathvec; 789 pvec = pathvec; 790 tbuf = bp; 791 p = pathbuf; 792 cp = use_terminfo_vars()? getenv("TERMCAP") : NULL; 793 794 /* 795 * TERMCAP can have one of two things in it. It can be the name of a file 796 * to use instead of /etc/termcap. In this case it better start with a 797 * "/". Or it can be an entry to use so we don't have to read the file. 798 * In this case it has to already have the newlines crunched out. If 799 * TERMCAP does not hold a file name then a path of names is searched 800 * instead. The path is found in the TERMPATH variable, or becomes 801 * "$HOME/.termcap /etc/termcap" if no TERMPATH exists. 802 */ 803 _nc_str_init(&desc, pathbuf, sizeof(pathbuf)); 804 if (cp == NULL) { 805 _nc_safe_strcpy(&desc, get_termpath()); 806 } else if (!_nc_is_abs_path(cp)) { /* TERMCAP holds an entry */ 807 if ((termpath = get_termpath()) != 0) { 808 _nc_safe_strcat(&desc, termpath); 809 } else { 810 char temp[PBUFSIZ]; 811 temp[0] = 0; 812 if ((home = getenv("HOME")) != 0 && *home != '\0' 813 && strchr(home, ' ') == 0 814 && strlen(home) < sizeof(temp) - 10) { /* setup path */ 815 sprintf(temp, "%s/", home); /* $HOME first */ 816 } 817 /* if no $HOME look in current directory */ 818 strcat(temp, ".termcap"); 819 _nc_safe_strcat(&desc, temp); 820 _nc_safe_strcat(&desc, " "); 821 _nc_safe_strcat(&desc, get_termpath()); 822 } 823 } else { /* user-defined name in TERMCAP */ 824 _nc_safe_strcat(&desc, cp); /* still can be tokenized */ 825 } 826 827 *fname++ = pathbuf; /* tokenize path into vector of names */ 828 while (*++p) { 829 if (*p == ' ' || *p == NCURSES_PATHSEP) { 830 *p = '\0'; 831 while (*++p) 832 if (*p != ' ' && *p != NCURSES_PATHSEP) 833 break; 834 if (*p == '\0') 835 break; 836 *fname++ = p; 837 if (fname >= pathvec + PVECSIZ) { 838 fname--; 839 break; 840 } 841 } 842 } 843 *fname = 0; /* mark end of vector */ 844 if (_nc_is_abs_path(cp)) { 845 if (_nc_cgetset(cp) < 0) { 846 return (TC_SYS_ERR); 847 } 848 } 849 850 i = _nc_cgetent(&dummy, lineno, pathvec, name); 851 852 /* ncurses' termcap-parsing routines cannot handle multiple adjacent 853 * empty fields, and mistakenly use the last valid cap entry instead of 854 * the first (breaks tc= includes) 855 */ 856 if (i >= 0) { 857 char *pd, *ps, *tok; 858 int endflag = FALSE; 859 char *list[1023]; 860 size_t n, count = 0; 861 862 pd = bp; 863 ps = dummy; 864 while (!endflag && (tok = get_tc_token(&ps, &endflag)) != 0) { 865 bool ignore = FALSE; 866 867 for (n = 1; n < count; n++) { 868 char *s = list[n]; 869 if (s[0] == tok[0] 870 && s[1] == tok[1]) { 871 ignore = TRUE; 872 break; 873 } 874 } 875 if (ignore != TRUE) { 876 list[count++] = tok; 877 pd = copy_tc_token(pd, tok, TBUFSIZ - (2 + pd - bp)); 878 if (pd == 0) { 879 i = -1; 880 break; 881 } 882 *pd++ = ':'; 883 *pd = '\0'; 884 } 885 } 886 } 887 888 FreeIfNeeded(dummy); 889 FreeIfNeeded(the_source); 890 the_source = 0; 891 892 /* This is not related to the BSD cgetent(), but to fake up a suitable 893 * filename for ncurses' error reporting. (If we are not using BSD 894 * cgetent, then it is the actual filename). 895 */ 896 if (i >= 0) { 897 #if HAVE_BSD_CGETENT 898 char temp[PATH_MAX]; 899 900 _nc_str_init(&desc, temp, sizeof(temp)); 901 _nc_safe_strcpy(&desc, pathvec[i]); 902 _nc_safe_strcat(&desc, ".db"); 903 if (_nc_access(temp, R_OK) == 0) { 904 _nc_safe_strcpy(&desc, pathvec[i]); 905 } 906 if ((the_source = strdup(temp)) != 0) 907 *sourcename = the_source; 908 #else 909 if ((the_source = strdup(pathvec[i])) != 0) 910 *sourcename = the_source; 911 #endif 912 } 913 914 return (i); 915 } 916 #endif /* USE_BSD_TGETENT */ 917 #endif /* USE_GETCAP */ 918 919 #define MAXPATHS 32 920 921 /* 922 * Add a filename to the list in 'termpaths[]', checking that we really have 923 * a right to open the file. 924 */ 925 #if !USE_GETCAP 926 static int 927 add_tc(char *termpaths[], char *path, int count) 928 { 929 char *save = strchr(path, NCURSES_PATHSEP); 930 if (save != 0) 931 *save = '\0'; 932 if (count < MAXPATHS 933 && _nc_access(path, R_OK) == 0) { 934 termpaths[count++] = path; 935 T(("Adding termpath %s", path)); 936 } 937 termpaths[count] = 0; 938 if (save != 0) 939 *save = NCURSES_PATHSEP; 940 return count; 941 } 942 #define ADD_TC(path, count) filecount = add_tc(termpaths, path, count) 943 #endif /* !USE_GETCAP */ 944 945 NCURSES_EXPORT(int) 946 _nc_read_termcap_entry(const char *const tn, TERMTYPE *const tp) 947 { 948 int found = TGETENT_NO; 949 ENTRY *ep; 950 #if USE_GETCAP_CACHE 951 char cwd_buf[PATH_MAX]; 952 #endif 953 #if USE_GETCAP 954 char *p, tc[TBUFSIZ]; 955 int status; 956 static char *source; 957 static int lineno; 958 959 T(("read termcap entry for %s", tn)); 960 961 if (strlen(tn) == 0 962 || strcmp(tn, ".") == 0 963 || strcmp(tn, "..") == 0 964 || _nc_pathlast(tn) != 0) { 965 T(("illegal or missing entry name '%s'", tn)); 966 return TGETENT_NO; 967 } 968 969 if (use_terminfo_vars() && (p = getenv("TERMCAP")) != 0 970 && !_nc_is_abs_path(p) && _nc_name_match(p, tn, "|:")) { 971 /* TERMCAP holds a termcap entry */ 972 strncpy(tc, p, sizeof(tc) - 1); 973 tc[sizeof(tc) - 1] = '\0'; 974 _nc_set_source("TERMCAP"); 975 } else { 976 /* we're using getcap(3) */ 977 if ((status = _nc_tgetent(tc, &source, &lineno, tn)) < 0) 978 return (status == TC_NOT_FOUND ? TGETENT_NO : TGETENT_ERR); 979 980 _nc_curr_line = lineno; 981 _nc_set_source(source); 982 } 983 _nc_read_entry_source((FILE *) 0, tc, FALSE, FALSE, NULLHOOK); 984 #else 985 /* 986 * Here is what the 4.4BSD termcap(3) page prescribes: 987 * 988 * It will look in the environment for a TERMCAP variable. If found, and 989 * the value does not begin with a slash, and the terminal type name is the 990 * same as the environment string TERM, the TERMCAP string is used instead 991 * of reading a termcap file. If it does begin with a slash, the string is 992 * used as a path name of the termcap file to search. If TERMCAP does not 993 * begin with a slash and name is different from TERM, tgetent() searches 994 * the files $HOME/.termcap and /usr/share/misc/termcap, in that order, 995 * unless the environment variable TERMPATH exists, in which case it 996 * specifies a list of file pathnames (separated by spaces or colons) to be 997 * searched instead. 998 * 999 * It goes on to state: 1000 * 1001 * Whenever multiple files are searched and a tc field occurs in the 1002 * requested entry, the entry it names must be found in the same file or 1003 * one of the succeeding files. 1004 * 1005 * However, this restriction is relaxed in ncurses; tc references to 1006 * previous files are permitted. 1007 * 1008 * This routine returns 1 if an entry is found, 0 if not found, and -1 if 1009 * the database is not accessible. 1010 */ 1011 FILE *fp; 1012 char *tc, *termpaths[MAXPATHS]; 1013 int filecount = 0; 1014 int j, k; 1015 bool use_buffer = FALSE; 1016 bool normal = TRUE; 1017 char tc_buf[1024]; 1018 char pathbuf[PATH_MAX]; 1019 char *copied = 0; 1020 char *cp; 1021 struct stat test_stat[MAXPATHS]; 1022 1023 termpaths[filecount] = 0; 1024 if (use_terminfo_vars() && (tc = getenv("TERMCAP")) != 0) { 1025 if (_nc_is_abs_path(tc)) { /* interpret as a filename */ 1026 ADD_TC(tc, 0); 1027 normal = FALSE; 1028 } else if (_nc_name_match(tc, tn, "|:")) { /* treat as a capability file */ 1029 use_buffer = TRUE; 1030 (void) sprintf(tc_buf, "%.*s\n", (int) sizeof(tc_buf) - 2, tc); 1031 normal = FALSE; 1032 } 1033 } 1034 1035 if (normal) { /* normal case */ 1036 char envhome[PATH_MAX], *h; 1037 1038 copied = strdup(get_termpath()); 1039 for (cp = copied; *cp; cp++) { 1040 if (*cp == NCURSES_PATHSEP) 1041 *cp = '\0'; 1042 else if (cp == copied || cp[-1] == '\0') { 1043 ADD_TC(cp, filecount); 1044 } 1045 } 1046 1047 #define PRIVATE_CAP "%s/.termcap" 1048 1049 if (use_terminfo_vars() && (h = getenv("HOME")) != NULL && *h != '\0' 1050 && (strlen(h) + sizeof(PRIVATE_CAP)) < PATH_MAX) { 1051 /* user's .termcap, if any, should override it */ 1052 (void) strcpy(envhome, h); 1053 (void) sprintf(pathbuf, PRIVATE_CAP, envhome); 1054 ADD_TC(pathbuf, filecount); 1055 } 1056 } 1057 1058 /* 1059 * Probably /etc/termcap is a symlink to /usr/share/misc/termcap. 1060 * Avoid reading the same file twice. 1061 */ 1062 #if HAVE_LINK 1063 for (j = 0; j < filecount; j++) { 1064 bool omit = FALSE; 1065 if (stat(termpaths[j], &test_stat[j]) != 0 1066 || (test_stat[j].st_mode & S_IFMT) != S_IFREG) { 1067 omit = TRUE; 1068 } else { 1069 for (k = 0; k < j; k++) { 1070 if (test_stat[k].st_dev == test_stat[j].st_dev 1071 && test_stat[k].st_ino == test_stat[j].st_ino) { 1072 omit = TRUE; 1073 break; 1074 } 1075 } 1076 } 1077 if (omit) { 1078 T(("Path %s is a duplicate", termpaths[j])); 1079 for (k = j + 1; k < filecount; k++) { 1080 termpaths[k - 1] = termpaths[k]; 1081 test_stat[k - 1] = test_stat[k]; 1082 } 1083 --filecount; 1084 --j; 1085 } 1086 } 1087 #endif 1088 1089 /* parse the sources */ 1090 if (use_buffer) { 1091 _nc_set_source("TERMCAP"); 1092 1093 /* 1094 * We don't suppress warning messages here. The presumption is 1095 * that since it's just a single entry, they won't be a pain. 1096 */ 1097 _nc_read_entry_source((FILE *) 0, tc_buf, FALSE, FALSE, NULLHOOK); 1098 } else { 1099 int i; 1100 1101 for (i = 0; i < filecount; i++) { 1102 1103 T(("Looking for %s in %s", tn, termpaths[i])); 1104 if (_nc_access(termpaths[i], R_OK) == 0 1105 && (fp = fopen(termpaths[i], "r")) != (FILE *) 0) { 1106 _nc_set_source(termpaths[i]); 1107 1108 /* 1109 * Suppress warning messages. Otherwise you get 400 lines of 1110 * crap from archaic termcap files as ncurses complains about 1111 * all the obsolete capabilities. 1112 */ 1113 _nc_read_entry_source(fp, (char *) 0, FALSE, TRUE, NULLHOOK); 1114 1115 (void) fclose(fp); 1116 } 1117 } 1118 } 1119 if (copied != 0) 1120 free(copied); 1121 #endif /* USE_GETCAP */ 1122 1123 if (_nc_head == 0) 1124 return (TGETENT_ERR); 1125 1126 /* resolve all use references */ 1127 _nc_resolve_uses2(TRUE, FALSE); 1128 1129 /* find a terminal matching tn, if we can */ 1130 #if USE_GETCAP_CACHE 1131 if (getcwd(cwd_buf, sizeof(cwd_buf)) != 0) { 1132 _nc_set_writedir((char *) 0); /* note: this does a chdir */ 1133 #endif 1134 for_entry_list(ep) { 1135 if (_nc_name_match(ep->tterm.term_names, tn, "|:")) { 1136 /* 1137 * Make a local copy of the terminal capabilities, delinked 1138 * from the list. 1139 */ 1140 *tp = ep->tterm; 1141 _nc_delink_entry(_nc_head, &(ep->tterm)); 1142 free(ep); 1143 1144 /* 1145 * OK, now try to write the type to user's terminfo directory. 1146 * Next time he loads this, it will come through terminfo. 1147 * 1148 * Advantage: Second and subsequent fetches of this entry will 1149 * be very fast. 1150 * 1151 * Disadvantage: After the first time a termcap type is loaded 1152 * by its user, editing it in the /etc/termcap file, or in 1153 * TERMCAP, or in a local ~/.termcap, will be ineffective 1154 * unless the terminfo entry is explicitly removed. 1155 */ 1156 #if USE_GETCAP_CACHE 1157 (void) _nc_write_entry(tp); 1158 #endif 1159 found = TGETENT_YES; 1160 break; 1161 } 1162 } 1163 #if USE_GETCAP_CACHE 1164 chdir(cwd_buf); 1165 } 1166 #endif 1167 1168 return (found); 1169 } 1170 #else 1171 extern 1172 NCURSES_EXPORT(void) 1173 _nc_read_termcap(void); 1174 NCURSES_EXPORT(void) 1175 _nc_read_termcap(void) 1176 { 1177 } 1178 #endif /* PURE_TERMINFO */ 1179