1 /****************************************************************************
2 * Copyright (c) 1998-2004,2005 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 #include <term_entry.h>
61 MODULE_ID("$Id: read_termcap.c,v 1.67 2005/06/04 21:49:20 tom Exp $")
65 #if defined(__EMX__) || defined(__DJGPP__)
66 #define is_pathname(s) ((((s) != 0) && ((s)[0] == '/')) \
67 || (((s)[0] != 0) && ((s)[1] == ':')))
69 #define is_pathname(s) ((s) != 0 && (s)[0] == '/')
73 #define TC_UNRESOLVED -1
74 #define TC_NOT_FOUND -2
76 #define TC_REF_LOOP -4
78 static NCURSES_CONST char *
81 NCURSES_CONST char *result;
83 if (!use_terminfo_vars() || (result = getenv("TERMPATH")) == 0)
85 T(("TERMPATH is %s", result));
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. 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.
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
138 /* static char sccsid[] = "@(#)getcap.c 8.3 (Berkeley) 3/25/94"; */
142 #define MAX_RECURSION 32 /* maximum getent recursion */
144 static size_t topreclen; /* toprec length */
145 static char *toprec; /* Additional record specified by cgetset() */
146 static int gottoprec; /* Flag indicating retrieval of toprecord */
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.
154 _nc_cgetset(const char *ent)
157 FreeIfNeeded(toprec);
162 topreclen = strlen(ent);
163 if ((toprec = typeMalloc(char, topreclen + 1)) == 0) {
168 (void) strcpy(toprec, ent);
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.
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
181 * If (cap, '@') or (cap, terminator, '@') is found before (cap, terminator)
185 _nc_cgetcap(char *buf, const char *cap, int type)
187 register const char *cp;
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.
200 else if (*bp++ == ':')
205 * Try to match (cap, type) in buf.
207 for (cp = cap; *cp == *bp && *bp != '\0'; cp++, bp++)
214 if (*bp != '\0' && *bp != ':')
221 return (*bp == '@' ? 0 : bp);
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.
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
241 _nc_cgetent(char **buf, int *oline, char **db_array, const char *name)
245 return (_nc_getent(buf, &dummy, oline, 0, db_array, -1, name, 0, 0));
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=
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
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
266 #define DOALLOC(size) typeRealloc(char, size, record)
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 */
279 register char *r_end, *rp;
287 * Return with ``loop detected'' error if we've recurred more than
288 * MAX_RECURSION times.
290 if (depth > MAX_RECURSION)
291 return (TC_REF_LOOP);
294 * Check if we have a top record from cgetset().
296 if (depth == 0 && toprec != 0 && _nc_cgetmatch(toprec, name) == 0) {
297 if ((record = DOALLOC(topreclen + BFRAG)) == 0) {
301 (void) strcpy(record, toprec);
302 rp = record + topreclen + 1;
309 * Allocate first chunk of memory.
311 if ((record = DOALLOC(BFRAG)) == 0) {
315 rp = r_end = record + BFRAG;
319 * Loop through database array until finding the record.
321 for (current = in_array; db_array[current] != 0; current++) {
325 * Open database if not already open.
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. */
342 * Find the requested capability record ...
346 register char *b_end = buf;
347 register char *bp = buf;
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.
360 int first = lineno + 1;
363 * Read in a line implementing (\, newline)
371 n = read(fd, buf, sizeof(buf));
390 if (rp == record || *(rp - 1) != '\\')
396 * Enforce loop invariant: if no room
397 * left in record buffer, try to get
405 newsize = r_end - record + BFRAG;
406 record = DOALLOC(newsize);
413 r_end = record + newsize;
417 /* loop invariant lets us do this */
421 * If encountered eof check next file.
427 * Toss blank lines and comments.
429 if (*record == '\0' || *record == '#')
433 * See if this is the record we want ...
435 if (_nc_cgetmatch(record, name) == 0
437 || !_nc_nfcmp(nfield, record))) {
440 break; /* found it! */
449 return (TC_NOT_FOUND);
453 * Got the capability record, but now we have to expand all tc=name
454 * references in it ...
457 register char *newicap, *s;
458 register int newilen;
460 int diff, iret, tclen, oline;
461 char *icap, *scan, *tc, *tcstart, *tcend;
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.
472 tc_not_resolved = FALSE;
474 if ((tc = _nc_cgetcap(scan, "tc", '=')) == 0)
478 * Find end of tc=name and stomp on the trailing `:'
479 * (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) {
504 if (iret == TC_UNRESOLVED)
505 tc_not_resolved = TRUE;
506 /* couldn't resolve tc */
507 if (iret == TC_NOT_FOUND) {
510 tc_not_resolved = TRUE;
515 /* not interested in name field of tc'ed record */
517 while (*s != '\0' && *s++ != ':') ;
518 newilen -= s - newicap;
521 /* make sure interpolated record is `:'-terminated */
523 if (*(s - 1) != ':') {
524 *s = ':'; /* overwrite NUL with : */
529 * Make sure there's enough room to insert the
532 diff = newilen - tclen;
533 if (diff >= r_end - rp) {
534 unsigned pos, tcpos, tcposend;
538 newsize = r_end - record + diff + BFRAG;
539 tcpos = tcstart - record;
540 tcposend = tcend - record;
541 record = DOALLOC(newsize);
549 r_end = record + newsize;
551 tcstart = record + tcpos;
552 tcend = record + tcposend;
556 * Insert tc'ed record into our record.
558 s = tcstart + newilen;
559 memmove(s, tcend, (size_t) (rp - tcend));
560 memmove(tcstart, newicap, (size_t) newilen);
565 * Start scan on `:' so next cgetcap works properly
566 * (cgetcap always skips first field).
573 * Close file (if we opened it), give back any extra memory, and
574 * return capability, length and success.
578 *len = rp - record - 1; /* don't count NUL */
580 if ((record = DOALLOC((size_t) (rp - record))) == 0) {
588 return (TC_UNRESOLVED);
593 * Cgetmatch will return 0 if name is one of the names of the capability
594 * record buf, -1 if not.
597 _nc_cgetmatch(char *buf, const char *name)
599 register const char *np;
603 * Start search at beginning of record.
608 * Try to match a record name.
613 if (*bp == '|' || *bp == ':' || *bp == '\0')
617 } else if (*bp++ != *np++) {
623 * Match failed, skip to next name in record.
625 bp--; /* a '|' or ':' may have stopped the match */
627 if (*bp == '\0' || *bp == ':')
628 return (-1); /* match failed totally */
629 else if (*bp++ == '|')
630 break; /* found next name */
636 * Compare name field of record.
639 _nc_nfcmp(const char *nf, char *rec)
644 for (cp = rec; *cp != ':'; cp++) ;
648 ret = strcmp(nf, rec);
653 #endif /* HAVE_BSD_CGETENT */
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
661 #define USE_BSD_TGETENT 1
665 * Copyright (c) 1980, 1993
666 * The Regents of the University of California. All rights reserved.
668 * Redistribution and use in source and binary forms, with or without
669 * modification, are permitted provided that the following conditions
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.
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
697 /* static char sccsid[] = "@(#)termcap.c 8.1 (Berkeley) 6/4/93" */
699 #define PBUFSIZ 512 /* max length of filename path */
700 #define PVECSIZ 32 /* max number of names in path */
701 #define TBUFSIZ (2048*2)
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 ':'.
710 get_tc_token(char **srcp, int *endp)
718 for (s = base = *srcp; *s != '\0';) {
723 } else if (*s++ == '\n') {
724 while (isspace(UChar(*s)))
729 } else if (ch == ':') {
738 } else if (isgraph(UChar(ch))) {
743 /* malformed entry may end without a ':' */
744 if (tok == 0 && found) {
752 copy_tc_token(char *dst, const char *src, size_t len)
756 while ((ch = *src++) != '\0') {
757 if (ch == '\\' && *src == '\n') {
758 while (isspace(UChar(*src)))
772 * Get an entry for terminal name in buffer bp from the termcap file.
775 _nc_tgetent(char *bp, char **sourcename, int *lineno, const char *name)
777 static char *the_source;
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;
795 cp = use_terminfo_vars()? getenv("TERMCAP") : NULL;
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.
806 _nc_str_init(&desc, pathbuf, sizeof(pathbuf));
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);
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 */
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());
826 } else { /* user-defined name in TERMCAP */
827 _nc_safe_strcat(&desc, cp); /* still can be tokenized */
830 *fname++ = pathbuf; /* tokenize path into vector of names */
832 if (*p == ' ' || *p == NCURSES_PATHSEP) {
835 if (*p != ' ' && *p != NCURSES_PATHSEP)
840 if (fname >= pathvec + PVECSIZ) {
846 *fname = 0; /* mark end of vector */
847 if (is_pathname(cp)) {
848 if (_nc_cgetset(cp) < 0) {
853 i = _nc_cgetent(&dummy, lineno, pathvec, name);
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)
867 while (!endflag && (tok = get_tc_token(&ps, &endflag)) != 0) {
870 for (n = 1; n < count; n++) {
878 if (ignore != TRUE) {
880 pd = copy_tc_token(pd, tok, TBUFSIZ - (2 + pd - bp));
892 FreeIfNeeded(the_source);
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).
900 if ((the_source = strdup(pathvec[i])) != 0)
901 *sourcename = the_source;
906 #endif /* USE_BSD_TGETENT */
907 #endif /* USE_GETCAP */
912 * Add a filename to the list in 'termpaths[]', checking that we really have
913 * a right to open the file.
917 add_tc(char *termpaths[], char *path, int count)
919 char *save = strchr(path, NCURSES_PATHSEP);
923 && _nc_access(path, R_OK) == 0) {
924 termpaths[count++] = path;
925 T(("Adding termpath %s", path));
927 termpaths[count] = 0;
929 *save = NCURSES_PATHSEP;
932 #define ADD_TC(path, count) filecount = add_tc(termpaths, path, count)
933 #endif /* !USE_GETCAP */
936 _nc_read_termcap_entry(const char *const tn, TERMTYPE *const tp)
941 char cwd_buf[PATH_MAX];
944 char *p, tc[TBUFSIZ];
948 T(("read termcap entry for %s", tn));
951 || strcmp(tn, ".") == 0
952 || strcmp(tn, "..") == 0
953 || _nc_pathlast(tn) != 0) {
954 T(("illegal or missing entry name '%s'", tn));
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");
965 /* we're using getcap(3) */
966 if (_nc_tgetent(tc, &source, &lineno, tn) < 0)
969 _nc_curr_line = lineno;
970 _nc_set_source(source);
972 _nc_read_entry_source((FILE *) 0, tc, FALSE, FALSE, NULLHOOK);
975 * Here is what the 4.4BSD termcap(3) page prescribes:
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
988 * It goes on to state:
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.
994 * However, this restriction is relaxed in ncurses; tc references to
995 * previous files are permitted.
997 * This routine returns 1 if an entry is found, 0 if not found, and -1 if
998 * the database is not accessible.
1001 char *tc, *termpaths[MAXPATHS];
1004 bool use_buffer = FALSE;
1007 char pathbuf[PATH_MAX];
1010 struct stat test_stat[MAXPATHS];
1012 termpaths[filecount] = 0;
1013 if (use_terminfo_vars() && (tc = getenv("TERMCAP")) != 0) {
1014 if (is_pathname(tc)) { /* interpret as a filename */
1017 } else if (_nc_name_match(tc, tn, "|:")) { /* treat as a capability file */
1019 (void) sprintf(tc_buf, "%.*s\n", (int) sizeof(tc_buf) - 2, tc);
1024 if (normal) { /* normal case */
1025 char envhome[PATH_MAX], *h;
1027 copied = strdup(get_termpath());
1028 for (cp = copied; *cp; cp++) {
1029 if (*cp == NCURSES_PATHSEP)
1031 else if (cp == copied || cp[-1] == '\0') {
1032 ADD_TC(cp, filecount);
1036 #define PRIVATE_CAP "%s/.termcap"
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);
1048 * Probably /etc/termcap is a symlink to /usr/share/misc/termcap.
1049 * Avoid reading the same file twice.
1052 for (j = 0; j < filecount; j++) {
1054 if (stat(termpaths[j], &test_stat[j]) != 0
1055 || (test_stat[j].st_mode & S_IFMT) != S_IFREG) {
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) {
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];
1078 /* parse the sources */
1080 _nc_set_source("TERMCAP");
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.
1086 _nc_read_entry_source((FILE *) 0, tc_buf, FALSE, FALSE, NULLHOOK);
1090 for (i = 0; i < filecount; i++) {
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]);
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.
1102 _nc_read_entry_source(fp, (char *) 0, FALSE, TRUE, NULLHOOK);
1110 #endif /* USE_GETCAP */
1115 /* resolve all use references */
1116 _nc_resolve_uses2(TRUE, FALSE);
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 */
1123 for_entry_list(ep) {
1124 if (_nc_name_match(ep->tterm.term_names, tn, "|:")) {
1126 * Make a local copy of the terminal capabilities, delinked
1130 _nc_delink_entry(_nc_head, &(ep->tterm));
1134 * OK, now try to write the type to user's terminfo directory.
1135 * Next time he loads this, it will come through terminfo.
1137 * Advantage: Second and subsequent fetches of this entry will
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.
1145 #if USE_GETCAP_CACHE
1146 (void) _nc_write_entry(tp);
1152 #if USE_GETCAP_CACHE
1161 NCURSES_EXPORT(void)
1162 _nc_read_termcap(void);
1163 NCURSES_EXPORT(void)
1164 _nc_read_termcap(void)
1167 #endif /* PURE_TERMINFO */