1 /****************************************************************************
2 * Copyright (c) 2006-2010,2011 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.26 2012/01/07 20:09:36 juergen 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)
61 char *last = my_blob + strlen(my_blob);
63 *last++ = NCURSES_PATHSEP;
69 check_existence(const char *name, struct stat *sb)
72 if (stat(name, sb) == 0
80 else if (strlen(name) < PATH_MAX - sizeof(DBM_SUFFIX)) {
82 sprintf(temp, "%s%s", name, DBM_SUFFIX);
83 if (stat(temp, sb) == 0
96 * Store the latest value of an environment variable in my_vars[] so we can
97 * detect if one changes, invalidating the cached search-list.
100 update_getenv(const char *name, DBDIRS which)
103 char *value = getenv(name);
105 if (which < dbdLAST) {
106 if (my_vars[which].name == 0 || strcmp(my_vars[which].name, name)) {
107 FreeIfNeeded(my_vars[which].value);
108 my_vars[which].name = name;
109 my_vars[which].value = value;
111 } else if ((my_vars[which].value != 0) ^ (value != 0)) {
112 FreeIfNeeded(my_vars[which].value);
113 my_vars[which].value = value;
115 } else if (value != 0 && strcmp(value, my_vars[which].value)) {
116 FreeIfNeeded(my_vars[which].value);
117 my_vars[which].value = value;
125 cache_getenv(const char *name, DBDIRS which)
129 (void) update_getenv(name, which);
130 if (which < dbdLAST) {
131 result = my_vars[which].value;
137 * The cache expires if at least a second has passed since the initial lookup,
138 * or if one of the environment variables changed.
140 * Only a few applications use multiple lookups of terminal entries, seems that
141 * aside from bulk I/O such as tic and toe, that leaves interactive programs
142 * which should not be modifying the terminal databases in a way that would
143 * invalidate the search-list.
145 * The "1-second" is to allow for user-directed changes outside the program.
151 time_t now = time((time_t *) 0);
157 for (n = (DBDIRS) 0; n < dbdLAST; ++n) {
158 if (my_vars[n].name != 0
159 && update_getenv(my_vars[n].name, n)) {
171 FreeAndNull(my_blob);
172 FreeAndNull(my_list);
176 * Record the "official" location of the terminfo directory, according to
177 * the place where we're writing to, or the normal default, if not.
179 NCURSES_EXPORT(const char *)
180 _nc_tic_dir(const char *path)
182 T(("_nc_tic_dir %s", NonNull(path)));
183 if (!KeepTicDirectory) {
186 HaveTicDirectory = TRUE;
187 } else if (!HaveTicDirectory && use_terminfo_vars()) {
189 if ((envp = getenv("TERMINFO")) != 0)
190 return _nc_tic_dir(envp);
193 return TicDirectory ? TicDirectory : TERMINFO;
197 * Special fix to prevent the terminfo directory from being moved after tic
198 * has chdir'd to it. If we let it be changed, then if $TERMINFO has a
199 * relative path, we'll lose track of the actual directory.
202 _nc_keep_tic_dir(const char *path)
205 KeepTicDirectory = TRUE;
214 if (my_blob != 0 && cache_expired()) {
220 * This is a simple iterator which allows the caller to step through the
221 * possible locations for a terminfo directory. ncurses uses this to find
222 * terminfo files to read.
224 NCURSES_EXPORT(const char *)
225 _nc_next_db(DBDIRS * state, int *offset)
230 if ((int) *state < my_size
232 && my_list[*state] != 0) {
233 result = my_list[*state];
239 T(("_nc_next_db %d %s", *state, result));
245 _nc_first_db(DBDIRS * state, int *offset)
252 /* build a blob containing all of the strings we will use for a lookup
257 const char *values[dbdLAST];
258 struct stat *my_stat;
261 for (j = 0; j < dbdLAST; ++j)
265 * This is the first item in the list, and is used only when tic is
266 * writing to the database, as a performance improvement.
268 values[dbdTIC] = TicDirectory;
272 values[dbdCfgList] = TERMINFO_DIRS;
275 values[dbdCfgOnce] = TERMINFO;
280 values[dbdCfgList2] = TERMPATH;
283 if (use_terminfo_vars()) {
285 values[dbdEnvOnce] = cache_getenv("TERMINFO", dbdEnvOnce);
286 values[dbdHome] = _nc_home_terminfo();
287 (void) cache_getenv("HOME", dbdHome);
288 values[dbdEnvList] = cache_getenv("TERMINFO_DIRS", dbdEnvList);
292 values[dbdEnvOnce2] = cache_getenv("TERMCAP", dbdEnvOnce2);
293 /* only use $TERMCAP if it is an absolute path */
294 if (values[dbdEnvOnce2] != 0
295 && *values[dbdEnvOnce2] != '/') {
296 values[dbdEnvOnce2] = 0;
298 values[dbdEnvList2] = cache_getenv("TERMPATH", dbdEnvList2);
299 #endif /* USE_TERMCAP */
302 for (j = 0; j < dbdLAST; ++j) {
305 blobsize += 2 + strlen(values[j]);
308 my_blob = malloc(blobsize);
311 for (j = 0; j < dbdLAST; ++j) {
312 add_to_blob(values[j]);
315 /* Now, build an array which will be pointers to the distinct
316 * strings in the blob.
319 for (j = 0; my_blob[j] != '\0'; ++j) {
320 if (my_blob[j] == NCURSES_PATHSEP)
323 my_list = typeCalloc(char *, blobsize);
324 my_stat = typeCalloc(struct stat, blobsize);
325 if (my_list != 0 && my_stat != 0) {
327 my_list[k++] = my_blob;
328 for (j = 0; my_blob[j] != '\0'; ++j) {
329 if (my_blob[j] == NCURSES_PATHSEP) {
331 my_list[k++] = &my_blob[j + 1];
336 * Eliminate duplicates from the list.
338 for (j = 0; my_list[j] != 0; ++j) {
340 if (*my_list[j] == '\0')
341 my_list[j] = strdup(TERMINFO);
343 for (k = 0; k < j; ++k) {
344 if (!strcmp(my_list[j], my_list[k])) {
346 while ((my_list[j] = my_list[j + 1]) != 0) {
356 * Eliminate non-existent databases, and those that happen to
357 * be symlinked to another location.
359 for (j = 0; my_list[j] != 0; ++j) {
360 bool found = check_existence(my_list[j], &my_stat[j]);
363 for (k = 0; k < j; ++k) {
364 if (my_stat[j].st_dev == my_stat[k].st_dev
365 && my_stat[j].st_ino == my_stat[k].st_ino) {
374 while ((my_list[k] = my_list[k + 1]) != 0) {
381 my_time = time((time_t *) 0);
383 FreeAndNull(my_blob);
392 _nc_db_iterator_leaks(void)
395 FreeAndNull(my_blob);
397 FreeAndNull(my_list);