1 /* path-elt.c: return the stuff between colons.
3 Copyright 1993, 1996 2008 Karl Berry.
4 Copyright 1997, 2001, 2005 Olaf Weber.
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.
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.
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/>. */
19 #include <kpathsea/config.h>
21 #include <kpathsea/c-pathch.h>
22 #include <kpathsea/pathsearch.h>
25 /* The static (but dynamically allocated) area we return the answer in,
26 and how much we've currently allocated for it. */
27 static string elt = NULL;
28 static unsigned elt_alloc = 0;
30 /* The path we're currently working on. */
31 static const_string path = NULL;
34 /* Upon entry, the static `path' is at the first (and perhaps last)
35 character of the return value, or else NULL if we're at the end (or
36 haven't been called). I make no provision for caching the results;
37 thus, we parse the same path over and over, on every lookup. If that
38 turns out to be a significant lose, it can be fixed, but I'm guessing
39 disk accesses overwhelm everything else. If ENV_P is true, use
40 IS_ENV_SEP; else use IS_DIR_SEP. */
43 element P2C(const_string, passed_path, boolean, env_p)
52 /* Check if called with NULL, and no previous path (perhaps we reached
57 /* OK, we have a non-null `path' if we get here. */
61 /* Find the next colon not enclosed by braces (or the end of the path). */
63 while (*p != 0 && !(brace_level == 0
64 && (env_p ? IS_ENV_SEP (*p) : IS_DIR_SEP (*p)))) {
65 if (*p == '{') ++brace_level;
66 else if (*p == '}') --brace_level;
70 /* Return the substring starting at `path'. */
73 /* Make sure we have enough space (including the null byte). */
74 if (len + 1 > elt_alloc)
77 elt = (string)xrealloc (elt, elt_alloc);
80 strncpy (elt, path, len);
84 /* If we are at the end, return NULL next time. */
94 kpse_path_element P1C(const_string, p)
96 return element (p, true);
100 kpse_filename_component P1C(const_string, p)
102 return element (p, false);
108 print_path_elements (const_string path)
111 printf ("Elements of `%s':", path ? path : "(null)");
113 for (elt = kpse_path_element (path); elt != NULL;
114 elt = kpse_path_element (NULL))
116 printf (" %s", *elt ? elt : "`'");
125 /* All lists end with NULL. */
126 print_path_elements (NULL); /* */
127 print_path_elements (""); /* "" */
128 print_path_elements ("a"); /* a */
129 print_path_elements (ENV_SEP_STRING); /* "", "" */
130 print_path_elements (ENV_SEP_STRING ENV_SEP_STRING); /* "", "", "" */
131 print_path_elements ("a" ENV_SEP_STRING); /* a, "" */
132 print_path_elements (ENV_SEP_STRING "b"); /* "", b */
133 print_path_elements ("a" ENV_SEP_STRING "b"); /* a, b */
143 standalone-compile-command: "gcc -g -I. -I.. -DTEST path-elt.c kpathsea.a"