]>
git.ipfire.org Git - thirdparty/glibc.git/blob - intl/l10nflist.c
1 /* Copyright (C) 1995-1999, 2000, 2001 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)
89 #endif /* !_LIBC && !HAVE___ARGZ_COUNT */
91 #if !defined _LIBC && !defined HAVE___ARGZ_STRINGIFY
92 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
93 except the last into the character SEP. */
94 static void argz_stringify__
PARAMS ((char *argz
, size_t len
, int sep
));
97 argz_stringify__ (argz
, len
, sep
)
104 size_t part_len
= strlen (argz
);
111 # undef __argz_stringify
112 # define __argz_stringify(argz, len, sep) argz_stringify__ (argz, len, sep)
113 #endif /* !_LIBC && !HAVE___ARGZ_STRINGIFY */
115 #if !defined _LIBC && !defined HAVE___ARGZ_NEXT
116 static char *argz_next__
PARAMS ((char *argz
, size_t argz_len
,
120 argz_next__ (argz
, argz_len
, entry
)
127 if (entry
< argz
+ argz_len
)
128 entry
= strchr (entry
, '\0') + 1;
130 return entry
>= argz
+ argz_len
? NULL
: (char *) entry
;
139 # define __argz_next(argz, len, entry) argz_next__ (argz, len, entry)
140 #endif /* !_LIBC && !HAVE___ARGZ_NEXT */
143 /* Return number of bits set in X. */
144 static int pop
PARAMS ((int x
));
150 /* We assume that no more than 16 bits are used. */
151 x
= ((x
& ~0x5555) >> 1) + (x
& 0x5555);
152 x
= ((x
& ~0x3333) >> 2) + (x
& 0x3333);
153 x
= ((x
>> 4) + x
) & 0x0f0f;
154 x
= ((x
>> 8) + x
) & 0xff;
160 struct loaded_l10nfile
*
161 _nl_make_l10nflist (l10nfile_list
, dirlist
, dirlist_len
, mask
, language
,
162 territory
, codeset
, normalized_codeset
, modifier
, special
,
163 sponsor
, revision
, filename
, do_allocate
)
164 struct loaded_l10nfile
**l10nfile_list
;
168 const char *language
;
169 const char *territory
;
171 const char *normalized_codeset
;
172 const char *modifier
;
175 const char *revision
;
176 const char *filename
;
180 struct loaded_l10nfile
*last
= NULL
;
181 struct loaded_l10nfile
*retval
;
186 /* Allocate room for the full file name. */
187 abs_filename
= (char *) malloc (dirlist_len
189 + ((mask
& TERRITORY
) != 0
190 ? strlen (territory
) + 1 : 0)
191 + ((mask
& XPG_CODESET
) != 0
192 ? strlen (codeset
) + 1 : 0)
193 + ((mask
& XPG_NORM_CODESET
) != 0
194 ? strlen (normalized_codeset
) + 1 : 0)
195 + (((mask
& XPG_MODIFIER
) != 0
196 || (mask
& CEN_AUDIENCE
) != 0)
197 ? strlen (modifier
) + 1 : 0)
198 + ((mask
& CEN_SPECIAL
) != 0
199 ? strlen (special
) + 1 : 0)
200 + (((mask
& CEN_SPONSOR
) != 0
201 || (mask
& CEN_REVISION
) != 0)
202 ? (1 + ((mask
& CEN_SPONSOR
) != 0
203 ? strlen (sponsor
) + 1 : 0)
204 + ((mask
& CEN_REVISION
) != 0
205 ? strlen (revision
) + 1 : 0)) : 0)
206 + 1 + strlen (filename
) + 1);
208 if (abs_filename
== NULL
)
214 /* Construct file name. */
215 memcpy (abs_filename
, dirlist
, dirlist_len
);
216 __argz_stringify (abs_filename
, dirlist_len
, ':');
217 cp
= abs_filename
+ (dirlist_len
- 1);
219 cp
= stpcpy (cp
, language
);
221 if ((mask
& TERRITORY
) != 0)
224 cp
= stpcpy (cp
, territory
);
226 if ((mask
& XPG_CODESET
) != 0)
229 cp
= stpcpy (cp
, codeset
);
231 if ((mask
& XPG_NORM_CODESET
) != 0)
234 cp
= stpcpy (cp
, normalized_codeset
);
236 if ((mask
& (XPG_MODIFIER
| CEN_AUDIENCE
)) != 0)
238 /* This component can be part of both syntaces but has different
239 leading characters. For CEN we use `+', else `@'. */
240 *cp
++ = (mask
& CEN_AUDIENCE
) != 0 ? '+' : '@';
241 cp
= stpcpy (cp
, modifier
);
243 if ((mask
& CEN_SPECIAL
) != 0)
246 cp
= stpcpy (cp
, special
);
248 if ((mask
& (CEN_SPONSOR
| CEN_REVISION
)) != 0)
251 if ((mask
& CEN_SPONSOR
) != 0)
252 cp
= stpcpy (cp
, sponsor
);
253 if ((mask
& CEN_REVISION
) != 0)
256 cp
= stpcpy (cp
, revision
);
261 stpcpy (cp
, filename
);
263 /* Look in list of already loaded domains whether it is already
266 for (retval
= *l10nfile_list
; retval
!= NULL
; retval
= retval
->next
)
267 if (retval
->filename
!= NULL
)
269 int compare
= strcmp (retval
->filename
, abs_filename
);
275 /* It's not in the list. */
283 if (retval
!= NULL
|| do_allocate
== 0)
289 retval
= (struct loaded_l10nfile
*)
290 malloc (sizeof (*retval
) + (__argz_count (dirlist
, dirlist_len
)
292 * sizeof (struct loaded_l10nfile
*)));
296 retval
->filename
= abs_filename
;
297 retval
->decided
= (__argz_count (dirlist
, dirlist_len
) != 1
298 || ((mask
& XPG_CODESET
) != 0
299 && (mask
& XPG_NORM_CODESET
) != 0));
304 retval
->next
= *l10nfile_list
;
305 *l10nfile_list
= retval
;
309 retval
->next
= last
->next
;
314 /* If the DIRLIST is a real list the RETVAL entry corresponds not to
315 a real file. So we have to use the DIRLIST separation mechanism
316 of the inner loop. */
317 cnt
= __argz_count (dirlist
, dirlist_len
) == 1 ? mask
- 1 : mask
;
318 for (; cnt
>= 0; --cnt
)
319 if ((cnt
& ~mask
) == 0
320 && ((cnt
& CEN_SPECIFIC
) == 0 || (cnt
& XPG_SPECIFIC
) == 0)
321 && ((cnt
& XPG_CODESET
) == 0 || (cnt
& XPG_NORM_CODESET
) == 0))
323 /* Iterate over all elements of the DIRLIST. */
326 while ((dir
= __argz_next ((char *) dirlist
, dirlist_len
, dir
))
328 retval
->successor
[entries
++]
329 = _nl_make_l10nflist (l10nfile_list
, dir
, strlen (dir
) + 1, cnt
,
330 language
, territory
, codeset
,
331 normalized_codeset
, modifier
, special
,
332 sponsor
, revision
, filename
, 1);
334 retval
->successor
[entries
] = NULL
;
339 /* Normalize codeset name. There is no standard for the codeset
340 names. Normalization allows the user to use any of the common
341 names. The return value is dynamically allocated and has to be
342 freed by the caller. */
344 _nl_normalize_codeset (codeset
, name_len
)
354 for (cnt
= 0; cnt
< name_len
; ++cnt
)
355 if (isalnum ((unsigned char) codeset
[cnt
]))
359 if (isalpha ((unsigned char) codeset
[cnt
]))
363 retval
= (char *) malloc ((only_digit
? 3 : 0) + len
+ 1);
368 wp
= stpcpy (retval
, "iso");
372 for (cnt
= 0; cnt
< name_len
; ++cnt
)
373 if (isalpha ((unsigned char) codeset
[cnt
]))
374 *wp
++ = tolower ((unsigned char) codeset
[cnt
]);
375 else if (isdigit ((unsigned char) codeset
[cnt
]))
376 *wp
++ = codeset
[cnt
];
381 return (const char *) retval
;
385 /* @@ begin of epilog @@ */
387 /* We don't want libintl.a to depend on any other library. So we
388 avoid the non-standard function stpcpy. In GNU C Library this
389 function is available, though. Also allow the symbol HAVE_STPCPY
391 #if !_LIBC && !HAVE_STPCPY
397 while ((*dest
++ = *src
++) != '\0')