No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / texinfo / intl / l10nflist.c
blob4812933f097d36d719348b057c366d4c052a0a8e
1 /* $NetBSD$ */
3 /* Copyright (C) 1995-1999, 2000-2003 Free Software Foundation, Inc.
4 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published
8 by the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 USA. */
21 /* Tell glibc's <string.h> to provide a prototype for stpcpy().
22 This must come before <config.h> because <config.h> may include
23 <features.h>, and once <features.h> has been included, it's too late. */
24 #ifndef _GNU_SOURCE
25 # define _GNU_SOURCE 1
26 #endif
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
32 #include <string.h>
34 #if defined _LIBC || defined HAVE_ARGZ_H
35 # include <argz.h>
36 #endif
37 #include <ctype.h>
38 #include <sys/types.h>
39 #include <stdlib.h>
41 #include "loadinfo.h"
43 /* On some strange systems still no definition of NULL is found. Sigh! */
44 #ifndef NULL
45 # if defined __STDC__ && __STDC__
46 # define NULL ((void *) 0)
47 # else
48 # define NULL 0
49 # endif
50 #endif
52 /* @@ end of prolog @@ */
54 #ifdef _LIBC
55 /* Rename the non ANSI C functions. This is required by the standard
56 because some ANSI C functions will require linking with this object
57 file and the name space must not be polluted. */
58 # ifndef stpcpy
59 # define stpcpy(dest, src) __stpcpy(dest, src)
60 # endif
61 #else
62 # ifndef HAVE_STPCPY
63 static char *stpcpy (char *dest, const char *src);
64 # endif
65 #endif
67 /* Pathname support.
68 ISSLASH(C) tests whether C is a directory separator character.
69 IS_ABSOLUTE_PATH(P) tests whether P is an absolute path. If it is not,
70 it may be concatenated to a directory pathname.
72 #if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
73 /* Win32, OS/2, DOS */
74 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
75 # define HAS_DEVICE(P) \
76 ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
77 && (P)[1] == ':')
78 # define IS_ABSOLUTE_PATH(P) (ISSLASH ((P)[0]) || HAS_DEVICE (P))
79 #else
80 /* Unix */
81 # define ISSLASH(C) ((C) == '/')
82 # define IS_ABSOLUTE_PATH(P) ISSLASH ((P)[0])
83 #endif
85 /* Define function which are usually not available. */
87 #if !defined _LIBC && !defined HAVE___ARGZ_COUNT
88 /* Returns the number of strings in ARGZ. */
89 static size_t
90 argz_count__ (const char *argz, size_t len)
92 size_t count = 0;
93 while (len > 0)
95 size_t part_len = strlen (argz);
96 argz += part_len + 1;
97 len -= part_len + 1;
98 count++;
100 return count;
102 # undef __argz_count
103 # define __argz_count(argz, len) argz_count__ (argz, len)
104 #else
105 # ifdef _LIBC
106 # define __argz_count(argz, len) INTUSE(__argz_count) (argz, len)
107 # endif
108 #endif /* !_LIBC && !HAVE___ARGZ_COUNT */
110 #if !defined _LIBC && !defined HAVE___ARGZ_STRINGIFY
111 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
112 except the last into the character SEP. */
113 static void
114 argz_stringify__ (char *argz, size_t len, int sep)
116 while (len > 0)
118 size_t part_len = strlen (argz);
119 argz += part_len;
120 len -= part_len + 1;
121 if (len > 0)
122 *argz++ = sep;
125 # undef __argz_stringify
126 # define __argz_stringify(argz, len, sep) argz_stringify__ (argz, len, sep)
127 #else
128 # ifdef _LIBC
129 # define __argz_stringify(argz, len, sep) \
130 INTUSE(__argz_stringify) (argz, len, sep)
131 # endif
132 #endif /* !_LIBC && !HAVE___ARGZ_STRINGIFY */
134 #if !defined _LIBC && !defined HAVE___ARGZ_NEXT
135 static char *
136 argz_next__ (char *argz, size_t argz_len, const char *entry)
138 if (entry)
140 if (entry < argz + argz_len)
141 entry = strchr (entry, '\0') + 1;
143 return entry >= argz + argz_len ? NULL : (char *) entry;
145 else
146 if (argz_len > 0)
147 return argz;
148 else
149 return 0;
151 # undef __argz_next
152 # define __argz_next(argz, len, entry) argz_next__ (argz, len, entry)
153 #endif /* !_LIBC && !HAVE___ARGZ_NEXT */
156 /* Return number of bits set in X. */
157 static inline int
158 pop (int x)
160 /* We assume that no more than 16 bits are used. */
161 x = ((x & ~0x5555) >> 1) + (x & 0x5555);
162 x = ((x & ~0x3333) >> 2) + (x & 0x3333);
163 x = ((x >> 4) + x) & 0x0f0f;
164 x = ((x >> 8) + x) & 0xff;
166 return x;
170 struct loaded_l10nfile *
171 _nl_make_l10nflist (struct loaded_l10nfile **l10nfile_list,
172 const char *dirlist, size_t dirlist_len,
173 int mask, const char *language, const char *territory,
174 const char *codeset, const char *normalized_codeset,
175 const char *modifier, const char *special,
176 const char *sponsor, const char *revision,
177 const char *filename, int do_allocate)
179 char *abs_filename;
180 struct loaded_l10nfile **lastp;
181 struct loaded_l10nfile *retval;
182 char *cp;
183 size_t dirlist_count;
184 size_t entries;
185 int cnt;
187 /* If LANGUAGE contains an absolute directory specification, we ignore
188 DIRLIST. */
189 if (IS_ABSOLUTE_PATH (language))
190 dirlist_len = 0;
192 /* Allocate room for the full file name. */
193 abs_filename = (char *) malloc (dirlist_len
194 + strlen (language)
195 + ((mask & TERRITORY) != 0
196 ? strlen (territory) + 1 : 0)
197 + ((mask & XPG_CODESET) != 0
198 ? strlen (codeset) + 1 : 0)
199 + ((mask & XPG_NORM_CODESET) != 0
200 ? strlen (normalized_codeset) + 1 : 0)
201 + (((mask & XPG_MODIFIER) != 0
202 || (mask & CEN_AUDIENCE) != 0)
203 ? strlen (modifier) + 1 : 0)
204 + ((mask & CEN_SPECIAL) != 0
205 ? strlen (special) + 1 : 0)
206 + (((mask & CEN_SPONSOR) != 0
207 || (mask & CEN_REVISION) != 0)
208 ? (1 + ((mask & CEN_SPONSOR) != 0
209 ? strlen (sponsor) : 0)
210 + ((mask & CEN_REVISION) != 0
211 ? strlen (revision) + 1 : 0)) : 0)
212 + 1 + strlen (filename) + 1);
214 if (abs_filename == NULL)
215 return NULL;
217 /* Construct file name. */
218 cp = abs_filename;
219 if (dirlist_len > 0)
221 memcpy (cp, dirlist, dirlist_len);
222 __argz_stringify (cp, dirlist_len, PATH_SEPARATOR);
223 cp += dirlist_len;
224 cp[-1] = '/';
227 cp = stpcpy (cp, language);
229 if ((mask & TERRITORY) != 0)
231 *cp++ = '_';
232 cp = stpcpy (cp, territory);
234 if ((mask & XPG_CODESET) != 0)
236 *cp++ = '.';
237 cp = stpcpy (cp, codeset);
239 if ((mask & XPG_NORM_CODESET) != 0)
241 *cp++ = '.';
242 cp = stpcpy (cp, normalized_codeset);
244 if ((mask & (XPG_MODIFIER | CEN_AUDIENCE)) != 0)
246 /* This component can be part of both syntaces but has different
247 leading characters. For CEN we use `+', else `@'. */
248 *cp++ = (mask & CEN_AUDIENCE) != 0 ? '+' : '@';
249 cp = stpcpy (cp, modifier);
251 if ((mask & CEN_SPECIAL) != 0)
253 *cp++ = '+';
254 cp = stpcpy (cp, special);
256 if ((mask & (CEN_SPONSOR | CEN_REVISION)) != 0)
258 *cp++ = ',';
259 if ((mask & CEN_SPONSOR) != 0)
260 cp = stpcpy (cp, sponsor);
261 if ((mask & CEN_REVISION) != 0)
263 *cp++ = '_';
264 cp = stpcpy (cp, revision);
268 *cp++ = '/';
269 stpcpy (cp, filename);
271 /* Look in list of already loaded domains whether it is already
272 available. */
273 lastp = l10nfile_list;
274 for (retval = *l10nfile_list; retval != NULL; retval = retval->next)
275 if (retval->filename != NULL)
277 int compare = strcmp (retval->filename, abs_filename);
278 if (compare == 0)
279 /* We found it! */
280 break;
281 if (compare < 0)
283 /* It's not in the list. */
284 retval = NULL;
285 break;
288 lastp = &retval->next;
291 if (retval != NULL || do_allocate == 0)
293 free (abs_filename);
294 return retval;
297 dirlist_count = (dirlist_len > 0 ? __argz_count (dirlist, dirlist_len) : 1);
299 /* Allocate a new loaded_l10nfile. */
300 retval =
301 (struct loaded_l10nfile *)
302 malloc (sizeof (*retval)
303 + (((dirlist_count << pop (mask)) + (dirlist_count > 1 ? 1 : 0))
304 * sizeof (struct loaded_l10nfile *)));
305 if (retval == NULL)
306 return NULL;
308 retval->filename = abs_filename;
310 /* We set retval->data to NULL here; it is filled in later.
311 Setting retval->decided to 1 here means that retval does not
312 correspond to a real file (dirlist_count > 1) or is not worth
313 looking up (if an unnormalized codeset was specified). */
314 retval->decided = (dirlist_count > 1
315 || ((mask & XPG_CODESET) != 0
316 && (mask & XPG_NORM_CODESET) != 0));
317 retval->data = NULL;
319 retval->next = *lastp;
320 *lastp = retval;
322 entries = 0;
323 /* Recurse to fill the inheritance list of RETVAL.
324 If the DIRLIST is a real list (i.e. DIRLIST_COUNT > 1), the RETVAL
325 entry does not correspond to a real file; retval->filename contains
326 colons. In this case we loop across all elements of DIRLIST and
327 across all bit patterns dominated by MASK.
328 If the DIRLIST is a single directory or entirely redundant (i.e.
329 DIRLIST_COUNT == 1), we loop across all bit patterns dominated by
330 MASK, excluding MASK itself.
331 In either case, we loop down from MASK to 0. This has the effect
332 that the extra bits in the locale name are dropped in this order:
333 first the modifier, then the territory, then the codeset, then the
334 normalized_codeset. */
335 for (cnt = dirlist_count > 1 ? mask : mask - 1; cnt >= 0; --cnt)
336 if ((cnt & ~mask) == 0
337 && ((cnt & CEN_SPECIFIC) == 0 || (cnt & XPG_SPECIFIC) == 0)
338 && ((cnt & XPG_CODESET) == 0 || (cnt & XPG_NORM_CODESET) == 0))
340 if (dirlist_count > 1)
342 /* Iterate over all elements of the DIRLIST. */
343 char *dir = NULL;
345 while ((dir = __argz_next ((char *) dirlist, dirlist_len, dir))
346 != NULL)
347 retval->successor[entries++]
348 = _nl_make_l10nflist (l10nfile_list, dir, strlen (dir) + 1,
349 cnt, language, territory, codeset,
350 normalized_codeset, modifier, special,
351 sponsor, revision, filename, 1);
353 else
354 retval->successor[entries++]
355 = _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len,
356 cnt, language, territory, codeset,
357 normalized_codeset, modifier, special,
358 sponsor, revision, filename, 1);
360 retval->successor[entries] = NULL;
362 return retval;
365 /* Normalize codeset name. There is no standard for the codeset
366 names. Normalization allows the user to use any of the common
367 names. The return value is dynamically allocated and has to be
368 freed by the caller. */
369 const char *
370 _nl_normalize_codeset (const char *codeset, size_t name_len)
372 int len = 0;
373 int only_digit = 1;
374 char *retval;
375 char *wp;
376 size_t cnt;
378 for (cnt = 0; cnt < name_len; ++cnt)
379 if (isalnum ((unsigned char) codeset[cnt]))
381 ++len;
383 if (isalpha ((unsigned char) codeset[cnt]))
384 only_digit = 0;
387 retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
389 if (retval != NULL)
391 if (only_digit)
392 wp = stpcpy (retval, "iso");
393 else
394 wp = retval;
396 for (cnt = 0; cnt < name_len; ++cnt)
397 if (isalpha ((unsigned char) codeset[cnt]))
398 *wp++ = tolower ((unsigned char) codeset[cnt]);
399 else if (isdigit ((unsigned char) codeset[cnt]))
400 *wp++ = codeset[cnt];
402 *wp = '\0';
405 return (const char *) retval;
409 /* @@ begin of epilog @@ */
411 /* We don't want libintl.a to depend on any other library. So we
412 avoid the non-standard function stpcpy. In GNU C Library this
413 function is available, though. Also allow the symbol HAVE_STPCPY
414 to be defined. */
415 #if !_LIBC && !HAVE_STPCPY
416 static char *
417 stpcpy (char *dest, const char *src)
419 while ((*dest++ = *src++) != '\0')
420 /* Do nothing. */ ;
421 return dest - 1;
423 #endif