lilypond-0.1.56
[lilypond.git] / intl / localealias.c
blobb432e91150c4727419fe921b73b758905d950ade
1 /* localealias.c -- handle aliases for locale names
2 Copyright (C) 1995 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include <ctype.h>
23 #include <stdio.h>
24 #include <sys/types.h>
26 #ifdef __GNUC__
27 # define alloca __builtin_alloca
28 #else
29 # if defined HAVE_ALLOCA_H || defined _LIBC
30 # include <alloca.h>
31 # else
32 # ifdef _AIX
33 #pragma alloca
34 # else
35 # ifndef alloca
36 char *alloca ();
37 # endif
38 # endif
39 # endif
40 #endif
42 #if defined STDC_HEADERS || defined _LIBC
43 # include <stdlib.h>
44 #else
45 char *getenv ();
46 # ifdef HAVE_MALLOC_H
47 # include <malloc.h>
48 # else
49 void free ();
50 # endif
51 #endif
53 #if defined HAVE_STRING_H || defined _LIBC
54 # include <string.h>
55 #else
56 # include <strings.h>
57 #endif
58 #if !HAVE_STRCHR && !defined _LIBC
59 # ifndef strchr
60 # define strchr index
61 # endif
62 #endif
64 #include "gettext.h"
65 #include "gettextP.h"
67 /* @@ end of prolog @@ */
69 #ifdef _LIBC
70 /* Rename the non ANSI C functions. This is required by the standard
71 because some ANSI C functions will require linking with this object
72 file and the name space must not be polluted. */
73 # define strcasecmp __strcasecmp
74 #endif
76 struct alias_map
78 const char *alias;
79 const char *value;
83 static struct alias_map *map;
84 static size_t nmap = 0;
85 static size_t maxmap = 0;
88 /* Prototypes for local functions. */
89 static size_t read_alias_file __P ((const char *fname, int fname_len));
90 static void extend_alias_table __P ((void));
91 static int alias_compare __P ((const struct alias_map *map1,
92 const struct alias_map *map2));
95 const char *
96 _nl_expand_alias (name)
97 const char *name;
99 static const char *locale_alias_path = LOCALE_ALIAS_PATH;
100 struct alias_map *retval;
101 size_t added;
105 struct alias_map item;
107 item.alias = name;
109 if (nmap > 0)
110 retval = (struct alias_map *) bsearch (&item, map, nmap,
111 sizeof (struct alias_map),
112 (int (*) __P ((const void *,
113 const void *)))
114 alias_compare);
115 else
116 retval = NULL;
118 /* We really found an alias. Return the value. */
119 if (retval != NULL)
120 return retval->value;
122 /* Perhaps we can find another alias file. */
123 added = 0;
124 while (added == 0 && locale_alias_path[0] != '\0')
126 const char *start;
128 while (locale_alias_path[0] == ':')
129 ++locale_alias_path;
130 start = locale_alias_path;
132 while (locale_alias_path[0] != '\0' && locale_alias_path[0] != ':')
133 ++locale_alias_path;
135 if (start < locale_alias_path)
136 added = read_alias_file (start, locale_alias_path - start);
139 while (added != 0);
141 return NULL;
145 static size_t
146 read_alias_file (fname, fname_len)
147 const char *fname;
148 int fname_len;
150 FILE *fp;
151 char *full_fname;
152 size_t added;
153 static const char aliasfile[] = "/locale.alias";
155 full_fname = (char *) alloca (fname_len + sizeof aliasfile);
156 memcpy (full_fname, fname, fname_len);
157 memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
159 fp = fopen (full_fname, "r");
160 if (fp == NULL)
161 return 0;
163 added = 0;
164 while (!feof (fp))
166 /* It is a reasonable approach to use a fix buffer here because
167 a) we are only interested in the first two fields
168 b) these fields must be usable as file names and so must not
169 be that long
171 char buf[BUFSIZ];
172 char *alias;
173 char *value;
174 char *cp;
176 if (fgets (buf, BUFSIZ, fp) == NULL)
177 /* EOF reached. */
178 break;
180 cp = buf;
181 /* Ignore leading white space. */
182 while (isspace (cp[0]))
183 ++cp;
185 /* A leading '#' signals a comment line. */
186 if (cp[0] != '\0' && cp[0] != '#')
188 alias = cp++;
189 while (cp[0] != '\0' && !isspace (cp[0]))
190 ++cp;
191 /* Terminate alias name. */
192 if (cp[0] != '\0')
193 *cp++ = '\0';
195 /* Now look for the beginning of the value. */
196 while (isspace (cp[0]))
197 ++cp;
199 if (cp[0] != '\0')
201 char *tp;
202 size_t len;
204 value = cp++;
205 while (cp[0] != '\0' && !isspace (cp[0]))
206 ++cp;
207 /* Terminate value. */
208 if (cp[0] == '\n')
210 /* This has to be done to make the following test
211 for the end of line possible. We are looking for
212 the terminating '\n' which do not overwrite here. */
213 *cp++ = '\0';
214 *cp = '\n';
216 else if (cp[0] != '\0')
217 *cp++ = '\0';
219 if (nmap >= maxmap)
220 extend_alias_table ();
222 /* We cannot depend on strdup available in the libc. Sigh! */
223 len = strlen (alias) + 1;
224 tp = (char *) malloc (len);
225 if (tp == NULL)
226 return added;
227 memcpy (tp, alias, len);
228 map[nmap].alias = tp;
230 len = strlen (value) + 1;
231 tp = (char *) malloc (len);
232 if (tp == NULL)
233 return added;
234 memcpy (tp, value, len);
235 map[nmap].value = tp;
237 ++nmap;
238 ++added;
242 /* Possibily not the whole line fitted into the buffer. Ignore
243 the rest of the line. */
244 while (strchr (cp, '\n') == NULL)
246 cp = buf;
247 if (fgets (buf, BUFSIZ, fp) == NULL)
248 /* Make sure the inner loop will be left. The outer loop
249 will exit at the `feof' test. */
250 *cp = '\n';
254 /* Should we test for ferror()? I think we have to silently ignore
255 errors. --drepper */
256 fclose (fp);
258 if (added > 0)
259 qsort (map, nmap, sizeof (struct alias_map),
260 (int (*) __P ((const void *, const void *))) alias_compare);
262 return added;
266 static void
267 extend_alias_table ()
269 size_t new_size;
270 struct alias_map *new_map;
272 new_size = maxmap == 0 ? 100 : 2 * maxmap;
273 new_map = (struct alias_map *) malloc (new_size
274 * sizeof (struct alias_map));
275 if (new_map == NULL)
276 /* Simply don't extend: we don't have any more core. */
277 return;
279 memcpy (new_map, map, nmap * sizeof (struct alias_map));
281 if (maxmap != 0)
282 free (map);
284 map = new_map;
285 maxmap = new_size;
289 static int
290 alias_compare (map1, map2)
291 const struct alias_map *map1;
292 const struct alias_map *map2;
294 #if defined _LIBC || defined HAVE_STRCASECMP
295 return strcasecmp (map1->alias, map2->alias);
296 #else
297 const unsigned char *p1 = (const unsigned char *) map1->alias;
298 const unsigned char *p2 = (const unsigned char *) map2->alias;
299 unsigned char c1, c2;
301 if (p1 == p2)
302 return 0;
306 /* I know this seems to be odd but the tolower() function in
307 some systems libc cannot handle nonalpha characters. */
308 c1 = isupper (*p1) ? tolower (*p1) : *p1;
309 c2 = isupper (*p2) ? tolower (*p2) : *p2;
310 if (c1 == '\0')
311 break;
313 while (c1 == c2);
315 return c1 - c2;
316 #endif