]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/tinfo/read_termcap.c
ncurses 6.0 - patch 20150912
[ncurses.git] / ncurses / tinfo / read_termcap.c
index 9487dd66b797d5183b8b42be55f77fa17e25745c..6bfb23cdc6434d70c8b23db15bf952cb12efa00f 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2012,2013 Free Software Foundation, Inc.              *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -29,6 +29,7 @@
 /****************************************************************************
  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
+ *     and: Thomas E. Dickey                        1996-on                 *
  ****************************************************************************/
 
 /*
 #include <curses.priv.h>
 
 #include <ctype.h>
+#include <sys/types.h>
 #include <tic.h>
-#include <term_entry.h>
 
-MODULE_ID("$Id: read_termcap.c,v 1.51 2000/10/21 00:34:11 Philip.Guenther Exp $")
+MODULE_ID("$Id: read_termcap.c,v 1.89 2013/12/15 00:32:43 tom Exp $")
 
 #if !PURE_TERMINFO
 
-#ifdef __EMX__
-#define is_pathname(s) ((((s) != 0) && ((s)[0] == '/')) \
-                 || (((s)[0] != 0) && ((s)[1] == ':')))
-#else
-#define is_pathname(s) ((s) != 0 && (s)[0] == '/')
-#endif
-
 #define TC_SUCCESS     0
-#define TC_UNRESOLVED -1
-#define TC_NOT_FOUND  -2
-#define TC_SYS_ERR    -3
-#define TC_REF_LOOP   -4
+#define TC_NOT_FOUND  -1
+#define TC_SYS_ERR    -2
+#define TC_REF_LOOP   -3
+#define TC_UNRESOLVED -4       /* this is not returned by BSD cgetent */
+
+static NCURSES_CONST char *
+get_termpath(void)
+{
+    NCURSES_CONST char *result;
+
+    if (!use_terminfo_vars() || (result = getenv("TERMPATH")) == 0)
+       result = TERMPATH;
+    TR(TRACE_DATABASE, ("TERMPATH is %s", result));
+    return result;
+}
 
+/*
+ * Note:
+ * getcap(), cgetent(), etc., are BSD functions.  A copy of those was added to
+ * this file in November 1995, derived from the BSD4.4 Lite sources.
+ *
+ * The initial adaptation uses 518 lines from that source.
+ * The current source (in 2009) uses 183 lines of BSD4.4 Lite (441 ignoring
+ * whitespace).
+ */
 #if USE_GETCAP
 
 #if HAVE_BSD_CGETENT
@@ -100,11 +114,7 @@ static int _nc_nfcmp(const char *, char *);
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgment:
- *     This product includes software developed by the University of
- *     California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
  *
@@ -125,13 +135,7 @@ static int _nc_nfcmp(const char *, char *);
 
 #define        BFRAG           1024
 #define        BSIZE           1024
-#define        ESC             ('[' & 037)     /* ASCII ESC */
 #define        MAX_RECURSION   32      /* maximum getent recursion */
-#define        SFRAG           100     /* cgetstr mallocs in SFRAG chunks */
-
-#define RECOK  (char)0
-#define TCERR  (char)1
-#define        SHADOW  (char)2
 
 static size_t topreclen;       /* toprec length */
 static char *toprec;           /* Additional record specified by cgetset() */
@@ -157,7 +161,7 @@ _nc_cgetset(const char *ent)
        return (-1);
     }
     gottoprec = 0;
-    (void) strcpy(toprec, ent);
+    _nc_STRCPY(toprec, ent, topreclen);
     return (0);
 }
 
@@ -224,10 +228,10 @@ _nc_cgetcap(char *buf, const char *cap, int type)
  * Returns:
  *
  * positive #    on success (i.e., the index in db_array)
