3 /* Copyright (C) 1995-1999, 2000, 2001 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
PARAMS ((char *dest
, const char *src
));
67 /* Define function which are usually not available. */
69 #if !defined _LIBC && !defined HAVE___ARGZ_COUNT
70 /* Returns the number of strings in ARGZ. */
71 static size_t argz_count__
PARAMS ((const char *argz
, size_t len
));
74 argz_count__ (argz
, len
)
81 size_t part_len
= strlen (argz
);
89 # define __argz_count(argz, len) argz_count__ (argz, len)
90 #endif /* !_LIBC && !HAVE___ARGZ_COUNT */
92 #if !defined _LIBC && !defined HAVE___ARGZ_STRINGIFY
93 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
94 except the last into the character SEP. */
95 static void argz_stringify__
PARAMS ((char *argz
, size_t len
, int sep
));
98 argz_stringify__ (argz
, len
, sep
)
105 size_t part_len
= strlen (argz
);
112 # undef __argz_stringify
113 # define __argz_stringify(argz, len, sep) argz_stringify__ (argz, len, sep)
114 #endif /* !_LIBC && !HAVE___ARGZ_STRINGIFY */
116 #if !defined _LIBC && !defined HAVE___ARGZ_NEXT
117 static char *argz_next__
PARAMS ((char *argz
, size_t argz_len
,
121 argz_next__ (argz
, argz_len
, entry
)
128 if (entry
< argz
+ argz_len
)
129 entry
= strchr (entry
, '\0') + 1;
131 return entry
>= argz
+ argz_len
? NULL
: (char *) entry
;
140 # define __argz_next(argz, len, entry) argz_next__ (argz, len, entry)
141 #endif /* !_LIBC && !HAVE___ARGZ_NEXT */
144 /* Return number of bits set in X. */
145 static int pop
PARAMS ((int x
));
151 /* We assume that no more than 16 bits are used. */
152 x
= ((x
& ~0x5555) >> 1) + (x
& 0x5555);
153 x
= ((x
& ~0x3333) >> 2) + (x
& 0x3333);
154 x
= ((x
>> 4) + x
) & 0x0f0f;
155 x
= ((x
>> 8) + x
) & 0xff;
161 struct loaded_l10nfile
*
162 _nl_make_l10nflist (l10nfile_list
, dirlist
, dirlist_len
, mask
, language
,
163 territory
, codeset
, normalized_codeset
, modifier
, special
,
164 sponsor
, revision
, filename
, do_allocate
)
165 struct loaded_l10nfile
**l10nfile_list
;
169 const char *language
;
170 const char *territory
;
172 const char *normalized_codeset
;
173 const char *modifier
;
176 const char *revision
;
177 const char *filename
;
181 struct loaded_l10nfile
*last
= NULL
;
182 struct loaded_l10nfile
*retval
;
187 /* Allocate room for the full file name. */
188 abs_filename
= (char *) malloc (dirlist_len
190 + ((mask
& TERRITORY
) != 0
191 ? strlen (territory
) + 1 : 0)
192 + ((mask
& XPG_CODESET
) != 0
193 ? strlen (codeset
) + 1 : 0)
194 + ((mask
& XPG_NORM_CODESET
) != 0
195 ? strlen (normalized_codeset
) + 1 : 0)
196 + (((mask
& XPG_MODIFIER
) != 0
197 || (mask
& CEN_AUDIENCE
) != 0)
198 ? strlen (modifier
) + 1 : 0)
199 + ((mask
& CEN_SPECIAL
) != 0
200 ? strlen (special
) + 1 : 0)
201 + (((mask
& CEN_SPONSOR
) != 0
202 || (mask
& CEN_REVISION
) != 0)
203 ? (1 + ((mask
& CEN_SPONSOR
) != 0
204 ? strlen (sponsor
) + 1 : 0)
205 + ((mask
& CEN_REVISION
) != 0
206 ? strlen (revision
) + 1 : 0)) : 0)
207 + 1 + strlen (filename
) + 1);
209 if (abs_filename
== NULL
)
215 /* Construct file name. */
216 memcpy (abs_filename
, dirlist
, dirlist_len
);
217 __argz_stringify (abs_filename
, dirlist_len
, PATH_SEPARATOR
);
218 cp
= abs_filename
+ (dirlist_len
- 1);
220 cp
= stpcpy (cp
, language
);
222 if ((mask
& TERRITORY
) != 0)
225 cp
= stpcpy (cp
, territory
);
227 if ((mask
& XPG_CODESET
) != 0)
230 cp
= stpcpy (cp
, codeset
);
232 if ((mask
& XPG_NORM_CODESET
) != 0)
235 cp
= stpcpy (cp
, normalized_codeset
);
237 if ((mask
& (XPG_MODIFIER
| CEN_AUDIENCE
)) != 0)
239 /* This component can be part of both syntaces but has different
240 leading characters. For CEN we use `+', else `@'. */
241 *cp
++ = (mask
& CEN_AUDIENCE
) != 0 ? '+' : '@';
242 cp
= stpcpy (cp
, modifier
);
244 if ((mask
& CEN_SPECIAL
) != 0)
247 cp
= stpcpy (cp
, special
);
249 if ((mask
& (CEN_SPONSOR
| CEN_REVISION
)) != 0)
252 if ((mask
& CEN_SPONSOR
) != 0)
253 cp
= stpcpy (cp
, sponsor
);
254 if ((mask
& CEN_REVISION
) != 0)
257 cp
= stpcpy (cp
, revision
);
262 stpcpy (cp
, filename
);
264 /* Look in list of already loaded domains whether it is already
267 for (retval
= *l10nfile_list
; retval
!= NULL
; retval
= retval
->next
)
268 if (retval
->filename
!= NULL
)
270 int compare
= strcmp (retval
->filename
, abs_filename
);
276 /* It's not in the list. */
284 if (retval
!= NULL
|| do_allocate
== 0)
290 retval
= (struct loaded_l10nfile
*)
291 malloc (sizeof (*retval
) + (__argz_count (dirlist
, dirlist_len
)
293 * sizeof (struct loaded_l10nfile
*)));
297 retval
->filename
= abs_filename
;
298 retval
->decided
= (__argz_count (dirlist
, dirlist_len
) != 1
299 || ((mask
& XPG_CODESET
) != 0
300 && (mask
& XPG_NORM_CODESET
) != 0));
305 retval
->next
= *l10nfile_list
;
306 *l10nfile_list
= retval
;
310 retval
->next
= last
->next
;
315 /* If the DIRLIST is a real list the RETVAL entry corresponds not to
316 a real file. So we have to use the DIRLIST separation mechanism
317 of the inner loop. */
318 cnt
= __argz_count (dirlist
, dirlist_len
) == 1 ? mask
- 1 : mask
;
319 for (; cnt
>= 0; --cnt
)
320 if ((cnt
& ~mask
) == 0
321 && ((cnt
& CEN_SPECIFIC
) == 0 || (cnt
& XPG_SPECIFIC
) == 0)
322 && ((cnt
& XPG_CODESET
) == 0 || (cnt
& XPG_NORM_CODESET
) == 0))
324 /* Iterate over all elements of the DIRLIST. */
327 while ((dir
= __argz_next ((char *) dirlist
, dirlist_len
, dir
))
329 retval
->successor
[entries
++]
330 = _nl_make_l10nflist (l10nfile_list
, dir
, strlen (dir
) + 1, cnt
,
331 language
, territory
, codeset
,
332 normalized_codeset
, modifier
, special
,
333 sponsor
, revision
, filename
, 1);
335 retval
->successor
[entries
] = NULL
;
340 /* Normalize codeset name. There is no standard for the codeset
341 names. Normalization allows the user to use any of the common
342 names. The return value is dynamically allocated and has to be
343 freed by the caller. */
345 _nl_normalize_codeset (codeset
, name_len
)
355 for (cnt
= 0; cnt
< name_len
; ++cnt
)
356 if (isalnum ((unsigned char) codeset
[cnt
]))
360 if (isalpha ((unsigned char) codeset
[cnt
]))
364 retval
= (char *) malloc ((only_digit
? 3 : 0) + len
+ 1);
369 wp
= stpcpy (retval
, "iso");
373 for (cnt
= 0; cnt
< name_len
; ++cnt
)
374 if (isalpha ((unsigned char) codeset
[cnt
]))
375 *wp
++ = tolower ((unsigned char) codeset
[cnt
]);
376 else if (isdigit ((unsigned char) codeset
[cnt
]))
377 *wp
++ = codeset
[cnt
];
382 return (const char *) retval
;
386 /* @@ begin of epilog @@ */
388 /* We don't want libintl.a to depend on any other library. So we
389 avoid the non-standard function stpcpy. In GNU C Library this
390 function is available, though. Also allow the symbol HAVE_STPCPY
392 #if !_LIBC && !HAVE_STPCPY
398 while ((*dest
++ = *src
++) != '\0')