Imported Upstream version 0.19.7
[platform/upstream/gettext.git] / gettext-runtime / intl / l10nflist.c
1 /* Copyright (C) 1995-2015 Free Software Foundation, Inc.
2    Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU Lesser General Public License as published by
6    the Free Software Foundation; either version 2.1 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Tell glibc's <string.h> to provide a prototype for stpcpy().
18    This must come before <config.h> because <config.h> may include
19    <features.h>, and once <features.h> has been included, it's too late.  */
20 #ifndef _GNU_SOURCE
21 # define _GNU_SOURCE    1
22 #endif
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #include <string.h>
29
30 #if defined _LIBC || defined HAVE_ARGZ_H
31 # include <argz.h>
32 #endif
33 #include <ctype.h>
34 #include <sys/types.h>
35 #include <stdlib.h>
36
37 #include "loadinfo.h"
38
39 /* On some strange systems still no definition of NULL is found.  Sigh!  */
40 #ifndef NULL
41 # if defined __STDC__ && __STDC__
42 #  define NULL ((void *) 0)
43 # else
44 #  define NULL 0
45 # endif
46 #endif
47
48 /* @@ end of prolog @@ */
49
50 #ifdef _LIBC
51 /* Rename the non ANSI C functions.  This is required by the standard
52    because some ANSI C functions will require linking with this object
53    file and the name space must not be polluted.  */
54 # ifndef stpcpy
55 #  define stpcpy(dest, src) __stpcpy(dest, src)
56 # endif
57 #else
58 # ifndef HAVE_STPCPY
59 static char *stpcpy (char *dest, const char *src);
60 # endif
61 #endif
62
63 /* Pathname support.
64    ISSLASH(C)           tests whether C is a directory separator character.
65    IS_ABSOLUTE_PATH(P)  tests whether P is an absolute path.  If it is not,
66                         it may be concatenated to a directory pathname.
67  */
68 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
69   /* Win32, Cygwin, OS/2, DOS */
70 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
71 # define HAS_DEVICE(P) \
72     ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
73      && (P)[1] == ':')
74 # define IS_ABSOLUTE_PATH(P) (ISSLASH ((P)[0]) || HAS_DEVICE (P))
75 #else
76   /* Unix */
77 # define ISSLASH(C) ((C) == '/')
78 # define IS_ABSOLUTE_PATH(P) ISSLASH ((P)[0])
79 #endif
80
81 /* Define function which are usually not available.  */
82
83 #if defined HAVE_ARGZ_COUNT
84 # undef __argz_count
85 # define __argz_count argz_count
86 #else
87 /* Returns the number of strings in ARGZ.  */
88 static size_t
89 argz_count__ (const char *argz, size_t len)
90 {
91   size_t count = 0;
92   while (len > 0)
93     {
94       size_t part_len = strlen (argz);
95       argz += part_len + 1;
96       len -= part_len + 1;
97       count++;
98     }
99   return count;
100 }
101 # undef __argz_count
102 # define __argz_count(argz, len) argz_count__ (argz, len)
103 #endif  /* !_LIBC && !HAVE_ARGZ_COUNT */
104
105 #if defined HAVE_ARGZ_STRINGIFY
106 # undef __argz_stringify
107 # define __argz_stringify argz_stringify
108 #else
109 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
110    except the last into the character SEP.  */
111 static void
112 argz_stringify__ (char *argz, size_t len, int sep)
113 {
114   while (len > 0)
115     {
116       size_t part_len = strlen (argz);
117       argz += part_len;
118       len -= part_len + 1;
119       if (len > 0)
120         *argz++ = sep;
121     }
122 }
123 # undef __argz_stringify
124 # define __argz_stringify(argz, len, sep) argz_stringify__ (argz, len, sep)
125 #endif  /* !_LIBC && !HAVE_ARGZ_STRINGIFY */
126
127 #ifdef _LIBC
128 #elif defined HAVE_ARGZ_NEXT
129 # undef __argz_next
130 # define __argz_next argz_next
131 #else
132 static char *
133 argz_next__ (char *argz, size_t argz_len, const char *entry)
134 {
135   if (entry)
136     {
137       if (entry < argz + argz_len)
138         entry = strchr (entry, '\0') + 1;
139
140       return entry >= argz + argz_len ? NULL : (char *) entry;
141     }
142   else
143     if (argz_len > 0)
144       return argz;
145     else
146       return 0;
147 }
148 # undef __argz_next
149 # define __argz_next(argz, len, entry) argz_next__ (argz, len, entry)
150 #endif  /* !_LIBC && !HAVE_ARGZ_NEXT */
151
152 /* Return number of bits set in X.  */
153 #ifndef ARCH_POP
154 static inline int
155 pop (int x)
156 {
157   /* We assume that no more than 16 bits are used.  */
158   x = ((x & ~0x5555) >> 1) + (x & 0x5555);
159   x = ((x & ~0x3333) >> 2) + (x & 0x3333);
160   x = ((x >> 4) + x) & 0x0f0f;
161   x = ((x >> 8) + x) & 0xff;
162
163   return x;
164 }
165 #endif
166
167 \f
168 struct loaded_l10nfile *
169 _nl_make_l10nflist (struct loaded_l10nfile **l10nfile_list,
170                     const char *dirlist, size_t dirlist_len,
171                     int mask, const char *language, const char *territory,
172                     const char *codeset, const char *normalized_codeset,
173                     const char *modifier,
174                     const char *filename, int do_allocate)
175 {
176   char *abs_filename;
177   struct loaded_l10nfile **lastp;
178   struct loaded_l10nfile *retval;
179   char *cp;
180   size_t dirlist_count;
181   size_t entries;
182   int cnt;
183
184   /* If LANGUAGE contains an absolute directory specification, we ignore
185      DIRLIST.  */
186   if (IS_ABSOLUTE_PATH (language))
187     dirlist_len = 0;
188
189   /* Allocate room for the full file name.  */
190   abs_filename = (char *) malloc (dirlist_len
191                                   + strlen (language)
192                                   + ((mask & XPG_TERRITORY) != 0
193                                      ? strlen (territory) + 1 : 0)
194                                   + ((mask & XPG_CODESET) != 0
195                                      ? strlen (codeset) + 1 : 0)
196                                   + ((mask & XPG_NORM_CODESET) != 0
197                                      ? strlen (normalized_codeset) + 1 : 0)
198                                   + ((mask & XPG_MODIFIER) != 0
199                                      ? strlen (modifier) + 1 : 0)
200                                   + 1 + strlen (filename) + 1);
201
202   if (abs_filename == NULL)
203     return NULL;
204
205   /* Construct file name.  */
206   cp = abs_filename;
207   if (dirlist_len > 0)
208     {
209       memcpy (cp, dirlist, dirlist_len);
210       __argz_stringify (cp, dirlist_len, PATH_SEPARATOR);
211       cp += dirlist_len;
212       cp[-1] = '/';
213     }
214
215   cp = stpcpy (cp, language);
216
217   if ((mask & XPG_TERRITORY) != 0)
218     {
219       *cp++ = '_';
220       cp = stpcpy (cp, territory);
221     }
222   if ((mask & XPG_CODESET) != 0)
223     {
224       *cp++ = '.';
225       cp = stpcpy (cp, codeset);
226     }
227   if ((mask & XPG_NORM_CODESET) != 0)
228     {
229       *cp++ = '.';
230       cp = stpcpy (cp, normalized_codeset);
231     }
232   if ((mask & XPG_MODIFIER) != 0)
233     {
234       *cp++ = '@';
235       cp = stpcpy (cp, modifier);
236     }
237
238   *cp++ = '/';
239   stpcpy (cp, filename);
240
241   /* Look in list of already loaded domains whether it is already
242      available.  */
243   lastp = l10nfile_list;
244   for (retval = *l10nfile_list; retval != NULL; retval = retval->next)
245     if (retval->filename != NULL)
246       {
247         int compare = strcmp (retval->filename, abs_filename);
248         if (compare == 0)
249           /* We found it!  */
250           break;
251         if (compare < 0)
252           {
253             /* It's not in the list.  */
254             retval = NULL;
255             break;
256           }
257
258         lastp = &retval->next;
259       }
260
261   if (retval != NULL || do_allocate == 0)
262     {
263       free (abs_filename);
264       return retval;
265     }
266
267   dirlist_count = (dirlist_len > 0 ? __argz_count (dirlist, dirlist_len) : 1);
268
269   /* Allocate a new loaded_l10nfile.  */
270   retval =
271     (struct loaded_l10nfile *)
272     malloc (sizeof (*retval)
273             + (((dirlist_count << pop (mask)) + (dirlist_count > 1 ? 1 : 0))
274                * sizeof (struct loaded_l10nfile *)));
275   if (retval == NULL)
276     {
277       free (abs_filename);
278       return NULL;
279     }
280
281   retval->filename = abs_filename;
282
283   /* We set retval->data to NULL here; it is filled in later.
284      Setting retval->decided to 1 here means that retval does not
285      correspond to a real file (dirlist_count > 1) or is not worth
286      looking up (if an unnormalized codeset was specified).  */
287   retval->decided = (dirlist_count > 1
288                      || ((mask & XPG_CODESET) != 0
289                          && (mask & XPG_NORM_CODESET) != 0));
290   retval->data = NULL;
291
292   retval->next = *lastp;
293   *lastp = retval;
294
295   entries = 0;
296   /* Recurse to fill the inheritance list of RETVAL.
297      If the DIRLIST is a real list (i.e. DIRLIST_COUNT > 1), the RETVAL
298      entry does not correspond to a real file; retval->filename contains
299      colons.  In this case we loop across all elements of DIRLIST and
300      across all bit patterns dominated by MASK.
301      If the DIRLIST is a single directory or entirely redundant (i.e.
302      DIRLIST_COUNT == 1), we loop across all bit patterns dominated by
303      MASK, excluding MASK itself.
304      In either case, we loop down from MASK to 0.  This has the effect
305      that the extra bits in the locale name are dropped in this order:
306      first the modifier, then the territory, then the codeset, then the
307      normalized_codeset.  */
308   for (cnt = dirlist_count > 1 ? mask : mask - 1; cnt >= 0; --cnt)
309     if ((cnt & ~mask) == 0
310         && !((cnt & XPG_CODESET) != 0 && (cnt & XPG_NORM_CODESET) != 0))
311       {
312         if (dirlist_count > 1)
313           {
314             /* Iterate over all elements of the DIRLIST.  */
315             char *dir = NULL;
316
317             while ((dir = __argz_next ((char *) dirlist, dirlist_len, dir))
318                    != NULL)
319               retval->successor[entries++]
320                 = _nl_make_l10nflist (l10nfile_list, dir, strlen (dir) + 1,
321                                       cnt, language, territory, codeset,
322                                       normalized_codeset, modifier, filename,
323                                       1);
324           }
325         else
326           retval->successor[entries++]
327             = _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len,
328                                   cnt, language, territory, codeset,
329                                   normalized_codeset, modifier, filename, 1);
330       }
331   retval->successor[entries] = NULL;
332
333   return retval;
334 }
335 \f
336 /* Normalize codeset name.  There is no standard for the codeset
337    names.  Normalization allows the user to use any of the common
338    names.  The return value is dynamically allocated and has to be
339    freed by the caller.  */
340 const char *
341 _nl_normalize_codeset (const char *codeset, size_t name_len)
342 {
343   size_t len = 0;
344   int only_digit = 1;
345   char *retval;
346   char *wp;
347   size_t cnt;
348
349   for (cnt = 0; cnt < name_len; ++cnt)
350     if (isalnum ((unsigned char) codeset[cnt]))
351       {
352         ++len;
353
354         if (isalpha ((unsigned char) codeset[cnt]))
355           only_digit = 0;
356       }
357
358   retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
359
360   if (retval != NULL)
361     {
362       if (only_digit)
363         wp = stpcpy (retval, "iso");
364       else
365         wp = retval;
366
367       for (cnt = 0; cnt < name_len; ++cnt)
368         if (isalpha ((unsigned char) codeset[cnt]))
369           *wp++ = tolower ((unsigned char) codeset[cnt]);
370         else if (isdigit ((unsigned char) codeset[cnt]))
371           *wp++ = codeset[cnt];
372
373       *wp = '\0';
374     }
375
376   return (const char *) retval;
377 }
378
379
380 /* @@ begin of epilog @@ */
381
382 /* We don't want libintl.a to depend on any other library.  So we
383    avoid the non-standard function stpcpy.  In GNU C Library this
384    function is available, though.  Also allow the symbol HAVE_STPCPY
385    to be defined.  */
386 #if !_LIBC && !HAVE_STPCPY
387 static char *
388 stpcpy (char *dest, const char *src)
389 {
390   while ((*dest++ = *src++) != '\0')
391     /* Do nothing. */ ;
392   return dest - 1;
393 }
394 #endif