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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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
32 #if defined _LIBC || defined HAVE_ARGZ_H
36 #include <sys/types.h>
41 /* On some strange systems still no definition of NULL is found. Sigh! */
43 # if defined __STDC__ && __STDC__
44 # define NULL ((void *) 0)
50 /* @@ end of prolog @@ */
53 /* Rename the non ANSI C functions. This is required by the standard
54 because some ANSI C functions will require linking with this object
55 file and the name space must not be polluted. */
57 # define stpcpy(dest, src) __stpcpy(dest, src)
61 static char *stpcpy PARAMS ((char *dest, const char *src));
65 /* Define function which are usually not available. */
67 #if !defined _LIBC && !defined HAVE___ARGZ_COUNT
68 /* Returns the number of strings in ARGZ. */
69 static size_t argz_count__ PARAMS ((const char *argz, size_t len));
72 argz_count__ (argz, len)
79 size_t part_len = strlen (argz);
87 # define __argz_count(argz, len) argz_count__ (argz, len)
88 #endif /* !_LIBC && !HAVE___ARGZ_COUNT */
90 #if !defined _LIBC && !defined HAVE___ARGZ_STRINGIFY
91 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
92 except the last into the character SEP. */
93 static void argz_stringify__ PARAMS ((char *argz, size_t len, int sep));
96 argz_stringify__ (argz, len, sep)
103 size_t part_len = strlen (argz);
110 # undef __argz_stringify
111 # define __argz_stringify(argz, len, sep) argz_stringify__ (argz, len, sep)
112 #endif /* !_LIBC && !HAVE___ARGZ_STRINGIFY */
114 #if !defined _LIBC && !defined HAVE___ARGZ_NEXT
115 static char *argz_next__ PARAMS ((char *argz, size_t argz_len,
119 argz_next__ (argz, argz_len, entry)
126 if (entry < argz + argz_len)
127 entry = strchr (entry, '\0') + 1;
129 return entry >= argz + argz_len ? NULL : (char *) entry;
138 # define __argz_next(argz, len, entry) argz_next__ (argz, len, entry)
139 #endif /* !_LIBC && !HAVE___ARGZ_NEXT */
142 /* Return number of bits set in X. */
143 static int pop PARAMS ((int x));
149 /* We assume that no more than 16 bits are used. */
150 x = ((x & ~0x5555) >> 1) + (x & 0x5555);
151 x = ((x & ~0x3333) >> 2) + (x & 0x3333);
152 x = ((x >> 4) + x) & 0x0f0f;
153 x = ((x >> 8) + x) & 0xff;
159 struct loaded_l10nfile *
160 _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len, mask, language,
161 territory, codeset, normalized_codeset, modifier, special,
162 sponsor, revision, filename, do_allocate)
163 struct loaded_l10nfile **l10nfile_list;
167 const char *language;
168 const char *territory;
170 const char *normalized_codeset;
171 const char *modifier;
174 const char *revision;
175 const char *filename;
179 struct loaded_l10nfile *last = NULL;
180 struct loaded_l10nfile *retval;
185 /* Allocate room for the full file name. */
186 abs_filename = (char *) malloc (dirlist_len
188 + ((mask & TERRITORY) != 0
189 ? strlen (territory) + 1 : 0)
190 + ((mask & XPG_CODESET) != 0
191 ? strlen (codeset) + 1 : 0)
192 + ((mask & XPG_NORM_CODESET) != 0
193 ? strlen (normalized_codeset) + 1 : 0)
194 + (((mask & XPG_MODIFIER) != 0
195 || (mask & CEN_AUDIENCE) != 0)
196 ? strlen (modifier) + 1 : 0)
197 + ((mask & CEN_SPECIAL) != 0
198 ? strlen (special) + 1 : 0)
199 + (((mask & CEN_SPONSOR) != 0
200 || (mask & CEN_REVISION) != 0)
201 ? (1 + ((mask & CEN_SPONSOR) != 0
202 ? strlen (sponsor) + 1 : 0)
203 + ((mask & CEN_REVISION) != 0
204 ? strlen (revision) + 1 : 0)) : 0)
205 + 1 + strlen (filename) + 1);
207 if (abs_filename == NULL)
213 /* Construct file name. */
214 memcpy (abs_filename, dirlist, dirlist_len);
215 __argz_stringify (abs_filename, dirlist_len, PATH_SEPARATOR);
216 cp = abs_filename + (dirlist_len - 1);
218 cp = stpcpy (cp, language);
220 if ((mask & TERRITORY) != 0)
223 cp = stpcpy (cp, territory);
225 if ((mask & XPG_CODESET) != 0)
228 cp = stpcpy (cp, codeset);
230 if ((mask & XPG_NORM_CODESET) != 0)
233 cp = stpcpy (cp, normalized_codeset);
235 if ((mask & (XPG_MODIFIER | CEN_AUDIENCE)) != 0)
237 /* This component can be part of both syntaces but has different
238 leading characters. For CEN we use `+', else `@'. */
239 *cp++ = (mask & CEN_AUDIENCE) != 0 ? '+' : '@';
240 cp = stpcpy (cp, modifier);
242 if ((mask & CEN_SPECIAL) != 0)
245 cp = stpcpy (cp, special);
247 if ((mask & (CEN_SPONSOR | CEN_REVISION)) != 0)
250 if ((mask & CEN_SPONSOR) != 0)
251 cp = stpcpy (cp, sponsor);
252 if ((mask & CEN_REVISION) != 0)
255 cp = stpcpy (cp, revision);
260 stpcpy (cp, filename);
262 /* Look in list of already loaded domains whether it is already
265 for (retval = *l10nfile_list; retval != NULL; retval = retval->next)
266 if (retval->filename != NULL)
268 int compare = strcmp (retval->filename, abs_filename);
274 /* It's not in the list. */
282 if (retval != NULL || do_allocate == 0)
288 retval = (struct loaded_l10nfile *)
289 malloc (sizeof (*retval) + (__argz_count (dirlist, dirlist_len)
291 * sizeof (struct loaded_l10nfile *)));
295 retval->filename = abs_filename;
296 retval->decided = (__argz_count (dirlist, dirlist_len) != 1
297 || ((mask & XPG_CODESET) != 0
298 && (mask & XPG_NORM_CODESET) != 0));
303 retval->next = *l10nfile_list;
304 *l10nfile_list = retval;
308 retval->next = last->next;
313 /* If the DIRLIST is a real list the RETVAL entry corresponds not to
314 a real file. So we have to use the DIRLIST separation mechanism
315 of the inner loop. */
316 cnt = __argz_count (dirlist, dirlist_len) == 1 ? mask - 1 : mask;
317 for (; cnt >= 0; --cnt)
318 if ((cnt & ~mask) == 0
319 && ((cnt & CEN_SPECIFIC) == 0 || (cnt & XPG_SPECIFIC) == 0)
320 && ((cnt & XPG_CODESET) == 0 || (cnt & XPG_NORM_CODESET) == 0))
322 /* Iterate over all elements of the DIRLIST. */
325 while ((dir = __argz_next ((char *) dirlist, dirlist_len, dir))
327 retval->successor[entries++]
328 = _nl_make_l10nflist (l10nfile_list, dir, strlen (dir) + 1, cnt,
329 language, territory, codeset,
330 normalized_codeset, modifier, special,
331 sponsor, revision, filename, 1);
333 retval->successor[entries] = NULL;
338 /* Normalize codeset name. There is no standard for the codeset
339 names. Normalization allows the user to use any of the common
340 names. The return value is dynamically allocated and has to be
341 freed by the caller. */
343 _nl_normalize_codeset (codeset, name_len)
353 for (cnt = 0; cnt < name_len; ++cnt)
354 if (isalnum ((unsigned char) codeset[cnt]))
358 if (isalpha ((unsigned char) codeset[cnt]))
362 retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
367 wp = stpcpy (retval, "iso");
371 for (cnt = 0; cnt < name_len; ++cnt)
372 if (isalpha ((unsigned char) codeset[cnt]))
373 *wp++ = tolower ((unsigned char) codeset[cnt]);
374 else if (isdigit ((unsigned char) codeset[cnt]))
375 *wp++ = codeset[cnt];
380 return (const char *) retval;
384 /* @@ begin of epilog @@ */
386 /* We don't want libintl.a to depend on any other library. So we
387 avoid the non-standard function stpcpy. In GNU C Library this
388 function is available, though. Also allow the symbol HAVE_STPCPY
390 #if !_LIBC && !HAVE_STPCPY
396 while ((*dest++ = *src++) != '\0')