1 /****************************************************************************
2  * Copyright (c) 2006-2007,2010 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 
29 /****************************************************************************
30  *  Author: Thomas E. Dickey                                                *
31  ****************************************************************************/
32 
33 /*
34  * Iterators for terminal databases.
35  */
36 
37 #include <curses.priv.h>
38 
39 #include <tic.h>
40 
41 MODULE_ID("$Id: db_iterator.c,v 1.9 2010/12/25 23:00:25 tom Exp $")
42 
43 #define HaveTicDirectory _nc_globals.have_tic_directory
44 #define KeepTicDirectory _nc_globals.keep_tic_directory
45 #define TicDirectory     _nc_globals.tic_directory
46 
47 /*
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.
50  */
51 NCURSES_EXPORT(const char *)
52 _nc_tic_dir(const char *path)
53 {
54     if (!KeepTicDirectory) {
55 	if (path != 0) {
56 	    TicDirectory = path;
57 	    HaveTicDirectory = TRUE;
58 	} else if (!HaveTicDirectory && use_terminfo_vars()) {
59 	    char *envp;
60 	    if ((envp = getenv("TERMINFO")) != 0)
61 		return _nc_tic_dir(envp);
62 	}
63     }
64     return TicDirectory;
65 }
66 
67 /*
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.
71  */
72 NCURSES_EXPORT(void)
73 _nc_keep_tic_dir(const char *path)
74 {
75     _nc_tic_dir(path);
76     KeepTicDirectory = TRUE;
77 }
78 
79 /*
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.
82  */
83 #define ThisDbList	_nc_globals.dbi_list
84 #define ThisDbSize	_nc_globals.dbi_size
85 
86 /*
87  * Cleanup.
88  */
89 NCURSES_EXPORT(void)
90 _nc_last_db(void)
91 {
92     if (ThisDbList != 0) {
93 	FreeAndNull(ThisDbList);
94     }
95     ThisDbSize = 0;
96 }
97 
98 /* The TERMINFO_DIRS value, if defined by the configure script, begins with a
99  * ":", which will be interpreted as TERMINFO.
100  */
101 static const char *
102 next_list_item(const char *source, int *offset)
103 {
104     if (source != 0) {
105 	FreeIfNeeded(ThisDbList);
106 	ThisDbList = strdup(source);
107 	ThisDbSize = (int) strlen(source);
108     }
109 
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);
114 
115 	/*
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.
119 	 */
120 	if (marker == 0) {
121 	    *offset += (int) strlen(result);
122 	} else {
123 	    *marker++ = 0;
124 	    *offset = (int) (marker - ThisDbList);
125 	}
126 	if (*result == 0 && result != (ThisDbList + ThisDbSize))
127 	    result = system_db;
128 	return result;
129     }
130     return 0;
131 }
132 
133 #define NEXT_DBD(var, offset) next_list_item((*offset == 0) ? var : 0, offset)
134 
135 /*
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.
139  */
140 NCURSES_EXPORT(const char *)
141 _nc_next_db(DBDIRS * state, int *offset)
142 {
143     const char *result;
144     char *envp;
145 
146     while (*state < dbdLAST) {
147 	DBDIRS next = (DBDIRS) ((int) (*state) + 1);
148 
149 	result = 0;
150 
151 	switch (*state) {
152 	case dbdTIC:
153 	    if (HaveTicDirectory)
154 		result = _nc_tic_dir(0);
155 	    break;
156 #if USE_DATABASE
157 	case dbdEnvOnce:
158 	    if (use_terminfo_vars()) {
159 		if ((envp = getenv("TERMINFO")) != 0)
160 		    result = _nc_tic_dir(envp);
161 	    }
162 	    break;
163 	case dbdHome:
164 	    if (use_terminfo_vars()) {
165 		result = _nc_home_terminfo();
166 	    }
167 	    break;
168 	case dbdEnvList:
169 	    if (use_terminfo_vars()) {
170 		if ((result = NEXT_DBD(getenv("TERMINFO_DIRS"), offset)) != 0)
171 		    next = *state;
172 	    }
173 	    break;
174 	case dbdCfgList:
175 #ifdef TERMINFO_DIRS
176 	    if ((result = NEXT_DBD(TERMINFO_DIRS, offset)) != 0)
177 		next = *state;
178 #endif
179 	    break;
180 	case dbdCfgOnce:
181 #ifndef TERMINFO_DIRS
182 	    result = TERMINFO;
183 #endif
184 	    break;
185 #endif /* USE_DATABASE */
186 #if USE_TERMCAP
187 	case dbdEnvOnce2:
188 	    if (use_terminfo_vars()) {
189 		if ((envp = getenv("TERMCAP")) != 0)
190 		    result = _nc_tic_dir(envp);
191 	    }
192 	    break;
193 	case dbdEnvList2:
194 	    if (use_terminfo_vars()) {
195 		if ((result = NEXT_DBD(getenv("TERMPATH"), offset)) != 0)
196 		    next = *state;
197 	    }
198 	    break;
199 	case dbdCfgList2:
200 	    if ((result = NEXT_DBD(TERMPATH, offset)) != 0)
201 		next = *state;
202 	    break;
203 #endif /* USE_TERMCAP */
204 	case dbdLAST:
205 	    break;
206 	}
207 	if (*state != next) {
208 	    *state = next;
209 	    *offset = 0;
210 	    _nc_last_db();
211 	}
212 	if (result != 0) {
213 	    return result;
214 	}
215     }
216     return 0;
217 }
218 
219 NCURSES_EXPORT(void)
220 _nc_first_db(DBDIRS * state, int *offset)
221 {
222     *state = dbdTIC;
223     *offset = 0;
224 }
225