* SDCPP synchronized with GCC CPP release version 3.4.6,
[fw/sdcc] / support / cpp2 / prefix.c
1 /* Utility to update paths from internal to external forms.
2    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Library General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or (at
10 your option) any later version.
11
12 GCC 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 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public
18 License along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 /* This file contains routines to update a path, both to canonicalize
23    the directory format and to handle any prefix translation.
24
25    This file must be compiled with -DPREFIX= to specify the "prefix"
26    value used by configure.  If a filename does not begin with this
27    prefix, it will not be affected other than by directory canonicalization.
28
29    Each caller of 'update_path' may specify both a filename and
30    a translation prefix and consist of the name of the package that contains
31    the file ("@GCC", "@BINUTIL", "@GNU", etc).
32
33    If the prefix is not specified, the filename will only undergo
34    directory canonicalization.
35
36    If it is specified, the string given by PREFIX will be replaced
37    by the specified prefix (with a '@' in front unless the prefix begins
38    with a '$') and further translation will be done as follows
39    until none of the two conditions below are met:
40
41    1) If the filename begins with '@', the string between the '@' and
42    the end of the name or the first '/' or directory separator will
43    be considered a "key" and looked up as follows:
44
45    -- If this is a Win32 OS, then the Registry will be examined for
46       an entry of "key" in
47
48       HKEY_LOCAL_MACHINE\SOFTWARE\Free Software Foundation\<KEY>
49
50       if found, that value will be used. <KEY> defaults to GCC version
51       string, but can be overridden at configuration time.
52
53    -- If not found (or not a Win32 OS), the environment variable
54       key_ROOT (the value of "key" concatenated with the constant "_ROOT")
55       is tried.  If that fails, then PREFIX (see above) is used.
56
57    2) If the filename begins with a '$', the rest of the string up
58    to the end or the first '/' or directory separator will be used
59    as an environment variable, whose value will be returned.
60
61    Once all this is done, any '/' will be converted to DIR_SEPARATOR,
62    if they are different.
63
64    NOTE:  using resolve_keyed_path under Win32 requires linking with
65    advapi32.dll.  */
66
67
68 #include "config.h"
69 #include "system.h"
70 #if defined(_WIN32) && defined(ENABLE_WIN32_REGISTRY)
71 #include <windows.h>
72 #endif
73 #include "prefix.h"
74
75 static const char *std_prefix = PREFIX;
76
77 static const char *get_key_value (char *);
78 static char *translate_name (char *);
79 static char *save_string (const char *, int);
80 static void tr (char *, int, int);
81
82 #if defined(_WIN32) && defined(ENABLE_WIN32_REGISTRY)
83 static char *lookup_key (char *);
84 static HKEY reg_key = (HKEY) INVALID_HANDLE_VALUE;
85 #endif
86
87 /* Given KEY, as above, return its value.  */
88
89 static const char *
90 get_key_value (char *key)
91 {
92   const char *prefix = 0;
93   char *temp = 0;
94
95 #if defined(_WIN32) && defined(ENABLE_WIN32_REGISTRY)
96   prefix = lookup_key (key);
97 #endif
98
99   if (prefix == 0)
100     prefix = getenv (temp = concat (key, "_ROOT", NULL));
101
102   if (prefix == 0)
103     prefix = std_prefix;
104
105   if (temp)
106     free (temp);
107
108   return prefix;
109 }
110
111 /* Return a copy of a string that has been placed in the heap.  */
112
113 static char *
114 save_string (const char *s, int len)
115 {
116   char *result = xmalloc (len + 1);
117
118   memcpy (result, s, len);
119   result[len] = 0;
120   return result;
121 }
122
123 #if defined(_WIN32) && defined(ENABLE_WIN32_REGISTRY)
124
125 /* Look up "key" in the registry, as above.  */
126
127 static char *
128 lookup_key (char *key)
129 {
130   char *dst;
131   DWORD size;
132   DWORD type;
133   LONG res;
134
135   if (reg_key == (HKEY) INVALID_HANDLE_VALUE)
136     {
137       res = RegOpenKeyExA (HKEY_LOCAL_MACHINE, "SOFTWARE", 0,
138                            KEY_READ, &reg_key);
139
140       if (res == ERROR_SUCCESS)
141         res = RegOpenKeyExA (reg_key, "Free Software Foundation", 0,
142                              KEY_READ, &reg_key);
143
144       if (res == ERROR_SUCCESS)
145         res = RegOpenKeyExA (reg_key, WIN32_REGISTRY_KEY, 0,
146                              KEY_READ, &reg_key);
147
148       if (res != ERROR_SUCCESS)
149         {
150           reg_key = (HKEY) INVALID_HANDLE_VALUE;
151           return 0;
152         }
153     }
154
155   size = 32;
156   dst = xmalloc (size);
157
158   res = RegQueryValueExA (reg_key, key, 0, &type, dst, &size);
159   if (res == ERROR_MORE_DATA && type == REG_SZ)
160     {
161       dst = xrealloc (dst, size);
162       res = RegQueryValueExA (reg_key, key, 0, &type, dst, &size);
163     }
164
165   if (type != REG_SZ || res != ERROR_SUCCESS)
166     {
167       free (dst);
168       dst = 0;
169     }
170
171   return dst;
172 }
173 #endif
174
175 /* If NAME, a malloc-ed string, starts with a '@' or '$', apply the
176    translation rules above and return a newly malloc-ed name.
177    Otherwise, return the given name.  */
178
179 static char *
180 translate_name (char *name)
181 {
182   char code;
183   char *key, *old_name;
184   const char *prefix;
185   int keylen;
186
187   for (;;)
188     {
189       code = name[0];
190       if (code != '@' && code != '$')
191         break;
192
193       for (keylen = 0;
194            (name[keylen + 1] != 0 && !IS_DIR_SEPARATOR (name[keylen + 1]));
195            keylen++)
196         ;
197
198       key = alloca (keylen + 1);
199       strncpy (key, &name[1], keylen);
200       key[keylen] = 0;
201
202       if (code == '@')
203         {
204           prefix = get_key_value (key);
205           if (prefix == 0)
206             prefix = std_prefix;
207         }
208       else
209         prefix = getenv (key);
210
211       if (prefix == 0)
212         prefix = PREFIX;
213
214       /* We used to strip trailing DIR_SEPARATORs here, but that can
215          sometimes yield a result with no separator when one was coded
216          and intended by the user, causing two path components to run
217          together.  */
218
219       old_name = name;
220       name = concat (prefix, &name[keylen + 1], NULL);
221       free (old_name);
222     }
223
224   return name;
225 }
226
227 /* In a NUL-terminated STRING, replace character C1 with C2 in-place.  */
228 static void
229 tr (char *string, int c1, int c2)
230 {
231   do
232     {
233       if (*string == c1)
234         *string = c2;
235     }
236   while (*string++);
237 }
238
239 /* Update PATH using KEY if PATH starts with PREFIX.  The returned
240    string is always malloc-ed, and the caller is responsible for
241    freeing it.  */
242
243 char *
244 update_path (const char *path, const char *key)
245 {
246   char *result, *p;
247
248   if (! strncmp (path, std_prefix, strlen (std_prefix)) && key != 0)
249     {
250       bool free_key = false;
251
252       if (key[0] != '$')
253         {
254           key = concat ("@", key, NULL);
255           free_key = true;
256         }
257
258       result = concat (key, &path[strlen (std_prefix)], NULL);
259       if (free_key)
260         free ((char *) key);
261       result = translate_name (result);
262     }
263   else
264     result = xstrdup (path);
265
266 #ifndef ALWAYS_STRIP_DOTDOT
267 #define ALWAYS_STRIP_DOTDOT 0
268 #endif
269
270   p = result;
271   while (1)
272     {
273       char *src, *dest;
274
275       p = strchr (p, '.');
276       if (p == NULL)
277         break;
278       /* Look for `/../'  */
279       if (p[1] == '.'
280           && IS_DIR_SEPARATOR (p[2])
281           && (p != result && IS_DIR_SEPARATOR (p[-1])))
282         {
283           *p = 0;
284           if (!ALWAYS_STRIP_DOTDOT && access (result, X_OK) == 0)
285             {
286               *p = '.';
287               break;
288             }
289           else
290             {
291               /* We can't access the dir, so we won't be able to
292                  access dir/.. either.  Strip out `dir/../'.  If `dir'
293                  turns out to be `.', strip one more path component.  */
294               dest = p;
295               do
296                 {
297                   --dest;
298                   while (dest != result && IS_DIR_SEPARATOR (*dest))
299                     --dest;
300                   while (dest != result && !IS_DIR_SEPARATOR (dest[-1]))
301                     --dest;
302                 }
303               while (dest != result && *dest == '.');
304               /* If we have something like `./..' or `/..', don't
305                  strip anything more.  */
306               if (*dest == '.' || IS_DIR_SEPARATOR (*dest))
307                 {
308                   *p = '.';
309                   break;
310                 }
311               src = p + 3;
312               while (IS_DIR_SEPARATOR (*src))
313                 ++src;
314               p = dest;
315               while ((*dest++ = *src++) != 0)
316                 ;
317             }
318         }
319       else
320         ++p;
321     }
322
323 #ifdef UPDATE_PATH_HOST_CANONICALIZE
324   /* Perform host dependent canonicalization when needed.  */
325   UPDATE_PATH_HOST_CANONICALIZE (result);
326 #endif
327
328 #ifdef DIR_SEPARATOR_2
329   /* Convert DIR_SEPARATOR_2 to DIR_SEPARATOR.  */
330   if (DIR_SEPARATOR_2 != DIR_SEPARATOR)
331     tr (result, DIR_SEPARATOR_2, DIR_SEPARATOR);
332 #endif
333
334 #if defined (DIR_SEPARATOR) && !defined (DIR_SEPARATOR_2)
335   if (DIR_SEPARATOR != '/')
336     tr (result, '/', DIR_SEPARATOR);
337 #endif
338
339   return result;
340 }
341
342 /* Reset the standard prefix.  */
343 void
344 set_std_prefix (const char *prefix, int len)
345 {
346   std_prefix = save_string (prefix, len);
347 }