| 1 |
/* expand.c: general expansion. |
| 2 |
|
| 3 |
Copyright 1993, 1994, 1995, 1996, 1997, 2005, 2008, 2009, 2011, |
| 4 |
2012, 2016, 2017, 2025 Karl Berry. |
| 5 |
Copyright 1997-2005 Olaf Weber. |
| 6 |
|
| 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. |
| 11 |
|
| 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. |
| 16 |
|
| 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/>. */ |
| 19 |
|
| 20 |
#include <kpathsea/config.h> |
| 21 |
|
| 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> |
| 30 |
|
| 31 |
/* Do variable expansion first so ~${USER} works. (Besides, it's what the |
| 32 |
shells do.) */ |
| 33 |
|
| 34 |
string |
| 35 |
kpathsea_expand (kpathsea kpse, const_string s) |
| 36 |
{ |
| 37 |
string var_expansion = kpathsea_var_expand (kpse, s); |
| 38 |
string tilde_expansion = kpathsea_tilde_expand (kpse, var_expansion); |
| 39 |
|
| 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); |
| 45 |
|
| 46 |
return tilde_expansion; |
| 47 |
} |
| 48 |
|
| 49 |
/* Forward declarations of functions from the original expand.c */ |
| 50 |
static str_list_type brace_expand (kpathsea, const_string*); |
| 51 |
|
| 52 |
/* If $KPSE_DOT is defined in the environment, prepend it to any relative |
| 53 |
path components in PATH and return result in new memory. */ |
| 54 |
|
| 55 |
static string |
| 56 |
kpathsea_expand_kpse_dot (kpathsea kpse, string path) |
| 57 |
{ |
| 58 |
string ret, elt; |
| 59 |
string kpse_dot = getenv("KPSE_DOT"); |
| 60 |
#ifdef MSDOS |
| 61 |
boolean malloced_kpse_dot = false; |
| 62 |
#endif |
| 63 |
|
| 64 |
if (kpse_dot == NULL) |
| 65 |
return path; |
| 66 |
ret = (string) xmalloc(1); |
| 67 |
*ret = 0; |
| 68 |
|
| 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; |
| 80 |
} |
| 81 |
#endif |
| 82 |
|
| 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 |
if (kpathsea_absolute_p (kpse, elt, false) |
| 89 |
|| (elt[0] == '!' && elt[1] == '!')) { |
| 90 |
ret = concat3 (ret, elt, ENV_SEP_STRING); |
| 91 |
|
| 92 |
/* Path element is sole '.' -> replace with kpse_dot. */ |
| 93 |
} else if (elt[0] == '.' && elt[1] == 0) { |
| 94 |
ret = concat3 (ret, kpse_dot, ENV_SEP_STRING); |
| 95 |
|
| 96 |
#ifndef VMS |
| 97 |
/* Path element is './foo' -> replace the '.' with kpse_dot. */ |
| 98 |
} else if (elt[0] == '.' && IS_DIR_SEP(elt[1])) { |
| 99 |
ret = concatn (ret, kpse_dot, elt + 1, ENV_SEP_STRING, NULL); |
| 100 |
|
| 101 |
/* Path element is anything element -> prepend "kpse_dot/" to elt. */ |
| 102 |
} else if (*elt) { |
| 103 |
ret = concatn (ret, kpse_dot, DIR_SEP_STRING, elt, ENV_SEP_STRING, NULL); |
| 104 |
#endif |
| 105 |
|
| 106 |
} else { |
| 107 |
/* omit empty path elements from TEXMFCNF. |
| 108 |
See https://bugs.debian.org/358330. */ |
| 109 |
ret_copied = false; |
| 110 |
} |
| 111 |
if (ret_copied) { |
| 112 |
free (save_ret); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
#ifdef MSDOS |
| 117 |
if (malloced_kpse_dot) free (kpse_dot); |
| 118 |
#endif |
| 119 |
|
| 120 |
ret[strlen (ret) - 1] = 0; |
| 121 |
return ret; |
| 122 |
} |
| 123 |
|
| 124 |
/* Do brace expansion on ELT; then do variable and ~ expansion on each |
| 125 |
element of the result; then do brace expansion again, in case a |
| 126 |
variable definition contained braces (e.g., $TEXMF). Return a |
| 127 |
string comprising all of the results separated by ENV_SEP_STRING. */ |
| 128 |
|
| 129 |
static string |
| 130 |
kpathsea_brace_expand_element (kpathsea kpse, const_string elt) |
| 131 |
{ |
| 132 |
unsigned i; |
| 133 |
str_list_type expansions = brace_expand (kpse, &elt); |
| 134 |
string ret = (string)xmalloc (1); |
| 135 |
*ret = 0; |
| 136 |
|
| 137 |
for (i = 0; i != STR_LIST_LENGTH(expansions); i++) { |
| 138 |
/* Do $ and ~ expansion on each element. */ |
| 139 |
string x = kpathsea_expand (kpse, STR_LIST_ELT(expansions,i)); |
| 140 |
string save_ret = ret; |
| 141 |
if (!STREQ (x, STR_LIST_ELT(expansions,i))) { |
| 142 |
/* If we did any expansions, do brace expansion again. Since |
| 143 |
recursive variable definitions are not allowed, this recursion |
| 144 |
must terminate. (In practice, it's unlikely there will ever be |
| 145 |
more than one level of recursion.) */ |
| 146 |
string save_x = x; |
| 147 |
x = kpathsea_brace_expand_element (kpse, x); |
| 148 |
free (save_x); |
| 149 |
} |
| 150 |
ret = concat3 (ret, x, ENV_SEP_STRING); |
| 151 |
free (save_ret); |
| 152 |
free (x); |
| 153 |
} |
| 154 |
for (i = 0; i != STR_LIST_LENGTH(expansions); ++i) { |
| 155 |
free(STR_LIST_ELT(expansions,i)); |
| 156 |
} |
| 157 |
str_list_free(&expansions); |
| 158 |
ret[strlen (ret) - 1] = 0; /* waste the trailing null */ |
| 159 |
return ret; |
| 160 |
} |
| 161 |
|
| 162 |
/* Be careful to not waste all the memory we allocate for each element. */ |
| 163 |
|
| 164 |
string |
| 165 |
kpathsea_brace_expand (kpathsea kpse, const_string path) |
| 166 |
{ |
| 167 |
string kpse_dot_expansion; |
| 168 |
string elt; |
| 169 |
unsigned len; |
| 170 |
/* Must do variable expansion first because if we have |
| 171 |
foo = .:~ |
| 172 |
TEXINPUTS = $foo |
| 173 |
we want to end up with TEXINPUTS = .:/home/karl. |
| 174 |
Since kpse_path_element is not reentrant, we must get all |
| 175 |
the path elements before we start the loop. */ |
| 176 |
string xpath = kpathsea_var_expand (kpse, path); |
| 177 |
string ret = (string)xmalloc (1); |
| 178 |
*ret = 0; |
| 179 |
|
| 180 |
for (elt = kpathsea_path_element (kpse, xpath); elt; |
| 181 |
elt = kpathsea_path_element (kpse, NULL)) { |
| 182 |
string save_ret = ret; |
| 183 |
/* Do brace expansion first, so tilde expansion happens in {~ka,~kb}. */ |
| 184 |
string expansion = kpathsea_brace_expand_element (kpse, elt); |
| 185 |
ret = concat3 (ret, expansion, ENV_SEP_STRING); |
| 186 |
free (expansion); |
| 187 |
free (save_ret); |
| 188 |
} |
| 189 |
|
| 190 |
/* Waste the last byte by overwriting the trailing env_sep with a null. */ |
| 191 |
len = strlen (ret); |
| 192 |
if (len != 0) |
| 193 |
ret[len - 1] = 0; |
| 194 |
free (xpath); |
| 195 |
|
| 196 |
kpse_dot_expansion = kpathsea_expand_kpse_dot (kpse, ret); |
| 197 |
if (kpse_dot_expansion != ret) |
| 198 |
free (ret); |
| 199 |
|
| 200 |
return kpse_dot_expansion; |
| 201 |
} |
| 202 |
|
| 203 |
#if defined(KPSE_COMPAT_API) |
| 204 |
string |
| 205 |
kpse_brace_expand (const_string path) |
| 206 |
{ |
| 207 |
return kpathsea_brace_expand (kpse_def, path); |
| 208 |
} |
| 209 |
#endif |
| 210 |
|
| 211 |
/* Expand all special constructs in a path, and include only the actually |
| 212 |
existing directories in the result. */ |
| 213 |
string |
| 214 |
kpathsea_path_expand (kpathsea kpse, const_string path) |
| 215 |
{ |
| 216 |
string ret; |
| 217 |
string xpath; |
| 218 |
string elt; |
| 219 |
unsigned len; |
| 220 |
const_string ypath; |
| 221 |
#if defined(WIN32) |
| 222 |
string zpath, p; |
| 223 |
#endif |
| 224 |
|
| 225 |
/* Initialise ret to the empty string. */ |
| 226 |
ret = (string)xmalloc (1); |
| 227 |
*ret = 0; |
| 228 |
len = 0; |
| 229 |
|
| 230 |
#if defined(WIN32) |
| 231 |
zpath = xstrdup (path); |
| 232 |
|
| 233 |
for (p = zpath; *p; p++) |
| 234 |
if (*p == '\\') |
| 235 |
*p = '/'; |
| 236 |
else if (kpathsea_IS_KANJI(kpse, p)) |
| 237 |
p++; |
| 238 |
|
| 239 |
ypath = zpath; |
| 240 |
#else |
| 241 |
ypath = path; |
| 242 |
#endif |
| 243 |
|
| 244 |
/* Expand variables and braces first. */ |
| 245 |
xpath = kpathsea_brace_expand (kpse, ypath); |
| 246 |
|
| 247 |
#if defined(WIN32) |
| 248 |
free (zpath); |
| 249 |
#endif |
| 250 |
|
| 251 |
/* Now expand each of the path elements, printing the results */ |
| 252 |
for (elt = kpathsea_path_element (kpse, xpath); elt; |
| 253 |
elt = kpathsea_path_element (kpse, NULL)) { |
| 254 |
str_llist_type *dirs; |
| 255 |
|
| 256 |
/* Skip and ignore magic leading chars. */ |
| 257 |
if (*elt == '!' && *(elt + 1) == '!') |
| 258 |
elt += 2; |
| 259 |
|
| 260 |
/* Search the disk for all dirs in the component specified. |
| 261 |
Be faster to check the database, but this is more reliable. */ |
| 262 |
dirs = kpathsea_element_dirs (kpse, elt); |
| 263 |
if (dirs && *dirs) { |
| 264 |
str_llist_elt_type *dir; |
| 265 |
|
| 266 |
for (dir = *dirs; dir; dir = STR_LLIST_NEXT (*dir)) { |
| 267 |
string thedir = STR_LLIST (*dir); |
| 268 |
unsigned dirlen = strlen (thedir); |
| 269 |
string save_ret = ret; |
| 270 |
/* We need to retain trailing slash if that's the root directory. |
| 271 |
* On unix, "/" is root dir, "" often taken to be current dir. |
| 272 |
* On windows, "C:/" is root dir of drive C, and "C:" is current |
| 273 |
* on drive C. There's no need to look at other cases, like UNC |
| 274 |
* names. |
| 275 |
*/ |
| 276 |
if (dirlen == 1 || (dirlen == 3 && NAME_BEGINS_WITH_DEVICE (thedir) |
| 277 |
&& IS_DIR_SEP (thedir[2]))) { |
| 278 |
ret = concat3 (ret, thedir, ENV_SEP_STRING); |
| 279 |
len += dirlen + 1; |
| 280 |
ret[len - 1] = ENV_SEP; |
| 281 |
} else { |
| 282 |
ret = concat (ret, thedir); |
| 283 |
len += dirlen; |
| 284 |
ret [len - 1] = ENV_SEP; |
| 285 |
} |
| 286 |
free (save_ret); |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
/* Get rid of trailing ':', if any. */ |
| 291 |
if (len != 0) |
| 292 |
ret[len - 1] = 0; |
| 293 |
return ret; |
| 294 |
} |
| 295 |
|
| 296 |
#if defined(KPSE_COMPAT_API) |
| 297 |
string |
| 298 |
kpse_path_expand (const_string path) |
| 299 |
{ |
| 300 |
return kpathsea_path_expand (kpse_def, path); |
| 301 |
} |
| 302 |
#endif |
| 303 |
|
| 304 |
/* ... */ |
| 305 |
static void expand_append (str_list_type* partial, |
| 306 |
const_string text, const_string p) |
| 307 |
{ |
| 308 |
string new_string; |
| 309 |
unsigned len; |
| 310 |
str_list_type tmp; |
| 311 |
tmp = str_list_init(); |
| 312 |
len = p - text; |
| 313 |
new_string = (string)xmalloc(len+1); |
| 314 |
strncpy(new_string, text, len); |
| 315 |
new_string[len]=0; |
| 316 |
str_list_add(&tmp, new_string); |
| 317 |
str_list_concat_elements(partial, tmp); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
static str_list_type |
| 322 |
brace_expand (kpathsea kpse, const_string *text) |
| 323 |
{ |
| 324 |
str_list_type result, partial, recurse; |
| 325 |
const_string p; |
| 326 |
result = str_list_init(); |
| 327 |
partial = str_list_init(); |
| 328 |
for (p = *text; *p && *p != '}'; ++p) { |
| 329 |
/* FIXME: Should be IS_ENV_SEP(*p) */ |
| 330 |
if (*p == ENV_SEP || *p == ',') { |
| 331 |
expand_append(&partial, *text, p); |
| 332 |
str_list_concat(&result, partial); |
| 333 |
str_list_free(&partial); |
| 334 |
*text = p+1; |
| 335 |
partial = str_list_init(); |
| 336 |
} else if (*p == '{') { |
| 337 |
expand_append(&partial, *text, p); |
| 338 |
++p; |
| 339 |
recurse = brace_expand(kpse, &p); |
| 340 |
str_list_concat_elements(&partial, recurse); |
| 341 |
str_list_free(&recurse); |
| 342 |
/* Check for missing closing brace. */ |
| 343 |
if (*p != '}') { |
| 344 |
WARNING1 ("kpathsea: %s: Unmatched {", *text); |
| 345 |
--p; /* undo ++p above for the next iteration, |
| 346 |
to avoid potential buffer overrun */ |
| 347 |
} |
| 348 |
*text = p+1; |
| 349 |
} else if (*p == '$') { |
| 350 |
/* Skip ${VAR} */ |
| 351 |
if (*(p+1) == '{') |
| 352 |
for (p+=2; *p!='}';++p); |
| 353 |
} |
| 354 |
#if defined(WIN32) |
| 355 |
else if (kpathsea_IS_KANJI(kpse, p)) |
| 356 |
p++; |
| 357 |
#endif |
| 358 |
} |
| 359 |
expand_append(&partial, *text, p); |
| 360 |
str_list_concat(&result, partial); |
| 361 |
str_list_free(&partial); |
| 362 |
*text = p; |
| 363 |
return result; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
#if defined (TEST) |
| 369 |
#include <stdio.h> |
| 370 |
|
| 371 |
fatal_error (format, arg1, arg2) |
| 372 |
char *format, *arg1, *arg2; |
| 373 |
{ |
| 374 |
report_error (format, arg1, arg2); |
| 375 |
exit (1); |
| 376 |
} |
| 377 |
|
| 378 |
report_error (format, arg1, arg2) |
| 379 |
char *format, *arg1, *arg2; |
| 380 |
{ |
| 381 |
fprintf (stderr, format, arg1, arg2); |
| 382 |
fprintf (stderr, "\n"); |
| 383 |
} |
| 384 |
|
| 385 |
int |
| 386 |
main (int argc, char **argv) |
| 387 |
{ |
| 388 |
char example[256]; |
| 389 |
char *result; |
| 390 |
|
| 391 |
kpse_set_program_name(argv[0], NULL); |
| 392 |
result = kpse_brace_expand ("a{\0exebad"); |
| 393 |
printf ("%s\n", result); |
| 394 |
|
| 395 |
#if 0 /* if you want an interactive loop */ |
| 396 |
for (;;) |
| 397 |
{ |
| 398 |
int i; |
| 399 |
|
| 400 |
fprintf (stderr, "brace_expand> "); |
| 401 |
|
| 402 |
if ((!fgets (example, 256, stdin)) |
| 403 |
|| strncmp (example, "quit", 4) == 0) |
| 404 |
break; |
| 405 |
|
| 406 |
if (strlen (example)) |
| 407 |
example[strlen (example) - 1] = 0; |
| 408 |
|
| 409 |
result = kpse_brace_expand (example); |
| 410 |
printf ("%s\n", result); |
| 411 |
} |
| 412 |
#endif |
| 413 |
return 0; |
| 414 |
|
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
#endif /* TEST */ |
| 419 |
|
| 420 |
/* |
| 421 |
Local variables: |
| 422 |
standalone-compile-command: "gcc -g -DMAKE_KPSE_DLL -I. -I.. -I$kp -I$kp/.. -DTEST $kp/expand.c .libs/libkpathsea.a" |
| 423 |
end: |
| 424 |
*/ |