dvitomp fix from Akira
[mplib] / src / texk / kpathsea / path-elt.c
1 /* path-elt.c: return the stuff between colons.
2
3    Copyright 1993, 1996 2008 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 /* 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;
29
30 /* The path we're currently working on.  */
31 static const_string path = NULL;
32
33
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.  */
41
42 static string
43 element P2C(const_string, passed_path,  boolean, env_p)
44 {
45   const_string p;
46   string ret;
47   int brace_level;
48   unsigned len;
49   
50   if (passed_path)
51     path = passed_path;
52   /* Check if called with NULL, and no previous path (perhaps we reached
53      the end).  */
54   else if (!path)
55     return NULL;
56   
57   /* OK, we have a non-null `path' if we get here.  */
58   assert (path);
59   p = path;
60   
61   /* Find the next colon not enclosed by braces (or the end of the path).  */
62   brace_level = 0;
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;
67     ++p;
68   }
69    
70   /* Return the substring starting at `path'.  */
71   len = p - path;
72
73   /* Make sure we have enough space (including the null byte).  */
74   if (len + 1 > elt_alloc)
75     {
76       elt_alloc = len + 1;
77       elt = (string)xrealloc (elt, elt_alloc);
78     }
79
80   strncpy (elt, path, len);
81   elt[len] = 0;
82   ret = elt;
83
84   /* If we are at the end, return NULL next time.  */
85   if (path[len] == 0)
86     path = NULL;
87   else
88     path += len + 1;
89
90   return ret;
91 }
92
93 string
94 kpse_path_element P1C(const_string, p)
95 {
96   return element (p, true);
97 }
98
99 string
100 kpse_filename_component P1C(const_string, p)
101 {
102   return element (p, false);
103 }
104 \f
105 #ifdef TEST
106
107 void
108 print_path_elements (const_string path)
109 {
110   string elt;
111   printf ("Elements of `%s':", path ? path : "(null)");
112   
113   for (elt = kpse_path_element (path); elt != NULL;
114        elt = kpse_path_element (NULL))
115     {
116       printf (" %s", *elt ? elt : "`'");
117     }
118   
119   puts (".");
120 }
121
122 int
123 main ()
124 {
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 */ 
134   
135   return 0;
136 }
137
138 #endif /* TEST */
139
140 \f
141 /*
142 Local variables:
143 standalone-compile-command: "gcc -g -I. -I.. -DTEST path-elt.c kpathsea.a"
144 End:
145 */