1 /****************************************************************************
2 * Copyright 2018-2019,2020 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 ****************************************************************************/
37 * Termcap compatibility support
39 * If your OS integrator didn't install a terminfo database, you can call
40 * _nc_read_termcap_entry() to support reading and translating capabilities
41 * from the system termcap file. This is a kludge; it will bulk up and slow
42 * down every program that uses ncurses, and translated termcap entries cannot
43 * use full terminfo capabilities. Don't use it unless you absolutely have to;
44 * instead, get your system people to run tic(1) from root on the terminfo
45 * master included with ncurses to translate it into a terminfo database.
47 * If USE_GETCAP is enabled, we use what is effectively a copy of the 4.4BSD
48 * getcap code to fetch entries. There are disadvantages to this; mainly that
49 * getcap(3) does its own resolution, meaning that entries read in in this way
50 * can't reference the terminfo tree. The only thing it buys is faster startup
51 * time, getcap(3) is much faster than our tic parser.
54 #include <curses.priv.h>
57 #include <sys/types.h>
60 MODULE_ID("$Id: read_termcap.c,v 1.98 2020/02/02 23:34:34 tom Exp $")
65 #define TC_NOT_FOUND -1
67 #define TC_REF_LOOP -3
68 #define TC_UNRESOLVED -4 /* this is not returned by BSD cgetent */
75 if (!use_terminfo_vars() || (result = getenv("TERMPATH")) == 0)
77 TR(TRACE_DATABASE, ("TERMPATH is %s", result));
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.
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
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
98 static int _nc_cgetmatch(char *, const char *);
99 static int _nc_getent(char **, unsigned *, int *, int, char **, int, const char
101 static int _nc_nfcmp(const char *, char *);
104 * Copyright (c) 1992, 1993
105 * The Regents of the University of California. All rights reserved.
107 * This code is derived from software contributed to Berkeley by
108 * Casey Leedom of Lawrence Livermore National Laboratory.
110 * Redistribution and use in source and binary forms, with or without
111 * modification, are permitted provided that the following conditions
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.
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
135 /* static char sccsid[] = "@(#)getcap.c 8.3 (Berkeley) 3/25/94"; */
139 #define MAX_RECURSION 32 /* maximum getent recursion */
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 */
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.
151 _nc_cgetset(const char *ent)
154 FreeIfNeeded(toprec);
159 topreclen = strlen(ent);
160 if ((toprec = typeMalloc(char, topreclen + 1)) == 0) {
165 _nc_STRCPY(toprec, ent, topreclen);
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.
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
178 * If (cap, '@') or (cap, terminator, '@') is found before (cap, terminator)
182 _nc_cgetcap(char *buf, const char *cap, int type)
184 register const char *cp;
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.
197 else if (*bp++ == ':')
202 * Try to match (cap, type) in buf.
204 for (cp = cap; *cp == *bp && *bp != '\0'; cp++, bp++)
211 if (*bp != '\0' && *bp != ':')
218 return (*bp == '@' ? 0 : bp);
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.
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
238 _nc_cgetent(char **buf, int *oline, char **db_array, const char *name)
242 return (_nc_getent(buf, &dummy, oline, 0, db_array, -1, name, 0, 0));
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=
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
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
263 #define DOALLOC(size) typeRealloc(char, size, record)
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 */
276 register char *r_end, *rp;
284 * Return with ``loop detected'' error if we've recurred more than
285 * MAX_RECURSION times.
287 if (depth > MAX_RECURSION)
288 return (TC_REF_LOOP);
291 * Check if we have a top record from cgetset().
293 if (depth == 0 && toprec != 0 && _nc_cgetmatch(toprec, name) == 0) {
294 if ((record = DOALLOC(topreclen + BFRAG)) == 0) {
298 _nc_STRCPY(record, toprec, topreclen + BFRAG);
299 rp = record + topreclen + 1;
306 * Allocate first chunk of memory.
308 if ((record = DOALLOC(BFRAG)) == 0) {
312 rp = r_end = record + BFRAG;
316 * Loop through database array until finding the record.
318 for (current = in_array; db_array[current] != 0; current++) {
322 * Open database if not already open.
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. */
339 * Find the requested capability record ...
343 register char *b_end = buf;
344 register char *bp = buf;
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.
357 int first = lineno + 1;
360 * Read in a line implementing (\, newline)
368 n = (int) read(fd, buf, sizeof(buf));
388 * Unlike BSD 4.3, this ignores a backslash at the
389 * end of a comment-line. That makes it consistent
390 * with the rest of ncurses -TD
394 || *(rp - 1) != '\\')
400 * Enforce loop invariant: if no room
401 * left in record buffer, try to get
408 pos = (unsigned) (rp - record);
409 newsize = (size_t) (r_end - record + BFRAG);
410 record = DOALLOC(newsize);
417 r_end = record + newsize;
421 /* loop invariant lets us do this */
425 * If encountered eof check next file.
431 * Toss blank lines and comments.
433 if (*record == '\0' || *record == '#')
437 * See if this is the record we want ...
439 if (_nc_cgetmatch(record, name) == 0
441 || !_nc_nfcmp(nfield, record))) {
444 break; /* found it! */
454 return (TC_NOT_FOUND);
459 * Got the capability record, but now we have to expand all tc=name
460 * references in it ...
463 register char *newicap, *s;
464 register int newilen;
466 int diff, iret, tclen, oline;
467 char *icap = 0, *scan, *tc, *tcstart, *tcend;
471 * There is room for one more character in record.
472 * R_end points just past end of record.
473 * Rp points just past last character in record.
474 * Scan points at remainder of record that needs to be
475 * scanned for tc=name constructs.
478 tc_not_resolved = FALSE;
480 if ((tc = _nc_cgetcap(scan, "tc", '=')) == 0) {
485 * Find end of tc=name and stomp on the trailing `:'
486 * (if present) so we can use it to call ourselves.
496 tclen = (int) (s - tcstart);
500 iret = _nc_getent(&icap, &ilen, &oline, current, db_array, fd,
502 newicap = icap; /* Put into a register. */
503 newilen = (int) ilen;
504 if (iret != TC_SUCCESS) {
506 if (iret < TC_NOT_FOUND) {
513 if (iret == TC_UNRESOLVED) {
514 tc_not_resolved = TRUE;
515 /* couldn't resolve tc */
516 } else if (iret == TC_NOT_FOUND) {
519 tc_not_resolved = TRUE;
524 /* not interested in name field of tc'ed record */
526 while (*s != '\0' && *s++ != ':') ;
527 newilen -= (int) (s - newicap);
530 /* make sure interpolated record is `:'-terminated */
532 if (*(s - 1) != ':') {
533 *s = ':'; /* overwrite NUL with : */
538 * Make sure there's enough room to insert the
541 diff = newilen - tclen;
542 if (diff >= r_end - rp) {
543 unsigned pos, tcpos, tcposend;
546 pos = (unsigned) (rp - record);
547 newsize = (size_t) (r_end - record + diff + BFRAG);
548 tcpos = (unsigned) (tcstart - record);
549 tcposend = (unsigned) (tcend - record);
550 record = DOALLOC(newsize);
558 r_end = record + newsize;
560 tcstart = record + tcpos;
561 tcend = record + tcposend;
565 * Insert tc'ed record into our record.
567 s = tcstart + newilen;
568 memmove(s, tcend, (size_t) (rp - tcend));
569 memmove(tcstart, newicap, (size_t) newilen);
574 * Start scan on `:' so next cgetcap works properly
575 * (cgetcap always skips first field).
582 * Close file (if we opened it), give back any extra memory, and
583 * return capability, length and success.
587 *len = (unsigned) (rp - record - 1); /* don't count NUL */
589 if ((record = DOALLOC((size_t) (rp - record))) == 0) {
596 if (tc_not_resolved) {
597 return (TC_UNRESOLVED);
603 * Cgetmatch will return 0 if name is one of the names of the capability
604 * record buf, -1 if not.
607 _nc_cgetmatch(char *buf, const char *name)
609 register const char *np;
613 * Start search at beginning of record.
618 * Try to match a record name.
623 if (*bp == '|' || *bp == ':' || *bp == '\0')
627 } else if (*bp++ != *np++) {
633 * Match failed, skip to next name in record.
635 bp--; /* a '|' or ':' may have stopped the match */
637 if (*bp == '\0' || *bp == ':')
638 return (-1); /* match failed totally */
639 else if (*bp++ == '|')
640 break; /* found next name */
646 * Compare name field of record.
649 _nc_nfcmp(const char *nf, char *rec)
654 for (cp = rec; *cp != ':'; cp++) ;
658 ret = strcmp(nf, rec);
663 #endif /* HAVE_BSD_CGETENT */
666 * Since ncurses provides its own 'tgetent()', we cannot use the native one.
667 * So we reproduce the logic to get down to cgetent() -- or our cut-down
668 * version of that -- to circumvent the problem of configuring against the
671 #define USE_BSD_TGETENT 1
675 * Copyright (c) 1980, 1993
676 * The Regents of the University of California. All rights reserved.
678 * Redistribution and use in source and binary forms, with or without
679 * modification, are permitted provided that the following conditions
681 * 1. Redistributions of source code must retain the above copyright
682 * notice, this list of conditions and the following disclaimer.
683 * 2. Redistributions in binary form must reproduce the above copyright
684 * notice, this list of conditions and the following disclaimer in the
685 * documentation and/or other materials provided with the distribution.
686 * 3. All advertising materials mentioning features or use of this software
687 * must display the following acknowledgment:
688 * This product includes software developed by the University of
689 * California, Berkeley and its contributors.
690 * 4. Neither the name of the University nor the names of its contributors
691 * may be used to endorse or promote products derived from this software
692 * without specific prior written permission.
694 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
695 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
696 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
697 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
698 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
699 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
700 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
701 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
702 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
703 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
707 /* static char sccsid[] = "@(#)termcap.c 8.1 (Berkeley) 6/4/93" */
709 #define PBUFSIZ 512 /* max length of filename path */
710 #define PVECSIZ 32 /* max number of names in path */
711 #define TBUFSIZ (2048*2)
714 * On entry, srcp points to a non ':' character which is the beginning of the
715 * token, if any. We'll try to return a string that doesn't end with a ':'.
718 get_tc_token(char **srcp, int *endp)
726 for (s = base = *srcp; *s != '\0';) {
731 } else if (*s++ == '\n') {
732 while (isspace(UChar(*s)))
737 } else if (ch == ':') {
746 } else if (isgraph(UChar(ch))) {
751 /* malformed entry may end without a ':' */
752 if (tok == 0 && found) {
760 copy_tc_token(char *dst, const char *src, size_t len)
764 while ((ch = *src++) != '\0') {
765 if (ch == '\\' && *src == '\n') {
766 while (isspace(UChar(*src)))
780 * Get an entry for terminal name in buffer bp from the termcap file.
783 _nc_tgetent(char *bp, char **sourcename, int *lineno, const char *name)
785 static char *the_source;
790 CGETENT_CONST char **fname;
793 char pathbuf[PBUFSIZ]; /* holds raw path of filenames */
794 CGETENT_CONST char *pathvec[PVECSIZ]; /* point to names in pathbuf */
795 const char *termpath;
801 cp = use_terminfo_vars()? getenv("TERMCAP") : NULL;
804 * TERMCAP can have one of two things in it. It can be the name of a file
805 * to use instead of /etc/termcap. In this case it better start with a
806 * "/". Or it can be an entry to use so we don't have to read the file.
807 * In this case it has to already have the newlines crunched out. If
808 * TERMCAP does not hold a file name then a path of names is searched
809 * instead. The path is found in the TERMPATH variable, or becomes
810 * "$HOME/.termcap /etc/termcap" if no TERMPATH exists.
812 _nc_str_init(&desc, pathbuf, sizeof(pathbuf));
814 _nc_safe_strcpy(&desc, get_termpath());
815 } else if (!_nc_is_abs_path(cp)) { /* TERMCAP holds an entry */
816 if ((termpath = get_termpath()) != 0) {
817 _nc_safe_strcat(&desc, termpath);
821 if ((home = getenv("HOME")) != 0 && *home != '\0'
822 && strchr(home, ' ') == 0
823 && strlen(home) < sizeof(temp) - 10) { /* setup path */
824 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
825 "%s/", home); /* $HOME first */
827 /* if no $HOME look in current directory */
828 _nc_STRCAT(temp, ".termcap", sizeof(temp));
829 _nc_safe_strcat(&desc, temp);
830 _nc_safe_strcat(&desc, " ");
831 _nc_safe_strcat(&desc, get_termpath());
833 } else { /* user-defined name in TERMCAP */
834 _nc_safe_strcat(&desc, cp); /* still can be tokenized */
837 *fname++ = pathbuf; /* tokenize path into vector of names */
839 if (*p == ' ' || *p == NCURSES_PATHSEP) {
842 if (*p != ' ' && *p != NCURSES_PATHSEP)
847 if (fname >= pathvec + PVECSIZ) {
853 *fname = 0; /* mark end of vector */
854 #if !HAVE_BSD_CGETENT
855 (void) _nc_cgetset(0);
857 if (_nc_is_abs_path(cp)) {
858 if (_nc_cgetset(cp) < 0) {
863 i = _nc_cgetent(&dummy, lineno, pathvec, name);
865 /* ncurses' termcap-parsing routines cannot handle multiple adjacent
866 * empty fields, and mistakenly use the last valid cap entry instead of
867 * the first (breaks tc= includes)
878 while (!endflag && (tok = get_tc_token(&ps, &endflag)) != 0) {
881 for (n = 1; n < count; n++) {
889 if (ignore != TRUE) {
891 pd = copy_tc_token(pd, tok, (size_t) (TBUFSIZ - (2 + pd - bp)));
903 FreeIfNeeded(the_source);
906 /* This is not related to the BSD cgetent(), but to fake up a suitable
907 * filename for ncurses' error reporting. (If we are not using BSD
908 * cgetent, then it is the actual filename).
914 _nc_str_init(&desc, temp, sizeof(temp));
915 _nc_safe_strcpy(&desc, pathvec[i]);
916 _nc_safe_strcat(&desc, ".db");
917 if (_nc_access(temp, R_OK) == 0) {
918 _nc_safe_strcpy(&desc, pathvec[i]);
920 if ((the_source = strdup(temp)) != 0)
921 *sourcename = the_source;
923 if ((the_source = strdup(pathvec[i])) != 0)
924 *sourcename = the_source;
930 #endif /* USE_BSD_TGETENT */
931 #endif /* USE_GETCAP */
936 * Add a filename to the list in 'termpaths[]', checking that we really have
937 * a right to open the file.
941 add_tc(char *termpaths[], char *path, int count)
943 char *save = strchr(path, NCURSES_PATHSEP);
947 && _nc_access(path, R_OK) == 0) {
948 termpaths[count++] = path;
949 TR(TRACE_DATABASE, ("Adding termpath %s", path));
951 termpaths[count] = 0;
953 *save = NCURSES_PATHSEP;
956 #define ADD_TC(path, count) filecount = add_tc(termpaths, path, count)
957 #endif /* !USE_GETCAP */
960 _nc_read_termcap_entry(const char *const tn, TERMTYPE2 *const tp)
962 int found = TGETENT_NO;
965 char cwd_buf[PATH_MAX];
968 char *p, tc[TBUFSIZ];
970 #define MY_SIZE sizeof(tc) - 1
975 TR(TRACE_DATABASE, ("read termcap entry for %s", tn));
978 || strcmp(tn, ".") == 0
979 || strcmp(tn, "..") == 0
980 || _nc_pathlast(tn) != 0) {
981 TR(TRACE_DATABASE, ("illegal or missing entry name '%s'", tn));
985 if (use_terminfo_vars() && (p = getenv("TERMCAP")) != 0
986 && !_nc_is_abs_path(p) && _nc_name_match(p, tn, "|:")) {
987 /* TERMCAP holds a termcap entry */
989 _nc_set_source("TERMCAP");
991 /* we're using getcap(3) */
992 if ((status = _nc_tgetent(tc, &source, &lineno, tn)) < 0)
993 return (status == TC_NOT_FOUND ? TGETENT_NO : TGETENT_ERR);
995 _nc_curr_line = lineno;
996 _nc_set_source(source);
1000 return (TGETENT_ERR);
1001 _nc_read_entry_source((FILE *) 0, tc_buf, FALSE, TRUE, NULLHOOK);
1006 * Here is what the 4.4BSD termcap(3) page prescribes:
1008 * It will look in the environment for a TERMCAP variable. If found, and
1009 * the value does not begin with a slash, and the terminal type name is the
1010 * same as the environment string TERM, the TERMCAP string is used instead
1011 * of reading a termcap file. If it does begin with a slash, the string is
1012 * used as a path name of the termcap file to search. If TERMCAP does not
1013 * begin with a slash and name is different from TERM, tgetent() searches
1014 * the files $HOME/.termcap and /usr/share/misc/termcap, in that order,
1015 * unless the environment variable TERMPATH exists, in which case it
1016 * specifies a list of file pathnames (separated by spaces or colons) to be
1019 * It goes on to state:
1021 * Whenever multiple files are searched and a tc field occurs in the
1022 * requested entry, the entry it names must be found in the same file or
1023 * one of the succeeding files.
1025 * However, this restriction is relaxed in ncurses; tc references to
1026 * previous files are permitted.
1028 * This routine returns 1 if an entry is found, 0 if not found, and -1 if
1029 * the database is not accessible.
1032 char *tc, *termpaths[MAXPATHS];
1035 bool use_buffer = FALSE;
1038 char pathbuf[PATH_MAX];
1041 struct stat test_stat[MAXPATHS];
1043 termpaths[filecount] = 0;
1044 if (use_terminfo_vars() && (tc = getenv("TERMCAP")) != 0) {
1045 if (_nc_is_abs_path(tc)) { /* interpret as a filename */
1048 } else if (_nc_name_match(tc, tn, "|:")) { /* treat as a capability file */
1049 tc_buf = strdup(tc);
1050 use_buffer = (tc_buf != 0);
1055 if (normal) { /* normal case */
1056 char envhome[PATH_MAX], *h;
1058 copied = strdup(get_termpath());
1059 for (cp = copied; *cp; cp++) {
1060 if (*cp == NCURSES_PATHSEP)
1062 else if (cp == copied || cp[-1] == '\0') {
1063 ADD_TC(cp, filecount);
1067 #define PRIVATE_CAP "%s/.termcap"
1069 if (use_terminfo_vars() && (h = getenv("HOME")) != NULL && *h != '\0'
1070 && (strlen(h) + sizeof(PRIVATE_CAP)) < PATH_MAX) {
1071 /* user's .termcap, if any, should override it */
1072 _nc_STRCPY(envhome, h, sizeof(envhome));
1073 _nc_SPRINTF(pathbuf, _nc_SLIMIT(sizeof(pathbuf))
1074 PRIVATE_CAP, envhome);
1075 ADD_TC(pathbuf, filecount);
1080 * Probably /etc/termcap is a symlink to /usr/share/misc/termcap.
1081 * Avoid reading the same file twice.
1084 for (j = 0; j < filecount; j++) {
1086 if (stat(termpaths[j], &test_stat[j]) != 0
1087 || !S_ISREG(test_stat[j].st_mode)) {
1090 for (k = 0; k < j; k++) {
1091 if (test_stat[k].st_dev == test_stat[j].st_dev
1092 && test_stat[k].st_ino == test_stat[j].st_ino) {
1099 TR(TRACE_DATABASE, ("Path %s is a duplicate", termpaths[j]));
1100 for (k = j + 1; k < filecount; k++) {
1101 termpaths[k - 1] = termpaths[k];
1102 test_stat[k - 1] = test_stat[k];
1110 /* parse the sources */
1112 _nc_set_source("TERMCAP");
1115 * We don't suppress warning messages here. The presumption is
1116 * that since it's just a single entry, they won't be a pain.
1118 _nc_read_entry_source((FILE *) 0, tc_buf, FALSE, FALSE, NULLHOOK);
1123 for (i = 0; i < filecount; i++) {
1125 TR(TRACE_DATABASE, ("Looking for %s in %s", tn, termpaths[i]));
1126 if (_nc_access(termpaths[i], R_OK) == 0
1127 && (fp = fopen(termpaths[i], "r")) != (FILE *) 0) {
1128 _nc_set_source(termpaths[i]);
1131 * Suppress warning messages. Otherwise you get 400 lines of
1132 * crap from archaic termcap files as ncurses complains about
1133 * all the obsolete capabilities.
1135 _nc_read_entry_source(fp, (char *) 0, FALSE, TRUE, NULLHOOK);
1143 #endif /* USE_GETCAP */
1146 return (TGETENT_ERR);
1148 /* resolve all use references */
1149 if (_nc_resolve_uses2(TRUE, FALSE) != TRUE)
1150 return (TGETENT_ERR);
1152 /* find a terminal matching tn, if we can */
1153 #if USE_GETCAP_CACHE
1154 if (getcwd(cwd_buf, sizeof(cwd_buf)) != 0) {
1155 _nc_set_writedir((char *) 0); /* note: this does a chdir */
1157 for_entry_list(ep) {
1158 if (_nc_name_match(ep->tterm.term_names, tn, "|:")) {
1160 * Make a local copy of the terminal capabilities, delinked
1164 _nc_free_entry(_nc_head, &(ep->tterm));
1167 * OK, now try to write the type to user's terminfo directory.
1168 * Next time he loads this, it will come through terminfo.
1170 * Advantage: Second and subsequent fetches of this entry will
1173 * Disadvantage: After the first time a termcap type is loaded
1174 * by its user, editing it in the /etc/termcap file, or in
1175 * TERMCAP, or in a local ~/.termcap, will be ineffective
1176 * unless the terminfo entry is explicitly removed.
1178 #if USE_GETCAP_CACHE
1179 (void) _nc_write_entry(tp);
1181 found = TGETENT_YES;
1185 #if USE_GETCAP_CACHE
1194 NCURSES_EXPORT(void)
1195 _nc_read_termcap(void);
1196 NCURSES_EXPORT(void)
1197 _nc_read_termcap(void)
1200 #endif /* PURE_TERMINFO */