]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/tinfo/db_iterator.c
ncurses 6.3 - patch 20221001
[ncurses.git] / ncurses / tinfo / db_iterator.c
index 17f160631df79e4c07f5fab8f853bd7023ee2bcf..e69e0f34e71ccc66ff6ef26a50b790fcb02f3554 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 2006-2016,2017 Free Software Foundation, Inc.              *
+ * Copyright 2018-2020,2022 Thomas E. Dickey                                *
+ * Copyright 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            *
@@ -43,7 +44,7 @@
 #include <hashed_db.h>
 #endif
 
-MODULE_ID("$Id: db_iterator.c,v 1.44 2017/02/04 23:27:01 tom Exp $")
+MODULE_ID("$Id: db_iterator.c,v 1.49 2022/04/23 20:03:15 tom Exp $")
 
 #define HaveTicDirectory _nc_globals.have_tic_directory
 #define KeepTicDirectory _nc_globals.keep_tic_directory
@@ -123,19 +124,21 @@ update_getenv(const char *name, DBDIRS which)
 
     if (which < dbdLAST) {
        char *value;
+       char *cached_value = my_vars[which].value;
+       bool same_value;
 
-       if ((value = getenv(name)) == 0 || (value = strdup(value)) == 0) {
-           ;
-       } else if (my_vars[which].name == 0 || strcmp(my_vars[which].name, name)) {
-           FreeIfNeeded(my_vars[which].value);
-           my_vars[which].name = name;
-           my_vars[which].value = value;
-           result = TRUE;
-       } else if ((my_vars[which].value != 0) ^ (value != 0)) {
-           FreeIfNeeded(my_vars[which].value);
-           my_vars[which].value = value;
-           result = TRUE;
-       } else if (value != 0 && strcmp(value, my_vars[which].value)) {
+       if ((value = getenv(name)) != 0) {
+           value = strdup(value);
+       }
+       same_value = ((value == 0 && cached_value == 0) ||
+                     (value != 0 &&
+                      cached_value != 0 &&
+                      strcmp(value, cached_value) == 0));
+
+       /* Set variable name to enable checks in cache_expired(). */
+       my_vars[which].name = name;
+
+       if (!same_value) {
            FreeIfNeeded(my_vars[which].value);
            my_vars[which].value = value;
            result = TRUE;
@@ -146,6 +149,7 @@ update_getenv(const char *name, DBDIRS which)
     return result;
 }
 
+#if NCURSES_USE_DATABASE || NCURSES_USE_TERMCAP
 static char *
 cache_getenv(const char *name, DBDIRS which)
 {
@@ -157,6 +161,7 @@ cache_getenv(const char *name, DBDIRS which)
     }
     return result;
 }
+#endif
 
 /*
  * The cache expires if at least a second has passed since the initial lookup,
@@ -197,6 +202,13 @@ free_cache(void)
     FreeAndNull(my_list);
 }
 
+static void
+update_tic_dir(const char *update)
+{
+    free((char *) TicDirectory);
+    TicDirectory = update;
+}
+
 /*
  * Record the "official" location of the terminfo directory, according to
  * the place where we're writing to, or the normal default, if not.
@@ -206,8 +218,9 @@ _nc_tic_dir(const char *path)
 {
     T(("_nc_tic_dir %s", NonNull(path)));
     if (!KeepTicDirectory) {
-       if (path != 0) {
-           TicDirectory = path;
+       if (path != NULL) {
+           if (path != TicDirectory)
+               update_tic_dir(strdup(path));
            HaveTicDirectory = TRUE;
        } else if (HaveTicDirectory == 0) {
            if (use_terminfo_vars()) {
@@ -439,5 +452,6 @@ _nc_db_iterator_leaks(void)
        FreeIfNeeded(my_vars[which].value);
        my_vars[which].value = 0;
     }
+    update_tic_dir(NULL);
 }
 #endif