1 /****************************************************************************
2 * Copyright (c) 2006-2017,2018 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: Thomas E. Dickey *
31 ****************************************************************************/
34 * Iterators for terminal databases.
37 #include <curses.priv.h>
43 #include <hashed_db.h>
46 MODULE_ID("$Id: db_iterator.c,v 1.47 2018/11/24 22:42:01 tom Exp $")
48 #define HaveTicDirectory _nc_globals.have_tic_directory
49 #define KeepTicDirectory _nc_globals.keep_tic_directory
50 #define TicDirectory _nc_globals.tic_directory
51 #define my_blob _nc_globals.dbd_blob
52 #define my_list _nc_globals.dbd_list
53 #define my_size _nc_globals.dbd_size
54 #define my_time _nc_globals.dbd_time
55 #define my_vars _nc_globals.dbd_vars
58 add_to_blob(const char *text, size_t limit)
63 char *last = my_blob + strlen(my_blob);
65 *last++ = NCURSES_PATHSEP;
66 _nc_STRCPY(last, text, limit);
71 check_existence(const char *name, struct stat *sb)
75 if (quick_prefix(name)) {
77 } else if (stat(name, sb) == 0
78 && (S_ISDIR(sb->st_mode)
79 || (S_ISREG(sb->st_mode) && sb->st_size))) {
83 else if (strlen(name) < PATH_MAX - sizeof(DBM_SUFFIX)) {
85 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "%s%s", name, DBM_SUFFIX);
86 if (stat(temp, sb) == 0 && S_ISREG(sb->st_mode) && sb->st_size) {
95 * Trim newlines (and backslashes preceding those) and tab characters to
96 * help simplify scripting of the quick-dump feature. Leave spaces and
97 * other backslashes alone.
100 trim_formatting(char *source)
102 char *target = source;
105 while ((ch = *source++) != '\0') {
106 if (ch == '\\' && *source == '\n')
108 if (ch == '\n' || ch == '\t')
116 * Store the latest value of an environment variable in my_vars[] so we can
117 * detect if one changes, invalidating the cached search-list.
120 update_getenv(const char *name, DBDIRS which)
124 if (which < dbdLAST) {
126 char *cached_value = my_vars[which].value;
129 if ((value = getenv(name)) != 0) {
130 value = strdup(value);
132 same_value = ((value == 0 && cached_value == 0) ||
135 strcmp(value, cached_value) == 0));
137 /* Set variable name to enable checks in cache_expired(). */
138 my_vars[which].name = name;
141 FreeIfNeeded(my_vars[which].value);
142 my_vars[which].value = value;
151 #if NCURSES_USE_DATABASE || NCURSES_USE_TERMCAP
153 cache_getenv(const char *name, DBDIRS which)
157 (void) update_getenv(name, which);
158 if (which < dbdLAST) {
159 result = my_vars[which].value;
166 * The cache expires if at least a second has passed since the initial lookup,
167 * or if one of the environment variables changed.
169 * Only a few applications use multiple lookups of terminal entries, seems that
170 * aside from bulk I/O such as tic and toe, that leaves interactive programs
171 * which should not be modifying the terminal databases in a way that would
172 * invalidate the search-list.
174 * The "1-second" is to allow for user-directed changes outside the program.
180 time_t now = time((time_t *) 0);
186 for (n = (DBDIRS) 0; n < dbdLAST; ++n) {
187 if (my_vars[n].name != 0
188 && update_getenv(my_vars[n].name, n)) {
200 FreeAndNull(my_blob);
201 FreeAndNull(my_list);
205 * Record the "official" location of the terminfo directory, according to
206 * the place where we're writing to, or the normal default, if not.
208 NCURSES_EXPORT(const char *)
209 _nc_tic_dir(const char *path)
211 T(("_nc_tic_dir %s", NonNull(path)));
212 if (!KeepTicDirectory) {
215 HaveTicDirectory = TRUE;
216 } else if (HaveTicDirectory == 0) {
217 if (use_terminfo_vars()) {
219 if ((envp = getenv("TERMINFO")) != 0)
220 return _nc_tic_dir(envp);
224 return TicDirectory ? TicDirectory : TERMINFO;
228 * Special fix to prevent the terminfo directory from being moved after tic
229 * has chdir'd to it. If we let it be changed, then if $TERMINFO has a
230 * relative path, we'll lose track of the actual directory.
233 _nc_keep_tic_dir(const char *path)
236 KeepTicDirectory = TRUE;
245 if (my_blob != 0 && cache_expired()) {
251 * This is a simple iterator which allows the caller to step through the
252 * possible locations for a terminfo directory. ncurses uses this to find
253 * terminfo files to read.
255 NCURSES_EXPORT(const char *)
256 _nc_next_db(DBDIRS * state, int *offset)
261 if ((int) *state < my_size
263 && my_list[*state] != 0) {
264 result = my_list[*state];
270 T(("_nc_next_db %d %s", *state, result));
276 _nc_first_db(DBDIRS * state, int *offset)
278 bool cache_has_expired = FALSE;
282 T((T_CALLED("_nc_first_db")));
284 /* build a blob containing all of the strings we will use for a lookup
287 if (my_blob == 0 || (cache_has_expired = cache_expired())) {
289 const char *values[dbdLAST];
290 struct stat *my_stat;
293 if (cache_has_expired)
296 for (j = 0; j < dbdLAST; ++j)
300 * This is the first item in the list, and is used only when tic is
301 * writing to the database, as a performance improvement.
303 values[dbdTIC] = TicDirectory;
305 #if NCURSES_USE_DATABASE
307 values[dbdCfgList] = TERMINFO_DIRS;
310 values[dbdCfgOnce] = TERMINFO;
314 #if NCURSES_USE_TERMCAP
315 values[dbdCfgList2] = TERMPATH;
318 if (use_terminfo_vars()) {
319 #if NCURSES_USE_DATABASE
320 values[dbdEnvOnce] = cache_getenv("TERMINFO", dbdEnvOnce);
321 values[dbdHome] = _nc_home_terminfo();
322 (void) cache_getenv("HOME", dbdHome);
323 values[dbdEnvList] = cache_getenv("TERMINFO_DIRS", dbdEnvList);
326 #if NCURSES_USE_TERMCAP
327 values[dbdEnvOnce2] = cache_getenv("TERMCAP", dbdEnvOnce2);
328 /* only use $TERMCAP if it is an absolute path */
329 if (values[dbdEnvOnce2] != 0
330 && *values[dbdEnvOnce2] != '/') {
331 values[dbdEnvOnce2] = 0;
333 values[dbdEnvList2] = cache_getenv("TERMPATH", dbdEnvList2);
334 #endif /* NCURSES_USE_TERMCAP */
337 for (j = 0; j < dbdLAST; ++j) {
340 blobsize += 2 + strlen(values[j]);
343 my_blob = malloc(blobsize);
346 for (j = 0; j < dbdLAST; ++j) {
347 add_to_blob(values[j], blobsize);
350 /* Now, build an array which will be pointers to the distinct
351 * strings in the blob.
354 for (j = 0; my_blob[j] != '\0'; ++j) {
355 if (my_blob[j] == NCURSES_PATHSEP)
358 my_list = typeCalloc(char *, blobsize);
359 my_stat = typeCalloc(struct stat, blobsize);
360 if (my_list != 0 && my_stat != 0) {
362 my_list[k++] = my_blob;
363 for (j = 0; my_blob[j] != '\0'; ++j) {
364 if (my_blob[j] == NCURSES_PATHSEP
365 && ((&my_blob[j] - my_list[k - 1]) != 3
366 || !quick_prefix(my_list[k - 1]))) {
368 my_list[k++] = &my_blob[j + 1];
373 * Eliminate duplicates from the list.
375 for (j = 0; my_list[j] != 0; ++j) {
377 if (*my_list[j] == '\0')
378 my_list[j] = strdup(TERMINFO);
380 trim_formatting(my_list[j]);
381 for (k = 0; k < j; ++k) {
382 if (!strcmp(my_list[j], my_list[k])) {
383 T(("duplicate %s", my_list[j]));
385 while ((my_list[j] = my_list[j + 1]) != 0) {
395 * Eliminate non-existent databases, and those that happen to
396 * be symlinked to another location.
398 for (j = 0; my_list[j] != 0; ++j) {
399 bool found = check_existence(my_list[j], &my_stat[j]);
402 for (k = 0; k < j; ++k) {
403 if (my_stat[j].st_dev == my_stat[k].st_dev
404 && my_stat[j].st_ino == my_stat[k].st_ino) {
412 T(("not found %s", my_list[j]));
414 while ((my_list[k] = my_list[k + 1]) != 0) {
421 my_time = time((time_t *) 0);
423 FreeAndNull(my_blob);
433 _nc_db_iterator_leaks(void)
438 FreeAndNull(my_blob);
440 FreeAndNull(my_list);
441 for (which = 0; (int) which < dbdLAST; ++which) {
442 my_vars[which].name = 0;
443 FreeIfNeeded(my_vars[which].value);
444 my_vars[which].value = 0;