1 /* Copyright (C) 1995, 1996 Free Software Foundation, Inc.
2 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
4 This file is part of the GNU C Library. Its master source is NOT part of
5 the C library, however. The master source lives in /gd/gnu/lib.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If
19 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 Cambridge, MA 02139, USA. */
22 #if defined _LIBC || (defined __ARGZ_COUNT && defined __ARGZ_STRINGIFY)
31 /* Define function which are usually not available. */
33 #if !defined _LIBC && !defined __ARGZ_COUNT
34 /* Returns the number of strings in ARGZ. */
35 static size_t __argz_count __P ((const char *argz, size_t len));
38 __argz_count (argz, len)
45 size_t part_len = strlen(argz);
52 #endif /* !_LIBC && !__ARGZ_COUNT */
54 #if !defined _LIBC && !defined __ARGZ_STRINGIFY
55 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
56 except the last into the character SEP. */
57 static void __argz_stringify __P ((char *argz, size_t len, int sep));
60 __argz_stringify (argz, len, sep)
67 size_t part_len = strlen(argz);
74 #endif /* !_LIBC && !__ARGZ_COUNT */
76 #if !defined _LIBC && !defined __ARGZ_NEXT
78 __argz_next (char *argz, size_t argz_len, const char *entry)
82 if (entry < argz + argz_len)
83 entry = strchr (entry, '\0') + 1;
85 return entry >= argz + argz_len ? NULL : (char *) entry;
96 /* Return number of bits set in X. */
97 static int pop __P ((int x));
103 /* We assume that no more than 16 bits are used. */
104 x = ((x & ~0x5555) >> 1) + (x & 0x5555);
105 x = ((x & ~0x3333) >> 2) + (x & 0x3333);
106 x = ((x >> 4) + x) & 0x0f0f;
107 x = ((x >> 8) + x) & 0xff;
113 struct loaded_l10nfile *
114 _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len, mask, language,
115 territory, codeset, normalized_codeset, modifier, special,
116 sponsor, revision, filename, do_allocate)
117 struct loaded_l10nfile **l10nfile_list;
121 const char *language;
122 const char *territory;
124 const char *normalized_codeset;
125 const char *modifier;
128 const char *revision;
129 const char *filename;
133 struct loaded_l10nfile *last = NULL;
134 struct loaded_l10nfile *retval;
139 /* Allocate room for the full file name. */
140 abs_filename = (char *) malloc (dirlist_len
142 + ((mask & TERRITORY) != 0
143 ? strlen (territory) + 1 : 0)
144 + ((mask & XPG_CODESET) != 0
145 ? strlen (codeset) + 1 : 0)
146 + ((mask & XPG_NORM_CODESET) != 0
147 ? strlen (normalized_codeset) + 1 : 0)
148 + (((mask & XPG_MODIFIER) != 0
149 || (mask & CEN_AUDIENCE) != 0) ?
150 strlen (modifier) + 1 : 0)
151 + ((mask & CEN_SPECIAL) != 0
152 ? strlen (special) + 1 : 0)
153 + ((mask & CEN_SPONSOR) != 0
154 ? strlen (sponsor) + 1 : 0)
155 + ((mask & CEN_REVISION) != 0
156 ? strlen (revision) + 1 : 0)
157 + 1 + strlen (filename) + 1);
159 if (abs_filename == NULL)
165 /* Construct file name. */
166 memcpy (abs_filename, dirlist, dirlist_len);
167 __argz_stringify (abs_filename, dirlist_len, ':');
168 cp = abs_filename + (dirlist_len - 1);
170 cp = stpcpy (cp, language);
172 if ((mask & TERRITORY) != 0)
175 cp = stpcpy (cp, territory);
177 if ((mask & XPG_CODESET) != 0)
180 cp = stpcpy (cp, codeset);
182 if ((mask & XPG_NORM_CODESET) != 0)
185 cp = stpcpy (cp, normalized_codeset);
187 if ((mask & (XPG_MODIFIER | CEN_AUDIENCE)) != 0)
189 /* This component can be part of both syntaces but has different
190 leading characters. For CEN we use `+', else `@'. */
191 *cp++ = (mask & CEN_AUDIENCE) != 0 ? '+' : '@';
192 cp = stpcpy (cp, modifier);
194 if ((mask & CEN_SPECIAL) != 0)
197 cp = stpcpy (cp, special);
199 if ((mask & CEN_SPONSOR) != 0)
202 cp = stpcpy (cp, sponsor);
204 if ((mask & CEN_REVISION) != 0)
207 cp = stpcpy (cp, revision);
211 stpcpy (cp, filename);
213 /* Look in list of already loaded domains whether it is already
216 for (retval = *l10nfile_list; retval != NULL; retval = retval->next)
217 if (retval->filename != NULL)
219 int compare = strcmp (retval->filename, abs_filename);
225 /* It's not in the list. */
233 if (retval != NULL || do_allocate == 0)
239 retval = (struct loaded_l10nfile *)
240 malloc (sizeof (*retval) + (__argz_count (dirlist, dirlist_len)
242 * sizeof (struct loaded_l10nfile *)));
246 retval->filename = abs_filename;
247 retval->decided = (__argz_count (dirlist, dirlist_len) != 1
248 || ((mask & XPG_CODESET) != 0
249 && (mask & XPG_NORM_CODESET) != 0));
254 retval->next = *l10nfile_list;
255 *l10nfile_list = retval;
259 retval->next = last->next;
264 /* If the DIRLIST is a real list the RETVAL entry correcponds not to
265 a real file. So we have to use the DIRLIST separation machanism
266 of the inner loop. */
267 cnt = __argz_count (dirlist, dirlist_len) == 1 ? mask - 1 : mask;
268 for (; cnt >= 0; --cnt)
269 if ((cnt & ~mask) == 0
270 && ((cnt & CEN_SPECIFIC) == 0 || (cnt & XPG_SPECIFIC) == 0)
271 && ((cnt & XPG_CODESET) == 0 || (cnt & XPG_NORM_CODESET) == 0))
273 /* Iterate over all elements of the DIRLIST. */
276 while ((dir = __argz_next ((char *) dirlist, dirlist_len, dir))
278 retval->successor[entries++]
279 = _nl_make_l10nflist (l10nfile_list, dir, strlen (dir) + 1, cnt,
280 language, territory, codeset,
281 normalized_codeset, modifier, special,
282 sponsor, revision, filename, 1);
284 retval->successor[entries] = NULL;
289 /* Normalize codeset name. There is no standard for the codeset
290 names. Normalization allows the user to use any of the common
293 _nl_normalize_codeset (codeset, name_len)
303 for (cnt = 0; cnt < name_len; ++cnt)
304 if (isalnum (codeset[cnt]))
308 if (isalpha (codeset[cnt]))
312 retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
317 wp = stpcpy (retval, "ISO");
321 for (cnt = 0; cnt < name_len; ++cnt)
322 if (isalpha (codeset[cnt]))
323 *wp++ = tolower (codeset[cnt]);
324 else if (isdigit (codeset[cnt]))
325 *wp++ = codeset[cnt];
330 return (const char *) retval;