]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/tinfo/db_iterator.c
ncurses 6.0 - patch 20170318
[ncurses.git] / ncurses / tinfo / db_iterator.c
index f9f0258e8d924d9d0f740b5fc55d0ca08f62be53..17f160631df79e4c07f5fab8f853bd7023ee2bcf 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
 /****************************************************************************
- * Copyright (c) 2006-2012,2013 Free Software Foundation, Inc.              *
+ * Copyright (c) 2006-2016,2017 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            *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -43,7 +43,7 @@
 #include <hashed_db.h>
 #endif
 
 #include <hashed_db.h>
 #endif
 
-MODULE_ID("$Id: db_iterator.c,v 1.37 2013/11/16 19:42:56 tom Exp $")
+MODULE_ID("$Id: db_iterator.c,v 1.44 2017/02/04 23:27:01 tom Exp $")
 
 #define HaveTicDirectory _nc_globals.have_tic_directory
 #define KeepTicDirectory _nc_globals.keep_tic_directory
 
 #define HaveTicDirectory _nc_globals.have_tic_directory
 #define KeepTicDirectory _nc_globals.keep_tic_directory
@@ -72,15 +72,18 @@ check_existence(const char *name, struct stat *sb)
 {
     bool result = FALSE;
 
 {
     bool result = FALSE;
 
-    if (stat(name, sb) == 0
-       && (S_ISDIR(sb->st_mode) || S_ISREG(sb->st_mode))) {
+    if (quick_prefix(name)) {
+       result = TRUE;
+    } else if (stat(name, sb) == 0
+              && (S_ISDIR(sb->st_mode)
+                  || (S_ISREG(sb->st_mode) && sb->st_size))) {
        result = TRUE;
     }
 #if USE_HASHED_DB
     else if (strlen(name) < PATH_MAX - sizeof(DBM_SUFFIX)) {
        char temp[PATH_MAX];
        _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "%s%s", name, DBM_SUFFIX);
        result = TRUE;
     }
 #if USE_HASHED_DB
     else if (strlen(name) < PATH_MAX - sizeof(DBM_SUFFIX)) {
        char temp[PATH_MAX];
        _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "%s%s", name, DBM_SUFFIX);
-       if (stat(temp, sb) == 0 && S_ISREG(sb->st_mode)) {
+       if (stat(temp, sb) == 0 && S_ISREG(sb->st_mode) && sb->st_size) {
            result = TRUE;
        }
     }
            result = TRUE;
        }
     }
@@ -88,6 +91,27 @@ check_existence(const char *name, struct stat *sb)
     return result;
 }
 
     return result;
 }
 
+/*
+ * Trim newlines (and backslashes preceding those) and tab characters to
+ * help simplify scripting of the quick-dump feature.  Leave spaces and
+ * other backslashes alone.
+ */
+static void
+trim_formatting(char *source)
+{
+    char *target = source;
+    char ch;
+
+    while ((ch = *source++) != '\0') {
+       if (ch == '\\' && *source == '\n')
+           continue;
+       if (ch == '\n' || ch == '\t')
+           continue;
+       *target++ = ch;
+    }
+    *target = '\0';
+}
+
 /*
  * Store the latest value of an environment variable in my_vars[] so we can
  * detect if one changes, invalidating the cached search-list.
 /*
  * Store the latest value of an environment variable in my_vars[] so we can
  * detect if one changes, invalidating the cached search-list.
@@ -185,10 +209,12 @@ _nc_tic_dir(const char *path)
        if (path != 0) {
            TicDirectory = path;
            HaveTicDirectory = TRUE;
        if (path != 0) {
            TicDirectory = path;
            HaveTicDirectory = TRUE;
-       } else if (HaveTicDirectory == 0 && use_terminfo_vars()) {
-           char *envp;
-           if ((envp = getenv("TERMINFO")) != 0)
-               return _nc_tic_dir(envp);
+       } else if (HaveTicDirectory == 0) {
+           if (use_terminfo_vars()) {
+               const char *envp;
+               if ((envp = getenv("TERMINFO")) != 0)
+                   return _nc_tic_dir(envp);
+           }
        }
     }
     return TicDirectory ? TicDirectory : TERMINFO;
        }
     }
     return TicDirectory ? TicDirectory : TERMINFO;
@@ -249,7 +275,7 @@ _nc_first_db(DBDIRS * state, int *offset)
     *state = dbdTIC;
     *offset = 0;
 
     *state = dbdTIC;
     *offset = 0;
 
-    T(("_nc_first_db"));
+    T((T_CALLED("_nc_first_db")));
 
     /* build a blob containing all of the strings we will use for a lookup
      * table.
 
     /* build a blob containing all of the strings we will use for a lookup
      * table.
@@ -258,7 +284,7 @@ _nc_first_db(DBDIRS * state, int *offset)
        size_t blobsize = 0;
        const char *values[dbdLAST];
        struct stat *my_stat;
        size_t blobsize = 0;
        const char *values[dbdLAST];
        struct stat *my_stat;
-       int j, k;
+       int j;
 
        if (cache_has_expired)
            free_cache();
 
        if (cache_has_expired)
            free_cache();
@@ -328,10 +354,12 @@ _nc_first_db(DBDIRS * state, int *offset)
            my_list = typeCalloc(char *, blobsize);
            my_stat = typeCalloc(struct stat, blobsize);
            if (my_list != 0 && my_stat != 0) {
            my_list = typeCalloc(char *, blobsize);
            my_stat = typeCalloc(struct stat, blobsize);
            if (my_list != 0 && my_stat != 0) {
-               k = 0;
+               int k = 0;
                my_list[k++] = my_blob;
                for (j = 0; my_blob[j] != '\0'; ++j) {
                my_list[k++] = my_blob;
                for (j = 0; my_blob[j] != '\0'; ++j) {
-                   if (my_blob[j] == NCURSES_PATHSEP) {
+                   if (my_blob[j] == NCURSES_PATHSEP
+                       && ((&my_blob[j] - my_list[k - 1]) != 3
+                           || !quick_prefix(my_list[k - 1]))) {
                        my_blob[j] = '\0';
                        my_list[k++] = &my_blob[j + 1];
                    }
                        my_blob[j] = '\0';
                        my_list[k++] = &my_blob[j + 1];
                    }
@@ -345,8 +373,10 @@ _nc_first_db(DBDIRS * state, int *offset)
                    if (*my_list[j] == '\0')
                        my_list[j] = strdup(TERMINFO);
 #endif
                    if (*my_list[j] == '\0')
                        my_list[j] = strdup(TERMINFO);
 #endif
+                   trim_formatting(my_list[j]);
                    for (k = 0; k < j; ++k) {
                        if (!strcmp(my_list[j], my_list[k])) {
                    for (k = 0; k < j; ++k) {
                        if (!strcmp(my_list[j], my_list[k])) {
+                           T(("duplicate %s", my_list[j]));
                            k = j - 1;
                            while ((my_list[j] = my_list[j + 1]) != 0) {
                                ++j;
                            k = j - 1;
                            while ((my_list[j] = my_list[j + 1]) != 0) {
                                ++j;
@@ -375,6 +405,7 @@ _nc_first_db(DBDIRS * state, int *offset)
                    }
 #endif
                    if (!found) {
                    }
 #endif
                    if (!found) {
+                       T(("not found %s", my_list[j]));
                        k = j;
                        while ((my_list[k] = my_list[k + 1]) != 0) {
                            ++k;
                        k = j;
                        while ((my_list[k] = my_list[k + 1]) != 0) {
                            ++k;
@@ -390,6 +421,7 @@ _nc_first_db(DBDIRS * state, int *offset)
            free(my_stat);
        }
     }
            free(my_stat);
        }
     }
+    returnVoid;
 }
 
 #if NO_LEAKS
 }
 
 #if NO_LEAKS