]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/intl/localealias.c
valarray_array.h: Fully qualify standard functions with std::, thus avoiding Koenig...
[thirdparty/gcc.git] / gcc / intl / localealias.c
CommitLineData
a94c6ebe 1/* Handle aliases for locale names.
71a94577 2 Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc.
a94c6ebe 3
71a94577
ZW
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published
6 by the Free Software Foundation; either version 2, or (at your option)
a94c6ebe
JL
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
71a94577
ZW
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 USA. */
18
19/* Tell glibc's <string.h> to provide a prototype for mempcpy().
20 This must come before <config.h> because <config.h> may include
21 <features.h>, and once <features.h> has been included, it's too late. */
22#ifndef _GNU_SOURCE
23# define _GNU_SOURCE 1
24#endif
a94c6ebe
JL
25
26#ifdef HAVE_CONFIG_H
27# include <config.h>
28#endif
29
30#include <ctype.h>
31#include <stdio.h>
32#include <sys/types.h>
33
34#ifdef __GNUC__
35# define alloca __builtin_alloca
36# define HAVE_ALLOCA 1
37#else
38# if defined HAVE_ALLOCA_H || defined _LIBC
39# include <alloca.h>
40# else
41# ifdef _AIX
42 #pragma alloca
43# else
44# ifndef alloca
45char *alloca ();
46# endif
47# endif
48# endif
49#endif
50
71a94577 51#include <stdlib.h>
a94c6ebe 52
71a94577 53#include <string.h>
a94c6ebe
JL
54#if !HAVE_STRCHR && !defined _LIBC
55# ifndef strchr
56# define strchr index
57# endif
58#endif
59
0309cd88
DR
60#ifdef STRING_WITH_STRINGS
61#include <strings.h>
62#endif
63
a94c6ebe
JL
64#include "gettextP.h"
65
66/* @@ end of prolog @@ */
67
68#ifdef _LIBC
69/* Rename the non ANSI C functions. This is required by the standard
70 because some ANSI C functions will require linking with this object
71 file and the name space must not be polluted. */
72# define strcasecmp __strcasecmp
73
71a94577
ZW
74# ifndef mempcpy
75# define mempcpy __mempcpy
76# endif
a94c6ebe
JL
77# define HAVE_MEMPCPY 1
78
79/* We need locking here since we can be called from different places. */
80# include <bits/libc-lock.h>
81
82__libc_lock_define_initialized (static, lock);
83#endif
84
71a94577
ZW
85#ifndef internal_function
86# define internal_function
87#endif
a94c6ebe 88
71a94577 89/* For those losing systems which don't have `alloca' we have to add
a94c6ebe
JL
90 some additional code emulating it. */
91#ifdef HAVE_ALLOCA
71a94577 92# define freea(p) /* nothing */
a94c6ebe 93#else
71a94577
ZW
94# define alloca(n) malloc (n)
95# define freea(p) free (p)
96#endif
97
98#if defined _LIBC_REENTRANT || defined HAVE_FGETS_UNLOCKED
99# undef fgets
100# define fgets(buf, len, s) fgets_unlocked (buf, len, s)
101#endif
102#if defined _LIBC_REENTRANT || defined HAVE_FEOF_UNLOCKED
103# undef feof
104# define feof(s) feof_unlocked (s)
105#endif
a94c6ebe
JL
106
107
108struct alias_map
109{
110 const char *alias;
111 const char *value;
112};
113
114
71a94577
ZW
115static char *string_space;
116static size_t string_space_act;
117static size_t string_space_max;
a94c6ebe 118static struct alias_map *map;
71a94577
ZW
119static size_t nmap;
120static size_t maxmap;
a94c6ebe
JL
121
122
123/* Prototypes for local functions. */
124static size_t read_alias_file PARAMS ((const char *fname, int fname_len))
125 internal_function;
71a94577 126static int extend_alias_table PARAMS ((void));
a94c6ebe
JL
127static int alias_compare PARAMS ((const struct alias_map *map1,
128 const struct alias_map *map2));
129
130
131const char *
132_nl_expand_alias (name)
133 const char *name;
134{
135 static const char *locale_alias_path = LOCALE_ALIAS_PATH;
136 struct alias_map *retval;
137 const char *result = NULL;
138 size_t added;
139
140#ifdef _LIBC
141 __libc_lock_lock (lock);
142#endif
143
144 do
145 {
146 struct alias_map item;
147
148 item.alias = name;
149
150 if (nmap > 0)
151 retval = (struct alias_map *) bsearch (&item, map, nmap,
152 sizeof (struct alias_map),
153 (int (*) PARAMS ((const void *,
154 const void *))
155 ) alias_compare);
156 else
157 retval = NULL;
158
159 /* We really found an alias. Return the value. */
160 if (retval != NULL)
161 {
162 result = retval->value;
163 break;
164 }
165
166 /* Perhaps we can find another alias file. */
167 added = 0;
168 while (added == 0 && locale_alias_path[0] != '\0')
169 {
170 const char *start;
171
71a94577 172 while (locale_alias_path[0] == PATH_SEPARATOR)
a94c6ebe
JL
173 ++locale_alias_path;
174 start = locale_alias_path;
175
71a94577
ZW
176 while (locale_alias_path[0] != '\0'
177 && locale_alias_path[0] != PATH_SEPARATOR)
a94c6ebe
JL
178 ++locale_alias_path;
179
180 if (start < locale_alias_path)
181 added = read_alias_file (start, locale_alias_path - start);
182 }
183 }
184 while (added != 0);
185
186#ifdef _LIBC
187 __libc_lock_unlock (lock);
188#endif
189
190 return result;
191}
192
193
194static size_t
195internal_function
196read_alias_file (fname, fname_len)
197 const char *fname;
198 int fname_len;
199{
a94c6ebe
JL
200 FILE *fp;
201 char *full_fname;
202 size_t added;
203 static const char aliasfile[] = "/locale.alias";
204
205 full_fname = (char *) alloca (fname_len + sizeof aliasfile);
71a94577
ZW
206#ifdef HAVE_MEMPCPY
207 mempcpy (mempcpy (full_fname, fname, fname_len),
208 aliasfile, sizeof aliasfile);
209#else
a94c6ebe
JL
210 memcpy (full_fname, fname, fname_len);
211 memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
71a94577 212#endif
a94c6ebe
JL
213
214 fp = fopen (full_fname, "r");
71a94577 215 freea (full_fname);
a94c6ebe 216 if (fp == NULL)
71a94577 217 return 0;
a94c6ebe
JL
218
219 added = 0;
220 while (!feof (fp))
221 {
222 /* It is a reasonable approach to use a fix buffer here because
223 a) we are only interested in the first two fields
224 b) these fields must be usable as file names and so must not
225 be that long
226 */
71a94577
ZW
227 char buf[BUFSIZ];
228 char *alias;
229 char *value;
230 char *cp;
a94c6ebe 231
71a94577 232 if (fgets (buf, sizeof buf, fp) == NULL)
a94c6ebe
JL
233 /* EOF reached. */
234 break;
235
236 /* Possibly not the whole line fits into the buffer. Ignore
237 the rest of the line. */
71a94577 238 if (strchr (buf, '\n') == NULL)
a94c6ebe
JL
239 {
240 char altbuf[BUFSIZ];
241 do
242 if (fgets (altbuf, sizeof altbuf, fp) == NULL)
243 /* Make sure the inner loop will be left. The outer loop
244 will exit at the `feof' test. */
245 break;
246 while (strchr (altbuf, '\n') == NULL);
247 }
248
249 cp = buf;
250 /* Ignore leading white space. */
e6313aee 251 while (isspace ((unsigned char)cp[0]))
a94c6ebe
JL
252 ++cp;
253
254 /* A leading '#' signals a comment line. */
255 if (cp[0] != '\0' && cp[0] != '#')
256 {
257 alias = cp++;
e6313aee 258 while (cp[0] != '\0' && !isspace ((unsigned char)cp[0]))
a94c6ebe
JL
259 ++cp;
260 /* Terminate alias name. */
261 if (cp[0] != '\0')
262 *cp++ = '\0';
263
264 /* Now look for the beginning of the value. */
e6313aee 265 while (isspace ((unsigned char)cp[0]))
a94c6ebe
JL
266 ++cp;
267
268 if (cp[0] != '\0')
269 {
270 size_t alias_len;
271 size_t value_len;
272
273 value = cp++;
e6313aee 274 while (cp[0] != '\0' && !isspace ((unsigned char)cp[0]))
a94c6ebe
JL
275 ++cp;
276 /* Terminate value. */
277 if (cp[0] == '\n')
278 {
279 /* This has to be done to make the following test
280 for the end of line possible. We are looking for
281 the terminating '\n' which do not overwrite here. */
282 *cp++ = '\0';
283 *cp = '\n';
284 }
285 else if (cp[0] != '\0')
286 *cp++ = '\0';
287
288 if (nmap >= maxmap)
71a94577
ZW
289 if (__builtin_expect (extend_alias_table (), 0))
290 return added;
a94c6ebe 291
71a94577
ZW
292 alias_len = strlen (alias) + 1;
293 value_len = strlen (value) + 1;
a94c6ebe
JL
294
295 if (string_space_act + alias_len + value_len > string_space_max)
296 {
297 /* Increase size of memory pool. */
298 size_t new_size = (string_space_max
299 + (alias_len + value_len > 1024
300 ? alias_len + value_len : 1024));
301 char *new_pool = (char *) realloc (string_space, new_size);
302 if (new_pool == NULL)
71a94577
ZW
303 return added;
304
305 if (__builtin_expect (string_space != new_pool, 0))
a94c6ebe 306 {
71a94577
ZW
307 size_t i;
308
309 for (i = 0; i < nmap; i++)
310 {
311 map[i].alias += new_pool - string_space;
312 map[i].value += new_pool - string_space;
313 }
a94c6ebe 314 }
71a94577 315
a94c6ebe
JL
316 string_space = new_pool;
317 string_space_max = new_size;
318 }
319
603832a0
PT
320 memcpy (&string_space[string_space_act], alias, alias_len);
321 map[nmap].alias = &string_space[string_space_act];
a94c6ebe
JL
322 string_space_act += alias_len;
323
603832a0
PT
324 memcpy (&string_space[string_space_act], value, value_len);
325 map[nmap].value = &string_space[string_space_act];
a94c6ebe
JL
326 string_space_act += value_len;
327
328 ++nmap;
329 ++added;
330 }
331 }
332 }
333
334 /* Should we test for ferror()? I think we have to silently ignore
335 errors. --drepper */
336 fclose (fp);
337
338 if (added > 0)
339 qsort (map, nmap, sizeof (struct alias_map),
340 (int (*) PARAMS ((const void *, const void *))) alias_compare);
341
a94c6ebe
JL
342 return added;
343}
344
345
71a94577 346static int
a94c6ebe
JL
347extend_alias_table ()
348{
349 size_t new_size;
350 struct alias_map *new_map;
351
352 new_size = maxmap == 0 ? 100 : 2 * maxmap;
353 new_map = (struct alias_map *) realloc (map, (new_size
354 * sizeof (struct alias_map)));
355 if (new_map == NULL)
356 /* Simply don't extend: we don't have any more core. */
71a94577 357 return -1;
a94c6ebe
JL
358
359 map = new_map;
360 maxmap = new_size;
71a94577 361 return 0;
a94c6ebe
JL
362}
363
364
365#ifdef _LIBC
366static void __attribute__ ((unused))
367free_mem (void)
368{
369 if (string_space != NULL)
370 free (string_space);
371 if (map != NULL)
372 free (map);
373}
374text_set_element (__libc_subfreeres, free_mem);
375#endif
376
377
378static int
379alias_compare (map1, map2)
380 const struct alias_map *map1;
381 const struct alias_map *map2;
382{
383#if defined _LIBC || defined HAVE_STRCASECMP
384 return strcasecmp (map1->alias, map2->alias);
385#else
386 const unsigned char *p1 = (const unsigned char *) map1->alias;
387 const unsigned char *p2 = (const unsigned char *) map2->alias;
388 unsigned char c1, c2;
389
390 if (p1 == p2)
391 return 0;
392
393 do
394 {
395 /* I know this seems to be odd but the tolower() function in
396 some systems libc cannot handle nonalpha characters. */
e6313aee
KG
397 c1 = isupper ((unsigned char)*p1) ? tolower ((unsigned char)*p1) : *p1;
398 c2 = isupper ((unsigned char)*p2) ? tolower ((unsigned char)*p2) : *p2;
a94c6ebe
JL
399 if (c1 == '\0')
400 break;
401 ++p1;
402 ++p2;
403 }
404 while (c1 == c2);
405
406 return c1 - c2;
407#endif
408}