]> git.ipfire.org Git - thirdparty/glibc.git/blame - intl/l10nflist.c
Update.
[thirdparty/glibc.git] / intl / l10nflist.c
CommitLineData
4caef86c 1/* Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
e4cf5070 2 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
7a12c6bb 3
e4cf5070 4 This file is part of the GNU C Library. Its master source is NOT part of
40a55d20 5 the C library, however.
5f2eab42 6
e4cf5070
UD
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.
7a12c6bb 11
e4cf5070
UD
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.
7a12c6bb 16
e4cf5070
UD
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 not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
7a12c6bb 21
92f3773b
RM
22#ifdef HAVE_CONFIG_H
23# include <config.h>
24#endif
25
92f3773b
RM
26
27#if defined HAVE_STRING_H || defined _LIBC
842907c6
RM
28# ifndef _GNU_SOURCE
29# define _GNU_SOURCE 1
30# endif
92f3773b
RM
31# include <string.h>
32#else
33# include <strings.h>
8f2ece69
UD
34# ifndef memcpy
35# define memcpy(Dst, Src, Num) bcopy (Src, Dst, Num)
36# endif
92f3773b
RM
37#endif
38#if !HAVE_STRCHR && !defined _LIBC
39# ifndef strchr
40# define strchr index
41# endif
42#endif
7a12c6bb 43
96e1bff2
RM
44#if defined _LIBC || defined HAVE_ARGZ_H
45# include <argz.h>
46#endif
47#include <ctype.h>
0c5ecdc4 48#include <sys/types.h>
96e1bff2
RM
49
50#if defined STDC_HEADERS || defined _LIBC
51# include <stdlib.h>
52#endif
53
7a12c6bb
RM
54#include "loadinfo.h"
55
842907c6
RM
56/* On some strange systems still no definition of NULL is found. Sigh! */
57#ifndef NULL
58# if defined __STDC__ && __STDC__
59# define NULL ((void *) 0)
60# else
61# define NULL 0
62# endif
63#endif
64
92f3773b
RM
65/* @@ end of prolog @@ */
66
67#ifdef _LIBC
68/* Rename the non ANSI C functions. This is required by the standard
69 because some ANSI C functions will require linking with this object
70 file and the name space must not be polluted. */
9a0a462c
UD
71# ifndef stpcpy
72# define stpcpy(dest, src) __stpcpy(dest, src)
73# endif
92f3773b
RM
74#else
75# ifndef HAVE_STPCPY
76static char *stpcpy PARAMS ((char *dest, const char *src));
77# endif
78#endif
79
5f2eab42
RM
80/* Define function which are usually not available. */
81
842907c6 82#if !defined _LIBC && !defined HAVE___ARGZ_COUNT
5f2eab42 83/* Returns the number of strings in ARGZ. */
842907c6 84static size_t argz_count__ PARAMS ((const char *argz, size_t len));
5f2eab42
RM
85
86static size_t
842907c6 87argz_count__ (argz, len)
5f2eab42
RM
88 const char *argz;
89 size_t len;
90{
91 size_t count = 0;
92 while (len > 0)
93 {
92f3773b 94 size_t part_len = strlen (argz);
5f2eab42
RM
95 argz += part_len + 1;
96 len -= part_len + 1;
97 count++;
98 }
99 return count;
100}
842907c6
RM
101# undef __argz_count
102# define __argz_count(argz, len) argz_count__ (argz, len)
103#endif /* !_LIBC && !HAVE___ARGZ_COUNT */
5f2eab42 104
842907c6 105#if !defined _LIBC && !defined HAVE___ARGZ_STRINGIFY
5f2eab42
RM
106/* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
107 except the last into the character SEP. */
842907c6 108static void argz_stringify__ PARAMS ((char *argz, size_t len, int sep));
5f2eab42
RM
109
110static void
842907c6 111argz_stringify__ (argz, len, sep)
5f2eab42
RM
112 char *argz;
113 size_t len;
114 int sep;
115{
116 while (len > 0)
117 {
92f3773b 118 size_t part_len = strlen (argz);
5f2eab42
RM
119 argz += part_len;
120 len -= part_len + 1;
121 if (len > 0)
122 *argz++ = sep;
123 }
124}
842907c6
RM
125# undef __argz_stringify
126# define __argz_stringify(argz, len, sep) argz_stringify__ (argz, len, sep)
127#endif /* !_LIBC && !HAVE___ARGZ_STRINGIFY */
128
129#if !defined _LIBC && !defined HAVE___ARGZ_NEXT
130static char *argz_next__ PARAMS ((char *argz, size_t argz_len,
131 const char *entry));
5f2eab42 132
5f2eab42 133static char *
842907c6 134argz_next__ (argz, argz_len, entry)
92f3773b
RM
135 char *argz;
136 size_t argz_len;
137 const char *entry;
5f2eab42
RM
138{
139 if (entry)
140 {
141 if (entry < argz + argz_len)
142 entry = strchr (entry, '\0') + 1;
143
144 return entry >= argz + argz_len ? NULL : (char *) entry;
145 }
146 else
147 if (argz_len > 0)
148 return argz;
149 else
150 return 0;
151}
842907c6
RM
152# undef __argz_next
153# define __argz_next(argz, len, entry) argz_next__ (argz, len, entry)
154#endif /* !_LIBC && !HAVE___ARGZ_NEXT */
5f2eab42
RM
155
156
7a12c6bb 157/* Return number of bits set in X. */
92f3773b 158static int pop PARAMS ((int x));
7a12c6bb
RM
159
160static inline int
161pop (x)
162 int x;
163{
164 /* We assume that no more than 16 bits are used. */
165 x = ((x & ~0x5555) >> 1) + (x & 0x5555);
166 x = ((x & ~0x3333) >> 2) + (x & 0x3333);
167 x = ((x >> 4) + x) & 0x0f0f;
168 x = ((x >> 8) + x) & 0xff;
169
170 return x;
171}
172
5f2eab42 173\f
7a12c6bb
RM
174struct loaded_l10nfile *
175_nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len, mask, language,
176 territory, codeset, normalized_codeset, modifier, special,
177 sponsor, revision, filename, do_allocate)
178 struct loaded_l10nfile **l10nfile_list;
179 const char *dirlist;
180 size_t dirlist_len;
181 int mask;
182 const char *language;
183 const char *territory;
184 const char *codeset;
185 const char *normalized_codeset;
186 const char *modifier;
187 const char *special;
188 const char *sponsor;
189 const char *revision;
190 const char *filename;
191 int do_allocate;
192{
193 char *abs_filename;
194 struct loaded_l10nfile *last = NULL;
195 struct loaded_l10nfile *retval;
196 char *cp;
197 size_t entries;
198 int cnt;
199
200 /* Allocate room for the full file name. */
201 abs_filename = (char *) malloc (dirlist_len
202 + strlen (language)
203 + ((mask & TERRITORY) != 0
204 ? strlen (territory) + 1 : 0)
205 + ((mask & XPG_CODESET) != 0
206 ? strlen (codeset) + 1 : 0)
207 + ((mask & XPG_NORM_CODESET) != 0
208 ? strlen (normalized_codeset) + 1 : 0)
209 + (((mask & XPG_MODIFIER) != 0
47707456
UD
210 || (mask & CEN_AUDIENCE) != 0)
211 ? strlen (modifier) + 1 : 0)
7a12c6bb
RM
212 + ((mask & CEN_SPECIAL) != 0
213 ? strlen (special) + 1 : 0)
1fb05e3d
UD
214 + (((mask & CEN_SPONSOR) != 0
215 || (mask & CEN_REVISION) != 0)
e4cf5070
UD
216 ? (1 + ((mask & CEN_SPONSOR) != 0
217 ? strlen (sponsor) + 1 : 0)
218 + ((mask & CEN_REVISION) != 0
219 ? strlen (revision) + 1 : 0)) : 0)
7a12c6bb
RM
220 + 1 + strlen (filename) + 1);
221
222 if (abs_filename == NULL)
223 return NULL;
224
225 retval = NULL;
226 last = NULL;
227
228 /* Construct file name. */
229 memcpy (abs_filename, dirlist, dirlist_len);
230 __argz_stringify (abs_filename, dirlist_len, ':');
231 cp = abs_filename + (dirlist_len - 1);
232 *cp++ = '/';
233 cp = stpcpy (cp, language);
234
235 if ((mask & TERRITORY) != 0)
236 {
237 *cp++ = '_';
238 cp = stpcpy (cp, territory);
239 }
240 if ((mask & XPG_CODESET) != 0)
241 {
242 *cp++ = '.';
243 cp = stpcpy (cp, codeset);
244 }
245 if ((mask & XPG_NORM_CODESET) != 0)
246 {
247 *cp++ = '.';
248 cp = stpcpy (cp, normalized_codeset);
249 }
250 if ((mask & (XPG_MODIFIER | CEN_AUDIENCE)) != 0)
251 {
252 /* This component can be part of both syntaces but has different
253 leading characters. For CEN we use `+', else `@'. */
254 *cp++ = (mask & CEN_AUDIENCE) != 0 ? '+' : '@';
255 cp = stpcpy (cp, modifier);
256 }
257 if ((mask & CEN_SPECIAL) != 0)
258 {
259 *cp++ = '+';
260 cp = stpcpy (cp, special);
261 }
e4cf5070 262 if ((mask & (CEN_SPONSOR | CEN_REVISION)) != 0)
7a12c6bb
RM
263 {
264 *cp++ = ',';
e4cf5070
UD
265 if ((mask & CEN_SPONSOR) != 0)
266 cp = stpcpy (cp, sponsor);
267 if ((mask & CEN_REVISION) != 0)
268 {
269 *cp++ = '_';
270 cp = stpcpy (cp, revision);
271 }
7a12c6bb
RM
272 }
273
274 *cp++ = '/';
275 stpcpy (cp, filename);
276
277 /* Look in list of already loaded domains whether it is already
278 available. */
279 last = NULL;
280 for (retval = *l10nfile_list; retval != NULL; retval = retval->next)
281 if (retval->filename != NULL)
282 {
283 int compare = strcmp (retval->filename, abs_filename);
284 if (compare == 0)
285 /* We found it! */
286 break;
287 if (compare < 0)
288 {
289 /* It's not in the list. */
290 retval = NULL;
291 break;
292 }
293
294 last = retval;
295 }
296
297 if (retval != NULL || do_allocate == 0)
298 {
299 free (abs_filename);
300 return retval;
301 }
302
303 retval = (struct loaded_l10nfile *)
304 malloc (sizeof (*retval) + (__argz_count (dirlist, dirlist_len)
305 * (1 << pop (mask))
306 * sizeof (struct loaded_l10nfile *)));
307 if (retval == NULL)
308 return NULL;
309
310 retval->filename = abs_filename;
311 retval->decided = (__argz_count (dirlist, dirlist_len) != 1
312 || ((mask & XPG_CODESET) != 0
313 && (mask & XPG_NORM_CODESET) != 0));
314 retval->data = NULL;
315
316 if (last == NULL)
317 {
318 retval->next = *l10nfile_list;
319 *l10nfile_list = retval;
320 }
321 else
322 {
323 retval->next = last->next;
324 last->next = retval;
325 }
326
327 entries = 0;
6d52618b
UD
328 /* If the DIRLIST is a real list the RETVAL entry corresponds not to
329 a real file. So we have to use the DIRLIST separation mechanism
7a12c6bb
RM
330 of the inner loop. */
331 cnt = __argz_count (dirlist, dirlist_len) == 1 ? mask - 1 : mask;
332 for (; cnt >= 0; --cnt)
333 if ((cnt & ~mask) == 0
334 && ((cnt & CEN_SPECIFIC) == 0 || (cnt & XPG_SPECIFIC) == 0)
335 && ((cnt & XPG_CODESET) == 0 || (cnt & XPG_NORM_CODESET) == 0))
336 {
337 /* Iterate over all elements of the DIRLIST. */
338 char *dir = NULL;
339
340 while ((dir = __argz_next ((char *) dirlist, dirlist_len, dir))
341 != NULL)
342 retval->successor[entries++]
343 = _nl_make_l10nflist (l10nfile_list, dir, strlen (dir) + 1, cnt,
344 language, territory, codeset,
345 normalized_codeset, modifier, special,
346 sponsor, revision, filename, 1);
347 }
348 retval->successor[entries] = NULL;
349
350 return retval;
351}
352\f
353/* Normalize codeset name. There is no standard for the codeset
354 names. Normalization allows the user to use any of the common
355 names. */
356const char *
357_nl_normalize_codeset (codeset, name_len)
8cb569b7 358 const char *codeset;
7a12c6bb
RM
359 size_t name_len;
360{
361 int len = 0;
362 int only_digit = 1;
363 char *retval;
364 char *wp;
365 size_t cnt;
366
367 for (cnt = 0; cnt < name_len; ++cnt)
368 if (isalnum (codeset[cnt]))
369 {
370 ++len;
371
372 if (isalpha (codeset[cnt]))
373 only_digit = 0;
374 }
375
376 retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
377
378 if (retval != NULL)
379 {
380 if (only_digit)
57ba7bb4 381 wp = stpcpy (retval, "iso");
7a12c6bb
RM
382 else
383 wp = retval;
384
385 for (cnt = 0; cnt < name_len; ++cnt)
386 if (isalpha (codeset[cnt]))
4caef86c 387 *wp++ = _tolower (codeset[cnt]);
7a12c6bb
RM
388 else if (isdigit (codeset[cnt]))
389 *wp++ = codeset[cnt];
390
391 *wp = '\0';
392 }
393
394 return (const char *) retval;
395}
92f3773b
RM
396
397
398/* @@ begin of epilog @@ */
399
400/* We don't want libintl.a to depend on any other library. So we
401 avoid the non-standard function stpcpy. In GNU C Library this
402 function is available, though. Also allow the symbol HAVE_STPCPY
403 to be defined. */
404#if !_LIBC && !HAVE_STPCPY
405static char *
406stpcpy (dest, src)
407 char *dest;
408 const char *src;
409{
410 while ((*dest++ = *src++) != '\0')
411 /* Do nothing. */ ;
412 return dest - 1;
413}
414#endif