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