| 1 |
/* path-elt.c: return the stuff between colons. |
| 2 |
|
| 3 |
Copyright 1993, 1996, 2008, 2016, 2018 Karl Berry. |
| 4 |
Copyright 1997, 2001, 2005 Olaf Weber. |
| 5 |
|
| 6 |
This library is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU Lesser General Public |
| 8 |
License as published by the Free Software Foundation; either |
| 9 |
version 2.1 of the License, or (at your option) any later version. |
| 10 |
|
| 11 |
This library is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
Lesser General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Lesser General Public License |
| 17 |
along with this library; if not, see <http://www.gnu.org/licenses/>. */ |
| 18 |
|
| 19 |
#include <kpathsea/config.h> |
| 20 |
|
| 21 |
#include <kpathsea/c-pathch.h> |
| 22 |
#include <kpathsea/pathsearch.h> |
| 23 |
|
| 24 |
|
| 25 |
/* Upon entry, the static `path' is at the first (and perhaps last) |
| 26 |
character of the return value, or else NULL if we're at the end (or |
| 27 |
haven't been called). I make no provision for caching the results; |
| 28 |
thus, we parse the same path over and over, on every lookup. If that |
| 29 |
turns out to be a significant lose, it can be fixed, but I'm guessing |
| 30 |
disk accesses overwhelm everything else. If ENV_P is true, use |
| 31 |
IS_KPSE_SEP; else use IS_DIR_SEP. */ |
| 32 |
|
| 33 |
static string |
| 34 |
element (kpathsea kpse, const_string passed_path, boolean env_p) |
| 35 |
{ |
| 36 |
const_string p; |
| 37 |
string ret; |
| 38 |
int brace_level; |
| 39 |
unsigned len; |
| 40 |
|
| 41 |
if (passed_path) |
| 42 |
kpse->path = passed_path; |
| 43 |
/* Check if called with NULL, and no previous path (perhaps we reached |
| 44 |
the end). */ |
| 45 |
else if (!kpse->path) |
| 46 |
return NULL; |
| 47 |
|
| 48 |
/* OK, we have a non-null `path' if we get here. */ |
| 49 |
assert (kpse->path); |
| 50 |
p = kpse->path; |
| 51 |
|
| 52 |
/* Find the next path separator not enclosed by braces (or the end of |
| 53 |
the path). Here we want to check for either : or ; because we might |
| 54 |
be called with the ;-using paths from the default texmf.cnf, e.g., |
| 55 |
with kpsewhich --expand-braces or kpsewhich --var-value, while on a |
| 56 |
Unix system. Otherwise, the path elements are not parsed and thus ~ |
| 57 |
expansion (for example), does not happen except on the first element. |
| 58 |
https://github.com/TeX-Live/texlive-source/issues/3 */ |
| 59 |
brace_level = 0; |
| 60 |
while (*p != 0 && !(brace_level == 0 |
| 61 |
&& (env_p ? IS_KPSE_SEP (*p) : IS_DIR_SEP (*p)))) { |
| 62 |
if (*p == '{') ++brace_level; |
| 63 |
else if (*p == '}') --brace_level; |
| 64 |
#if defined(WIN32) |
| 65 |
else if (kpathsea_IS_KANJI(kpse, p)) |
| 66 |
p++; |
| 67 |
#endif |
| 68 |
p++; |
| 69 |
} |
| 70 |
|
| 71 |
/* Return the substring starting at `path'. */ |
| 72 |
len = p - kpse->path; |
| 73 |
|
| 74 |
/* Make sure we have enough space (including the null byte). */ |
| 75 |
if (len + 1 > kpse->elt_alloc) |
| 76 |
{ |
| 77 |
kpse->elt_alloc = len + 1; |
| 78 |
kpse->elt = (string)xrealloc (kpse->elt, kpse->elt_alloc); |
| 79 |
} |
| 80 |
|
| 81 |
strncpy (kpse->elt, kpse->path, len); |
| 82 |
kpse->elt[len] = 0; |
| 83 |
ret = kpse->elt; |
| 84 |
|
| 85 |
/* If we are at the end, return NULL next time. */ |
| 86 |
if (kpse->path[len] == 0) |
| 87 |
kpse->path = NULL; |
| 88 |
else |
| 89 |
kpse->path += len + 1; |
| 90 |
|
| 91 |
return ret; |
| 92 |
} |
| 93 |
|
| 94 |
string |
| 95 |
kpathsea_path_element (kpathsea kpse, const_string p) |
| 96 |
{ |
| 97 |
return element (kpse, p, true); |
| 98 |
} |
| 99 |
|
| 100 |
string |
| 101 |
kpathsea_filename_component (kpathsea kpse, const_string p) |
| 102 |
{ |
| 103 |
return element (kpse, p, false); |
| 104 |
} |
| 105 |
|
| 106 |
#ifdef TEST |
| 107 |
|
| 108 |
void |
| 109 |
print_path_elements (const_string path) |
| 110 |
{ |
| 111 |
string elt; |
| 112 |
printf ("Elements of `%s':", path ? path : "(null)"); |
| 113 |
|
| 114 |
for (elt = kpathsea_path_element (kpse_def, path); elt != NULL; |
| 115 |
elt = kpathsea_path_element (kpse_def, NULL)) |
| 116 |
{ |
| 117 |
printf (" %s", *elt ? elt : "`'"); |
| 118 |
} |
| 119 |
|
| 120 |
puts ("."); |
| 121 |
} |
| 122 |
|
| 123 |
int |
| 124 |
main () |
| 125 |
{ |
| 126 |
/* All lists end with NULL. */ |
| 127 |
print_path_elements (NULL); /* */ |
| 128 |
print_path_elements (""); /* "" */ |
| 129 |
print_path_elements ("a"); /* a */ |
| 130 |
print_path_elements (ENV_SEP_STRING); /* "", "" */ |
| 131 |
print_path_elements (ENV_SEP_STRING ENV_SEP_STRING); /* "", "", "" */ |
| 132 |
print_path_elements ("a" ENV_SEP_STRING); /* a, "" */ |
| 133 |
print_path_elements (ENV_SEP_STRING "b"); /* "", b */ |
| 134 |
print_path_elements ("a" ENV_SEP_STRING "b"); /* a, b */ |
| 135 |
|
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
#endif /* TEST */ |
| 140 |
|
| 141 |
|
| 142 |
/* |
| 143 |
Local variables: |
| 144 |
standalone-compile-command: "gcc -g -I. -I.. -DTEST path-elt.c kpathsea.a" |
| 145 |
End: |
| 146 |
*/ |