Install gettext-0.18.1.1.tar.gz
[msysgit.git] / mingw / share / gettext / intl / l10nflist.c
blob7252d54e76f226724c5aeba2b600a7b419b501a0
1 /* Copyright (C) 1995-1999, 2000-2007 Free Software Foundation, Inc.
2 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published
6 by the Free Software Foundation; either version 2, or (at your option)
7 any later version.
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 GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 USA. */
19 /* Tell glibc's <string.h> to provide a prototype for stpcpy().
20 This must come before <config.h> because <config.h> may include
21 <features.h>, and once <features.h> has been included, it's too late. */
22 #ifndef _GNU_SOURCE
23 # define _GNU_SOURCE 1
24 #endif
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
30 #include <string.h>
32 #if defined _LIBC || defined HAVE_ARGZ_H
33 # include <argz.h>
34 #endif
35 #include <ctype.h>
36 #include <sys/types.h>
37 #include <stdlib.h>
39 #include "loadinfo.h"
41 /* On some strange systems still no definition of NULL is found. Sigh! */
42 #ifndef NULL
43 # if defined __STDC__ && __STDC__
44 # define NULL ((void *) 0)
45 # else
46 # define NULL 0
47 # endif
48 #endif
50 /* @@ end of prolog @@ */
52 #ifdef _LIBC
53 /* Rename the non ANSI C functions. This is required by the standard
54 because some ANSI C functions will require linking with this object
55 file and the name space must not be polluted. */
56 # ifndef stpcpy
57 # define stpcpy(dest, src) __stpcpy(dest, src)
58 # endif
59 #else
60 # ifndef HAVE_STPCPY
61 static char *stpcpy (char *dest, const char *src);
62 # endif
63 #endif
65 /* Pathname support.
66 ISSLASH(C) tests whether C is a directory separator character.
67 IS_ABSOLUTE_PATH(P) tests whether P is an absolute path. If it is not,
68 it may be concatenated to a directory pathname.
70 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
71 /* Win32, Cygwin, OS/2, DOS */
72 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
73 # define HAS_DEVICE(P) \
74 ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
75 && (P)[1] == ':')
76 # define IS_ABSOLUTE_PATH(P) (ISSLASH ((P)[0]) || HAS_DEVICE (P))
77 #else
78 /* Unix */
79 # define ISSLASH(C) ((C) == '/')
80 # define IS_ABSOLUTE_PATH(P) ISSLASH ((P)[0])
81 #endif
83 /* Define function which are usually not available. */
85 #ifdef _LIBC
86 # define __argz_count(argz, len) INTUSE(__argz_count) (argz, len)
87 #elif defined HAVE_ARGZ_COUNT
88 # undef __argz_count
89 # define __argz_count argz_count
90 #else
91 /* Returns the number of strings in ARGZ. */
92 static size_t
93 argz_count__ (const char *argz, size_t len)
95 size_t count = 0;
96 while (len > 0)
98 size_t part_len = strlen (argz);
99 argz += part_len + 1;
100 len -= part_len + 1;
101 count++;
103 return count;
105 # undef __argz_count
106 # define __argz_count(argz, len) argz_count__ (argz, len)
107 #endif /* !_LIBC && !HAVE_ARGZ_COUNT */
109 #ifdef _LIBC
110 # define __argz_stringify(argz, len, sep) \
111 INTUSE(__argz_stringify) (argz, len, sep)
112 #elif defined HAVE_ARGZ_STRINGIFY
113 # undef __argz_stringify
114 # define __argz_stringify argz_stringify
115 #else
116 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
117 except the last into the character SEP. */
118 static void
119 argz_stringify__ (char *argz, size_t len, int sep)
121 while (len > 0)
123 size_t part_len = strlen (argz);
124 argz += part_len;
125 len -= part_len + 1;
126 if (len > 0)
127 *argz++ = sep;
130 # undef __argz_stringify
131 # define __argz_stringify(argz, len, sep) argz_stringify__ (argz, len, sep)
132 #endif /* !_LIBC && !HAVE_ARGZ_STRINGIFY */
134 #ifdef _LIBC
135 #elif defined HAVE_ARGZ_NEXT
136 # undef __argz_next
137 # define __argz_next argz_next
138 #else
139 static char *
140 argz_next__ (char *argz, size_t argz_len, const char *entry)
142 if (entry)
144 if (entry < argz + argz_len)
145 entry = strchr (entry, '\0') + 1;
147 return entry >= argz + argz_len ? NULL : (char *) entry;
149 else
150 if (argz_len > 0)
151 return argz;
152 else
153 return 0;
155 # undef __argz_next
156 # define __argz_next(argz, len, entry) argz_next__ (argz, len, entry)
157 #endif /* !_LIBC && !HAVE_ARGZ_NEXT */
160 /* Return number of bits set in X. */
161 static inline int
162 pop (int x)
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;
170 return x;
174 struct loaded_l10nfile *
175 _nl_make_l10nflist (struct loaded_l10nfile **l10nfile_list,
176 const char *dirlist, size_t dirlist_len,
177 int mask, const char *language, const char *territory,
178 const char *codeset, const char *normalized_codeset,
179 const char *modifier,
180 const char *filename, int do_allocate)
182 char *abs_filename;
183 struct loaded_l10nfile **lastp;
184 struct loaded_l10nfile *retval;
185 char *cp;
186 size_t dirlist_count;
187 size_t entries;
188 int cnt;
190 /* If LANGUAGE contains an absolute directory specification, we ignore
191 DIRLIST. */
192 if (IS_ABSOLUTE_PATH (language))
193 dirlist_len = 0;
195 /* Allocate room for the full file name. */
196 abs_filename = (char *) malloc (dirlist_len
197 + strlen (language)
198 + ((mask & XPG_TERRITORY) != 0
199 ? strlen (territory) + 1 : 0)
200 + ((mask & XPG_CODESET) != 0
201 ? strlen (codeset) + 1 : 0)
202 + ((mask & XPG_NORM_CODESET) != 0
203 ? strlen (normalized_codeset) + 1 : 0)
204 + ((mask & XPG_MODIFIER) != 0
205 ? strlen (modifier) + 1 : 0)
206 + 1 + strlen (filename) + 1);
208 if (abs_filename == NULL)
209 return NULL;
211 /* Construct file name. */
212 cp = abs_filename;
213 if (dirlist_len > 0)
215 memcpy (cp, dirlist, dirlist_len);
216 __argz_stringify (cp, dirlist_len, PATH_SEPARATOR);
217 cp += dirlist_len;
218 cp[-1] = '/';
221 cp = stpcpy (cp, language);
223 if ((mask & XPG_TERRITORY) != 0)
225 *cp++ = '_';
226 cp = stpcpy (cp, territory);
228 if ((mask & XPG_CODESET) != 0)
230 *cp++ = '.';
231 cp = stpcpy (cp, codeset);
233 if ((mask & XPG_NORM_CODESET) != 0)
235 *cp++ = '.';
236 cp = stpcpy (cp, normalized_codeset);
238 if ((mask & XPG_MODIFIER) != 0)
240 *cp++ = '@';
241 cp = stpcpy (cp, modifier);
244 *cp++ = '/';
245 stpcpy (cp, filename);
247 /* Look in list of already loaded domains whether it is already
248 available. */
249 lastp = l10nfile_list;
250 for (retval = *l10nfile_list; retval != NULL; retval = retval->next)
251 if (retval->filename != NULL)
253 int compare = strcmp (retval->filename, abs_filename);
254 if (compare == 0)
255 /* We found it! */
256 break;
257 if (compare < 0)
259 /* It's not in the list. */
260 retval = NULL;
261 break;
264 lastp = &retval->next;
267 if (retval != NULL || do_allocate == 0)
269 free (abs_filename);
270 return retval;
273 dirlist_count = (dirlist_len > 0 ? __argz_count (dirlist, dirlist_len) : 1);
275 /* Allocate a new loaded_l10nfile. */
276 retval =
277 (struct loaded_l10nfile *)
278 malloc (sizeof (*retval)
279 + (((dirlist_count << pop (mask)) + (dirlist_count > 1 ? 1 : 0))
280 * sizeof (struct loaded_l10nfile *)));
281 if (retval == NULL)
283 free (abs_filename);
284 return NULL;
287 retval->filename = abs_filename;
289 /* We set retval->data to NULL here; it is filled in later.
290 Setting retval->decided to 1 here means that retval does not
291 correspond to a real file (dirlist_count > 1) or is not worth
292 looking up (if an unnormalized codeset was specified). */
293 retval->decided = (dirlist_count > 1
294 || ((mask & XPG_CODESET) != 0
295 && (mask & XPG_NORM_CODESET) != 0));
296 retval->data = NULL;
298 retval->next = *lastp;
299 *lastp = retval;
301 entries = 0;
302 /* Recurse to fill the inheritance list of RETVAL.
303 If the DIRLIST is a real list (i.e. DIRLIST_COUNT > 1), the RETVAL
304 entry does not correspond to a real file; retval->filename contains
305 colons. In this case we loop across all elements of DIRLIST and
306 across all bit patterns dominated by MASK.
307 If the DIRLIST is a single directory or entirely redundant (i.e.
308 DIRLIST_COUNT == 1), we loop across all bit patterns dominated by
309 MASK, excluding MASK itself.
310 In either case, we loop down from MASK to 0. This has the effect
311 that the extra bits in the locale name are dropped in this order:
312 first the modifier, then the territory, then the codeset, then the
313 normalized_codeset. */
314 for (cnt = dirlist_count > 1 ? mask : mask - 1; cnt >= 0; --cnt)
315 if ((cnt & ~mask) == 0
316 && !((cnt & XPG_CODESET) != 0 && (cnt & XPG_NORM_CODESET) != 0))
318 if (dirlist_count > 1)
320 /* Iterate over all elements of the DIRLIST. */
321 char *dir = NULL;
323 while ((dir = __argz_next ((char *) dirlist, dirlist_len, dir))
324 != NULL)
325 retval->successor[entries++]
326 = _nl_make_l10nflist (l10nfile_list, dir, strlen (dir) + 1,
327 cnt, language, territory, codeset,
328 normalized_codeset, modifier, filename,
331 else
332 retval->successor[entries++]
333 = _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len,
334 cnt, language, territory, codeset,
335 normalized_codeset, modifier, filename, 1);
337 retval->successor[entries] = NULL;
339 return retval;
342 /* Normalize codeset name. There is no standard for the codeset
343 names. Normalization allows the user to use any of the common
344 names. The return value is dynamically allocated and has to be
345 freed by the caller. */
346 const char *
347 _nl_normalize_codeset (const char *codeset, size_t name_len)
349 size_t len = 0;
350 int only_digit = 1;
351 char *retval;
352 char *wp;
353 size_t cnt;
355 for (cnt = 0; cnt < name_len; ++cnt)
356 if (isalnum ((unsigned char) codeset[cnt]))
358 ++len;
360 if (isalpha ((unsigned char) codeset[cnt]))
361 only_digit = 0;
364 retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
366 if (retval != NULL)
368 if (only_digit)
369 wp = stpcpy (retval, "iso");
370 else
371 wp = retval;
373 for (cnt = 0; cnt < name_len; ++cnt)
374 if (isalpha ((unsigned char) codeset[cnt]))
375 *wp++ = tolower ((unsigned char) codeset[cnt]);
376 else if (isdigit ((unsigned char) codeset[cnt]))
377 *wp++ = codeset[cnt];
379 *wp = '\0';
382 return (const char *) retval;
386 /* @@ begin of epilog @@ */
388 /* We don't want libintl.a to depend on any other library. So we
389 avoid the non-standard function stpcpy. In GNU C Library this
390 function is available, though. Also allow the symbol HAVE_STPCPY
391 to be defined. */
392 #if !_LIBC && !HAVE_STPCPY
393 static char *
394 stpcpy (char *dest, const char *src)
396 while ((*dest++ = *src++) != '\0')
397 /* Do nothing. */ ;
398 return dest - 1;
400 #endif