1 /****************************************************************************
2 * Copyright (c) 2006,2007 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>
41 MODULE_ID("$Id: db_iterator.c,v 1.6 2007/04/22 00:00:26 tom Exp $")
43 #define HaveTicDirectory _nc_globals.have_tic_directory
44 #define KeepTicDirectory _nc_globals.keep_tic_directory
45 #define TicDirectory _nc_globals.tic_directory
48 * Record the "official" location of the terminfo directory, according to
49 * the place where we're writing to, or the normal default, if not.
51 NCURSES_EXPORT(const char *)
52 _nc_tic_dir(const char *path)
54 if (!KeepTicDirectory) {
57 HaveTicDirectory = TRUE;
58 } else if (!HaveTicDirectory && use_terminfo_vars()) {
60 if ((envp = getenv("TERMINFO")) != 0)
61 return _nc_tic_dir(envp);
68 * Special fix to prevent the terminfo directory from being moved after tic
69 * has chdir'd to it. If we let it be changed, then if $TERMINFO has a
70 * relative path, we'll lose track of the actual directory.
73 _nc_keep_tic_dir(const char *path)
76 KeepTicDirectory = TRUE;
80 * Process the list of :-separated directories, looking for the terminal type.
81 * We don't use strtok because it does not show us empty tokens.
83 #define ThisDbList _nc_globals.dbi_list
84 #define ThisDbSize _nc_globals.dbi_size
92 if (ThisDbList != 0) {
93 FreeAndNull(ThisDbList);
98 /* The TERMINFO_DIRS value, if defined by the configure script, begins with a
99 * ":", which will be interpreted as TERMINFO.
102 next_list_item(const char *source, int *offset)
105 FreeIfNeeded(ThisDbList);
106 ThisDbList = strdup(source);
107 ThisDbSize = strlen(source);
110 if (ThisDbList != 0 && ThisDbSize && *offset < ThisDbSize) {
111 static char system_db[] = TERMINFO;
112 char *result = ThisDbList + *offset;
113 char *marker = strchr(result, NCURSES_PATHSEP);
116 * Put a null on the marker if a separator was found. Set the offset
117 * to the next position after the marker so we can call this function
118 * again, using the data at the offset.
121 *offset += strlen(result) + 1;
122 marker = result + *offset;
125 *offset = marker - ThisDbList;
127 if (*result == 0 && result != (ThisDbList + ThisDbSize))
134 #define NEXT_DBD(var, offset) next_list_item((*offset == 0) ? var : 0, offset)
137 * This is a simple iterator which allows the caller to step through the
138 * possible locations for a terminfo directory. ncurses uses this to find
139 * terminfo files to read.
141 NCURSES_EXPORT(const char *)
142 _nc_next_db(DBDIRS * state, int *offset)
147 while (*state < dbdLAST) {
148 DBDIRS next = (DBDIRS) ((int) (*state) + 1);
154 if (HaveTicDirectory)
155 result = _nc_tic_dir(0);
159 if (use_terminfo_vars()) {
160 if ((envp = getenv("TERMINFO")) != 0)
161 result = _nc_tic_dir(envp);
165 if (use_terminfo_vars()) {
166 result = _nc_home_terminfo();
170 if (use_terminfo_vars()) {
171 if ((result = NEXT_DBD(getenv("TERMINFO_DIRS"), offset)) != 0)
177 if ((result = NEXT_DBD(TERMINFO_DIRS, offset)) != 0)
182 #ifndef TERMINFO_DIRS
186 #endif /* USE_DATABASE */
189 if (use_terminfo_vars()) {
190 if ((envp = getenv("TERMCAP")) != 0)
191 result = _nc_tic_dir(envp);
195 if (use_terminfo_vars()) {
196 if ((result = NEXT_DBD(getenv("TERMPATH"), offset)) != 0)
201 if ((result = NEXT_DBD(TERMPATH, offset)) != 0)
204 #endif /* USE_TERMCAP */
208 if (*state != next) {
221 _nc_first_db(DBDIRS * state, int *offset)