- * TC_UNRESOLVED if we had too many recurrences to resolve
  * TC_NOT_FOUND  if the requested record couldn't be found
  * TC_SYS_ERR    if a system error was encountered (e.g.,couldn't open a file)
  * TC_REF_LOOP   if a potential reference loop is detected
+ * TC_UNRESOLVED if we had too many recurrences to resolve
  */
 static int
 _nc_cgetent(char **buf, int *oline, char **db_array, const char *name)
@@ -290,7 +294,7 @@ _nc_getent(
            errno = ENOMEM;
            return (TC_SYS_ERR);
        }
-       (void) strcpy(record, toprec);
+       _nc_STRCPY(record, toprec, topreclen + BFRAG);
        rp = record + topreclen + 1;
        r_end = rp + BFRAG;
        current = in_array;
@@ -379,7 +383,14 @@ _nc_getent(
                        c = *bp++;
                        if (c == '\n') {
                            lineno++;
-                           if (rp == record || *(rp - 1) != '\\')
+                           /*
+                            * Unlike BSD 4.3, this ignores a backslash at the
+                            * end of a comment-line.  That makes it consistent
+                            * with the rest of ncurses -TD
+                            */
+                           if (rp == record
+                               || *record == '#'
+                               || *(rp - 1) != '\\')
                                break;
                        }
                        *rp++ = c;
@@ -437,8 +448,10 @@ _nc_getent(
                break;
        }
 
-       if (!foundit)
+       if (!foundit) {
+           free(record);
            return (TC_NOT_FOUND);
+       }
     }
 
     /*
@@ -450,7 +463,7 @@ _nc_getent(
        register int newilen;
        unsigned ilen;
        int diff, iret, tclen, oline;
-       char *icap, *scan, *tc, *tcstart, *tcend;
+       char *icap = 0, *scan, *tc, *tcstart, *tcend;
 
        /*
         * Loop invariants:
@@ -463,8 +476,9 @@ _nc_getent(
        scan = record;
        tc_not_resolved = FALSE;
        for (;;) {
-           if ((tc = _nc_cgetcap(scan, "tc", '=')) == 0)
+           if ((tc = _nc_cgetcap(scan, "tc", '=')) == 0) {
                break;
+           }
 
            /*
             * Find end of tc=name and stomp on the trailing `:'
@@ -481,6 +495,7 @@ _nc_getent(
            tclen = s - tcstart;
            tcend = s;
 
+           icap = 0;
            iret = _nc_getent(&icap, &ilen, &oline, current, db_array, fd,
                              tc, depth + 1, 0);
            newicap = icap;     /* Put into a register. */
@@ -491,12 +506,13 @@ _nc_getent(
                    if (myfd)
                        (void) close(fd);
                    free(record);
+                   FreeIfNeeded(icap);
                    return (iret);
                }
-               if (iret == TC_UNRESOLVED)
+               if (iret == TC_UNRESOLVED) {
                    tc_not_resolved = TRUE;
-               /* couldn't resolve tc */
-               if (iret == TC_NOT_FOUND) {
+                   /* couldn't resolve tc */
+               } else if (iret == TC_NOT_FOUND) {
                    *(s - 1) = ':';
                    scan = s - 1;
                    tc_not_resolved = TRUE;
@@ -576,8 +592,9 @@ _nc_getent(
     }
 
     *cap = record;
-    if (tc_not_resolved)
+    if (tc_not_resolved) {
        return (TC_UNRESOLVED);
+    }
     return (current);
 }
 
@@ -692,8 +709,6 @@ _nc_nfcmp(const char *nf, char *rec)
 #define        PVECSIZ         32      /* max number of names in path */
 #define TBUFSIZ (2048*2)
 
-static char *tbuf;
-
 /*
  * On entry, srcp points to a non ':' character which is the beginning of the
  * token, if any.  We'll try to return a string that doesn't end with a ':'.
@@ -713,7 +728,7 @@ get_tc_token(char **srcp, int *endp)
            if (*s == '\0') {
                break;
            } else if (*s++ == '\n') {
-               while (isspace(*s))
+               while (isspace(UChar(*s)))
                    s++;
            } else {
                found = TRUE;
@@ -727,7 +742,7 @@ get_tc_token(char **srcp, int *endp)
                break;
            }
            base = s;
-       } else if (isgraph(ch)) {
+       } else if (isgraph(UChar(ch))) {
            found = TRUE;
        }
     }
@@ -747,7 +762,7 @@ copy_tc_token(char *dst, const char *src, size_t len)
 
     while ((ch = *src++) != '\0') {
        if (ch == '\\' && *src == '\n') {
-           while (isspace(*src))
+           while (isspace(UChar(*src)))
                src++;
            continue;
        }
@@ -755,7 +770,7 @@ copy_tc_token(char *dst, const char *src, size_t len)
            dst = 0;
            break;
        }
-       *dst++ = ch;
+       *dst++ = (char) ch;
     }
     return dst;
 }
@@ -771,20 +786,18 @@ _nc_tgetent(char *bp, char **sourcename, int *lineno, const char *name)
     register char *p;
     register char *cp;
     char *dummy = NULL;
-    char **fname;
+    CGETENT_CONST char **fname;
     char *home;
     int i;
     char pathbuf[PBUFSIZ];     /* holds raw path of filenames */
-    char *pathvec[PVECSIZ];    /* to point to names in pathbuf */
-    char **pvec;               /* holds usable tail of path vector */
-    char *termpath;
+    CGETENT_CONST char *pathvec[PVECSIZ];      /* point to names in pathbuf */
+    NCURSES_CONST char *termpath;
     string_desc desc;
 
+    *lineno = 1;
     fname = pathvec;
-    pvec = pathvec;
-    tbuf = bp;
     p = pathbuf;
-    cp = use_terminfo_vars() ? getenv("TERMCAP") : NULL;
+    cp = use_terminfo_vars()? getenv("TERMCAP") : NULL;
 
     /*
      * TERMCAP can have one of two things in it.  It can be the name of a file
@@ -797,9 +810,9 @@ _nc_tgetent(char *bp, char **sourcename, int *lineno, const char *name)
      */
     _nc_str_init(&desc, pathbuf, sizeof(pathbuf));
     if (cp == NULL) {
-       _nc_safe_strcpy(&desc, "/etc/termcap /usr/share/misc/termcap");
-    } else if (!is_pathname(cp)) {     /* TERMCAP holds an entry */
-       if ((termpath = getenv("TERMPATH")) != 0) {
+       _nc_safe_strcpy(&desc, get_termpath());
+    } else if (!_nc_is_abs_path(cp)) { /* TERMCAP holds an entry */
+       if ((termpath = get_termpath()) != 0) {
            _nc_safe_strcat(&desc, termpath);
        } else {
            char temp[PBUFSIZ];
@@ -807,13 +820,14 @@ _nc_tgetent(char *bp, char **sourcename, int *lineno, const char *name)
            if ((home = getenv("HOME")) != 0 && *home != '\0'
                && strchr(home, ' ') == 0
                && strlen(home) < sizeof(temp) - 10) {  /* setup path */
-               sprintf(temp, "%s/", home);     /* $HOME first */
+               _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
+                           "%s/", home);       /* $HOME first */
            }
            /* if no $HOME look in current directory */
-           strcat(temp, ".termcap");
+           _nc_STRCAT(temp, ".termcap", sizeof(temp));
            _nc_safe_strcat(&desc, temp);
-           _nc_safe_strcat(&desc, " /etc/termcap");
-           _nc_safe_strcat(&desc, " /usr/share/misc/termcap");
+           _nc_safe_strcat(&desc, " ");
+           _nc_safe_strcat(&desc, get_termpath());
        }
     } else {                   /* user-defined name in TERMCAP */
        _nc_safe_strcat(&desc, cp);     /* still can be tokenized */
@@ -821,10 +835,10 @@ _nc_tgetent(char *bp, char **sourcename, int *lineno, const char *name)
 
     *fname++ = pathbuf;                /* tokenize path into vector of names */
     while (*++p) {
-       if (*p == ' ' || *p == ':') {
+       if (*p == ' ' || *p == NCURSES_PATHSEP) {
            *p = '\0';
            while (*++p)
-               if (*p != ' ' && *p != ':')
+               if (*p != ' ' && *p != NCURSES_PATHSEP)
                    break;
            if (*p == '\0')
                break;
@@ -836,7 +850,10 @@ _nc_tgetent(char *bp, char **sourcename, int *lineno, const char *name)
        }
     }
     *fname = 0;                        /* mark end of vector */
-    if (is_pathname(cp)) {
+#if !HAVE_BSD_CGETENT
+    (void) _nc_cgetset(0);
+#endif
+    if (_nc_is_abs_path(cp)) {
        if (_nc_cgetset(cp) < 0) {
            return (TC_SYS_ERR);
        }
@@ -848,6 +865,7 @@ _nc_tgetent(char *bp, char **sourcename, int *lineno, const char *name)
      * empty fields, and mistakenly use the last valid cap entry instead of
      * the first (breaks tc= includes)
      */
+    *bp = '\0';
     if (i >= 0) {
        char *pd, *ps, *tok;
        int endflag = FALSE;
@@ -869,7 +887,7 @@ _nc_tgetent(char *bp, char **sourcename, int *lineno, const char *name)
            }
            if (ignore != TRUE) {
                list[count++] = tok;
-               pd = copy_tc_token(pd, tok, TBUFSIZ - (2 + pd - bp));
+               pd = copy_tc_token(pd, tok, (size_t) (TBUFSIZ - (2 + pd - bp)));
                if (pd == 0) {
                    i = -1;
                    break;
@@ -889,8 +907,21 @@ _nc_tgetent(char *bp, char **sourcename, int *lineno, const char *name)
      * cgetent, then it is the actual filename).
      */
     if (i >= 0) {
+#if HAVE_BSD_CGETENT
+       char temp[PATH_MAX];
+
+       _nc_str_init(&desc, temp, sizeof(temp));
+       _nc_safe_strcpy(&desc, pathvec[i]);
+       _nc_safe_strcat(&desc, ".db");
+       if (_nc_access(temp, R_OK) == 0) {
+           _nc_safe_strcpy(&desc, pathvec[i]);
+       }
+       if ((the_source = strdup(temp)) != 0)
+           *sourcename = the_source;
+#else
        if ((the_source = strdup(pathvec[i])) != 0)
            *sourcename = the_source;
+#endif
     }
 
     return (i);
@@ -908,43 +939,61 @@ _nc_tgetent(char *bp, char **sourcename, int *lineno, const char *name)
 static int
 add_tc(char *termpaths[], char *path, int count)
 {
+    char *save = strchr(path, NCURSES_PATHSEP);
+    if (save != 0)
+       *save = '\0';
     if (count < MAXPATHS
-       && _nc_access(path, R_OK) == 0)
+       && _nc_access(path, R_OK) == 0) {
        termpaths[count++] = path;
+       TR(TRACE_DATABASE, ("Adding termpath %s", path));
+    }
     termpaths[count] = 0;
+    if (save != 0)
+       *save = NCURSES_PATHSEP;
     return count;
 }
 #define ADD_TC(path, count) filecount = add_tc(termpaths, path, count)
 #endif /* !USE_GETCAP */
 
-int
-_nc_read_termcap_entry(const char *const tn, TERMTYPE * const tp)
+NCURSES_EXPORT(int)
+_nc_read_termcap_entry(const char *const tn, TERMTYPE *const tp)
 {
-    int found = FALSE;
+    int found = TGETENT_NO;
     ENTRY *ep;
 #if USE_GETCAP_CACHE
     char cwd_buf[PATH_MAX];
 #endif
 #if USE_GETCAP
     char *p, tc[TBUFSIZ];
+    int status;
     static char *source;
     static int lineno;
 
+    TR(TRACE_DATABASE, ("read termcap entry for %s", tn));
+
+    if (strlen(tn) == 0
+       || strcmp(tn, ".") == 0
+       || strcmp(tn, "..") == 0
+       || _nc_pathlast(tn) != 0) {
+       TR(TRACE_DATABASE, ("illegal or missing entry name '%s'", tn));
+       return TGETENT_NO;
+    }
+
     if (use_terminfo_vars() && (p = getenv("TERMCAP")) != 0
-       && !is_pathname(p) && _nc_name_match(p, tn, "|:")) {
+       && !_nc_is_abs_path(p) && _nc_name_match(p, tn, "|:")) {
        /* TERMCAP holds a termcap entry */
        strncpy(tc, p, sizeof(tc) - 1);
        tc[sizeof(tc) - 1] = '\0';
        _nc_set_source("TERMCAP");
     } else {
        /* we're using getcap(3) */
-       if (_nc_tgetent(tc, &source, &lineno, tn) < 0)
-           return (ERR);
+       if ((status = _nc_tgetent(tc, &source, &lineno, tn)) < 0)
+           return (status == TC_NOT_FOUND ? TGETENT_NO : TGETENT_ERR);
 
        _nc_curr_line = lineno;
        _nc_set_source(source);
     }
-    _nc_read_entry_source((FILE *) 0, tc, FALSE, FALSE, NULLHOOK);
+    _nc_read_entry_source((FILE *) 0, tc, FALSE, TRUE, NULLHOOK);
 #else
     /*
      * Here is what the 4.4BSD termcap(3) page prescribes:
@@ -975,53 +1024,84 @@ _nc_read_termcap_entry(const char *const tn, TERMTYPE * const tp)
     FILE *fp;
     char *tc, *termpaths[MAXPATHS];
     int filecount = 0;
+    int j, k;
     bool use_buffer = FALSE;
+    bool normal = TRUE;
     char tc_buf[1024];
     char pathbuf[PATH_MAX];
+    char *copied = 0;
+    char *cp;
+    struct stat test_stat[MAXPATHS];
 
     termpaths[filecount] = 0;
     if (use_terminfo_vars() && (tc = getenv("TERMCAP")) != 0) {
-       if (is_pathname(tc)) {  /* interpret as a filename */
+       if (_nc_is_abs_path(tc)) {      /* interpret as a filename */
            ADD_TC(tc, 0);
+           normal = FALSE;
        } else if (_nc_name_match(tc, tn, "|:")) {      /* treat as a capability file */
            use_buffer = TRUE;
-           (void) sprintf(tc_buf, "%.*s\n", (int) sizeof(tc_buf) - 2, tc);
-       } else if ((tc = getenv("TERMPATH")) != 0) {
-           char *cp;
-
-           for (cp = tc; *cp; cp++) {
-               if (*cp == ':')
-                   *cp = '\0';
-               else if (cp == tc || cp[-1] == '\0') {
-                   ADD_TC(cp, filecount);
-               }
-           }
+           _nc_SPRINTF(tc_buf,
+                       _nc_SLIMIT(sizeof(tc_buf))
+                       "%.*s\n", (int) sizeof(tc_buf) - 2, tc);
+           normal = FALSE;
        }
-    } else {                   /* normal case */
-       char envhome[PATH_MAX], *h;
+    }
 
-       filecount = 0;
+    if (normal) {              /* normal case */
+       char envhome[PATH_MAX], *h;
 
-       /*
-        * Probably /etc/termcap is a symlink to /usr/share/misc/termcap.
-        * Avoid reading the same file twice.
-        */
-       if (_nc_access("/etc/termcap", F_OK) == 0)
-           ADD_TC("/etc/termcap", filecount);
-       else
-           ADD_TC("/usr/share/misc/termcap", filecount);
+       copied = strdup(get_termpath());
+       for (cp = copied; *cp; cp++) {
+           if (*cp == NCURSES_PATHSEP)
+               *cp = '\0';
+           else if (cp == copied || cp[-1] == '\0') {
+               ADD_TC(cp, filecount);
+           }
+       }
 
 #define PRIVATE_CAP "%s/.termcap"
 
-       if ((h = getenv("HOME")) != NULL && *h != '\0'
+       if (use_terminfo_vars() && (h = getenv("HOME")) != NULL && *h != '\0'
            && (strlen(h) + sizeof(PRIVATE_CAP)) < PATH_MAX) {
            /* user's .termcap, if any, should override it */
-           (void) strcpy(envhome, h);
-           (void) sprintf(pathbuf, PRIVATE_CAP, envhome);
+           _nc_STRCPY(envhome, h, sizeof(envhome));
+           _nc_SPRINTF(pathbuf, _nc_SLIMIT(sizeof(pathbuf))
+                       PRIVATE_CAP, envhome);
            ADD_TC(pathbuf, filecount);
        }
     }
 
+    /*
+     * Probably /etc/termcap is a symlink to /usr/share/misc/termcap.
+     * Avoid reading the same file twice.
+     */
+#if HAVE_LINK
+    for (j = 0; j < filecount; j++) {
+       bool omit = FALSE;
+       if (stat(termpaths[j], &test_stat[j]) != 0
+           || !S_ISREG(test_stat[j].st_mode)) {
+           omit = TRUE;
+       } else {
+           for (k = 0; k < j; k++) {
+               if (test_stat[k].st_dev == test_stat[j].st_dev
+                   && test_stat[k].st_ino == test_stat[j].st_ino) {
+                   omit = TRUE;
+                   break;
+               }
+           }
+       }
+       if (omit) {
+           TR(TRACE_DATABASE, ("Path %s is a duplicate", termpaths[j]));
+           for (k = j + 1; k < filecount; k++) {
+               termpaths[k - 1] = termpaths[k];
+               test_stat[k - 1] = test_stat[k];
+           }
+           --filecount;
+           --j;
+       }
+    }
+#endif
+
     /* parse the sources */
     if (use_buffer) {
        _nc_set_source("TERMCAP");
@@ -1036,8 +1116,9 @@ _nc_read_termcap_entry(const char *const tn, TERMTYPE * const tp)
 
        for (i = 0; i < filecount; i++) {
 
-           T(("Looking for %s in %s", tn, termpaths[i]));
-           if ((fp = fopen(termpaths[i], "r")) != (FILE *) 0) {
+           TR(TRACE_DATABASE, ("Looking for %s in %s", tn, termpaths[i]));
+           if (_nc_access(termpaths[i], R_OK) == 0
+               && (fp = fopen(termpaths[i], "r")) != (FILE *) 0) {
                _nc_set_source(termpaths[i]);
 
                /*
@@ -1051,13 +1132,15 @@ _nc_read_termcap_entry(const char *const tn, TERMTYPE * const tp)
            }
        }
     }
+    if (copied != 0)
+       free(copied);
 #endif /* USE_GETCAP */
 
     if (_nc_head == 0)
-       return (ERR);
+       return (TGETENT_ERR);
 
     /* resolve all use references */
-    _nc_resolve_uses(TRUE);
+    _nc_resolve_uses2(TRUE, FALSE);
 
     /* find a terminal matching tn, if we can */
 #if USE_GETCAP_CACHE
@@ -1067,13 +1150,11 @@ _nc_read_termcap_entry(const char *const tn, TERMTYPE * const tp)
        for_entry_list(ep) {
            if (_nc_name_match(ep->tterm.term_names, tn, "|:")) {
                /*
-                * Make a local copy of the terminal capabilities.  Free all
-                * entry storage except the string table for the loaded type
-                * (which we disconnected from the list by NULLing out
-                * ep->tterm.str_table above).
+                * Make a local copy of the terminal capabilities, delinked
+                * from the list.
                 */
                *tp = ep->tterm;
-               ep->tterm.str_table = (char *) 0;
+               _nc_free_entry(_nc_head, &(ep->tterm));
 
                /*
                 * OK, now try to write the type to user's terminfo directory. 
@@ -1090,7 +1171,7 @@ _nc_read_termcap_entry(const char *const tn, TERMTYPE * const tp)
 #if USE_GETCAP_CACHE
                (void) _nc_write_entry(tp);
 #endif
-               found = TRUE;
+               found = TGETENT_YES;
                break;
            }
        }
@@ -1099,12 +1180,13 @@ _nc_read_termcap_entry(const char *const tn, TERMTYPE * const tp)
     }
 #endif
 
-    _nc_free_entries(_nc_head);
     return (found);
 }
 #else
-extern void _nc_read_termcap(void);
-void
+extern
+NCURSES_EXPORT(void)
+_nc_read_termcap(void);
+NCURSES_EXPORT(void)
 _nc_read_termcap(void)
 {
 }