1 /* Copyright (C) 1995-2002, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 /* Tell glibc's <string.h> to provide a prototype for stpcpy().
21 This must come before <config.h> because <config.h> may include
22 <features.h>, and once <features.h> has been included, it's too late. */
24 # define _GNU_SOURCE 1
33 #if defined _LIBC || defined HAVE_ARGZ_H
37 #include <sys/types.h>
42 /* On some strange systems still no definition of NULL is found. Sigh! */
44 # if defined __STDC__ && __STDC__
45 # define NULL ((void *) 0)
51 /* @@ end of prolog @@ */
54 /* Rename the non ANSI C functions. This is required by the standard
55 because some ANSI C functions will require linking with this object
56 file and the name space must not be polluted. */
58 # define stpcpy(dest, src) __stpcpy(dest, src)
62 static char *stpcpy
PARAMS ((char *dest
, const char *src
));
66 /* Define function which are usually not available. */
68 #if !defined _LIBC && !defined HAVE___ARGZ_COUNT
69 /* Returns the number of strings in ARGZ. */
70 static size_t argz_count__
PARAMS ((const char *argz
, size_t len
));
73 argz_count__ (argz
, len
)
80 size_t part_len
= strlen (argz
);
88 # define __argz_count(argz, len) argz_count__ (argz, len)
91 # define __argz_count(argz, len) INTUSE(__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)
119 # define __argz_stringify(argz, len, sep) \
120 INTUSE(__argz_stringify) (argz, len, sep)
122 #endif /* !_LIBC && !HAVE___ARGZ_STRINGIFY */
124 #if !defined _LIBC && !defined HAVE___ARGZ_NEXT
125 static char *argz_next__
PARAMS ((char *argz
, size_t argz_len
,
129 argz_next__ (argz
, argz_len
, entry
)
136 if (entry
< argz
+ argz_len
)
137 entry
= strchr (entry
, '\0') + 1;
139 return entry
>= argz
+ argz_len
? NULL
: (char *) entry
;
148 # define __argz_next(argz, len, entry) argz_next__ (argz, len, entry)
149 #endif /* !_LIBC && !HAVE___ARGZ_NEXT */
152 /* Return number of bits set in X. */
153 static int pop
PARAMS ((int x
));
159 /* We assume that no more than 16 bits are used. */
160 x
= ((x
& ~0x5555) >> 1) + (x
& 0x5555);
161 x
= ((x
& ~0x3333) >> 2) + (x
& 0x3333);
162 x
= ((x
>> 4) + x
) & 0x0f0f;
163 x
= ((x
>> 8) + x
) & 0xff;
169 struct loaded_l10nfile
*
170 _nl_make_l10nflist (l10nfile_list
, dirlist
, dirlist_len
, mask
, language
,
171 territory
, codeset
, normalized_codeset
, modifier
,
172 filename
, do_allocate
)
173 struct loaded_l10nfile
**l10nfile_list
;
177 const char *language
;
178 const char *territory
;
180 const char *normalized_codeset
;
181 const char *modifier
;
182 const char *filename
;
186 struct loaded_l10nfile
*last
= NULL
;
187 struct loaded_l10nfile
*retval
;
192 /* Allocate room for the full file name. */
193 abs_filename
= (char *) malloc (dirlist_len
195 + ((mask
& XPG_TERRITORY
) != 0
196 ? strlen (territory
) + 1 : 0)
197 + ((mask
& XPG_CODESET
) != 0
198 ? strlen (codeset
) + 1 : 0)
199 + ((mask
& XPG_NORM_CODESET
) != 0
200 ? strlen (normalized_codeset
) + 1 : 0)
201 + ((mask
& XPG_MODIFIER
) != 0
202 ? strlen (modifier
) + 1 : 0)
203 + 1 + strlen (filename
) + 1);
205 if (abs_filename
== NULL
)
211 /* Construct file name. */
212 memcpy (abs_filename
, dirlist
, dirlist_len
);
213 __argz_stringify (abs_filename
, dirlist_len
, ':');
214 cp
= abs_filename
+ (dirlist_len
- 1);
216 cp
= stpcpy (cp
, language
);
218 if ((mask
& XPG_TERRITORY
) != 0)
221 cp
= stpcpy (cp
, territory
);
223 if ((mask
& XPG_CODESET
) != 0)
226 cp
= stpcpy (cp
, codeset
);
228 if ((mask
& XPG_NORM_CODESET
) != 0)
231 cp
= stpcpy (cp
, normalized_codeset
);
233 if ((mask
& XPG_MODIFIER
) != 0)
236 cp
= stpcpy (cp
, modifier
);
240 stpcpy (cp
, filename
);
242 /* Look in list of already loaded domains whether it is already
245 for (retval
= *l10nfile_list
; retval
!= NULL
; retval
= retval
->next
)
246 if (retval
->filename
!= NULL
)
248 int compare
= strcmp (retval
->filename
, abs_filename
);
254 /* It's not in the list. */
262 if (retval
!= NULL
|| do_allocate
== 0)
268 retval
= (struct loaded_l10nfile
*)
269 malloc (sizeof (*retval
) + (__argz_count (dirlist
, dirlist_len
)
271 * sizeof (struct loaded_l10nfile
*)));
275 retval
->filename
= abs_filename
;
276 /* If more than one directory is in the list this is a pseudo-entry
277 which just references others. We do not try to load data for it,
279 retval
->decided
= (__argz_count (dirlist
, dirlist_len
) != 1
280 || ((mask
& XPG_CODESET
) != 0
281 && (mask
& XPG_NORM_CODESET
) != 0));
286 retval
->next
= *l10nfile_list
;
287 *l10nfile_list
= retval
;
291 retval
->next
= last
->next
;
296 /* If the DIRLIST is a real list the RETVAL entry corresponds not to
297 a real file. So we have to use the DIRLIST separation mechanism
298 of the inner loop. */
299 cnt
= __argz_count (dirlist
, dirlist_len
) == 1 ? mask
- 1 : mask
;
300 for (; cnt
>= 0; --cnt
)
301 if ((cnt
& ~mask
) == 0)
303 /* Iterate over all elements of the DIRLIST. */
306 while ((dir
= __argz_next ((char *) dirlist
, dirlist_len
, dir
))
308 retval
->successor
[entries
++]
309 = _nl_make_l10nflist (l10nfile_list
, dir
, strlen (dir
) + 1, cnt
,
310 language
, territory
, codeset
,
311 normalized_codeset
, modifier
, filename
, 1);
313 retval
->successor
[entries
] = NULL
;
318 /* Normalize codeset name. There is no standard for the codeset
319 names. Normalization allows the user to use any of the common
320 names. The return value is dynamically allocated and has to be
321 freed by the caller. */
323 _nl_normalize_codeset (codeset
, name_len
)
333 for (cnt
= 0; cnt
< name_len
; ++cnt
)
334 if (isalnum ((unsigned char) codeset
[cnt
]))
338 if (isalpha ((unsigned char) codeset
[cnt
]))
342 retval
= (char *) malloc ((only_digit
? 3 : 0) + len
+ 1);
347 wp
= stpcpy (retval
, "iso");
351 for (cnt
= 0; cnt
< name_len
; ++cnt
)
352 if (isalpha ((unsigned char) codeset
[cnt
]))
353 *wp
++ = tolower ((unsigned char) codeset
[cnt
]);
354 else if (isdigit ((unsigned char) codeset
[cnt
]))
355 *wp
++ = codeset
[cnt
];
360 return (const char *) retval
;
364 /* @@ begin of epilog @@ */
366 /* We don't want libintl.a to depend on any other library. So we
367 avoid the non-standard function stpcpy. In GNU C Library this
368 function is available, though. Also allow the symbol HAVE_STPCPY
370 #if !_LIBC && !HAVE_STPCPY
376 while ((*dest
++ = *src
++) != '\0')