]> 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 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 # define strdup __strdup
86
87 /* We need locking here since we can be called from different palces. */
88 # include <bits/libc-lock.h>
89
90 __libc_lock_define_initialized (static, lock);
91 #endif
92
93
94 /* For those loosing systems which don't have `alloca' we have to add
95 some additional code emulating it. */
96 #ifdef HAVE_ALLOCA
97 /* Nothing has to be done. */
98 # define ADD_BLOCK(list, address) /* nothing */
99 # define FREE_BLOCKS(list) /* nothing */
100 #else
101 struct block_list
102 {
103 void *address;
104 struct block_list *next;
105 };
106 # define ADD_BLOCK(list, addr) \
107 do { \
108 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
109 /* If we cannot get a free block we cannot add the new element to \
110 the list. */ \
111 if (newp != NULL) { \
112 newp->address = (addr); \
113 newp->next = (list); \
114 (list) = newp; \
115 } \
116 } while (0)
117 # define FREE_BLOCKS(list) \
118 do { \
119 while (list != NULL) { \
120 struct block_list *old = list; \
121 list = list->next; \
122 free (old); \
123 } \
124 } while (0)
125 # undef alloca
126 # define alloca(size) (malloc (size))
127 #endif /* have alloca */
128
129
130 struct alias_map
131 {
132 const char *alias;
133 const char *value;
134 };
135
136
137 static struct alias_map *map;
138 static size_t nmap = 0;
139 static size_t maxmap = 0;
140
141
142 /* Prototypes for local functions. */
143 static size_t read_alias_file PARAMS ((const char *fname, int fname_len));
144 static void extend_alias_table PARAMS ((void));
145 static int alias_compare PARAMS ((const struct alias_map *map1,
146 const struct alias_map *map2));
147
148
149 const char *
150 _nl_expand_alias (name)
151 const char *name;
152 {
153 static const char *locale_alias_path = LOCALE_ALIAS_PATH;
154 struct alias_map *retval;
155 const char *result = NULL;
156 size_t added;
157
158 #ifdef _LIBC
159 __libc_lock_lock (lock);
160 #endif
161
162 do
163 {
164 struct alias_map item;
165
166 item.alias = name;
167
168 if (nmap > 0)
169 retval = (struct alias_map *) bsearch (&item, map, nmap,
170 sizeof (struct alias_map),
171 (int (*) PARAMS ((const void *,
172 const void *))
173 ) alias_compare);
174 else
175 retval = NULL;
176
177 /* We really found an alias. Return the value. */
178 if (retval != NULL)
179 {
180 result = retval->value;
181 break;
182 }
183
184 /* Perhaps we can find another alias file. */
185 added = 0;
186 while (added == 0 && locale_alias_path[0] != '\0')
187 {
188 const char *start;
189
190 while (locale_alias_path[0] == ':')
191 ++locale_alias_path;
192 start = locale_alias_path;
193
194 while (locale_alias_path[0] != '\0' && locale_alias_path[0] != ':')
195 ++locale_alias_path;
196
197 if (start < locale_alias_path)
198 added = read_alias_file (start, locale_alias_path - start);
199 }
200 }
201 while (added != 0);
202
203 #ifdef _LIBC
204 __libc_lock_unlock (lock);
205 #endif
206
207 return result;
208 }
209
210
211 static size_t
212 read_alias_file (fname, fname_len)
213 const char *fname;
214 int fname_len;
215 {
216 #ifndef HAVE_ALLOCA
217 struct block_list *block_list = NULL;
218 #endif
219 FILE *fp;
220 char *full_fname;
221 size_t added;
222 static const char aliasfile[] = "/locale.alias";
223
224 full_fname = (char *) alloca (fname_len + sizeof aliasfile);
225 ADD_BLOCK (block_list, full_fname);
226 memcpy (full_fname, fname, fname_len);
227 memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
228
229 fp = fopen (full_fname, "r");
230 if (fp == NULL)
231 {
232 FREE_BLOCKS (block_list);
233 return 0;
234 }
235
236 added = 0;
237 while (!feof (fp))
238 {
239 /* It is a reasonable approach to use a fix buffer here because
240 a) we are only interested in the first two fields
241 b) these fields must be usable as file names and so must not
242 be that long
243 */
244 char buf[BUFSIZ];
245 char *alias;
246 char *value;
247 char *cp;
248
249 if (fgets (buf, BUFSIZ, fp) == NULL)
250 /* EOF reached. */
251 break;
252
253 cp = buf;
254 /* Ignore leading white space. */
255 while (isspace (cp[0]))
256 ++cp;
257
258 /* A leading '#' signals a comment line. */
259 if (cp[0] != '\0' && cp[0] != '#')
260 {
261 alias = cp++;
262 while (cp[0] != '\0' && !isspace (cp[0]))
263 ++cp;
264 /* Terminate alias name. */
265 if (cp[0] != '\0')
266 *cp++ = '\0';
267
268 /* Now look for the beginning of the value. */
269 while (isspace (cp[0]))
270 ++cp;
271
272 if (cp[0] != '\0')
273 {
274 char *tp;
275 size_t len;
276
277 value = cp++;
278 while (cp[0] != '\0' && !isspace (cp[0]))
279 ++cp;
280 /* Terminate value. */
281 if (cp[0] == '\n')
282 {
283 /* This has to be done to make the following test
284 for the end of line possible. We are looking for
285 the terminating '\n' which do not overwrite here. */
286 *cp++ = '\0';
287 *cp = '\n';
288 }
289 else if (cp[0] != '\0')
290 *cp++ = '\0';
291
292 if (nmap >= maxmap)
293 extend_alias_table ();
294
295 #if defined _LIBC || defined HAVE_STRDUP
296 map[nmap].alias = strdup (alias);
297 map[nmap].value = strdup (value);
298 if (map[nmap].alias == NULL || map[nmap].value == NULL)
299 {
300 FREE_BLOCKS (block_list);
301 return added;
302 }
303 #else
304 len = strlen (alias) + 1;
305 tp = (char *) malloc (len);
306 if (tp == NULL)
307 {
308 FREE_BLOCKS (block_list);
309 return added;
310 }
311 memcpy (tp, alias, len);
312 map[nmap].alias = tp;
313
314 len = strlen (value) + 1;
315 tp = (char *) malloc (len);
316 if (tp == NULL)
317 {
318 FREE_BLOCKS (block_list);
319 return added;
320 }
321 memcpy (tp, value, len);
322 map[nmap].value = tp;
323 #endif
324
325 ++nmap;
326 ++added;
327 }
328 }
329
330 /* Possibly not the whole line fits into the buffer. Ignore
331 the rest of the line. */
332 while (strchr (cp, '\n') == NULL)
333 {
334 cp = buf;
335 if (fgets (buf, BUFSIZ, fp) == NULL)
336 /* Make sure the inner loop will be left. The outer loop
337 will exit at the `feof' test. */
338 *cp = '\n';
339 }
340 }
341
342 /* Should we test for ferror()? I think we have to silently ignore
343 errors. --drepper */
344 fclose (fp);
345
346 if (added > 0)
347 qsort (map, nmap, sizeof (struct alias_map),
348 (int (*) PARAMS ((const void *, const void *))) alias_compare);
349
350 FREE_BLOCKS (block_list);
351 return added;
352 }
353
354
355 static void
356 extend_alias_table ()
357 {
358 size_t new_size;
359 struct alias_map *new_map;
360
361 new_size = maxmap == 0 ? 100 : 2 * maxmap;
362 new_map = (struct alias_map *) malloc (new_size
363 * sizeof (struct alias_map));
364 if (new_map == NULL)
365 /* Simply don't extend: we don't have any more core. */
366 return;
367
368 memcpy (new_map, map, nmap * sizeof (struct alias_map));
369
370 if (maxmap != 0)
371 free (map);
372
373 map = new_map;
374 maxmap = new_size;
375 }
376
377
378 static int
379 alias_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. */
397 c1 = isupper (*p1) ? tolower (*p1) : *p1;
398 c2 = isupper (*p2) ? tolower (*p2) : *p2;
399 if (c1 == '\0')
400 break;
401 ++p1;
402 ++p2;
403 }
404 while (c1 == c2);
405
406 return c1 - c2;
407 #endif
408 }