1 /* Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc.
2 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library 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 the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
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 defined HAVE_STRING_H || defined _LIBC
36 # define memcpy(Dst, Src, Num) (bcopy (Src, Dst, Num), (Dst))
39 #if !HAVE_STRCHR && !defined _LIBC
45 #if defined _LIBC || defined HAVE_ARGZ_H
49 #include <sys/types.h>
51 #if defined STDC_HEADERS || defined _LIBC
57 /* On some strange systems still no definition of NULL is found. Sigh! */
59 # if defined __STDC__ && __STDC__
60 # define NULL ((void *) 0)
66 /* @@ end of prolog @@ */
69 /* Rename the non ANSI C functions. This is required by the standard
70 because some ANSI C functions will require linking with this object
71 file and the name space must not be polluted. */
73 # define stpcpy(dest, src) __stpcpy(dest, src)
77 static char *stpcpy
PARAMS ((char *dest
, const char *src
));
81 /* Define function which are usually not available. */
83 #if !defined _LIBC && !defined HAVE___ARGZ_COUNT
84 /* Returns the number of strings in ARGZ. */
85 static size_t argz_count__
PARAMS ((const char *argz
, size_t len
));
88 argz_count__ (argz
, len
)
95 size_t part_len
= strlen (argz
);
103 # define __argz_count(argz, len) argz_count__ (argz, len)
104 #endif /* !_LIBC && !HAVE___ARGZ_COUNT */
106 #if !defined _LIBC && !defined HAVE___ARGZ_STRINGIFY
107 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
108 except the last into the character SEP. */
109 static void argz_stringify__
PARAMS ((char *argz
, size_t len
, int sep
));
112 argz_stringify__ (argz
, len
, sep
)
119 size_t part_len
= strlen (argz
);
126 # undef __argz_stringify
127 # define __argz_stringify(argz, len, sep) argz_stringify__ (argz, len, sep)
128 #endif /* !_LIBC && !HAVE___ARGZ_STRINGIFY */
130 #if !defined _LIBC && !defined HAVE___ARGZ_NEXT
131 static char *argz_next__
PARAMS ((char *argz
, size_t argz_len
,
135 argz_next__ (argz
, argz_len
, entry
)
142 if (entry
< argz
+ argz_len
)
143 entry
= strchr (entry
, '\0') + 1;
145 return entry
>= argz
+ argz_len
? NULL
: (char *) entry
;
154 # define __argz_next(argz, len, entry) argz_next__ (argz, len, entry)
155 #endif /* !_LIBC && !HAVE___ARGZ_NEXT */
158 /* Return number of bits set in X. */
159 static int pop
PARAMS ((int x
));
165 /* We assume that no more than 16 bits are used. */
166 x
= ((x
& ~0x5555) >> 1) + (x
& 0x5555);
167 x
= ((x
& ~0x3333) >> 2) + (x
& 0x3333);
168 x
= ((x
>> 4) + x
) & 0x0f0f;
169 x
= ((x
>> 8) + x
) & 0xff;
175 struct loaded_l10nfile
*
176 _nl_make_l10nflist (l10nfile_list
, dirlist
, dirlist_len
, mask
, language
,
177 territory
, codeset
, normalized_codeset
, modifier
, special
,
178 sponsor
, revision
, filename
, do_allocate
)
179 struct loaded_l10nfile
**l10nfile_list
;
183 const char *language
;
184 const char *territory
;
186 const char *normalized_codeset
;
187 const char *modifier
;
190 const char *revision
;
191 const char *filename
;
195 struct loaded_l10nfile
*last
= NULL
;
196 struct loaded_l10nfile
*retval
;
201 /* Allocate room for the full file name. */
202 abs_filename
= (char *) malloc (dirlist_len
204 + ((mask
& TERRITORY
) != 0
205 ? strlen (territory
) + 1 : 0)
206 + ((mask
& XPG_CODESET
) != 0
207 ? strlen (codeset
) + 1 : 0)
208 + ((mask
& XPG_NORM_CODESET
) != 0
209 ? strlen (normalized_codeset
) + 1 : 0)
210 + (((mask
& XPG_MODIFIER
) != 0
211 || (mask
& CEN_AUDIENCE
) != 0)
212 ? strlen (modifier
) + 1 : 0)
213 + ((mask
& CEN_SPECIAL
) != 0
214 ? strlen (special
) + 1 : 0)
215 + (((mask
& CEN_SPONSOR
) != 0
216 || (mask
& CEN_REVISION
) != 0)
217 ? (1 + ((mask
& CEN_SPONSOR
) != 0
218 ? strlen (sponsor
) + 1 : 0)
219 + ((mask
& CEN_REVISION
) != 0
220 ? strlen (revision
) + 1 : 0)) : 0)
221 + 1 + strlen (filename
) + 1);
223 if (abs_filename
== NULL
)
229 /* Construct file name. */
230 memcpy (abs_filename
, dirlist
, dirlist_len
);
231 __argz_stringify (abs_filename
, dirlist_len
, ':');
232 cp
= abs_filename
+ (dirlist_len
- 1);
234 cp
= stpcpy (cp
, language
);
236 if ((mask
& TERRITORY
) != 0)
239 cp
= stpcpy (cp
, territory
);
241 if ((mask
& XPG_CODESET
) != 0)
244 cp
= stpcpy (cp
, codeset
);
246 if ((mask
& XPG_NORM_CODESET
) != 0)
249 cp
= stpcpy (cp
, normalized_codeset
);
251 if ((mask
& (XPG_MODIFIER
| CEN_AUDIENCE
)) != 0)
253 /* This component can be part of both syntaces but has different
254 leading characters. For CEN we use `+', else `@'. */
255 *cp
++ = (mask
& CEN_AUDIENCE
) != 0 ? '+' : '@';
256 cp
= stpcpy (cp
, modifier
);
258 if ((mask
& CEN_SPECIAL
) != 0)
261 cp
= stpcpy (cp
, special
);
263 if ((mask
& (CEN_SPONSOR
| CEN_REVISION
)) != 0)
266 if ((mask
& CEN_SPONSOR
) != 0)
267 cp
= stpcpy (cp
, sponsor
);
268 if ((mask
& CEN_REVISION
) != 0)
271 cp
= stpcpy (cp
, revision
);
276 stpcpy (cp
, filename
);
278 /* Look in list of already loaded domains whether it is already
281 for (retval
= *l10nfile_list
; retval
!= NULL
; retval
= retval
->next
)
282 if (retval
->filename
!= NULL
)
284 int compare
= strcmp (retval
->filename
, abs_filename
);
290 /* It's not in the list. */
298 if (retval
!= NULL
|| do_allocate
== 0)
304 retval
= (struct loaded_l10nfile
*)
305 malloc (sizeof (*retval
) + (__argz_count (dirlist
, dirlist_len
)
307 * sizeof (struct loaded_l10nfile
*)));
311 retval
->filename
= abs_filename
;
312 retval
->decided
= (__argz_count (dirlist
, dirlist_len
) != 1
313 || ((mask
& XPG_CODESET
) != 0
314 && (mask
& XPG_NORM_CODESET
) != 0));
319 retval
->next
= *l10nfile_list
;
320 *l10nfile_list
= retval
;
324 retval
->next
= last
->next
;
329 /* If the DIRLIST is a real list the RETVAL entry corresponds not to
330 a real file. So we have to use the DIRLIST separation mechanism
331 of the inner loop. */
332 cnt
= __argz_count (dirlist
, dirlist_len
) == 1 ? mask
- 1 : mask
;
333 for (; cnt
>= 0; --cnt
)
334 if ((cnt
& ~mask
) == 0
335 && ((cnt
& CEN_SPECIFIC
) == 0 || (cnt
& XPG_SPECIFIC
) == 0)
336 && ((cnt
& XPG_CODESET
) == 0 || (cnt
& XPG_NORM_CODESET
) == 0))
338 /* Iterate over all elements of the DIRLIST. */
341 while ((dir
= __argz_next ((char *) dirlist
, dirlist_len
, dir
))
343 retval
->successor
[entries
++]
344 = _nl_make_l10nflist (l10nfile_list
, dir
, strlen (dir
) + 1, cnt
,
345 language
, territory
, codeset
,
346 normalized_codeset
, modifier
, special
,
347 sponsor
, revision
, filename
, 1);
349 retval
->successor
[entries
] = NULL
;
354 /* Normalize codeset name. There is no standard for the codeset
355 names. Normalization allows the user to use any of the common
356 names. The return value is dynamically allocated and has to be
357 freed by the caller. */
359 _nl_normalize_codeset (codeset
, name_len
)
369 for (cnt
= 0; cnt
< name_len
; ++cnt
)
370 if (isalnum (codeset
[cnt
]))
374 if (isalpha (codeset
[cnt
]))
378 retval
= (char *) malloc ((only_digit
? 3 : 0) + len
+ 1);
383 wp
= stpcpy (retval
, "iso");
387 for (cnt
= 0; cnt
< name_len
; ++cnt
)
388 if (isalpha (codeset
[cnt
]))
389 *wp
++ = tolower (codeset
[cnt
]);
390 else if (isdigit (codeset
[cnt
]))
391 *wp
++ = codeset
[cnt
];
396 return (const char *) retval
;
400 /* @@ begin of epilog @@ */
402 /* We don't want libintl.a to depend on any other library. So we
403 avoid the non-standard function stpcpy. In GNU C Library this
404 function is available, though. Also allow the symbol HAVE_STPCPY
406 #if !_LIBC && !HAVE_STPCPY
412 while ((*dest
++ = *src
++) != '\0')