beta-0.89.2
[luatex.git] / source / texk / kpathsea / expand.c
blobd9eadd4bc147541201cc454c4b1e0337e49f4d86
1 /* expand.c: general expansion.
3 Copyright 1993, 1994, 1995, 1996, 1997, 2005, 2008, 2009, 2011,
4 2012 Karl Berry.
5 Copyright 1997-2005 Olaf Weber.
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with this library; if not, see <http://www.gnu.org/licenses/>. */
20 #include <kpathsea/config.h>
22 #include <kpathsea/c-pathch.h>
23 #include <kpathsea/expand.h>
24 #include <kpathsea/pathsearch.h>
25 #include <kpathsea/tilde.h>
26 #include <kpathsea/variable.h>
27 #include <kpathsea/concatn.h>
28 #include <kpathsea/absolute.h>
29 #include <kpathsea/str-list.h>
31 /* Do variable expansion first so ~${USER} works. (Besides, it's what the
32 shells do.) */
34 string
35 kpathsea_expand (kpathsea kpse, const_string s)
37 string var_expansion = kpathsea_var_expand (kpse, s);
38 string tilde_expansion = kpathsea_tilde_expand (kpse, var_expansion);
40 /* `kpse_var_expand' always gives us new memory; `kpse_tilde_expand'
41 doesn't, necessarily. So be careful that we don't free what we are
42 about to return. */
43 if (tilde_expansion != var_expansion)
44 free (var_expansion);
46 return tilde_expansion;
49 /* Forward declarations of functions from the original expand.c */
50 static str_list_type brace_expand (kpathsea, const_string*);
52 /* If $KPSE_DOT is defined in the environment, prepend it to any relative
53 path components. */
55 static string
56 kpathsea_expand_kpse_dot (kpathsea kpse, string path)
58 string ret, elt;
59 string kpse_dot = getenv("KPSE_DOT");
60 #ifdef MSDOS
61 boolean malloced_kpse_dot = false;
62 #endif
64 if (kpse_dot == NULL)
65 return path;
66 ret = (string)xmalloc(1);
67 *ret = 0;
69 #ifdef MSDOS
70 /* Some setups of ported Bash force $KPSE_DOT to have the //d/foo/bar
71 form (when `pwd' is used), which is not understood by libc and the OS.
72 Convert them back to the usual d:/foo/bar form. */
73 if (kpse_dot[0] == '/' && kpse_dot[1] == '/'
74 && kpse_dot[2] >= 'A' && kpse_dot[2] <= 'z' && kpse_dot[3] == '/') {
75 kpse_dot++;
76 kpse_dot = xstrdup (kpse_dot);
77 kpse_dot[0] = kpse_dot[1]; /* drive letter */
78 kpse_dot[1] = ':';
79 malloced_kpse_dot = true;
81 #endif
83 for (elt = kpathsea_path_element (kpse, path); elt;
84 elt = kpathsea_path_element (kpse, NULL)) {
85 string save_ret = ret;
86 boolean ret_copied = true;
87 /* We assume that the !! magic is only used on absolute components.
88 Single "." gets special treatment, as does "./" or its equivalent. */
89 if (kpathsea_absolute_p (kpse, elt, false)
90 || (elt[0] == '!' && elt[1] == '!')) {
91 ret = concat3(ret, elt, ENV_SEP_STRING);
92 } else if (elt[0] == '.' && elt[1] == 0) {
93 ret = concat3 (ret, kpse_dot, ENV_SEP_STRING);
94 #ifndef VMS
95 } else if (elt[0] == '.' && IS_DIR_SEP(elt[1])) {
96 ret = concatn (ret, kpse_dot, elt + 1, ENV_SEP_STRING, NULL);
97 } else if (*elt) {
98 ret = concatn (ret, kpse_dot, DIR_SEP_STRING, elt, ENV_SEP_STRING, NULL);
99 #endif
100 } else {
101 /* omit empty path elements from TEXMFCNF.
102 See http://bugs.debian.org/358330. */
103 ret_copied = false;
105 if (ret_copied)
106 free (save_ret);
109 #ifdef MSDOS
110 if (malloced_kpse_dot) free (kpse_dot);
111 #endif
113 ret[strlen (ret) - 1] = 0;
114 return ret;
117 /* Do brace expansion on ELT; then do variable and ~ expansion on each
118 element of the result; then do brace expansion again, in case a
119 variable definition contained braces (e.g., $TEXMF). Return a
120 string comprising all of the results separated by ENV_SEP_STRING. */
122 static string
123 kpathsea_brace_expand_element (kpathsea kpse, const_string elt)
125 unsigned i;
126 str_list_type expansions = brace_expand (kpse, &elt);
127 string ret = (string)xmalloc (1);
128 *ret = 0;
130 for (i = 0; i != STR_LIST_LENGTH(expansions); i++) {
131 /* Do $ and ~ expansion on each element. */
132 string x = kpathsea_expand (kpse, STR_LIST_ELT(expansions,i));
133 string save_ret = ret;
134 if (!STREQ (x, STR_LIST_ELT(expansions,i))) {
135 /* If we did any expansions, do brace expansion again. Since
136 recursive variable definitions are not allowed, this recursion
137 must terminate. (In practice, it's unlikely there will ever be
138 more than one level of recursion.) */
139 string save_x = x;
140 x = kpathsea_brace_expand_element (kpse, x);
141 free (save_x);
143 ret = concat3 (ret, x, ENV_SEP_STRING);
144 free (save_ret);
145 free (x);
147 for (i = 0; i != STR_LIST_LENGTH(expansions); ++i) {
148 free(STR_LIST_ELT(expansions,i));
150 str_list_free(&expansions);
151 ret[strlen (ret) - 1] = 0; /* waste the trailing null */
152 return ret;
155 /* Be careful to not waste all the memory we allocate for each element. */
157 string
158 kpathsea_brace_expand (kpathsea kpse, const_string path)
160 string kpse_dot_expansion;
161 string elt;
162 unsigned len;
163 /* Must do variable expansion first because if we have
164 foo = .:~
165 TEXINPUTS = $foo
166 we want to end up with TEXINPUTS = .:/home/karl.
167 Since kpse_path_element is not reentrant, we must get all
168 the path elements before we start the loop. */
169 string xpath = kpathsea_var_expand (kpse, path);
170 string ret = (string)xmalloc (1);
171 *ret = 0;
173 for (elt = kpathsea_path_element (kpse, xpath); elt;
174 elt = kpathsea_path_element (kpse, NULL)) {
175 string save_ret = ret;
176 /* Do brace expansion first, so tilde expansion happens in {~ka,~kb}. */
177 string expansion = kpathsea_brace_expand_element (kpse, elt);
178 ret = concat3 (ret, expansion, ENV_SEP_STRING);
179 free (expansion);
180 free (save_ret);
183 /* Waste the last byte by overwriting the trailing env_sep with a null. */
184 len = strlen (ret);
185 if (len != 0)
186 ret[len - 1] = 0;
187 free (xpath);
189 kpse_dot_expansion = kpathsea_expand_kpse_dot (kpse, ret);
190 if (kpse_dot_expansion != ret)
191 free (ret);
193 return kpse_dot_expansion;
196 #if defined(KPSE_COMPAT_API)
197 string
198 kpse_brace_expand (const_string path)
200 return kpathsea_brace_expand (kpse_def, path);
202 #endif
204 /* Expand all special constructs in a path, and include only the actually
205 existing directories in the result. */
206 string
207 kpathsea_path_expand (kpathsea kpse, const_string path)
209 string ret;
210 string xpath;
211 string elt;
212 unsigned len;
213 const_string ypath;
214 #if defined(WIN32)
215 string zpath, p;
216 #endif
218 /* Initialise ret to the empty string. */
219 ret = (string)xmalloc (1);
220 *ret = 0;
221 len = 0;
223 #if defined(WIN32)
224 zpath = xstrdup (path);
226 for (p = zpath; *p; p++)
227 if (*p == '\\')
228 *p = '/';
229 else if (IS_KANJI(p))
230 p++;
232 ypath = zpath;
233 #else
234 ypath = path;
235 #endif
237 /* Expand variables and braces first. */
238 xpath = kpathsea_brace_expand (kpse, ypath);
240 #if defined(WIN32)
241 free (zpath);
242 #endif
244 /* Now expand each of the path elements, printing the results */
245 for (elt = kpathsea_path_element (kpse, xpath); elt;
246 elt = kpathsea_path_element (kpse, NULL)) {
247 str_llist_type *dirs;
249 /* Skip and ignore magic leading chars. */
250 if (*elt == '!' && *(elt + 1) == '!')
251 elt += 2;
253 /* Search the disk for all dirs in the component specified.
254 Be faster to check the database, but this is more reliable. */
255 dirs = kpathsea_element_dirs (kpse, elt);
256 if (dirs && *dirs) {
257 str_llist_elt_type *dir;
259 for (dir = *dirs; dir; dir = STR_LLIST_NEXT (*dir)) {
260 string thedir = STR_LLIST (*dir);
261 unsigned dirlen = strlen (thedir);
262 string save_ret = ret;
263 /* We need to retain trailing slash if that's the root directory.
264 * On unix, "/" is root dir, "" often taken to be current dir.
265 * On windows, "C:/" is root dir of drive C, and "C:" is current
266 * on drive C. There's no need to look at other cases, like UNC
267 * names.
269 if (dirlen == 1 || (dirlen == 3 && NAME_BEGINS_WITH_DEVICE (thedir)
270 && IS_DIR_SEP (thedir[2]))) {
271 ret = concat3 (ret, thedir, ENV_SEP_STRING);
272 len += dirlen + 1;
273 ret[len - 1] = ENV_SEP;
274 } else {
275 ret = concat (ret, thedir);
276 len += dirlen;
277 ret [len - 1] = ENV_SEP;
279 free (save_ret);
283 /* Get rid of trailing ':', if any. */
284 if (len != 0)
285 ret[len - 1] = 0;
286 return ret;
289 #if defined(KPSE_COMPAT_API)
290 string
291 kpse_path_expand (const_string path)
293 return kpathsea_path_expand (kpse_def, path);
295 #endif
297 /* ... */
298 static void expand_append (str_list_type* partial,
299 const_string text, const_string p)
301 string new_string;
302 unsigned len;
303 str_list_type tmp;
304 tmp = str_list_init();
305 len = p - text;
306 new_string = (string)xmalloc(len+1);
307 strncpy(new_string, text, len);
308 new_string[len]=0;
309 str_list_add(&tmp, new_string);
310 str_list_concat_elements(partial, tmp);
314 static str_list_type
315 brace_expand (kpathsea kpse, const_string *text)
317 str_list_type result, partial, recurse;
318 const_string p;
319 result = str_list_init();
320 partial = str_list_init();
321 for (p = *text; *p && *p != '}'; ++p) {
322 /* FIXME: Should be IS_ENV_SEP(*p) */
323 if (*p == ENV_SEP || *p == ',') {
324 expand_append(&partial, *text, p);
325 str_list_concat(&result, partial);
326 str_list_free(&partial);
327 *text = p+1;
328 partial = str_list_init();
329 } else if (*p == '{') {
330 expand_append(&partial, *text, p);
331 ++p;
332 recurse = brace_expand(kpse, &p);
333 str_list_concat_elements(&partial, recurse);
334 str_list_free(&recurse);
335 /* Check for missing closing brace. */
336 if (*p != '}') {
337 WARNING1 ("kpathsea: %s: Unmatched {", *text);
339 *text = p+1;
340 } else if (*p == '$') {
341 /* Skip ${VAR} */
342 if (*(p+1) == '{')
343 for (p+=2; *p!='}';++p);
345 #if defined(WIN32)
346 else if (IS_KANJI(p))
347 p++;
348 #endif
350 expand_append(&partial, *text, p);
351 str_list_concat(&result, partial);
352 str_list_free(&partial);
353 *text = p;
354 return result;
359 #if defined (TEST)
360 #include <stdio.h>
362 fatal_error (format, arg1, arg2)
363 char *format, *arg1, *arg2;
365 report_error (format, arg1, arg2);
366 exit (1);
369 report_error (format, arg1, arg2)
370 char *format, *arg1, *arg2;
372 fprintf (stderr, format, arg1, arg2);
373 fprintf (stderr, "\n");
376 main (int argc, char **argv)
378 char example[256];
379 kpse_set_program_name(argv[0], NULL);
381 for (;;)
383 char *result;
384 int i;
386 fprintf (stderr, "brace_expand> ");
388 if ((!fgets (example, 256, stdin)) ||
389 (strncmp (example, "quit", 4) == 0))
390 break;
392 if (strlen (example))
393 example[strlen (example) - 1] = 0;
395 result = kpse_brace_expand (example);
397 printf ("%s\n", result);
403 #endif /* TEST */
406 Local variables:
407 standalone-compile-command: "gcc -g -I. -I.. -DTEST expand.c kpathsea.a"
408 end: