1 /****************************************************************************
2 * Copyright (c) 1998-2010,2011 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
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 ****************************************************************************/
36 * Termcap compatibility support
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.
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.
53 #include <curses.priv.h>
56 #include <sys/types.h>
59 MODULE_ID("$Id: read_termcap.c,v 1.81 2011/08/07 18:56:35 tom Exp $")
64 #define TC_NOT_FOUND -1
66 #define TC_REF_LOOP -3
67 #define TC_UNRESOLVED -4 /* this is not returned by BSD cgetent */
69 static NCURSES_CONST char *
72 NCURSES_CONST char *result;
74 if (!use_terminfo_vars() || (result = getenv("TERMPATH")) == 0)
76 T(("TERMPATH is %s", result));
82 * getcap(), cgetent(), etc., are BSD functions. A copy of those was added to
83 * this file in November 1995, derived from the BSD4.4 Lite sources.
85 * The initial adaptation uses 518 lines from that source.
86 * The current source (in 2009) uses 183 lines of BSD4.4 Lite (441 ignoring
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
97 static int _nc_cgetmatch(char *, const char *);
98 static int _nc_getent(char **, unsigned *, int *, int, char **, int, const char
100 static int _nc_nfcmp(const char *, char *);
103 * Copyright (c) 1992, 1993
104 * The Regents of the University of California. All rights reserved.
106 * This code is derived from software contributed to Berkeley by
107 * Casey Leedom of Lawrence Livermore National Laboratory.
109 * Redistribution and use in source and binary forms, with or without
110 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
118 * may be used to endorse or promote products derived from this software
119 * without specific prior written permission.
121 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
122 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
123 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
124 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
125 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
126 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
127 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
128 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
129 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
130 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
134 /* static char sccsid[] = "@(#)getcap.c 8.3 (Berkeley) 3/25/94"; */
138 #define MAX_RECURSION 32 /* maximum getent recursion */
140 static size_t topreclen; /* toprec length */
141 static char *toprec; /* Additional record specified by cgetset() */
142 static int gottoprec; /* Flag indicating retrieval of toprecord */
145 * Cgetset() allows the addition of a user specified buffer to be added to the
146 * database array, in effect "pushing" the buffer on top of the virtual
147 * database. 0 is returned on success, -1 on failure.
150 _nc_cgetset(const char *ent)
153 FreeIfNeeded(toprec);
158 topreclen = strlen(ent);
159 if ((toprec = typeMalloc(char, topreclen + 1)) == 0) {
164 (void) strcpy(toprec, ent);
169 * Cgetcap searches the capability record buf for the capability cap with type
170 * `type'. A pointer to the value of cap is returned on success, 0 if the
171 * requested capability couldn't be found.
173 * Specifying a type of ':' means that nothing should follow cap (:cap:). In
174 * this case a pointer to the terminating ':' or NUL will be returned if cap is
177 * If (cap, '@') or (cap, terminator, '@') is found before (cap, terminator)
181 _nc_cgetcap(char *buf, const char *cap, int type)
183 register const char *cp;
189 * Skip past the current capability field - it's either the
190 * name field if this is the first time through the loop, or
191 * the remainder of a field whose name failed to match cap.
196 else if (*bp++ == ':')
201 * Try to match (cap, type) in buf.
203 for (cp = cap; *cp == *bp && *bp != '\0'; cp++, bp++)
210 if (*bp != '\0' && *bp != ':')
217 return (*bp == '@' ? 0 : bp);
223 * Cgetent extracts the capability record name from the NULL terminated file
224 * array db_array and returns a pointer to a malloc'd copy of it in buf. Buf
225 * must be retained through all subsequent calls to cgetcap, cgetnum, cgetflag,
226 * and cgetstr, but may then be freed.
230 * positive # on success (i.e., the index in db_array)
231 * TC_NOT_FOUND if the requested record couldn't be found
232 * TC_SYS_ERR if a system error was encountered (e.g.,couldn't open a file)
233 * TC_REF_LOOP if a potential reference loop is detected
234 * TC_UNRESOLVED if we had too many recurrences to resolve
237 _nc_cgetent(char **buf, int *oline, char **db_array, const char *name)
241 return (_nc_getent(buf, &dummy, oline, 0, db_array, -1, name, 0, 0));
245 * Getent implements the functions of cgetent. If fd is non-negative,
246 * *db_array has already been opened and fd is the open file descriptor. We
247 * do this to save time and avoid using up file descriptors for tc=
250 * Getent returns the same success/failure codes as cgetent. On success, a
251 * pointer to a malloc'd capability record with all tc= capabilities fully
252 * expanded and its length (not including trailing ASCII NUL) are left in
256 * + Allocate memory incrementally as needed in chunks of size BFRAG
257 * for capability buffer.
258 * + Recurse for each tc=name and interpolate result. Stop when all
259 * names interpolated, a name can't be found, or depth exceeds
262 #define DOALLOC(size) typeRealloc(char, size, record)
265 char **cap, /* termcap-content */
266 unsigned *len, /* length, needed for recursion */
267 int *beginning, /* line-number at match */
268 int in_array, /* index in 'db_array[] */
269 char **db_array, /* list of files to search */
275 register char *r_end, *rp;
283 * Return with ``loop detected'' error if we've recurred more than
284 * MAX_RECURSION times.
286 if (depth > MAX_RECURSION)
287 return (TC_REF_LOOP);
290 * Check if we have a top record from cgetset().
292 if (depth == 0 && toprec != 0 && _nc_cgetmatch(toprec, name) == 0) {
293 if ((record = DOALLOC(topreclen + BFRAG)) == 0) {
297 (void) strcpy(record, toprec);
298 rp = record + topreclen + 1;
305 * Allocate first chunk of memory.
307 if ((record = DOALLOC(BFRAG)) == 0) {
311 rp = r_end = record + BFRAG;
315 * Loop through database array until finding the record.
317 for (current = in_array; db_array[current] != 0; current++) {
321 * Open database if not already open.
324 (void) lseek(fd, (off_t) 0, SEEK_SET);
325 } else if ((_nc_access(db_array[current], R_OK) < 0)
326 || (fd = open(db_array[current], O_RDONLY, 0)) < 0) {
327 /* No error on unfound file. */
338 * Find the requested capability record ...
342 register char *b_end = buf;
343 register char *bp = buf;
348 * There is always room for one more character in record.
349 * R_end always points just past end of record.
350 * Rp always points just past last character in record.
351 * B_end always points just past last character in buf.
352 * Bp always points at next character in buf.
356 int first = lineno + 1;
359 * Read in a line implementing (\, newline)
367 n = read(fd, buf, sizeof(buf));
386 if (rp == record || *(rp - 1) != '\\')
392 * Enforce loop invariant: if no room
393 * left in record buffer, try to get
401 newsize = r_end - record + BFRAG;
402 record = DOALLOC(newsize);
409 r_end = record + newsize;
413 /* loop invariant lets us do this */
417 * If encountered eof check next file.
423 * Toss blank lines and comments.
425 if (*record == '\0' || *record == '#')
429 * See if this is the record we want ...
431 if (_nc_cgetmatch(record, name) == 0
433 || !_nc_nfcmp(nfield, record))) {
436 break; /* found it! */
446 return (TC_NOT_FOUND);
451 * Got the capability record, but now we have to expand all tc=name
452 * references in it ...
455 register char *newicap, *s;
456 register int newilen;
458 int diff, iret, tclen, oline;
459 char *icap = 0, *scan, *tc, *tcstart, *tcend;
463 * There is room for one more character in record.
464 * R_end points just past end of record.
465 * Rp points just past last character in record.
466 * Scan points at remainder of record that needs to be
467 * scanned for tc=name constructs.
470 tc_not_resolved = FALSE;
472 if ((tc = _nc_cgetcap(scan, "tc", '=')) == 0) {
477 * Find end of tc=name and stomp on the trailing `:'
478 * (if present) so we can use it to call ourselves.
492 iret = _nc_getent(&icap, &ilen, &oline, current, db_array, fd,
494 newicap = icap; /* Put into a register. */
496 if (iret != TC_SUCCESS) {
498 if (iret < TC_NOT_FOUND) {
505 if (iret == TC_UNRESOLVED) {
506 tc_not_resolved = TRUE;
507 /* couldn't resolve tc */
508 } else if (iret == TC_NOT_FOUND) {
511 tc_not_resolved = TRUE;
516 /* not interested in name field of tc'ed record */
518 while (*s != '\0' && *s++ != ':') ;
519 newilen -= s - newicap;
522 /* make sure interpolated record is `:'-terminated */
524 if (*(s - 1) != ':') {
525 *s = ':'; /* overwrite NUL with : */
530 * Make sure there's enough room to insert the
533 diff = newilen - tclen;
534 if (diff >= r_end - rp) {
535 unsigned pos, tcpos, tcposend;
539 newsize = r_end - record + diff + BFRAG;
540 tcpos = tcstart - record;
541 tcposend = tcend - record;
542 record = DOALLOC(newsize);
550 r_end = record + newsize;
552 tcstart = record + tcpos;
553 tcend = record + tcposend;
557 * Insert tc'ed record into our record.
559 s = tcstart + newilen;
560 memmove(s, tcend, (size_t) (rp - tcend));
561 memmove(tcstart, newicap, (size_t) newilen);
566 * Start scan on `:' so next cgetcap works properly
567 * (cgetcap always skips first field).
574 * Close file (if we opened it), give back any extra memory, and
575 * return capability, length and success.
579 *len = rp - record - 1; /* don't count NUL */
581 if ((record = DOALLOC((size_t) (rp - record))) == 0) {
588 if (tc_not_resolved) {
589 return (TC_UNRESOLVED);
595 * Cgetmatch will return 0 if name is one of the names of the capability
596 * record buf, -1 if not.
599 _nc_cgetmatch(char *buf, const char *name)
601 register const char *np;
605 * Start search at beginning of record.
610 * Try to match a record name.
615 if (*bp == '|' || *bp == ':' || *bp == '\0')
619 } else if (*bp++ != *np++) {
625 * Match failed, skip to next name in record.
627 bp--; /* a '|' or ':' may have stopped the match */
629 if (*bp == '\0' || *bp == ':')
630 return (-1); /* match failed totally */
631 else if (*bp++ == '|')
632 break; /* found next name */
638 * Compare name field of record.
641 _nc_nfcmp(const char *nf, char *rec)
646 for (cp = rec; *cp != ':'; cp++) ;
650 ret = strcmp(nf, rec);
655 #endif /* HAVE_BSD_CGETENT */
658 * Since ncurses provides its own 'tgetent()', we cannot use the native one.
659 * So we reproduce the logic to get down to cgetent() -- or our cut-down
660 * version of that -- to circumvent the problem of configuring against the
663 #define USE_BSD_TGETENT 1
667 * Copyright (c) 1980, 1993
668 * The Regents of the University of California. All rights reserved.
670 * Redistribution and use in source and binary forms, with or without
671 * modification, are permitted provided that the following conditions
673 * 1. Redistributions of source code must retain the above copyright
674 * notice, this list of conditions and the following disclaimer.
675 * 2. Redistributions in binary form must reproduce the above copyright
676 * notice, this list of conditions and the following disclaimer in the
677 * documentation and/or other materials provided with the distribution.
678 * 3. All advertising materials mentioning features or use of this software
679 * must display the following acknowledgment:
680 * This product includes software developed by the University of
681 * California, Berkeley and its contributors.
682 * 4. Neither the name of the University nor the names of its contributors
683 * may be used to endorse or promote products derived from this software
684 * without specific prior written permission.
686 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
687 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
688 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
689 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
690 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
691 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
692 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
693 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
694 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
695 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
699 /* static char sccsid[] = "@(#)termcap.c 8.1 (Berkeley) 6/4/93" */
701 #define PBUFSIZ 512 /* max length of filename path */
702 #define PVECSIZ 32 /* max number of names in path */
703 #define TBUFSIZ (2048*2)
708 * On entry, srcp points to a non ':' character which is the beginning of the
709 * token, if any. We'll try to return a string that doesn't end with a ':'.
712 get_tc_token(char **srcp, int *endp)
720 for (s = base = *srcp; *s != '\0';) {
725 } else if (*s++ == '\n') {
726 while (isspace(UChar(*s)))
731 } else if (ch == ':') {
740 } else if (isgraph(UChar(ch))) {
745 /* malformed entry may end without a ':' */
746 if (tok == 0 && found) {
754 copy_tc_token(char *dst, const char *src, size_t len)
758 while ((ch = *src++) != '\0') {
759 if (ch == '\\' && *src == '\n') {
760 while (isspace(UChar(*src)))
774 * Get an entry for terminal name in buffer bp from the termcap file.
777 _nc_tgetent(char *bp, char **sourcename, int *lineno, const char *name)
779 static char *the_source;
787 char pathbuf[PBUFSIZ]; /* holds raw path of filenames */
788 CGETENT_CONST char *pathvec[PVECSIZ]; /* point to names in pathbuf */
789 CGETENT_CONST char **pvec; /* holds usable tail of path vector */
790 NCURSES_CONST char *termpath;
798 cp = use_terminfo_vars()? getenv("TERMCAP") : NULL;
801 * TERMCAP can have one of two things in it. It can be the name of a file
802 * to use instead of /etc/termcap. In this case it better start with a
803 * "/". Or it can be an entry to use so we don't have to read the file.
804 * In this case it has to already have the newlines crunched out. If
805 * TERMCAP does not hold a file name then a path of names is searched
806 * instead. The path is found in the TERMPATH variable, or becomes
807 * "$HOME/.termcap /etc/termcap" if no TERMPATH exists.
809 _nc_str_init(&desc, pathbuf, sizeof(pathbuf));
811 _nc_safe_strcpy(&desc, get_termpath());
812 } else if (!_nc_is_abs_path(cp)) { /* TERMCAP holds an entry */
813 if ((termpath = get_termpath()) != 0) {
814 _nc_safe_strcat(&desc, termpath);
818 if ((home = getenv("HOME")) != 0 && *home != '\0'
819 && strchr(home, ' ') == 0
820 && strlen(home) < sizeof(temp) - 10) { /* setup path */
821 sprintf(temp, "%s/", home); /* $HOME first */
823 /* if no $HOME look in current directory */
824 strcat(temp, ".termcap");
825 _nc_safe_strcat(&desc, temp);
826 _nc_safe_strcat(&desc, " ");
827 _nc_safe_strcat(&desc, get_termpath());
829 } else { /* user-defined name in TERMCAP */
830 _nc_safe_strcat(&desc, cp); /* still can be tokenized */
833 *fname++ = pathbuf; /* tokenize path into vector of names */
835 if (*p == ' ' || *p == NCURSES_PATHSEP) {
838 if (*p != ' ' && *p != NCURSES_PATHSEP)
843 if (fname >= pathvec + PVECSIZ) {
849 *fname = 0; /* mark end of vector */
850 #if !HAVE_BSD_CGETENT
851 (void) _nc_cgetset(0);
853 if (_nc_is_abs_path(cp)) {
854 if (_nc_cgetset(cp) < 0) {
859 i = _nc_cgetent(&dummy, lineno, pathvec, name);
861 /* ncurses' termcap-parsing routines cannot handle multiple adjacent
862 * empty fields, and mistakenly use the last valid cap entry instead of
863 * the first (breaks tc= includes)
873 while (!endflag && (tok = get_tc_token(&ps, &endflag)) != 0) {
876 for (n = 1; n < count; n++) {
884 if (ignore != TRUE) {
886 pd = copy_tc_token(pd, tok, TBUFSIZ - (2 + pd - bp));
898 FreeIfNeeded(the_source);
901 /* This is not related to the BSD cgetent(), but to fake up a suitable
902 * filename for ncurses' error reporting. (If we are not using BSD
903 * cgetent, then it is the actual filename).
909 _nc_str_init(&desc, temp, sizeof(temp));
910 _nc_safe_strcpy(&desc, pathvec[i]);
911 _nc_safe_strcat(&desc, ".db");
912 if (_nc_access(temp, R_OK) == 0) {
913 _nc_safe_strcpy(&desc, pathvec[i]);
915 if ((the_source = strdup(temp)) != 0)
916 *sourcename = the_source;
918 if ((the_source = strdup(pathvec[i])) != 0)
919 *sourcename = the_source;
925 #endif /* USE_BSD_TGETENT */
926 #endif /* USE_GETCAP */
931 * Add a filename to the list in 'termpaths[]', checking that we really have
932 * a right to open the file.
936 add_tc(char *termpaths[], char *path, int count)
938 char *save = strchr(path, NCURSES_PATHSEP);
942 && _nc_access(path, R_OK) == 0) {
943 termpaths[count++] = path;
944 T(("Adding termpath %s", path));
946 termpaths[count] = 0;
948 *save = NCURSES_PATHSEP;
951 #define ADD_TC(path, count) filecount = add_tc(termpaths, path, count)
952 #endif /* !USE_GETCAP */
955 _nc_read_termcap_entry(const char *const tn, TERMTYPE *const tp)
957 int found = TGETENT_NO;
960 char cwd_buf[PATH_MAX];
963 char *p, tc[TBUFSIZ];
968 T(("read termcap entry for %s", tn));
971 || strcmp(tn, ".") == 0
972 || strcmp(tn, "..") == 0
973 || _nc_pathlast(tn) != 0) {
974 T(("illegal or missing entry name '%s'", tn));
978 if (use_terminfo_vars() && (p = getenv("TERMCAP")) != 0
979 && !_nc_is_abs_path(p) && _nc_name_match(p, tn, "|:")) {
980 /* TERMCAP holds a termcap entry */
981 strncpy(tc, p, sizeof(tc) - 1);
982 tc[sizeof(tc) - 1] = '\0';
983 _nc_set_source("TERMCAP");
985 /* we're using getcap(3) */
986 if ((status = _nc_tgetent(tc, &source, &lineno, tn)) < 0)
987 return (status == TC_NOT_FOUND ? TGETENT_NO : TGETENT_ERR);
989 _nc_curr_line = lineno;
990 _nc_set_source(source);
992 _nc_read_entry_source((FILE *) 0, tc, FALSE, TRUE, NULLHOOK);
995 * Here is what the 4.4BSD termcap(3) page prescribes:
997 * It will look in the environment for a TERMCAP variable. If found, and
998 * the value does not begin with a slash, and the terminal type name is the
999 * same as the environment string TERM, the TERMCAP string is used instead
1000 * of reading a termcap file. If it does begin with a slash, the string is
1001 * used as a path name of the termcap file to search. If TERMCAP does not
1002 * begin with a slash and name is different from TERM, tgetent() searches
1003 * the files $HOME/.termcap and /usr/share/misc/termcap, in that order,
1004 * unless the environment variable TERMPATH exists, in which case it
1005 * specifies a list of file pathnames (separated by spaces or colons) to be
1008 * It goes on to state:
1010 * Whenever multiple files are searched and a tc field occurs in the
1011 * requested entry, the entry it names must be found in the same file or
1012 * one of the succeeding files.
1014 * However, this restriction is relaxed in ncurses; tc references to
1015 * previous files are permitted.
1017 * This routine returns 1 if an entry is found, 0 if not found, and -1 if
1018 * the database is not accessible.
1021 char *tc, *termpaths[MAXPATHS];
1024 bool use_buffer = FALSE;
1027 char pathbuf[PATH_MAX];
1030 struct stat test_stat[MAXPATHS];
1032 termpaths[filecount] = 0;
1033 if (use_terminfo_vars() && (tc = getenv("TERMCAP")) != 0) {
1034 if (_nc_is_abs_path(tc)) { /* interpret as a filename */
1037 } else if (_nc_name_match(tc, tn, "|:")) { /* treat as a capability file */
1039 (void) sprintf(tc_buf, "%.*s\n", (int) sizeof(tc_buf) - 2, tc);
1044 if (normal) { /* normal case */
1045 char envhome[PATH_MAX], *h;
1047 copied = strdup(get_termpath());
1048 for (cp = copied; *cp; cp++) {
1049 if (*cp == NCURSES_PATHSEP)
1051 else if (cp == copied || cp[-1] == '\0') {
1052 ADD_TC(cp, filecount);
1056 #define PRIVATE_CAP "%s/.termcap"
1058 if (use_terminfo_vars() && (h = getenv("HOME")) != NULL && *h != '\0'
1059 && (strlen(h) + sizeof(PRIVATE_CAP)) < PATH_MAX) {
1060 /* user's .termcap, if any, should override it */
1061 (void) strcpy(envhome, h);
1062 (void) sprintf(pathbuf, PRIVATE_CAP, envhome);
1063 ADD_TC(pathbuf, filecount);
1068 * Probably /etc/termcap is a symlink to /usr/share/misc/termcap.
1069 * Avoid reading the same file twice.
1072 for (j = 0; j < filecount; j++) {
1074 if (stat(termpaths[j], &test_stat[j]) != 0
1075 || !S_ISREG(test_stat[j].st_mode)) {
1078 for (k = 0; k < j; k++) {
1079 if (test_stat[k].st_dev == test_stat[j].st_dev
1080 && test_stat[k].st_ino == test_stat[j].st_ino) {
1087 T(("Path %s is a duplicate", termpaths[j]));
1088 for (k = j + 1; k < filecount; k++) {
1089 termpaths[k - 1] = termpaths[k];
1090 test_stat[k - 1] = test_stat[k];
1098 /* parse the sources */
1100 _nc_set_source("TERMCAP");
1103 * We don't suppress warning messages here. The presumption is
1104 * that since it's just a single entry, they won't be a pain.
1106 _nc_read_entry_source((FILE *) 0, tc_buf, FALSE, FALSE, NULLHOOK);
1110 for (i = 0; i < filecount; i++) {
1112 T(("Looking for %s in %s", tn, termpaths[i]));
1113 if (_nc_access(termpaths[i], R_OK) == 0
1114 && (fp = fopen(termpaths[i], "r")) != (FILE *) 0) {
1115 _nc_set_source(termpaths[i]);
1118 * Suppress warning messages. Otherwise you get 400 lines of
1119 * crap from archaic termcap files as ncurses complains about
1120 * all the obsolete capabilities.
1122 _nc_read_entry_source(fp, (char *) 0, FALSE, TRUE, NULLHOOK);
1130 #endif /* USE_GETCAP */
1133 return (TGETENT_ERR);
1135 /* resolve all use references */
1136 _nc_resolve_uses2(TRUE, FALSE);
1138 /* find a terminal matching tn, if we can */
1139 #if USE_GETCAP_CACHE
1140 if (getcwd(cwd_buf, sizeof(cwd_buf)) != 0) {
1141 _nc_set_writedir((char *) 0); /* note: this does a chdir */
1143 for_entry_list(ep) {
1144 if (_nc_name_match(ep->tterm.term_names, tn, "|:")) {
1146 * Make a local copy of the terminal capabilities, delinked
1150 _nc_delink_entry(_nc_head, &(ep->tterm));
1154 * OK, now try to write the type to user's terminfo directory.
1155 * Next time he loads this, it will come through terminfo.
1157 * Advantage: Second and subsequent fetches of this entry will
1160 * Disadvantage: After the first time a termcap type is loaded
1161 * by its user, editing it in the /etc/termcap file, or in
1162 * TERMCAP, or in a local ~/.termcap, will be ineffective
1163 * unless the terminfo entry is explicitly removed.
1165 #if USE_GETCAP_CACHE
1166 (void) _nc_write_entry(tp);
1168 found = TGETENT_YES;
1172 #if USE_GETCAP_CACHE
1181 NCURSES_EXPORT(void)
1182 _nc_read_termcap(void);
1183 NCURSES_EXPORT(void)
1184 _nc_read_termcap(void)
1187 #endif /* PURE_TERMINFO */