OSDN Git Service

* c-common.c: Include <stdlib.h> and <string.h>/<strings.h>.
[pf3gnuchains/gcc-fork.git] / gcc / prefix.c
1 /* Utility to update paths from internal to external forms.
2    Copyright (C) 1997 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 GCC 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 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with GCC; see the file COPYING.  If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 /* This file contains routines to update a path, both to canonicalize
22    the directory format and to handle any prefix translation.
23
24    This file must be compiled with -DPREFIX= to specify the "prefix"
25    value used by configure.  If a filename does not begin with this
26    prefix, it will not be affected other than by directory canonicalization.
27
28    Each caller of 'update_path' may specify both a filename and
29    a translation prefix and consist of the name of the package that contains
30    the file ("@GCC", "@BINUTIL", "@GNU", etc).
31
32    If the prefix is not specified, the filename will only undergo
33    directory canonicalization.
34
35    If it is specified, the string given by PREFIX will be replaced
36    by the specified prefix (with a '@' in front unless the prefix begins
37    with a '$') and further translation will be done as follows
38    until none of the two conditions below are met:
39
40    1) If the filename begins with '@', the string between the '@' and
41    the end of the name or the first '/' or directory separator will
42    be considered a "key" and looked up as follows:
43
44    -- If this is a Win32 OS, then the Registry will be examined for
45       an entry of "key" in 
46
47       HKEY_LOCAL_MACHINE\SOFTWARE\Free Software Foundation\
48
49       if found, that value will be used.
50
51    -- If not found (or not a Win32 OS), the environment variable
52       key_ROOT (the value of "key" concatenated with the constant "_ROOT")
53       is tried.  If that fails, then PREFIX (see above) is used.
54
55    2) If the filename begins with a '$', the rest of the string up
56    to the end or the first '/' or directory separator will be used
57    as an environment variable, whose value will be returned.
58
59    Once all this is done, any '/' will be converted to DIR_SEPARATOR,
60    if they are different. 
61
62    NOTE:  using resolve_keyed_path under Win32 requires linking with
63    advapi32.dll.  */
64
65
66 #include "config.h"
67 #include "gansidecl.h"
68 #ifdef __STDC__
69 #include <stdarg.h>
70 #else
71 #include <varargs.h>
72 #endif
73
74 #ifdef HAVE_STDLIB_H
75 #include <stdlib.h>
76 #endif
77
78 #ifdef HAVE_STRING_H
79 #include <string.h>
80 #else
81 #ifdef HAVE_STRINGS_H
82 #include <strings.h>
83 #endif
84 #endif
85
86 #ifdef _WIN32
87 #include <windows.h>
88 #endif
89
90 static char *get_key_value      PROTO((char *));
91 static char *translate_name     PROTO((char *));
92 static char *concat             PVPROTO((char *, ...));
93 static char *save_string        PROTO((char *, int));
94
95 #ifdef _WIN32
96 static char *lookup_key         PROTO((char *));
97 static HKEY reg_key = (HKEY) INVALID_HANDLE_VALUE;
98 #endif
99
100 extern char *getenv ();
101
102 /* Given KEY, as above, return its value.  */
103
104 static char *
105 get_key_value (key)
106      char *key;
107 {
108   char *prefix = 0;
109
110 #ifdef _WIN32
111   prefix = lookup_key (key);
112 #endif
113
114   if (prefix == 0)
115     prefix = getenv (concat (key, "_ROOT", NULL_PTR));
116
117   if (prefix == 0)
118     prefix = PREFIX;
119
120   return prefix;
121 }
122
123 /* Concatenate a sequence of strings, returning the result.
124
125    This function is based on the one in libiberty.  */
126
127 static char *
128 concat VPROTO((char *first, ...))
129 {
130   register int length;
131   register char *newstr;
132   register char *end;
133   register char *arg;
134   va_list args;
135 #ifndef __STDC__
136   char *first;
137 #endif
138
139   /* First compute the size of the result and get sufficient memory.  */
140
141   VA_START (args, first);
142 #ifndef __STDC__
143   first = va_arg (args, char *);
144 #endif
145
146   arg = first;
147   length = 0;
148
149   while (arg != 0)
150     {
151       length += strlen (arg);
152       arg = va_arg (args, char *);
153     }
154
155   newstr = (char *) malloc (length + 1);
156   va_end (args);
157
158   /* Now copy the individual pieces to the result string.  */
159
160   VA_START (args, first);
161 #ifndef __STDC__
162   first = va_arg (args, char *);
163 #endif
164
165   end = newstr;
166   arg = first;
167   while (arg != 0)
168     {
169       while (*arg)
170         *end++ = *arg++;
171       arg = va_arg (args, char *);
172     }
173   *end = '\000';
174   va_end (args);
175
176   return (newstr);
177 }
178
179 /* Return a copy of a string that has been placed in the heap.  */
180
181 static char *
182 save_string (s, len)
183      char *s;
184      int len;
185 {
186   register char *result = (char *) malloc (len + 1);
187
188   bcopy (s, result, len);
189   result[len] = 0;
190   return result;
191 }
192
193 #ifdef _WIN32
194
195 /* Look up "key" in the registry, as above.  */
196
197 static char *
198 lookup_key (key)
199      char *key;
200 {
201   char *dst;
202   DWORD size;
203   DWORD type;
204   LONG res;
205
206   if (reg_key == (HKEY) INVALID_HANDLE_VALUE)
207     {
208       res = RegOpenKeyExA (HKEY_LOCAL_MACHINE, "SOFTWARE", 0,
209                            KEY_READ, &reg_key);
210
211       if (res == ERROR_SUCCESS)
212         res = RegOpenKeyExA (reg_key, "Free Software Foundation", 0,
213                              KEY_READ, &reg_key);
214
215       if (res != ERROR_SUCCESS)
216         {
217           reg_key = (HKEY) INVALID_HANDLE_VALUE;
218           return 0;
219         }
220     }
221
222   size = 32;
223   dst = (char *) malloc (size);
224
225   res = RegQueryValueExA (reg_key, key, 0, &type, dst, &size);
226   if (res == ERROR_MORE_DATA && type == REG_SZ)
227     {
228       dst = (char *) realloc (dst, size);
229       res = RegQueryValueExA (reg_key, key, 0, &type, dst, &size);
230     }
231
232   if (type != REG_SZ || res != ERROR_SUCCESS)
233     {
234       free (dst);
235       dst = 0;
236     }
237
238   return dst;
239 }
240 #endif
241
242 /* If NAME starts with a '@' or '$', apply the translation rules above
243    and return a new name.  Otherwise, return the given name.  */
244
245 static char *
246 translate_name (name)
247      char *name;
248 {
249   char code = name[0];
250   char *key, *prefix;
251   int keylen;
252
253   if (code != '@' && code != '$')
254     return name;
255
256   for (keylen = 0;
257        (name[keylen + 1] != 0 && name[keylen + 1] != '/'
258 #ifdef DIR_SEPARATOR
259         && name[keylen + 1] != DIR_SEPARATOR
260 #endif
261         );
262        keylen++)
263     ;
264
265   key = alloca (keylen + 1);
266   strncpy (key, &name[1], keylen);
267   key[keylen] = 0;
268
269   name = &name[keylen + 1];
270
271   if (code == '@')
272     {
273       prefix = get_key_value (key);
274       if (prefix == 0)
275         prefix = PREFIX;
276     }
277   else
278     {
279       prefix = getenv (key);
280       if (prefix == 0)
281         prefix = concat ("$", key, NULL_PTR);
282     }
283
284   /* Remove any trailing directory separator from what we got.  */
285   if (prefix[strlen (prefix) - 1] == '/'
286 #ifdef DIR_SEPARATOR
287       || prefix[strlen (prefix) - 1] == DIR_SEPARATOR
288 #endif
289       )
290     {
291       prefix = save_string (prefix, strlen (prefix));
292       prefix[strlen (prefix) - 1] = 0;
293     }
294
295   return concat (prefix, name, NULL_PTR);
296 }
297
298 /* Update PATH using KEY if PATH starts with PREFIX.  */
299
300 char *
301 update_path (path, key)
302      char *path;
303      char *key;
304 {
305   if (! strncmp (path, PREFIX, strlen (PREFIX)) && key != 0)
306     {
307       if (key[0] != '$')
308         key = concat ("@", key, NULL_PTR);
309
310       path = concat (key, &path[strlen (PREFIX)], NULL_PTR);
311
312       while (path[0] == '@' || path[0] == '$')
313         path = translate_name (path);
314     }
315       
316 #ifdef DIR_SEPARATOR
317   if (DIR_SEPARATOR != '/')
318     {
319       int i;
320       int len = strlen (path);
321
322       path = save_string (path, len);
323       for (i = 0; i < len; i++)
324         if (path[i] == '/')
325           path[i] = DIR_SEPARATOR;
326     }
327 #endif
328
329   return path;
330 }