3 /* Copyright (C) 1995-1999, 2000-2003 Free Software Foundation, Inc.
4 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published
8 by the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 /* Tell glibc's <string.h> to provide a prototype for stpcpy().
22 This must come before <config.h> because <config.h> may include
23 <features.h>, and once <features.h> has been included, it's too late. */
25 # define _GNU_SOURCE 1
34 #if defined _LIBC || defined HAVE_ARGZ_H
38 #include <sys/types.h>
43 /* On some strange systems still no definition of NULL is found. Sigh! */
45 # if defined __STDC__ && __STDC__
46 # define NULL ((void *) 0)
52 /* @@ end of prolog @@ */
55 /* Rename the non ANSI C functions. This is required by the standard
56 because some ANSI C functions will require linking with this object
57 file and the name space must not be polluted. */
59 # define stpcpy(dest, src) __stpcpy(dest, src)
63 static char *stpcpy (char *dest
, const char *src
);
68 ISSLASH(C) tests whether C is a directory separator character.
69 IS_ABSOLUTE_PATH(P) tests whether P is an absolute path. If it is not,
70 it may be concatenated to a directory pathname.
72 #if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
73 /* Win32, OS/2, DOS */
74 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
75 # define HAS_DEVICE(P) \
76 ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
78 # define IS_ABSOLUTE_PATH(P) (ISSLASH ((P)[0]) || HAS_DEVICE (P))
81 # define ISSLASH(C) ((C) == '/')
82 # define IS_ABSOLUTE_PATH(P) ISSLASH ((P)[0])
85 /* Define function which are usually not available. */
87 #if !defined _LIBC && !defined HAVE___ARGZ_COUNT
88 /* Returns the number of strings in ARGZ. */
90 argz_count__ (const char *argz
, size_t len
)
95 size_t part_len
= strlen (argz
);
103 # define __argz_count(argz, len) argz_count__ (argz, len)
106 # define __argz_count(argz, len) INTUSE(__argz_count) (argz, len)
108 #endif /* !_LIBC && !HAVE___ARGZ_COUNT */
110 #if !defined _LIBC && !defined HAVE___ARGZ_STRINGIFY
111 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
112 except the last into the character SEP. */
114 argz_stringify__ (char *argz
, size_t len
, int sep
)
118 size_t part_len
= strlen (argz
);
125 # undef __argz_stringify
126 # define __argz_stringify(argz, len, sep) argz_stringify__ (argz, len, sep)
129 # define __argz_stringify(argz, len, sep) \
130 INTUSE(__argz_stringify) (argz, len, sep)
132 #endif /* !_LIBC && !HAVE___ARGZ_STRINGIFY */
134 #if !defined _LIBC && !defined HAVE___ARGZ_NEXT
136 argz_next__ (char *argz
, size_t argz_len
, const char *entry
)
140 if (entry
< argz
+ argz_len
)
141 entry
= strchr (entry
, '\0') + 1;
143 return entry
>= argz
+ argz_len
? NULL
: (char *) entry
;
152 # define __argz_next(argz, len, entry) argz_next__ (argz, len, entry)
153 #endif /* !_LIBC && !HAVE___ARGZ_NEXT */
156 /* Return number of bits set in X. */
160 /* We assume that no more than 16 bits are used. */
161 x
= ((x
& ~0x5555) >> 1) + (x
& 0x5555);
162 x
= ((x
& ~0x3333) >> 2) + (x
& 0x3333);
163 x
= ((x
>> 4) + x
) & 0x0f0f;
164 x
= ((x
>> 8) + x
) & 0xff;
170 struct loaded_l10nfile
*
171 _nl_make_l10nflist (struct loaded_l10nfile
**l10nfile_list
,
172 const char *dirlist
, size_t dirlist_len
,
173 int mask
, const char *language
, const char *territory
,
174 const char *codeset
, const char *normalized_codeset
,
175 const char *modifier
, const char *special
,
176 const char *sponsor
, const char *revision
,
177 const char *filename
, int do_allocate
)
180 struct loaded_l10nfile
**lastp
;
181 struct loaded_l10nfile
*retval
;
183 size_t dirlist_count
;
187 /* If LANGUAGE contains an absolute directory specification, we ignore
189 if (IS_ABSOLUTE_PATH (language
))
192 /* Allocate room for the full file name. */
193 abs_filename
= (char *) malloc (dirlist_len
195 + ((mask
& 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 || (mask
& CEN_AUDIENCE
) != 0)
203 ? strlen (modifier
) + 1 : 0)
204 + ((mask
& CEN_SPECIAL
) != 0
205 ? strlen (special
) + 1 : 0)
206 + (((mask
& CEN_SPONSOR
) != 0
207 || (mask
& CEN_REVISION
) != 0)
208 ? (1 + ((mask
& CEN_SPONSOR
) != 0
209 ? strlen (sponsor
) : 0)
210 + ((mask
& CEN_REVISION
) != 0
211 ? strlen (revision
) + 1 : 0)) : 0)
212 + 1 + strlen (filename
) + 1);
214 if (abs_filename
== NULL
)
217 /* Construct file name. */
221 memcpy (cp
, dirlist
, dirlist_len
);
222 __argz_stringify (cp
, dirlist_len
, PATH_SEPARATOR
);
227 cp
= stpcpy (cp
, language
);
229 if ((mask
& TERRITORY
) != 0)
232 cp
= stpcpy (cp
, territory
);
234 if ((mask
& XPG_CODESET
) != 0)
237 cp
= stpcpy (cp
, codeset
);
239 if ((mask
& XPG_NORM_CODESET
) != 0)
242 cp
= stpcpy (cp
, normalized_codeset
);
244 if ((mask
& (XPG_MODIFIER
| CEN_AUDIENCE
)) != 0)
246 /* This component can be part of both syntaces but has different
247 leading characters. For CEN we use `+', else `@'. */
248 *cp
++ = (mask
& CEN_AUDIENCE
) != 0 ? '+' : '@';
249 cp
= stpcpy (cp
, modifier
);
251 if ((mask
& CEN_SPECIAL
) != 0)
254 cp
= stpcpy (cp
, special
);
256 if ((mask
& (CEN_SPONSOR
| CEN_REVISION
)) != 0)
259 if ((mask
& CEN_SPONSOR
) != 0)
260 cp
= stpcpy (cp
, sponsor
);
261 if ((mask
& CEN_REVISION
) != 0)
264 cp
= stpcpy (cp
, revision
);
269 stpcpy (cp
, filename
);
271 /* Look in list of already loaded domains whether it is already
273 lastp
= l10nfile_list
;
274 for (retval
= *l10nfile_list
; retval
!= NULL
; retval
= retval
->next
)
275 if (retval
->filename
!= NULL
)
277 int compare
= strcmp (retval
->filename
, abs_filename
);
283 /* It's not in the list. */
288 lastp
= &retval
->next
;
291 if (retval
!= NULL
|| do_allocate
== 0)
297 dirlist_count
= (dirlist_len
> 0 ? __argz_count (dirlist
, dirlist_len
) : 1);
299 /* Allocate a new loaded_l10nfile. */
301 (struct loaded_l10nfile
*)
302 malloc (sizeof (*retval
)
303 + (((dirlist_count
<< pop (mask
)) + (dirlist_count
> 1 ? 1 : 0))
304 * sizeof (struct loaded_l10nfile
*)));
308 retval
->filename
= abs_filename
;
310 /* We set retval->data to NULL here; it is filled in later.
311 Setting retval->decided to 1 here means that retval does not
312 correspond to a real file (dirlist_count > 1) or is not worth
313 looking up (if an unnormalized codeset was specified). */
314 retval
->decided
= (dirlist_count
> 1
315 || ((mask
& XPG_CODESET
) != 0
316 && (mask
& XPG_NORM_CODESET
) != 0));
319 retval
->next
= *lastp
;
323 /* Recurse to fill the inheritance list of RETVAL.
324 If the DIRLIST is a real list (i.e. DIRLIST_COUNT > 1), the RETVAL
325 entry does not correspond to a real file; retval->filename contains
326 colons. In this case we loop across all elements of DIRLIST and
327 across all bit patterns dominated by MASK.
328 If the DIRLIST is a single directory or entirely redundant (i.e.
329 DIRLIST_COUNT == 1), we loop across all bit patterns dominated by
330 MASK, excluding MASK itself.
331 In either case, we loop down from MASK to 0. This has the effect
332 that the extra bits in the locale name are dropped in this order:
333 first the modifier, then the territory, then the codeset, then the
334 normalized_codeset. */
335 for (cnt
= dirlist_count
> 1 ? mask
: mask
- 1; cnt
>= 0; --cnt
)
336 if ((cnt
& ~mask
) == 0
337 && ((cnt
& CEN_SPECIFIC
) == 0 || (cnt
& XPG_SPECIFIC
) == 0)
338 && ((cnt
& XPG_CODESET
) == 0 || (cnt
& XPG_NORM_CODESET
) == 0))
340 if (dirlist_count
> 1)
342 /* Iterate over all elements of the DIRLIST. */
345 while ((dir
= __argz_next ((char *) dirlist
, dirlist_len
, dir
))
347 retval
->successor
[entries
++]
348 = _nl_make_l10nflist (l10nfile_list
, dir
, strlen (dir
) + 1,
349 cnt
, language
, territory
, codeset
,
350 normalized_codeset
, modifier
, special
,
351 sponsor
, revision
, filename
, 1);
354 retval
->successor
[entries
++]
355 = _nl_make_l10nflist (l10nfile_list
, dirlist
, dirlist_len
,
356 cnt
, language
, territory
, codeset
,
357 normalized_codeset
, modifier
, special
,
358 sponsor
, revision
, filename
, 1);
360 retval
->successor
[entries
] = NULL
;
365 /* Normalize codeset name. There is no standard for the codeset
366 names. Normalization allows the user to use any of the common
367 names. The return value is dynamically allocated and has to be
368 freed by the caller. */
370 _nl_normalize_codeset (const char *codeset
, size_t name_len
)
378 for (cnt
= 0; cnt
< name_len
; ++cnt
)
379 if (isalnum ((unsigned char) codeset
[cnt
]))
383 if (isalpha ((unsigned char) codeset
[cnt
]))
387 retval
= (char *) malloc ((only_digit
? 3 : 0) + len
+ 1);
392 wp
= stpcpy (retval
, "iso");
396 for (cnt
= 0; cnt
< name_len
; ++cnt
)
397 if (isalpha ((unsigned char) codeset
[cnt
]))
398 *wp
++ = tolower ((unsigned char) codeset
[cnt
]);
399 else if (isdigit ((unsigned char) codeset
[cnt
]))
400 *wp
++ = codeset
[cnt
];
405 return (const char *) retval
;
409 /* @@ begin of epilog @@ */
411 /* We don't want libintl.a to depend on any other library. So we
412 avoid the non-standard function stpcpy. In GNU C Library this
413 function is available, though. Also allow the symbol HAVE_STPCPY
415 #if !_LIBC && !HAVE_STPCPY
417 stpcpy (char *dest
, const char *src
)
419 while ((*dest
++ = *src
++) != '\0')