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