1 /****************************************************************************
2 * Copyright (c) 2006-2007,2010 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.9 2010/12/25 23:00:25 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
= (int) 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
+= (int) strlen(result
);
124 *offset
= (int) (marker
- ThisDbList
);
126 if (*result
== 0 && result
!= (ThisDbList
+ ThisDbSize
))
133 #define NEXT_DBD(var, offset) next_list_item((*offset == 0) ? var : 0, offset)
136 * This is a simple iterator which allows the caller to step through the
137 * possible locations for a terminfo directory. ncurses uses this to find
138 * terminfo files to read.
140 NCURSES_EXPORT(const char *)
141 _nc_next_db(DBDIRS
* state
, int *offset
)
146 while (*state
< dbdLAST
) {
147 DBDIRS next
= (DBDIRS
) ((int) (*state
) + 1);
153 if (HaveTicDirectory
)
154 result
= _nc_tic_dir(0);
158 if (use_terminfo_vars()) {
159 if ((envp
= getenv("TERMINFO")) != 0)
160 result
= _nc_tic_dir(envp
);
164 if (use_terminfo_vars()) {
165 result
= _nc_home_terminfo();
169 if (use_terminfo_vars()) {
170 if ((result
= NEXT_DBD(getenv("TERMINFO_DIRS"), offset
)) != 0)
176 if ((result
= NEXT_DBD(TERMINFO_DIRS
, offset
)) != 0)
181 #ifndef TERMINFO_DIRS
185 #endif /* USE_DATABASE */
188 if (use_terminfo_vars()) {
189 if ((envp
= getenv("TERMCAP")) != 0)
190 result
= _nc_tic_dir(envp
);
194 if (use_terminfo_vars()) {
195 if ((result
= NEXT_DBD(getenv("TERMPATH"), offset
)) != 0)
200 if ((result
= NEXT_DBD(TERMPATH
, offset
)) != 0)
203 #endif /* USE_TERMCAP */
207 if (*state
!= next
) {
220 _nc_first_db(DBDIRS
* state
, int *offset
)