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