Update.
[glibc.git] / intl / loadmsgcat.c
blob569ace2105e3e56aff677816f7091dd83bc3220f
1 /* Load needed message catalogs.
2 Copyright (C) 1995-2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 /* Tell glibc's <string.h> to provide a prototype for mempcpy().
21 This must come before <config.h> because <config.h> may include
22 <features.h>, and once <features.h> has been included, it's too late. */
23 #ifndef _GNU_SOURCE
24 # define _GNU_SOURCE 1
25 #endif
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
31 #include <ctype.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
37 #ifdef __GNUC__
38 # undef alloca
39 # define alloca __builtin_alloca
40 # define HAVE_ALLOCA 1
41 #else
42 # if defined HAVE_ALLOCA_H || defined _LIBC
43 # include <alloca.h>
44 # else
45 # ifdef _AIX
46 #pragma alloca
47 # else
48 # ifndef alloca
49 char *alloca ();
50 # endif
51 # endif
52 # endif
53 #endif
55 #include <stdlib.h>
56 #include <string.h>
58 #if defined HAVE_UNISTD_H || defined _LIBC
59 # include <unistd.h>
60 #endif
62 #ifdef _LIBC
63 # include <langinfo.h>
64 # include <locale.h>
65 #endif
67 #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \
68 || (defined _LIBC && defined _POSIX_MAPPED_FILES)
69 # include <sys/mman.h>
70 # undef HAVE_MMAP
71 # define HAVE_MMAP 1
72 #else
73 # undef HAVE_MMAP
74 #endif
76 #if defined HAVE_STDINT_H_WITH_UINTMAX || defined _LIBC
77 # include <stdint.h>
78 #endif
79 #if defined HAVE_INTTYPES_H || defined _LIBC
80 # include <inttypes.h>
81 #endif
83 #include "gmo.h"
84 #include "gettextP.h"
85 #include "hash-string.h"
86 #include "plural-exp.h"
88 #ifdef _LIBC
89 # include "../locale/localeinfo.h"
90 # include <not-cancel.h>
91 #endif
93 /* Provide fallback values for macros that ought to be defined in <inttypes.h>.
94 Note that our fallback values need not be literal strings, because we don't
95 use them with preprocessor string concatenation. */
96 #if !defined PRId8 || PRI_MACROS_BROKEN
97 # undef PRId8
98 # define PRId8 "d"
99 #endif
100 #if !defined PRIi8 || PRI_MACROS_BROKEN
101 # undef PRIi8
102 # define PRIi8 "i"
103 #endif
104 #if !defined PRIo8 || PRI_MACROS_BROKEN
105 # undef PRIo8
106 # define PRIo8 "o"
107 #endif
108 #if !defined PRIu8 || PRI_MACROS_BROKEN
109 # undef PRIu8
110 # define PRIu8 "u"
111 #endif
112 #if !defined PRIx8 || PRI_MACROS_BROKEN
113 # undef PRIx8
114 # define PRIx8 "x"
115 #endif
116 #if !defined PRIX8 || PRI_MACROS_BROKEN
117 # undef PRIX8
118 # define PRIX8 "X"
119 #endif
120 #if !defined PRId16 || PRI_MACROS_BROKEN
121 # undef PRId16
122 # define PRId16 "d"
123 #endif
124 #if !defined PRIi16 || PRI_MACROS_BROKEN
125 # undef PRIi16
126 # define PRIi16 "i"
127 #endif
128 #if !defined PRIo16 || PRI_MACROS_BROKEN
129 # undef PRIo16
130 # define PRIo16 "o"
131 #endif
132 #if !defined PRIu16 || PRI_MACROS_BROKEN
133 # undef PRIu16
134 # define PRIu16 "u"
135 #endif
136 #if !defined PRIx16 || PRI_MACROS_BROKEN
137 # undef PRIx16
138 # define PRIx16 "x"
139 #endif
140 #if !defined PRIX16 || PRI_MACROS_BROKEN
141 # undef PRIX16
142 # define PRIX16 "X"
143 #endif
144 #if !defined PRId32 || PRI_MACROS_BROKEN
145 # undef PRId32
146 # define PRId32 "d"
147 #endif
148 #if !defined PRIi32 || PRI_MACROS_BROKEN
149 # undef PRIi32
150 # define PRIi32 "i"
151 #endif
152 #if !defined PRIo32 || PRI_MACROS_BROKEN
153 # undef PRIo32
154 # define PRIo32 "o"
155 #endif
156 #if !defined PRIu32 || PRI_MACROS_BROKEN
157 # undef PRIu32
158 # define PRIu32 "u"
159 #endif
160 #if !defined PRIx32 || PRI_MACROS_BROKEN
161 # undef PRIx32
162 # define PRIx32 "x"
163 #endif
164 #if !defined PRIX32 || PRI_MACROS_BROKEN
165 # undef PRIX32
166 # define PRIX32 "X"
167 #endif
168 #if !defined PRId64 || PRI_MACROS_BROKEN
169 # undef PRId64
170 # define PRId64 (sizeof (long) == 8 ? "ld" : "lld")
171 #endif
172 #if !defined PRIi64 || PRI_MACROS_BROKEN
173 # undef PRIi64
174 # define PRIi64 (sizeof (long) == 8 ? "li" : "lli")
175 #endif
176 #if !defined PRIo64 || PRI_MACROS_BROKEN
177 # undef PRIo64
178 # define PRIo64 (sizeof (long) == 8 ? "lo" : "llo")
179 #endif
180 #if !defined PRIu64 || PRI_MACROS_BROKEN
181 # undef PRIu64
182 # define PRIu64 (sizeof (long) == 8 ? "lu" : "llu")
183 #endif
184 #if !defined PRIx64 || PRI_MACROS_BROKEN
185 # undef PRIx64
186 # define PRIx64 (sizeof (long) == 8 ? "lx" : "llx")
187 #endif
188 #if !defined PRIX64 || PRI_MACROS_BROKEN
189 # undef PRIX64
190 # define PRIX64 (sizeof (long) == 8 ? "lX" : "llX")
191 #endif
192 #if !defined PRIdLEAST8 || PRI_MACROS_BROKEN
193 # undef PRIdLEAST8
194 # define PRIdLEAST8 "d"
195 #endif
196 #if !defined PRIiLEAST8 || PRI_MACROS_BROKEN
197 # undef PRIiLEAST8
198 # define PRIiLEAST8 "i"
199 #endif
200 #if !defined PRIoLEAST8 || PRI_MACROS_BROKEN
201 # undef PRIoLEAST8
202 # define PRIoLEAST8 "o"
203 #endif
204 #if !defined PRIuLEAST8 || PRI_MACROS_BROKEN
205 # undef PRIuLEAST8
206 # define PRIuLEAST8 "u"
207 #endif
208 #if !defined PRIxLEAST8 || PRI_MACROS_BROKEN
209 # undef PRIxLEAST8
210 # define PRIxLEAST8 "x"
211 #endif
212 #if !defined PRIXLEAST8 || PRI_MACROS_BROKEN
213 # undef PRIXLEAST8
214 # define PRIXLEAST8 "X"
215 #endif
216 #if !defined PRIdLEAST16 || PRI_MACROS_BROKEN
217 # undef PRIdLEAST16
218 # define PRIdLEAST16 "d"
219 #endif
220 #if !defined PRIiLEAST16 || PRI_MACROS_BROKEN
221 # undef PRIiLEAST16
222 # define PRIiLEAST16 "i"
223 #endif
224 #if !defined PRIoLEAST16 || PRI_MACROS_BROKEN
225 # undef PRIoLEAST16
226 # define PRIoLEAST16 "o"
227 #endif
228 #if !defined PRIuLEAST16 || PRI_MACROS_BROKEN
229 # undef PRIuLEAST16
230 # define PRIuLEAST16 "u"
231 #endif
232 #if !defined PRIxLEAST16 || PRI_MACROS_BROKEN
233 # undef PRIxLEAST16
234 # define PRIxLEAST16 "x"
235 #endif
236 #if !defined PRIXLEAST16 || PRI_MACROS_BROKEN
237 # undef PRIXLEAST16
238 # define PRIXLEAST16 "X"
239 #endif
240 #if !defined PRIdLEAST32 || PRI_MACROS_BROKEN
241 # undef PRIdLEAST32
242 # define PRIdLEAST32 "d"
243 #endif
244 #if !defined PRIiLEAST32 || PRI_MACROS_BROKEN
245 # undef PRIiLEAST32
246 # define PRIiLEAST32 "i"
247 #endif
248 #if !defined PRIoLEAST32 || PRI_MACROS_BROKEN
249 # undef PRIoLEAST32
250 # define PRIoLEAST32 "o"
251 #endif
252 #if !defined PRIuLEAST32 || PRI_MACROS_BROKEN
253 # undef PRIuLEAST32
254 # define PRIuLEAST32 "u"
255 #endif
256 #if !defined PRIxLEAST32 || PRI_MACROS_BROKEN
257 # undef PRIxLEAST32
258 # define PRIxLEAST32 "x"
259 #endif
260 #if !defined PRIXLEAST32 || PRI_MACROS_BROKEN
261 # undef PRIXLEAST32
262 # define PRIXLEAST32 "X"
263 #endif
264 #if !defined PRIdLEAST64 || PRI_MACROS_BROKEN
265 # undef PRIdLEAST64
266 # define PRIdLEAST64 PRId64
267 #endif
268 #if !defined PRIiLEAST64 || PRI_MACROS_BROKEN
269 # undef PRIiLEAST64
270 # define PRIiLEAST64 PRIi64
271 #endif
272 #if !defined PRIoLEAST64 || PRI_MACROS_BROKEN
273 # undef PRIoLEAST64
274 # define PRIoLEAST64 PRIo64
275 #endif
276 #if !defined PRIuLEAST64 || PRI_MACROS_BROKEN
277 # undef PRIuLEAST64
278 # define PRIuLEAST64 PRIu64
279 #endif
280 #if !defined PRIxLEAST64 || PRI_MACROS_BROKEN
281 # undef PRIxLEAST64
282 # define PRIxLEAST64 PRIx64
283 #endif
284 #if !defined PRIXLEAST64 || PRI_MACROS_BROKEN
285 # undef PRIXLEAST64
286 # define PRIXLEAST64 PRIX64
287 #endif
288 #if !defined PRIdFAST8 || PRI_MACROS_BROKEN
289 # undef PRIdFAST8
290 # define PRIdFAST8 "d"
291 #endif
292 #if !defined PRIiFAST8 || PRI_MACROS_BROKEN
293 # undef PRIiFAST8
294 # define PRIiFAST8 "i"
295 #endif
296 #if !defined PRIoFAST8 || PRI_MACROS_BROKEN
297 # undef PRIoFAST8
298 # define PRIoFAST8 "o"
299 #endif
300 #if !defined PRIuFAST8 || PRI_MACROS_BROKEN
301 # undef PRIuFAST8
302 # define PRIuFAST8 "u"
303 #endif
304 #if !defined PRIxFAST8 || PRI_MACROS_BROKEN
305 # undef PRIxFAST8
306 # define PRIxFAST8 "x"
307 #endif
308 #if !defined PRIXFAST8 || PRI_MACROS_BROKEN
309 # undef PRIXFAST8
310 # define PRIXFAST8 "X"
311 #endif
312 #if !defined PRIdFAST16 || PRI_MACROS_BROKEN
313 # undef PRIdFAST16
314 # define PRIdFAST16 "d"
315 #endif
316 #if !defined PRIiFAST16 || PRI_MACROS_BROKEN
317 # undef PRIiFAST16
318 # define PRIiFAST16 "i"
319 #endif
320 #if !defined PRIoFAST16 || PRI_MACROS_BROKEN
321 # undef PRIoFAST16
322 # define PRIoFAST16 "o"
323 #endif
324 #if !defined PRIuFAST16 || PRI_MACROS_BROKEN
325 # undef PRIuFAST16
326 # define PRIuFAST16 "u"
327 #endif
328 #if !defined PRIxFAST16 || PRI_MACROS_BROKEN
329 # undef PRIxFAST16
330 # define PRIxFAST16 "x"
331 #endif
332 #if !defined PRIXFAST16 || PRI_MACROS_BROKEN
333 # undef PRIXFAST16
334 # define PRIXFAST16 "X"
335 #endif
336 #if !defined PRIdFAST32 || PRI_MACROS_BROKEN
337 # undef PRIdFAST32
338 # define PRIdFAST32 "d"
339 #endif
340 #if !defined PRIiFAST32 || PRI_MACROS_BROKEN
341 # undef PRIiFAST32
342 # define PRIiFAST32 "i"
343 #endif
344 #if !defined PRIoFAST32 || PRI_MACROS_BROKEN
345 # undef PRIoFAST32
346 # define PRIoFAST32 "o"
347 #endif
348 #if !defined PRIuFAST32 || PRI_MACROS_BROKEN
349 # undef PRIuFAST32
350 # define PRIuFAST32 "u"
351 #endif
352 #if !defined PRIxFAST32 || PRI_MACROS_BROKEN
353 # undef PRIxFAST32
354 # define PRIxFAST32 "x"
355 #endif
356 #if !defined PRIXFAST32 || PRI_MACROS_BROKEN
357 # undef PRIXFAST32
358 # define PRIXFAST32 "X"
359 #endif
360 #if !defined PRIdFAST64 || PRI_MACROS_BROKEN
361 # undef PRIdFAST64
362 # define PRIdFAST64 PRId64
363 #endif
364 #if !defined PRIiFAST64 || PRI_MACROS_BROKEN
365 # undef PRIiFAST64
366 # define PRIiFAST64 PRIi64
367 #endif
368 #if !defined PRIoFAST64 || PRI_MACROS_BROKEN
369 # undef PRIoFAST64
370 # define PRIoFAST64 PRIo64
371 #endif
372 #if !defined PRIuFAST64 || PRI_MACROS_BROKEN
373 # undef PRIuFAST64
374 # define PRIuFAST64 PRIu64
375 #endif
376 #if !defined PRIxFAST64 || PRI_MACROS_BROKEN
377 # undef PRIxFAST64
378 # define PRIxFAST64 PRIx64
379 #endif
380 #if !defined PRIXFAST64 || PRI_MACROS_BROKEN
381 # undef PRIXFAST64
382 # define PRIXFAST64 PRIX64
383 #endif
384 #if !defined PRIdMAX || PRI_MACROS_BROKEN
385 # undef PRIdMAX
386 # define PRIdMAX (sizeof (uintmax_t) == sizeof (long) ? "ld" : "lld")
387 #endif
388 #if !defined PRIiMAX || PRI_MACROS_BROKEN
389 # undef PRIiMAX
390 # define PRIiMAX (sizeof (uintmax_t) == sizeof (long) ? "li" : "lli")
391 #endif
392 #if !defined PRIoMAX || PRI_MACROS_BROKEN
393 # undef PRIoMAX
394 # define PRIoMAX (sizeof (uintmax_t) == sizeof (long) ? "lo" : "llo")
395 #endif
396 #if !defined PRIuMAX || PRI_MACROS_BROKEN
397 # undef PRIuMAX
398 # define PRIuMAX (sizeof (uintmax_t) == sizeof (long) ? "lu" : "llu")
399 #endif
400 #if !defined PRIxMAX || PRI_MACROS_BROKEN
401 # undef PRIxMAX
402 # define PRIxMAX (sizeof (uintmax_t) == sizeof (long) ? "lx" : "llx")
403 #endif
404 #if !defined PRIXMAX || PRI_MACROS_BROKEN
405 # undef PRIXMAX
406 # define PRIXMAX (sizeof (uintmax_t) == sizeof (long) ? "lX" : "llX")
407 #endif
408 #if !defined PRIdPTR || PRI_MACROS_BROKEN
409 # undef PRIdPTR
410 # define PRIdPTR \
411 (sizeof (void *) == sizeof (long) ? "ld" : \
412 sizeof (void *) == sizeof (int) ? "d" : \
413 "lld")
414 #endif
415 #if !defined PRIiPTR || PRI_MACROS_BROKEN
416 # undef PRIiPTR
417 # define PRIiPTR \
418 (sizeof (void *) == sizeof (long) ? "li" : \
419 sizeof (void *) == sizeof (int) ? "i" : \
420 "lli")
421 #endif
422 #if !defined PRIoPTR || PRI_MACROS_BROKEN
423 # undef PRIoPTR
424 # define PRIoPTR \
425 (sizeof (void *) == sizeof (long) ? "lo" : \
426 sizeof (void *) == sizeof (int) ? "o" : \
427 "llo")
428 #endif
429 #if !defined PRIuPTR || PRI_MACROS_BROKEN
430 # undef PRIuPTR
431 # define PRIuPTR \
432 (sizeof (void *) == sizeof (long) ? "lu" : \
433 sizeof (void *) == sizeof (int) ? "u" : \
434 "llu")
435 #endif
436 #if !defined PRIxPTR || PRI_MACROS_BROKEN
437 # undef PRIxPTR
438 # define PRIxPTR \
439 (sizeof (void *) == sizeof (long) ? "lx" : \
440 sizeof (void *) == sizeof (int) ? "x" : \
441 "llx")
442 #endif
443 #if !defined PRIXPTR || PRI_MACROS_BROKEN
444 # undef PRIXPTR
445 # define PRIXPTR \
446 (sizeof (void *) == sizeof (long) ? "lX" : \
447 sizeof (void *) == sizeof (int) ? "X" : \
448 "llX")
449 #endif
451 /* @@ end of prolog @@ */
453 #ifdef _LIBC
454 /* Rename the non ISO C functions. This is required by the standard
455 because some ISO C functions will require linking with this object
456 file and the name space must not be polluted. */
457 # define open(name, flags) open_not_cancel_2 (name, flags)
458 # define close(fd) close_not_cancel_no_status (fd)
459 # define read(fd, buf, n) read_not_cancel (fd, buf, n)
460 # define mmap(addr, len, prot, flags, fd, offset) \
461 __mmap (addr, len, prot, flags, fd, offset)
462 # define munmap(addr, len) __munmap (addr, len)
463 #endif
465 /* For those losing systems which don't have `alloca' we have to add
466 some additional code emulating it. */
467 #ifdef HAVE_ALLOCA
468 # define freea(p) /* nothing */
469 #else
470 # define alloca(n) malloc (n)
471 # define freea(p) free (p)
472 #endif
475 /* Prototypes for local functions. Needed to ensure compiler checking of
476 function argument counts despite of K&R C function definition syntax. */
477 static const char *get_sysdep_segment_value PARAMS ((const char *name));
480 /* We need a sign, whether a new catalog was loaded, which can be associated
481 with all translations. This is important if the translations are
482 cached by one of GCC's features. */
483 int _nl_msg_cat_cntr;
486 /* Expand a system dependent string segment. Return NULL if unsupported. */
487 static const char *
488 get_sysdep_segment_value (name)
489 const char *name;
491 /* Test for an ISO C 99 section 7.8.1 format string directive.
492 Syntax:
493 P R I { d | i | o | u | x | X }
494 { { | LEAST | FAST } { 8 | 16 | 32 | 64 } | MAX | PTR } */
495 /* We don't use a table of 14 times 6 'const char *' strings here, because
496 data relocations cost startup time. */
497 if (name[0] == 'P' && name[1] == 'R' && name[2] == 'I')
499 if (name[3] == 'd' || name[3] == 'i' || name[3] == 'o' || name[3] == 'u'
500 || name[3] == 'x' || name[3] == 'X')
502 if (name[4] == '8' && name[5] == '\0')
504 if (name[3] == 'd')
505 return PRId8;
506 if (name[3] == 'i')
507 return PRIi8;
508 if (name[3] == 'o')
509 return PRIo8;
510 if (name[3] == 'u')
511 return PRIu8;
512 if (name[3] == 'x')
513 return PRIx8;
514 if (name[3] == 'X')
515 return PRIX8;
516 abort ();
518 if (name[4] == '1' && name[5] == '6' && name[6] == '\0')
520 if (name[3] == 'd')
521 return PRId16;
522 if (name[3] == 'i')
523 return PRIi16;
524 if (name[3] == 'o')
525 return PRIo16;
526 if (name[3] == 'u')
527 return PRIu16;
528 if (name[3] == 'x')
529 return PRIx16;
530 if (name[3] == 'X')
531 return PRIX16;
532 abort ();
534 if (name[4] == '3' && name[5] == '2' && name[6] == '\0')
536 if (name[3] == 'd')
537 return PRId32;
538 if (name[3] == 'i')
539 return PRIi32;
540 if (name[3] == 'o')
541 return PRIo32;
542 if (name[3] == 'u')
543 return PRIu32;
544 if (name[3] == 'x')
545 return PRIx32;
546 if (name[3] == 'X')
547 return PRIX32;
548 abort ();
550 if (name[4] == '6' && name[5] == '4' && name[6] == '\0')
552 if (name[3] == 'd')
553 return PRId64;
554 if (name[3] == 'i')
555 return PRIi64;
556 if (name[3] == 'o')
557 return PRIo64;
558 if (name[3] == 'u')
559 return PRIu64;
560 if (name[3] == 'x')
561 return PRIx64;
562 if (name[3] == 'X')
563 return PRIX64;
564 abort ();
566 if (name[4] == 'L' && name[5] == 'E' && name[6] == 'A'
567 && name[7] == 'S' && name[8] == 'T')
569 if (name[9] == '8' && name[10] == '\0')
571 if (name[3] == 'd')
572 return PRIdLEAST8;
573 if (name[3] == 'i')
574 return PRIiLEAST8;
575 if (name[3] == 'o')
576 return PRIoLEAST8;
577 if (name[3] == 'u')
578 return PRIuLEAST8;
579 if (name[3] == 'x')
580 return PRIxLEAST8;
581 if (name[3] == 'X')
582 return PRIXLEAST8;
583 abort ();
585 if (name[9] == '1' && name[10] == '6' && name[11] == '\0')
587 if (name[3] == 'd')
588 return PRIdLEAST16;
589 if (name[3] == 'i')
590 return PRIiLEAST16;
591 if (name[3] == 'o')
592 return PRIoLEAST16;
593 if (name[3] == 'u')
594 return PRIuLEAST16;
595 if (name[3] == 'x')
596 return PRIxLEAST16;
597 if (name[3] == 'X')
598 return PRIXLEAST16;
599 abort ();
601 if (name[9] == '3' && name[10] == '2' && name[11] == '\0')
603 if (name[3] == 'd')
604 return PRIdLEAST32;
605 if (name[3] == 'i')
606 return PRIiLEAST32;
607 if (name[3] == 'o')
608 return PRIoLEAST32;
609 if (name[3] == 'u')
610 return PRIuLEAST32;
611 if (name[3] == 'x')
612 return PRIxLEAST32;
613 if (name[3] == 'X')
614 return PRIXLEAST32;
615 abort ();
617 if (name[9] == '6' && name[10] == '4' && name[11] == '\0')
619 if (name[3] == 'd')
620 return PRIdLEAST64;
621 if (name[3] == 'i')
622 return PRIiLEAST64;
623 if (name[3] == 'o')
624 return PRIoLEAST64;
625 if (name[3] == 'u')
626 return PRIuLEAST64;
627 if (name[3] == 'x')
628 return PRIxLEAST64;
629 if (name[3] == 'X')
630 return PRIXLEAST64;
631 abort ();
634 if (name[4] == 'F' && name[5] == 'A' && name[6] == 'S'
635 && name[7] == 'T')
637 if (name[8] == '8' && name[9] == '\0')
639 if (name[3] == 'd')
640 return PRIdFAST8;
641 if (name[3] == 'i')
642 return PRIiFAST8;
643 if (name[3] == 'o')
644 return PRIoFAST8;
645 if (name[3] == 'u')
646 return PRIuFAST8;
647 if (name[3] == 'x')
648 return PRIxFAST8;
649 if (name[3] == 'X')
650 return PRIXFAST8;
651 abort ();
653 if (name[8] == '1' && name[9] == '6' && name[10] == '\0')
655 if (name[3] == 'd')
656 return PRIdFAST16;
657 if (name[3] == 'i')
658 return PRIiFAST16;
659 if (name[3] == 'o')
660 return PRIoFAST16;
661 if (name[3] == 'u')
662 return PRIuFAST16;
663 if (name[3] == 'x')
664 return PRIxFAST16;
665 if (name[3] == 'X')
666 return PRIXFAST16;
667 abort ();
669 if (name[8] == '3' && name[9] == '2' && name[10] == '\0')
671 if (name[3] == 'd')
672 return PRIdFAST32;
673 if (name[3] == 'i')
674 return PRIiFAST32;
675 if (name[3] == 'o')
676 return PRIoFAST32;
677 if (name[3] == 'u')
678 return PRIuFAST32;
679 if (name[3] == 'x')
680 return PRIxFAST32;
681 if (name[3] == 'X')
682 return PRIXFAST32;
683 abort ();
685 if (name[8] == '6' && name[9] == '4' && name[10] == '\0')
687 if (name[3] == 'd')
688 return PRIdFAST64;
689 if (name[3] == 'i')
690 return PRIiFAST64;
691 if (name[3] == 'o')
692 return PRIoFAST64;
693 if (name[3] == 'u')
694 return PRIuFAST64;
695 if (name[3] == 'x')
696 return PRIxFAST64;
697 if (name[3] == 'X')
698 return PRIXFAST64;
699 abort ();
702 if (name[4] == 'M' && name[5] == 'A' && name[6] == 'X'
703 && name[7] == '\0')
705 if (name[3] == 'd')
706 return PRIdMAX;
707 if (name[3] == 'i')
708 return PRIiMAX;
709 if (name[3] == 'o')
710 return PRIoMAX;
711 if (name[3] == 'u')
712 return PRIuMAX;
713 if (name[3] == 'x')
714 return PRIxMAX;
715 if (name[3] == 'X')
716 return PRIXMAX;
717 abort ();
719 if (name[4] == 'P' && name[5] == 'T' && name[6] == 'R'
720 && name[7] == '\0')
722 if (name[3] == 'd')
723 return PRIdPTR;
724 if (name[3] == 'i')
725 return PRIiPTR;
726 if (name[3] == 'o')
727 return PRIoPTR;
728 if (name[3] == 'u')
729 return PRIuPTR;
730 if (name[3] == 'x')
731 return PRIxPTR;
732 if (name[3] == 'X')
733 return PRIXPTR;
734 abort ();
738 /* Test for a glibc specific printf() format directive flag. */
739 if (name[0] == 'I' && name[1] == '\0')
741 #if defined _LIBC || __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
742 /* The 'I' flag, in numeric format directives, replaces ASCII digits
743 with the 'outdigits' defined in the LC_CTYPE locale facet. This is
744 used for Farsi (Persian) and maybe Arabic. */
745 return "I";
746 #else
747 return "";
748 #endif
750 /* Other system dependent strings are not valid. */
751 return NULL;
754 /* Initialize the codeset dependent parts of an opened message catalog.
755 Return the header entry. */
756 const char *
757 internal_function
758 _nl_init_domain_conv (domain_file, domain, domainbinding)
759 struct loaded_l10nfile *domain_file;
760 struct loaded_domain *domain;
761 struct binding *domainbinding;
763 /* Find out about the character set the file is encoded with.
764 This can be found (in textual form) in the entry "". If this
765 entry does not exist or if this does not contain the `charset='
766 information, we will assume the charset matches the one the
767 current locale and we don't have to perform any conversion. */
768 char *nullentry;
769 size_t nullentrylen;
771 /* Preinitialize fields, to avoid recursion during _nl_find_msg. */
772 domain->codeset_cntr =
773 (domainbinding != NULL ? domainbinding->codeset_cntr : 0);
774 #ifdef _LIBC
775 domain->conv = (__gconv_t) -1;
776 #else
777 # if HAVE_ICONV
778 domain->conv = (iconv_t) -1;
779 # endif
780 #endif
781 domain->conv_tab = NULL;
783 /* Get the header entry. */
784 nullentry = _nl_find_msg (domain_file, domainbinding, "", &nullentrylen);
786 if (nullentry != NULL)
788 #if defined _LIBC || HAVE_ICONV
789 const char *charsetstr;
791 charsetstr = strstr (nullentry, "charset=");
792 if (charsetstr != NULL)
794 size_t len;
795 char *charset;
796 const char *outcharset;
798 charsetstr += strlen ("charset=");
799 len = strcspn (charsetstr, " \t\n");
801 charset = (char *) alloca (len + 1);
802 # if defined _LIBC || HAVE_MEMPCPY
803 *((char *) mempcpy (charset, charsetstr, len)) = '\0';
804 # else
805 memcpy (charset, charsetstr, len);
806 charset[len] = '\0';
807 # endif
809 /* The output charset should normally be determined by the
810 locale. But sometimes the locale is not used or not correctly
811 set up, so we provide a possibility for the user to override
812 this. Moreover, the value specified through
813 bind_textdomain_codeset overrides both. */
814 if (domainbinding != NULL && domainbinding->codeset != NULL)
815 outcharset = domainbinding->codeset;
816 else
818 outcharset = getenv ("OUTPUT_CHARSET");
819 if (outcharset == NULL || outcharset[0] == '\0')
821 # ifdef _LIBC
822 outcharset = _NL_CURRENT (LC_CTYPE, CODESET);
823 # else
824 # if HAVE_ICONV
825 extern const char *locale_charset PARAMS ((void));
826 outcharset = locale_charset ();
827 # endif
828 # endif
832 # ifdef _LIBC
833 /* We always want to use transliteration. */
834 outcharset = norm_add_slashes (outcharset, "TRANSLIT");
835 charset = norm_add_slashes (charset, "");
836 if (__gconv_open (outcharset, charset, &domain->conv,
837 GCONV_AVOID_NOCONV)
838 != __GCONV_OK)
839 domain->conv = (__gconv_t) -1;
840 # else
841 # if HAVE_ICONV
842 /* When using GNU libc >= 2.2 or GNU libiconv >= 1.5,
843 we want to use transliteration. */
844 # if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2 \
845 || _LIBICONV_VERSION >= 0x0105
846 if (strchr (outcharset, '/') == NULL)
848 char *tmp;
850 len = strlen (outcharset);
851 tmp = (char *) alloca (len + 10 + 1);
852 memcpy (tmp, outcharset, len);
853 memcpy (tmp + len, "//TRANSLIT", 10 + 1);
854 outcharset = tmp;
856 domain->conv = iconv_open (outcharset, charset);
858 freea (outcharset);
860 else
861 # endif
862 domain->conv = iconv_open (outcharset, charset);
863 # endif
864 # endif
866 freea (charset);
868 #endif /* _LIBC || HAVE_ICONV */
871 return nullentry;
874 /* Frees the codeset dependent parts of an opened message catalog. */
875 void
876 internal_function
877 _nl_free_domain_conv (domain)
878 struct loaded_domain *domain;
880 if (domain->conv_tab != NULL && domain->conv_tab != (char **) -1)
881 free (domain->conv_tab);
883 #ifdef _LIBC
884 if (domain->conv != (__gconv_t) -1)
885 __gconv_close (domain->conv);
886 #else
887 # if HAVE_ICONV
888 if (domain->conv != (iconv_t) -1)
889 iconv_close (domain->conv);
890 # endif
891 #endif
894 /* Load the message catalogs specified by FILENAME. If it is no valid
895 message catalog do nothing. */
896 void
897 internal_function
898 _nl_load_domain (domain_file, domainbinding)
899 struct loaded_l10nfile *domain_file;
900 struct binding *domainbinding;
902 int fd;
903 size_t size;
904 #ifdef _LIBC
905 struct stat64 st;
906 #else
907 struct stat st;
908 #endif
909 struct mo_file_header *data = (struct mo_file_header *) -1;
910 int use_mmap = 0;
911 struct loaded_domain *domain;
912 int revision;
913 const char *nullentry;
915 domain_file->decided = 1;
916 domain_file->data = NULL;
918 /* Note that it would be useless to store domainbinding in domain_file
919 because domainbinding might be == NULL now but != NULL later (after
920 a call to bind_textdomain_codeset). */
922 /* If the record does not represent a valid locale the FILENAME
923 might be NULL. This can happen when according to the given
924 specification the locale file name is different for XPG and CEN
925 syntax. */
926 if (domain_file->filename == NULL)
927 return;
929 /* Try to open the addressed file. */
930 fd = open (domain_file->filename, O_RDONLY);
931 if (fd == -1)
932 return;
934 /* We must know about the size of the file. */
935 if (
936 #ifdef _LIBC
937 __builtin_expect (fstat64 (fd, &st) != 0, 0)
938 #else
939 __builtin_expect (fstat (fd, &st) != 0, 0)
940 #endif
941 || __builtin_expect ((size = (size_t) st.st_size) != st.st_size, 0)
942 || __builtin_expect (size < sizeof (struct mo_file_header), 0))
944 /* Something went wrong. */
945 close (fd);
946 return;
949 #ifdef HAVE_MMAP
950 /* Now we are ready to load the file. If mmap() is available we try
951 this first. If not available or it failed we try to load it. */
952 data = (struct mo_file_header *) mmap (NULL, size, PROT_READ,
953 MAP_PRIVATE, fd, 0);
955 if (__builtin_expect (data != (struct mo_file_header *) -1, 1))
957 /* mmap() call was successful. */
958 close (fd);
959 use_mmap = 1;
961 #endif
963 /* If the data is not yet available (i.e. mmap'ed) we try to load
964 it manually. */
965 if (data == (struct mo_file_header *) -1)
967 size_t to_read;
968 char *read_ptr;
970 data = (struct mo_file_header *) malloc (size);
971 if (data == NULL)
972 return;
974 to_read = size;
975 read_ptr = (char *) data;
978 long int nb = (long int) read (fd, read_ptr, to_read);
979 if (nb <= 0)
981 #ifdef EINTR
982 if (nb == -1 && errno == EINTR)
983 continue;
984 #endif
985 close (fd);
986 return;
988 read_ptr += nb;
989 to_read -= nb;
991 while (to_read > 0);
993 close (fd);
996 /* Using the magic number we can test whether it really is a message
997 catalog file. */
998 if (__builtin_expect (data->magic != _MAGIC && data->magic != _MAGIC_SWAPPED,
1001 /* The magic number is wrong: not a message catalog file. */
1002 #ifdef HAVE_MMAP
1003 if (use_mmap)
1004 munmap ((caddr_t) data, size);
1005 else
1006 #endif
1007 free (data);
1008 return;
1011 domain = (struct loaded_domain *) malloc (sizeof (struct loaded_domain));
1012 if (domain == NULL)
1013 return;
1014 domain_file->data = domain;
1016 domain->data = (char *) data;
1017 domain->use_mmap = use_mmap;
1018 domain->mmap_size = size;
1019 domain->must_swap = data->magic != _MAGIC;
1020 domain->malloced = NULL;
1022 /* Fill in the information about the available tables. */
1023 revision = W (domain->must_swap, data->revision);
1024 /* We support only the major revisions 0 and 1. */
1025 switch (revision >> 16)
1027 case 0:
1028 case 1:
1029 domain->nstrings = W (domain->must_swap, data->nstrings);
1030 domain->orig_tab = (const struct string_desc *)
1031 ((char *) data + W (domain->must_swap, data->orig_tab_offset));
1032 domain->trans_tab = (const struct string_desc *)
1033 ((char *) data + W (domain->must_swap, data->trans_tab_offset));
1034 domain->hash_size = W (domain->must_swap, data->hash_tab_size);
1035 domain->hash_tab =
1036 (domain->hash_size > 2
1037 ? (const nls_uint32 *)
1038 ((char *) data + W (domain->must_swap, data->hash_tab_offset))
1039 : NULL);
1040 domain->must_swap_hash_tab = domain->must_swap;
1042 /* Now dispatch on the minor revision. */
1043 switch (revision & 0xffff)
1045 case 0:
1046 domain->n_sysdep_strings = 0;
1047 domain->orig_sysdep_tab = NULL;
1048 domain->trans_sysdep_tab = NULL;
1049 break;
1050 case 1:
1051 default:
1053 nls_uint32 n_sysdep_strings;
1055 if (domain->hash_tab == NULL)
1056 /* This is invalid. These minor revisions need a hash table. */
1057 goto invalid;
1059 n_sysdep_strings =
1060 W (domain->must_swap, data->n_sysdep_strings);
1061 if (n_sysdep_strings > 0)
1063 nls_uint32 n_sysdep_segments;
1064 const struct sysdep_segment *sysdep_segments;
1065 const char **sysdep_segment_values;
1066 const nls_uint32 *orig_sysdep_tab;
1067 const nls_uint32 *trans_sysdep_tab;
1068 nls_uint32 n_inmem_sysdep_strings;
1069 size_t memneed;
1070 char *mem;
1071 struct sysdep_string_desc *inmem_orig_sysdep_tab;
1072 struct sysdep_string_desc *inmem_trans_sysdep_tab;
1073 nls_uint32 *inmem_hash_tab;
1074 unsigned int i, j;
1076 /* Get the values of the system dependent segments. */
1077 n_sysdep_segments =
1078 W (domain->must_swap, data->n_sysdep_segments);
1079 sysdep_segments = (const struct sysdep_segment *)
1080 ((char *) data
1081 + W (domain->must_swap, data->sysdep_segments_offset));
1082 sysdep_segment_values =
1083 alloca (n_sysdep_segments * sizeof (const char *));
1084 for (i = 0; i < n_sysdep_segments; i++)
1086 const char *name =
1087 (char *) data
1088 + W (domain->must_swap, sysdep_segments[i].offset);
1089 nls_uint32 namelen =
1090 W (domain->must_swap, sysdep_segments[i].length);
1092 if (!(namelen > 0 && name[namelen - 1] == '\0'))
1094 freea (sysdep_segment_values);
1095 goto invalid;
1098 sysdep_segment_values[i] = get_sysdep_segment_value (name);
1101 orig_sysdep_tab = (const nls_uint32 *)
1102 ((char *) data
1103 + W (domain->must_swap, data->orig_sysdep_tab_offset));
1104 trans_sysdep_tab = (const nls_uint32 *)
1105 ((char *) data
1106 + W (domain->must_swap, data->trans_sysdep_tab_offset));
1108 /* Compute the amount of additional memory needed for the
1109 system dependent strings and the augmented hash table.
1110 At the same time, also drop string pairs which refer to
1111 an undefined system dependent segment. */
1112 n_inmem_sysdep_strings = 0;
1113 memneed = domain->hash_size * sizeof (nls_uint32);
1114 for (i = 0; i < n_sysdep_strings; i++)
1116 int valid = 1;
1117 size_t needs[2];
1119 for (j = 0; j < 2; j++)
1121 const struct sysdep_string *sysdep_string =
1122 (const struct sysdep_string *)
1123 ((char *) data
1124 + W (domain->must_swap,
1125 j == 0
1126 ? orig_sysdep_tab[i]
1127 : trans_sysdep_tab[i]));
1128 size_t need = 0;
1129 const struct segment_pair *p = sysdep_string->segments;
1131 if (W (domain->must_swap, p->sysdepref) != SEGMENTS_END)
1132 for (p = sysdep_string->segments;; p++)
1134 nls_uint32 sysdepref;
1136 need += W (domain->must_swap, p->segsize);
1138 sysdepref = W (domain->must_swap, p->sysdepref);
1139 if (sysdepref == SEGMENTS_END)
1140 break;
1142 if (sysdepref >= n_sysdep_segments)
1144 /* Invalid. */
1145 freea (sysdep_segment_values);
1146 goto invalid;
1149 if (sysdep_segment_values[sysdepref] == NULL)
1151 /* This particular string pair is invalid. */
1152 valid = 0;
1153 break;
1156 need += strlen (sysdep_segment_values[sysdepref]);
1159 needs[j] = need;
1160 if (!valid)
1161 break;
1164 if (valid)
1166 n_inmem_sysdep_strings++;
1167 memneed += needs[0] + needs[1];
1170 memneed += 2 * n_inmem_sysdep_strings
1171 * sizeof (struct sysdep_string_desc);
1173 if (n_inmem_sysdep_strings > 0)
1175 unsigned int k;
1177 /* Allocate additional memory. */
1178 mem = (char *) malloc (memneed);
1179 if (mem == NULL)
1180 goto invalid;
1182 domain->malloced = mem;
1183 inmem_orig_sysdep_tab = (struct sysdep_string_desc *) mem;
1184 mem += n_inmem_sysdep_strings
1185 * sizeof (struct sysdep_string_desc);
1186 inmem_trans_sysdep_tab = (struct sysdep_string_desc *) mem;
1187 mem += n_inmem_sysdep_strings
1188 * sizeof (struct sysdep_string_desc);
1189 inmem_hash_tab = (nls_uint32 *) mem;
1190 mem += domain->hash_size * sizeof (nls_uint32);
1192 /* Compute the system dependent strings. */
1193 k = 0;
1194 for (i = 0; i < n_sysdep_strings; i++)
1196 int valid = 1;
1198 for (j = 0; j < 2; j++)
1200 const struct sysdep_string *sysdep_string =
1201 (const struct sysdep_string *)
1202 ((char *) data
1203 + W (domain->must_swap,
1204 j == 0
1205 ? orig_sysdep_tab[i]
1206 : trans_sysdep_tab[i]));
1207 const struct segment_pair *p =
1208 sysdep_string->segments;
1210 if (W (domain->must_swap, p->sysdepref)
1211 != SEGMENTS_END)
1212 for (p = sysdep_string->segments;; p++)
1214 nls_uint32 sysdepref;
1216 sysdepref =
1217 W (domain->must_swap, p->sysdepref);
1218 if (sysdepref == SEGMENTS_END)
1219 break;
1221 if (sysdep_segment_values[sysdepref] == NULL)
1223 /* This particular string pair is
1224 invalid. */
1225 valid = 0;
1226 break;
1230 if (!valid)
1231 break;
1234 if (valid)
1236 for (j = 0; j < 2; j++)
1238 const struct sysdep_string *sysdep_string =
1239 (const struct sysdep_string *)
1240 ((char *) data
1241 + W (domain->must_swap,
1242 j == 0
1243 ? orig_sysdep_tab[i]
1244 : trans_sysdep_tab[i]));
1245 const char *static_segments =
1246 (char *) data
1247 + W (domain->must_swap, sysdep_string->offset);
1248 const struct segment_pair *p =
1249 sysdep_string->segments;
1251 /* Concatenate the segments, and fill
1252 inmem_orig_sysdep_tab[k] (for j == 0) and
1253 inmem_trans_sysdep_tab[k] (for j == 1). */
1255 struct sysdep_string_desc *inmem_tab_entry =
1256 (j == 0
1257 ? inmem_orig_sysdep_tab
1258 : inmem_trans_sysdep_tab)
1259 + k;
1261 if (W (domain->must_swap, p->sysdepref)
1262 == SEGMENTS_END)
1264 /* Only one static segment. */
1265 inmem_tab_entry->length =
1266 W (domain->must_swap, p->segsize);
1267 inmem_tab_entry->pointer = static_segments;
1269 else
1271 inmem_tab_entry->pointer = mem;
1273 for (p = sysdep_string->segments;; p++)
1275 nls_uint32 segsize =
1276 W (domain->must_swap, p->segsize);
1277 nls_uint32 sysdepref =
1278 W (domain->must_swap, p->sysdepref);
1279 size_t n;
1281 if (segsize > 0)
1283 memcpy (mem, static_segments, segsize);
1284 mem += segsize;
1285 static_segments += segsize;
1288 if (sysdepref == SEGMENTS_END)
1289 break;
1291 n = strlen (sysdep_segment_values[sysdepref]);
1292 memcpy (mem, sysdep_segment_values[sysdepref], n);
1293 mem += n;
1296 inmem_tab_entry->length =
1297 mem - inmem_tab_entry->pointer;
1301 k++;
1304 if (k != n_inmem_sysdep_strings)
1305 abort ();
1307 /* Compute the augmented hash table. */
1308 for (i = 0; i < domain->hash_size; i++)
1309 inmem_hash_tab[i] =
1310 W (domain->must_swap_hash_tab, domain->hash_tab[i]);
1311 for (i = 0; i < n_inmem_sysdep_strings; i++)
1313 const char *msgid = inmem_orig_sysdep_tab[i].pointer;
1314 nls_uint32 hash_val = __hash_string (msgid);
1315 nls_uint32 idx = hash_val % domain->hash_size;
1316 nls_uint32 incr =
1317 1 + (hash_val % (domain->hash_size - 2));
1319 for (;;)
1321 if (inmem_hash_tab[idx] == 0)
1323 /* Hash table entry is empty. Use it. */
1324 inmem_hash_tab[idx] = 1 + domain->nstrings + i;
1325 break;
1328 if (idx >= domain->hash_size - incr)
1329 idx -= domain->hash_size - incr;
1330 else
1331 idx += incr;
1335 domain->n_sysdep_strings = n_inmem_sysdep_strings;
1336 domain->orig_sysdep_tab = inmem_orig_sysdep_tab;
1337 domain->trans_sysdep_tab = inmem_trans_sysdep_tab;
1339 domain->hash_tab = inmem_hash_tab;
1340 domain->must_swap_hash_tab = 0;
1342 else
1344 domain->n_sysdep_strings = 0;
1345 domain->orig_sysdep_tab = NULL;
1346 domain->trans_sysdep_tab = NULL;
1349 freea (sysdep_segment_values);
1351 else
1353 domain->n_sysdep_strings = 0;
1354 domain->orig_sysdep_tab = NULL;
1355 domain->trans_sysdep_tab = NULL;
1358 break;
1360 break;
1361 default:
1362 /* This is an invalid revision. */
1363 invalid:
1364 /* This is an invalid .mo file. */
1365 if (domain->malloced)
1366 free (domain->malloced);
1367 #ifdef HAVE_MMAP
1368 if (use_mmap)
1369 munmap ((caddr_t) data, size);
1370 else
1371 #endif
1372 free (data);
1373 free (domain);
1374 domain_file->data = NULL;
1375 return;
1378 /* Now initialize the character set converter from the character set
1379 the file is encoded with (found in the header entry) to the domain's
1380 specified character set or the locale's character set. */
1381 nullentry = _nl_init_domain_conv (domain_file, domain, domainbinding);
1383 /* Also look for a plural specification. */
1384 EXTRACT_PLURAL_EXPRESSION (nullentry, &domain->plural, &domain->nplurals);
1388 #ifdef _LIBC
1389 void
1390 internal_function __libc_freeres_fn_section
1391 _nl_unload_domain (domain)
1392 struct loaded_domain *domain;
1394 if (domain->plural != &__gettext_germanic_plural)
1395 __gettext_free_exp (domain->plural);
1397 _nl_free_domain_conv (domain);
1399 if (domain->malloced)
1400 free (domain->malloced);
1402 # ifdef _POSIX_MAPPED_FILES
1403 if (domain->use_mmap)
1404 munmap ((caddr_t) domain->data, domain->mmap_size);
1405 else
1406 # endif /* _POSIX_MAPPED_FILES */
1407 free ((void *) domain->data);
1409 free (domain);
1411 #endif