1 /* expand.c: general expansion.
3 Copyright 1993, 1994, 1995, 1996, 1997, 2005, 2008, 2009, 2011,
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
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
43 if (tilde_expansion
!= 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
56 kpathsea_expand_kpse_dot (kpathsea kpse
, string path
)
59 string kpse_dot
= getenv("KPSE_DOT");
61 boolean malloced_kpse_dot
= false;
66 ret
= (string
)xmalloc(1);
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] == '/') {
76 kpse_dot
= xstrdup (kpse_dot
);
77 kpse_dot
[0] = kpse_dot
[1]; /* drive letter */
79 malloced_kpse_dot
= true;
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
);
95 } else if (elt
[0] == '.' && IS_DIR_SEP(elt
[1])) {
96 ret
= concatn (ret
, kpse_dot
, elt
+ 1, ENV_SEP_STRING
, NULL
);
98 ret
= concatn (ret
, kpse_dot
, DIR_SEP_STRING
, elt
, ENV_SEP_STRING
, NULL
);
101 /* omit empty path elements from TEXMFCNF.
102 See http://bugs.debian.org/358330. */
110 if (malloced_kpse_dot
) free (kpse_dot
);
113 ret
[strlen (ret
) - 1] = 0;
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. */
123 kpathsea_brace_expand_element (kpathsea kpse
, const_string elt
)
126 str_list_type expansions
= brace_expand (kpse
, &elt
);
127 string ret
= (string
)xmalloc (1);
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.) */
140 x
= kpathsea_brace_expand_element (kpse
, x
);
143 ret
= concat3 (ret
, x
, ENV_SEP_STRING
);
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 */
155 /* Be careful to not waste all the memory we allocate for each element. */
158 kpathsea_brace_expand (kpathsea kpse
, const_string path
)
160 string kpse_dot_expansion
;
163 /* Must do variable expansion first because if we have
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);
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
);
183 /* Waste the last byte by overwriting the trailing env_sep with a null. */
189 kpse_dot_expansion
= kpathsea_expand_kpse_dot (kpse
, ret
);
190 if (kpse_dot_expansion
!= ret
)
193 return kpse_dot_expansion
;
196 #if defined(KPSE_COMPAT_API)
198 kpse_brace_expand (const_string path
)
200 return kpathsea_brace_expand (kpse_def
, path
);
204 /* Expand all special constructs in a path, and include only the actually
205 existing directories in the result. */
207 kpathsea_path_expand (kpathsea kpse
, const_string path
)
218 /* Initialise ret to the empty string. */
219 ret
= (string
)xmalloc (1);
224 zpath
= xstrdup (path
);
226 for (p
= zpath
; *p
; p
++)
229 else if (IS_KANJI(p
))
237 /* Expand variables and braces first. */
238 xpath
= kpathsea_brace_expand (kpse
, ypath
);
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) == '!')
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
);
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
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
);
273 ret
[len
- 1] = ENV_SEP
;
275 ret
= concat (ret
, thedir
);
277 ret
[len
- 1] = ENV_SEP
;
283 /* Get rid of trailing ':', if any. */
289 #if defined(KPSE_COMPAT_API)
291 kpse_path_expand (const_string path
)
293 return kpathsea_path_expand (kpse_def
, path
);
298 static void expand_append (str_list_type
* partial
,
299 const_string text
, const_string p
)
304 tmp
= str_list_init();
306 new_string
= (string
)xmalloc(len
+1);
307 strncpy(new_string
, text
, len
);
309 str_list_add(&tmp
, new_string
);
310 str_list_concat_elements(partial
, tmp
);
315 brace_expand (kpathsea kpse
, const_string
*text
)
317 str_list_type result
, partial
, recurse
;
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
);
328 partial
= str_list_init();
329 } else if (*p
== '{') {
330 expand_append(&partial
, *text
, 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. */
337 WARNING1 ("kpathsea: %s: Unmatched {", *text
);
340 } else if (*p
== '$') {
343 for (p
+=2; *p
!='}';++p
);
346 else if (IS_KANJI(p
))
350 expand_append(&partial
, *text
, p
);
351 str_list_concat(&result
, partial
);
352 str_list_free(&partial
);
362 fatal_error (format
, arg1
, arg2
)
363 char *format
, *arg1
, *arg2
;
365 report_error (format
, arg1
, arg2
);
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
)
379 kpse_set_program_name(argv
[0], NULL
);
386 fprintf (stderr
, "brace_expand> ");
388 if ((!fgets (example
, 256, stdin
)) ||
389 (strncmp (example
, "quit", 4) == 0))
392 if (strlen (example
))
393 example
[strlen (example
) - 1] = 0;
395 result
= kpse_brace_expand (example
);
397 printf ("%s\n", result
);
407 standalone-compile-command: "gcc -g -I. -I.. -DTEST expand.c kpathsea.a"