1 /* Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc.
2 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published
6 by the Free Software Foundation; either version 2, or (at your option)
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 GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 /* Tell glibc's <string.h> to provide a prototype for stpcpy().
20 This must come before <config.h> because <config.h> may include
21 <features.h>, and once <features.h> has been included, it's too late. */
23 # define _GNU_SOURCE 1
31 #if !HAVE_STRCHR && !defined _LIBC
37 #if defined _LIBC || defined HAVE_ARGZ_H
41 #include <sys/types.h>
46 /* On some strange systems still no definition of NULL is found. Sigh! */
48 # if defined __STDC__ && __STDC__
49 # define NULL ((void *) 0)
55 /* @@ end of prolog @@ */
58 /* Rename the non ANSI C functions. This is required by the standard
59 because some ANSI C functions will require linking with this object
60 file and the name space must not be polluted. */
62 # define stpcpy(dest, src) __stpcpy(dest, src)
66 static char *stpcpy PARAMS ((char *dest, const char *src));
70 /* Define function which are usually not available. */
72 #if !defined _LIBC && !defined HAVE___ARGZ_COUNT
73 /* Returns the number of strings in ARGZ. */
74 static size_t argz_count__ PARAMS ((const char *argz, size_t len));
77 argz_count__ (argz, len)
84 size_t part_len = strlen (argz);
92 # define __argz_count(argz, len) argz_count__ (argz, len)
93 #endif /* !_LIBC && !HAVE___ARGZ_COUNT */
95 #if !defined _LIBC && !defined HAVE___ARGZ_STRINGIFY
96 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
97 except the last into the character SEP. */
98 static void argz_stringify__ PARAMS ((char *argz, size_t len, int sep));
101 argz_stringify__ (argz, len, sep)
108 size_t part_len = strlen (argz);
115 # undef __argz_stringify
116 # define __argz_stringify(argz, len, sep) argz_stringify__ (argz, len, sep)
117 #endif /* !_LIBC && !HAVE___ARGZ_STRINGIFY */
119 #if !defined _LIBC && !defined HAVE___ARGZ_NEXT
120 static char *argz_next__ PARAMS ((char *argz, size_t argz_len,
124 argz_next__ (argz, argz_len, entry)
131 if (entry < argz + argz_len)
132 entry = strchr (entry, '\0') + 1;
134 return entry >= argz + argz_len ? NULL : (char *) entry;
143 # define __argz_next(argz, len, entry) argz_next__ (argz, len, entry)
144 #endif /* !_LIBC && !HAVE___ARGZ_NEXT */
147 /* Return number of bits set in X. */
148 static int pop PARAMS ((int x));
154 /* We assume that no more than 16 bits are used. */
155 x = ((x & ~0x5555) >> 1) + (x & 0x5555);
156 x = ((x & ~0x3333) >> 2) + (x & 0x3333);
157 x = ((x >> 4) + x) & 0x0f0f;
158 x = ((x >> 8) + x) & 0xff;
164 struct loaded_l10nfile *
165 _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len, mask, language,
166 territory, codeset, normalized_codeset, modifier, special,
167 sponsor, revision, filename, do_allocate)
168 struct loaded_l10nfile **l10nfile_list;
172 const char *language;
173 const char *territory;
175 const char *normalized_codeset;
176 const char *modifier;
179 const char *revision;
180 const char *filename;
184 struct loaded_l10nfile *last = NULL;
185 struct loaded_l10nfile *retval;
190 /* Allocate room for the full file name. */
191 abs_filename = (char *) malloc (dirlist_len
193 + ((mask & TERRITORY) != 0
194 ? strlen (territory) + 1 : 0)
195 + ((mask & XPG_CODESET) != 0
196 ? strlen (codeset) + 1 : 0)
197 + ((mask & XPG_NORM_CODESET) != 0
198 ? strlen (normalized_codeset) + 1 : 0)
199 + (((mask & XPG_MODIFIER) != 0
200 || (mask & CEN_AUDIENCE) != 0)
201 ? strlen (modifier) + 1 : 0)
202 + ((mask & CEN_SPECIAL) != 0
203 ? strlen (special) + 1 : 0)
204 + (((mask & CEN_SPONSOR) != 0
205 || (mask & CEN_REVISION) != 0)
206 ? (1 + ((mask & CEN_SPONSOR) != 0
207 ? strlen (sponsor) + 1 : 0)
208 + ((mask & CEN_REVISION) != 0
209 ? strlen (revision) + 1 : 0)) : 0)
210 + 1 + strlen (filename) + 1);
212 if (abs_filename == NULL)
218 /* Construct file name. */
219 memcpy (abs_filename, dirlist, dirlist_len);
220 __argz_stringify (abs_filename, dirlist_len, PATH_SEPARATOR);
221 cp = abs_filename + (dirlist_len - 1);
223 cp = stpcpy (cp, language);
225 if ((mask & TERRITORY) != 0)
228 cp = stpcpy (cp, territory);
230 if ((mask & XPG_CODESET) != 0)
233 cp = stpcpy (cp, codeset);
235 if ((mask & XPG_NORM_CODESET) != 0)
238 cp = stpcpy (cp, normalized_codeset);
240 if ((mask & (XPG_MODIFIER | CEN_AUDIENCE)) != 0)
242 /* This component can be part of both syntaces but has different
243 leading characters. For CEN we use `+', else `@'. */
244 *cp++ = (mask & CEN_AUDIENCE) != 0 ? '+' : '@';
245 cp = stpcpy (cp, modifier);
247 if ((mask & CEN_SPECIAL) != 0)
250 cp = stpcpy (cp, special);
252 if ((mask & (CEN_SPONSOR | CEN_REVISION)) != 0)
255 if ((mask & CEN_SPONSOR) != 0)
256 cp = stpcpy (cp, sponsor);
257 if ((mask & CEN_REVISION) != 0)
260 cp = stpcpy (cp, revision);
265 stpcpy (cp, filename);
267 /* Look in list of already loaded domains whether it is already
270 for (retval = *l10nfile_list; retval != NULL; retval = retval->next)
271 if (retval->filename != NULL)
273 int compare = strcmp (retval->filename, abs_filename);
279 /* It's not in the list. */
287 if (retval != NULL || do_allocate == 0)
293 retval = (struct loaded_l10nfile *)
294 malloc (sizeof (*retval) + (__argz_count (dirlist, dirlist_len)
296 * sizeof (struct loaded_l10nfile *)));
300 retval->filename = abs_filename;
301 retval->decided = (__argz_count (dirlist, dirlist_len) != 1
302 || ((mask & XPG_CODESET) != 0
303 && (mask & XPG_NORM_CODESET) != 0));
308 retval->next = *l10nfile_list;
309 *l10nfile_list = retval;
313 retval->next = last->next;
318 /* If the DIRLIST is a real list the RETVAL entry corresponds not to
319 a real file. So we have to use the DIRLIST separation mechanism
320 of the inner loop. */
321 cnt = __argz_count (dirlist, dirlist_len) == 1 ? mask - 1 : mask;
322 for (; cnt >= 0; --cnt)
323 if ((cnt & ~mask) == 0
324 && ((cnt & CEN_SPECIFIC) == 0 || (cnt & XPG_SPECIFIC) == 0)
325 && ((cnt & XPG_CODESET) == 0 || (cnt & XPG_NORM_CODESET) == 0))
327 /* Iterate over all elements of the DIRLIST. */
330 while ((dir = __argz_next ((char *) dirlist, dirlist_len, dir))
332 retval->successor[entries++]
333 = _nl_make_l10nflist (l10nfile_list, dir, strlen (dir) + 1, cnt,
334 language, territory, codeset,
335 normalized_codeset, modifier, special,
336 sponsor, revision, filename, 1);
338 retval->successor[entries] = NULL;
343 /* Normalize codeset name. There is no standard for the codeset
344 names. Normalization allows the user to use any of the common
345 names. The return value is dynamically allocated and has to be
346 freed by the caller. */
348 _nl_normalize_codeset (codeset, name_len)
358 for (cnt = 0; cnt < name_len; ++cnt)
359 if (isalnum ((unsigned char) codeset[cnt]))
363 if (isalpha ((unsigned char) codeset[cnt]))
367 retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
372 wp = stpcpy (retval, "iso");
376 for (cnt = 0; cnt < name_len; ++cnt)
377 if (isalpha ((unsigned char) codeset[cnt]))
378 *wp++ = tolower ((unsigned char) codeset[cnt]);
379 else if (isdigit ((unsigned char) codeset[cnt]))
380 *wp++ = codeset[cnt];
385 return (const char *) retval;
389 /* @@ begin of epilog @@ */
391 /* We don't want libintl.a to depend on any other library. So we
392 avoid the non-standard function stpcpy. In GNU C Library this
393 function is available, though. Also allow the symbol HAVE_STPCPY
395 #if !_LIBC && !HAVE_STPCPY
401 while ((*dest++ = *src++) != '\0')