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