1 /****************************************************************************
2 * Copyright (c) 2006 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 2006 *
31 ****************************************************************************/
34 * Iterators for terminal databases.
37 #include <curses.priv.h>
41 MODULE_ID("$Id: db_iterator.c,v 1.5 2006/12/16 19:06:42 tom Exp $")
43 static bool have_tic_directory = FALSE;
44 static bool keep_tic_directory = FALSE;
47 * Record the "official" location of the terminfo directory, according to
48 * the place where we're writing to, or the normal default, if not.
50 NCURSES_EXPORT(const char *)
51 _nc_tic_dir(const char *path)
53 static const char *result = TERMINFO;
55 if (!keep_tic_directory) {
58 have_tic_directory = TRUE;
59 } else if (!have_tic_directory && use_terminfo_vars()) {
61 if ((envp = getenv("TERMINFO")) != 0)
62 return _nc_tic_dir(envp);
69 * Special fix to prevent the terminfo directory from being moved after tic
70 * has chdir'd to it. If we let it be changed, then if $TERMINFO has a
71 * relative path, we'll lose track of the actual directory.
74 _nc_keep_tic_dir(const char *path)
77 keep_tic_directory = TRUE;
81 * Process the list of :-separated directories, looking for the terminal type.
82 * We don't use strtok because it does not show us empty tokens.
85 static char *this_db_list = 0;
86 static int size_db_list;
94 if (this_db_list != 0) {
95 FreeAndNull(this_db_list);
100 /* The TERMINFO_DIRS value, if defined by the configure script, begins with a
101 * ":", which will be interpreted as TERMINFO.
104 next_list_item(const char *source, int *offset)
107 FreeIfNeeded(this_db_list);
108 this_db_list = strdup(source);
109 size_db_list = strlen(source);
112 if (this_db_list != 0 && size_db_list && *offset < size_db_list) {
113 static char system_db[] = TERMINFO;
114 char *result = this_db_list + *offset;
115 char *marker = strchr(result, NCURSES_PATHSEP);
118 * Put a null on the marker if a separator was found. Set the offset
119 * to the next position after the marker so we can call this function
120 * again, using the data at the offset.
123 *offset += strlen(result) + 1;
124 marker = result + *offset;
127 *offset = marker - this_db_list;
129 if (*result == 0 && result != (this_db_list + size_db_list))
136 #define NEXT_DBD(var, offset) next_list_item((*offset == 0) ? var : 0, offset)
139 * This is a simple iterator which allows the caller to step through the
140 * possible locations for a terminfo directory. ncurses uses this to find
141 * terminfo files to read.
143 NCURSES_EXPORT(const char *)
144 _nc_next_db(DBDIRS * state, int *offset)
149 while (*state < dbdLAST) {
150 DBDIRS next = (DBDIRS) ((int) (*state) + 1);
156 if (have_tic_directory)
157 result = _nc_tic_dir(0);
161 if (use_terminfo_vars()) {
162 if ((envp = getenv("TERMINFO")) != 0)
163 result = _nc_tic_dir(envp);
167 if (use_terminfo_vars()) {
168 result = _nc_home_terminfo();
172 if (use_terminfo_vars()) {
173 if ((result = NEXT_DBD(getenv("TERMINFO_DIRS"), offset)) != 0)
179 if ((result = NEXT_DBD(TERMINFO_DIRS, offset)) != 0)
184 #ifndef TERMINFO_DIRS
188 #endif /* USE_DATABASE */
191 if (use_terminfo_vars()) {
192 if ((envp = getenv("TERMCAP")) != 0)
193 result = _nc_tic_dir(envp);
197 if (use_terminfo_vars()) {
198 if ((result = NEXT_DBD(getenv("TERMPATH"), offset)) != 0)
203 if ((result = NEXT_DBD(TERMPATH, offset)) != 0)
206 #endif /* USE_TERMCAP */
210 if (*state != next) {
223 _nc_first_db(DBDIRS * state, int *offset)