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