* Makerules (elfobjdir): Use $(objdir) if set, even in elf subdir.
[glibc.git] / intl / l10nflist.c
blob8add1756012c9c2386094759bc4107f370a50575
1 /* Copyright (C) 1995, 1996 Free Software Foundation, Inc.
2 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
4 This file is part of the GNU C Library. Its master source is NOT part of
5 the C library, however. The master source lives in /gd/gnu/lib.
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.
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.
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
19 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 Cambridge, MA 02139, USA. */
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
26 #if defined _LIBC && (defined __ARGZ_COUNT || defined __ARGZ_STRINGIFY)
27 # include <argz.h>
28 #endif
29 #include <ctype.h>
31 #if defined STDC_HEADERS || defined _LIBC
32 # include <stdlib.h>
33 #endif
35 #if defined HAVE_STRING_H || defined _LIBC
36 # include <string.h>
37 #else
38 # include <strings.h>
39 #endif
40 #if !HAVE_STRCHR && !defined _LIBC
41 # ifndef strchr
42 # define strchr index
43 # endif
44 #endif
46 #include "loadinfo.h"
48 /* @@ end of prolog @@ */
50 #ifdef _LIBC
51 /* Rename the non ANSI C functions. This is required by the standard
52 because some ANSI C functions will require linking with this object
53 file and the name space must not be polluted. */
54 # define stpcpy(dest, src) __stpcpy(dest, src)
55 #else
56 # ifndef HAVE_STPCPY
57 static char *stpcpy PARAMS ((char *dest, const char *src));
58 # endif
59 #endif
61 /* Define function which are usually not available. */
63 #if !defined _LIBC && !defined __ARGZ_COUNT
64 /* Returns the number of strings in ARGZ. */
65 static size_t __argz_count PARAMS ((const char *argz, size_t len));
67 static size_t
68 __argz_count (argz, len)
69 const char *argz;
70 size_t len;
72 size_t count = 0;
73 while (len > 0)
75 size_t part_len = strlen (argz);
76 argz += part_len + 1;
77 len -= part_len + 1;
78 count++;
80 return count;
82 #endif /* !_LIBC && !__ARGZ_COUNT */
84 #if !defined _LIBC && !defined __ARGZ_STRINGIFY
85 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
86 except the last into the character SEP. */
87 static void __argz_stringify PARAMS ((char *argz, size_t len, int sep));
89 static void
90 __argz_stringify (argz, len, sep)
91 char *argz;
92 size_t len;
93 int sep;
95 while (len > 0)
97 size_t part_len = strlen (argz);
98 argz += part_len;
99 len -= part_len + 1;
100 if (len > 0)
101 *argz++ = sep;
104 #endif /* !_LIBC && !__ARGZ_COUNT */
106 #if !defined _LIBC && !defined __ARGZ_NEXT
107 static char *
108 __argz_next (argz, argz_len, entry)
109 char *argz;
110 size_t argz_len;
111 const char *entry;
113 if (entry)
115 if (entry < argz + argz_len)
116 entry = strchr (entry, '\0') + 1;
118 return entry >= argz + argz_len ? NULL : (char *) entry;
120 else
121 if (argz_len > 0)
122 return argz;
123 else
124 return 0;
126 #endif
129 /* Return number of bits set in X. */
130 static int pop PARAMS ((int x));
132 static inline int
133 pop (x)
134 int x;
136 /* We assume that no more than 16 bits are used. */
137 x = ((x & ~0x5555) >> 1) + (x & 0x5555);
138 x = ((x & ~0x3333) >> 2) + (x & 0x3333);
139 x = ((x >> 4) + x) & 0x0f0f;
140 x = ((x >> 8) + x) & 0xff;
142 return x;
146 struct loaded_l10nfile *
147 _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len, mask, language,
148 territory, codeset, normalized_codeset, modifier, special,
149 sponsor, revision, filename, do_allocate)
150 struct loaded_l10nfile **l10nfile_list;
151 const char *dirlist;
152 size_t dirlist_len;
153 int mask;
154 const char *language;
155 const char *territory;
156 const char *codeset;
157 const char *normalized_codeset;
158 const char *modifier;
159 const char *special;
160 const char *sponsor;
161 const char *revision;
162 const char *filename;
163 int do_allocate;
165 char *abs_filename;
166 struct loaded_l10nfile *last = NULL;
167 struct loaded_l10nfile *retval;
168 char *cp;
169 size_t entries;
170 int cnt;
172 /* Allocate room for the full file name. */
173 abs_filename = (char *) malloc (dirlist_len
174 + strlen (language)
175 + ((mask & TERRITORY) != 0
176 ? strlen (territory) + 1 : 0)
177 + ((mask & XPG_CODESET) != 0
178 ? strlen (codeset) + 1 : 0)
179 + ((mask & XPG_NORM_CODESET) != 0
180 ? strlen (normalized_codeset) + 1 : 0)
181 + (((mask & XPG_MODIFIER) != 0
182 || (mask & CEN_AUDIENCE) != 0) ?
183 strlen (modifier) + 1 : 0)
184 + ((mask & CEN_SPECIAL) != 0
185 ? strlen (special) + 1 : 0)
186 + ((mask & CEN_SPONSOR) != 0
187 ? strlen (sponsor) + 1 : 0)
188 + ((mask & CEN_REVISION) != 0
189 ? strlen (revision) + 1 : 0)
190 + 1 + strlen (filename) + 1);
192 if (abs_filename == NULL)
193 return NULL;
195 retval = NULL;
196 last = NULL;
198 /* Construct file name. */
199 memcpy (abs_filename, dirlist, dirlist_len);
200 __argz_stringify (abs_filename, dirlist_len, ':');
201 cp = abs_filename + (dirlist_len - 1);
202 *cp++ = '/';
203 cp = stpcpy (cp, language);
205 if ((mask & TERRITORY) != 0)
207 *cp++ = '_';
208 cp = stpcpy (cp, territory);
210 if ((mask & XPG_CODESET) != 0)
212 *cp++ = '.';
213 cp = stpcpy (cp, codeset);
215 if ((mask & XPG_NORM_CODESET) != 0)
217 *cp++ = '.';
218 cp = stpcpy (cp, normalized_codeset);
220 if ((mask & (XPG_MODIFIER | CEN_AUDIENCE)) != 0)
222 /* This component can be part of both syntaces but has different
223 leading characters. For CEN we use `+', else `@'. */
224 *cp++ = (mask & CEN_AUDIENCE) != 0 ? '+' : '@';
225 cp = stpcpy (cp, modifier);
227 if ((mask & CEN_SPECIAL) != 0)
229 *cp++ = '+';
230 cp = stpcpy (cp, special);
232 if ((mask & CEN_SPONSOR) != 0)
234 *cp++ = ',';
235 cp = stpcpy (cp, sponsor);
237 if ((mask & CEN_REVISION) != 0)
239 *cp++ = '_';
240 cp = stpcpy (cp, revision);
243 *cp++ = '/';
244 stpcpy (cp, filename);
246 /* Look in list of already loaded domains whether it is already
247 available. */
248 last = NULL;
249 for (retval = *l10nfile_list; retval != NULL; retval = retval->next)
250 if (retval->filename != NULL)
252 int compare = strcmp (retval->filename, abs_filename);
253 if (compare == 0)
254 /* We found it! */
255 break;
256 if (compare < 0)
258 /* It's not in the list. */
259 retval = NULL;
260 break;
263 last = retval;
266 if (retval != NULL || do_allocate == 0)
268 free (abs_filename);
269 return retval;
272 retval = (struct loaded_l10nfile *)
273 malloc (sizeof (*retval) + (__argz_count (dirlist, dirlist_len)
274 * (1 << pop (mask))
275 * sizeof (struct loaded_l10nfile *)));
276 if (retval == NULL)
277 return NULL;
279 retval->filename = abs_filename;
280 retval->decided = (__argz_count (dirlist, dirlist_len) != 1
281 || ((mask & XPG_CODESET) != 0
282 && (mask & XPG_NORM_CODESET) != 0));
283 retval->data = NULL;
285 if (last == NULL)
287 retval->next = *l10nfile_list;
288 *l10nfile_list = retval;
290 else
292 retval->next = last->next;
293 last->next = retval;
296 entries = 0;
297 /* If the DIRLIST is a real list the RETVAL entry correcponds not to
298 a real file. So we have to use the DIRLIST separation machanism
299 of the inner loop. */
300 cnt = __argz_count (dirlist, dirlist_len) == 1 ? mask - 1 : mask;
301 for (; cnt >= 0; --cnt)
302 if ((cnt & ~mask) == 0
303 && ((cnt & CEN_SPECIFIC) == 0 || (cnt & XPG_SPECIFIC) == 0)
304 && ((cnt & XPG_CODESET) == 0 || (cnt & XPG_NORM_CODESET) == 0))
306 /* Iterate over all elements of the DIRLIST. */
307 char *dir = NULL;
309 while ((dir = __argz_next ((char *) dirlist, dirlist_len, dir))
310 != NULL)
311 retval->successor[entries++]
312 = _nl_make_l10nflist (l10nfile_list, dir, strlen (dir) + 1, cnt,
313 language, territory, codeset,
314 normalized_codeset, modifier, special,
315 sponsor, revision, filename, 1);
317 retval->successor[entries] = NULL;
319 return retval;
322 /* Normalize codeset name. There is no standard for the codeset
323 names. Normalization allows the user to use any of the common
324 names. */
325 const char *
326 _nl_normalize_codeset (codeset, name_len)
327 const char *codeset;
328 size_t name_len;
330 int len = 0;
331 int only_digit = 1;
332 char *retval;
333 char *wp;
334 size_t cnt;
336 for (cnt = 0; cnt < name_len; ++cnt)
337 if (isalnum (codeset[cnt]))
339 ++len;
341 if (isalpha (codeset[cnt]))
342 only_digit = 0;
345 retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
347 if (retval != NULL)
349 if (only_digit)
350 wp = stpcpy (retval, "ISO");
351 else
352 wp = retval;
354 for (cnt = 0; cnt < name_len; ++cnt)
355 if (isalpha (codeset[cnt]))
356 *wp++ = tolower (codeset[cnt]);
357 else if (isdigit (codeset[cnt]))
358 *wp++ = codeset[cnt];
360 *wp = '\0';
363 return (const char *) retval;
367 /* @@ begin of epilog @@ */
369 /* We don't want libintl.a to depend on any other library. So we
370 avoid the non-standard function stpcpy. In GNU C Library this
371 function is available, though. Also allow the symbol HAVE_STPCPY
372 to be defined. */
373 #if !_LIBC && !HAVE_STPCPY
374 static char *
375 stpcpy (dest, src)
376 char *dest;
377 const char *src;
379 while ((*dest++ = *src++) != '\0')
380 /* Do nothing. */ ;
381 return dest - 1;
383 #endif