Update.
[glibc.git] / intl / loadmsgcat.c
blobb9e59f5682450e6b1330abb77d7b138448ecc493
1 /* Load needed message catalogs.
2 Copyright (C) 1995-1999, 2000, 2001, 2002 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 "gettext.h"
84 #include "gettextP.h"
85 #include "hash-string.h"
86 #include "plural-exp.h"
88 #ifdef _LIBC
89 # include "../locale/localeinfo.h"
90 #endif
92 /* Provide fallback values for macros that ought to be defined in <inttypes.h>.
93 Note that our fallback values need not be literal strings, because we don't
94 use them with preprocessor string concatenation. */
95 #if !defined PRId8 || PRI_MACROS_BROKEN
96 # undef PRId8
97 # define PRId8 "d"
98 #endif
99 #if !defined PRIi8 || PRI_MACROS_BROKEN
100 # undef PRIi8
101 # define PRIi8 "i"
102 #endif
103 #if !defined PRIo8 || PRI_MACROS_BROKEN
104 # undef PRIo8
105 # define PRIo8 "o"
106 #endif
107 #if !defined PRIu8 || PRI_MACROS_BROKEN
108 # undef PRIu8
109 # define PRIu8 "u"
110 #endif
111 #if !defined PRIx8 || PRI_MACROS_BROKEN
112 # undef PRIx8
113 # define PRIx8 "x"
114 #endif
115 #if !defined PRIX8 || PRI_MACROS_BROKEN
116 # undef PRIX8
117 # define PRIX8 "X"
118 #endif
119 #if !defined PRId16 || PRI_MACROS_BROKEN
120 # undef PRId16
121 # define PRId16 "d"
122 #endif
123 #if !defined PRIi16 || PRI_MACROS_BROKEN
124 # undef PRIi16
125 # define PRIi16 "i"
126 #endif
127 #if !defined PRIo16 || PRI_MACROS_BROKEN
128 # undef PRIo16
129 # define PRIo16 "o"
130 #endif
131 #if !defined PRIu16 || PRI_MACROS_BROKEN
132 # undef PRIu16
133 # define PRIu16 "u"
134 #endif
135 #if !defined PRIx16 || PRI_MACROS_BROKEN
136 # undef PRIx16
137 # define PRIx16 "x"
138 #endif
139 #if !defined PRIX16 || PRI_MACROS_BROKEN
140 # undef PRIX16
141 # define PRIX16 "X"
142 #endif
143 #if !defined PRId32 || PRI_MACROS_BROKEN
144 # undef PRId32
145 # define PRId32 "d"
146 #endif
147 #if !defined PRIi32 || PRI_MACROS_BROKEN
148 # undef PRIi32
149 # define PRIi32 "i"
150 #endif
151 #if !defined PRIo32 || PRI_MACROS_BROKEN
152 # undef PRIo32
153 # define PRIo32 "o"
154 #endif
155 #if !defined PRIu32 || PRI_MACROS_BROKEN
156 # undef PRIu32
157 # define PRIu32 "u"
158 #endif
159 #if !defined PRIx32 || PRI_MACROS_BROKEN
160 # undef PRIx32
161 # define PRIx32 "x"
162 #endif
163 #if !defined PRIX32 || PRI_MACROS_BROKEN
164 # undef PRIX32
165 # define PRIX32 "X"
166 #endif
167 #if !defined PRId64 || PRI_MACROS_BROKEN
168 # undef PRId64
169 # define PRId64 (sizeof (long) == 8 ? "ld" : "lld")
170 #endif
171 #if !defined PRIi64 || PRI_MACROS_BROKEN
172 # undef PRIi64
173 # define PRIi64 (sizeof (long) == 8 ? "li" : "lli")
174 #endif
175 #if !defined PRIo64 || PRI_MACROS_BROKEN
176 # undef PRIo64
177 # define PRIo64 (sizeof (long) == 8 ? "lo" : "llo")
178 #endif
179 #if !defined PRIu64 || PRI_MACROS_BROKEN
180 # undef PRIu64
181 # define PRIu64 (sizeof (long) == 8 ? "lu" : "llu")
182 #endif
183 #if !defined PRIx64 || PRI_MACROS_BROKEN
184 # undef PRIx64
185 # define PRIx64 (sizeof (long) == 8 ? "lx" : "llx")
186 #endif
187 #if !defined PRIX64 || PRI_MACROS_BROKEN
188 # undef PRIX64
189 # define PRIX64 (sizeof (long) == 8 ? "lX" : "llX")
190 #endif
191 #if !defined PRIdLEAST8 || PRI_MACROS_BROKEN
192 # undef PRIdLEAST8
193 # define PRIdLEAST8 "d"
194 #endif
195 #if !defined PRIiLEAST8 || PRI_MACROS_BROKEN
196 # undef PRIiLEAST8
197 # define PRIiLEAST8 "i"
198 #endif
199 #if !defined PRIoLEAST8 || PRI_MACROS_BROKEN
200 # undef PRIoLEAST8
201 # define PRIoLEAST8 "o"
202 #endif
203 #if !defined PRIuLEAST8 || PRI_MACROS_BROKEN
204 # undef PRIuLEAST8
205 # define PRIuLEAST8 "u"
206 #endif
207 #if !defined PRIxLEAST8 || PRI_MACROS_BROKEN
208 # undef PRIxLEAST8
209 # define PRIxLEAST8 "x"
210 #endif
211 #if !defined PRIXLEAST8 || PRI_MACROS_BROKEN
212 # undef PRIXLEAST8
213 # define PRIXLEAST8 "X"
214 #endif
215 #if !defined PRIdLEAST16 || PRI_MACROS_BROKEN
216 # undef PRIdLEAST16
217 # define PRIdLEAST16 "d"
218 #endif
219 #if !defined PRIiLEAST16 || PRI_MACROS_BROKEN
220 # undef PRIiLEAST16
221 # define PRIiLEAST16 "i"
222 #endif
223 #if !defined PRIoLEAST16 || PRI_MACROS_BROKEN
224 # undef PRIoLEAST16
225 # define PRIoLEAST16 "o"
226 #endif
227 #if !defined PRIuLEAST16 || PRI_MACROS_BROKEN
228 # undef PRIuLEAST16
229 # define PRIuLEAST16 "u"
230 #endif
231 #if !defined PRIxLEAST16 || PRI_MACROS_BROKEN
232 # undef PRIxLEAST16
233 # define PRIxLEAST16 "x"
234 #endif
235 #if !defined PRIXLEAST16 || PRI_MACROS_BROKEN
236 # undef PRIXLEAST16
237 # define PRIXLEAST16 "X"
238 #endif
239 #if !defined PRIdLEAST32 || PRI_MACROS_BROKEN
240 # undef PRIdLEAST32
241 # define PRIdLEAST32 "d"
242 #endif
243 #if !defined PRIiLEAST32 || PRI_MACROS_BROKEN
244 # undef PRIiLEAST32
245 # define PRIiLEAST32 "i"
246 #endif
247 #if !defined PRIoLEAST32 || PRI_MACROS_BROKEN
248 # undef PRIoLEAST32
249 # define PRIoLEAST32 "o"
250 #endif
251 #if !defined PRIuLEAST32 || PRI_MACROS_BROKEN
252 # undef PRIuLEAST32
253 # define PRIuLEAST32 "u"
254 #endif
255 #if !defined PRIxLEAST32 || PRI_MACROS_BROKEN
256 # undef PRIxLEAST32
257 # define PRIxLEAST32 "x"
258 #endif
259 #if !defined PRIXLEAST32 || PRI_MACROS_BROKEN
260 # undef PRIXLEAST32
261 # define PRIXLEAST32 "X"
262 #endif
263 #if !defined PRIdLEAST64 || PRI_MACROS_BROKEN
264 # undef PRIdLEAST64
265 # define PRIdLEAST64 PRId64
266 #endif
267 #if !defined PRIiLEAST64 || PRI_MACROS_BROKEN
268 # undef PRIiLEAST64
269 # define PRIiLEAST64 PRIi64
270 #endif
271 #if !defined PRIoLEAST64 || PRI_MACROS_BROKEN
272 # undef PRIoLEAST64
273 # define PRIoLEAST64 PRIo64
274 #endif
275 #if !defined PRIuLEAST64 || PRI_MACROS_BROKEN
276 # undef PRIuLEAST64
277 # define PRIuLEAST64 PRIu64
278 #endif
279 #if !defined PRIxLEAST64 || PRI_MACROS_BROKEN
280 # undef PRIxLEAST64
281 # define PRIxLEAST64 PRIx64
282 #endif
283 #if !defined PRIXLEAST64 || PRI_MACROS_BROKEN
284 # undef PRIXLEAST64
285 # define PRIXLEAST64 PRIX64
286 #endif
287 #if !defined PRIdFAST8 || PRI_MACROS_BROKEN
288 # undef PRIdFAST8
289 # define PRIdFAST8 "d"
290 #endif
291 #if !defined PRIiFAST8 || PRI_MACROS_BROKEN
292 # undef PRIiFAST8
293 # define PRIiFAST8 "i"
294 #endif
295 #if !defined PRIoFAST8 || PRI_MACROS_BROKEN
296 # undef PRIoFAST8
297 # define PRIoFAST8 "o"
298 #endif
299 #if !defined PRIuFAST8 || PRI_MACROS_BROKEN
300 # undef PRIuFAST8
301 # define PRIuFAST8 "u"
302 #endif
303 #if !defined PRIxFAST8 || PRI_MACROS_BROKEN
304 # undef PRIxFAST8
305 # define PRIxFAST8 "x"
306 #endif
307 #if !defined PRIXFAST8 || PRI_MACROS_BROKEN
308 # undef PRIXFAST8
309 # define PRIXFAST8 "X"
310 #endif
311 #if !defined PRIdFAST16 || PRI_MACROS_BROKEN
312 # undef PRIdFAST16
313 # define PRIdFAST16 "d"
314 #endif
315 #if !defined PRIiFAST16 || PRI_MACROS_BROKEN
316 # undef PRIiFAST16
317 # define PRIiFAST16 "i"
318 #endif
319 #if !defined PRIoFAST16 || PRI_MACROS_BROKEN
320 # undef PRIoFAST16
321 # define PRIoFAST16 "o"
322 #endif
323 #if !defined PRIuFAST16 || PRI_MACROS_BROKEN
324 # undef PRIuFAST16
325 # define PRIuFAST16 "u"
326 #endif
327 #if !defined PRIxFAST16 || PRI_MACROS_BROKEN
328 # undef PRIxFAST16
329 # define PRIxFAST16 "x"
330 #endif
331 #if !defined PRIXFAST16 || PRI_MACROS_BROKEN
332 # undef PRIXFAST16
333 # define PRIXFAST16 "X"
334 #endif
335 #if !defined PRIdFAST32 || PRI_MACROS_BROKEN
336 # undef PRIdFAST32
337 # define PRIdFAST32 "d"
338 #endif
339 #if !defined PRIiFAST32 || PRI_MACROS_BROKEN
340 # undef PRIiFAST32
341 # define PRIiFAST32 "i"
342 #endif
343 #if !defined PRIoFAST32 || PRI_MACROS_BROKEN
344 # undef PRIoFAST32
345 # define PRIoFAST32 "o"
346 #endif
347 #if !defined PRIuFAST32 || PRI_MACROS_BROKEN
348 # undef PRIuFAST32
349 # define PRIuFAST32 "u"
350 #endif
351 #if !defined PRIxFAST32 || PRI_MACROS_BROKEN
352 # undef PRIxFAST32
353 # define PRIxFAST32 "x"
354 #endif
355 #if !defined PRIXFAST32 || PRI_MACROS_BROKEN
356 # undef PRIXFAST32
357 # define PRIXFAST32 "X"
358 #endif
359 #if !defined PRIdFAST64 || PRI_MACROS_BROKEN
360 # undef PRIdFAST64
361 # define PRIdFAST64 PRId64
362 #endif
363 #if !defined PRIiFAST64 || PRI_MACROS_BROKEN
364 # undef PRIiFAST64
365 # define PRIiFAST64 PRIi64
366 #endif
367 #if !defined PRIoFAST64 || PRI_MACROS_BROKEN
368 # undef PRIoFAST64
369 # define PRIoFAST64 PRIo64
370 #endif
371 #if !defined PRIuFAST64 || PRI_MACROS_BROKEN
372 # undef PRIuFAST64
373 # define PRIuFAST64 PRIu64
374 #endif
375 #if !defined PRIxFAST64 || PRI_MACROS_BROKEN
376 # undef PRIxFAST64
377 # define PRIxFAST64 PRIx64
378 #endif
379 #if !defined PRIXFAST64 || PRI_MACROS_BROKEN
380 # undef PRIXFAST64
381 # define PRIXFAST64 PRIX64
382 #endif
383 #if !defined PRIdMAX || PRI_MACROS_BROKEN
384 # undef PRIdMAX
385 # define PRIdMAX (sizeof (uintmax_t) == sizeof (long) ? "ld" : "lld")
386 #endif
387 #if !defined PRIiMAX || PRI_MACROS_BROKEN
388 # undef PRIiMAX
389 # define PRIiMAX (sizeof (uintmax_t) == sizeof (long) ? "li" : "lli")
390 #endif
391 #if !defined PRIoMAX || PRI_MACROS_BROKEN
392 # undef PRIoMAX
393 # define PRIoMAX (sizeof (uintmax_t) == sizeof (long) ? "lo" : "llo")
394 #endif
395 #if !defined PRIuMAX || PRI_MACROS_BROKEN
396 # undef PRIuMAX
397 # define PRIuMAX (sizeof (uintmax_t) == sizeof (long) ? "lu" : "llu")
398 #endif
399 #if !defined PRIxMAX || PRI_MACROS_BROKEN
400 # undef PRIxMAX
401 # define PRIxMAX (sizeof (uintmax_t) == sizeof (long) ? "lx" : "llx")
402 #endif
403 #if !defined PRIXMAX || PRI_MACROS_BROKEN
404 # undef PRIXMAX
405 # define PRIXMAX (sizeof (uintmax_t) == sizeof (long) ? "lX" : "llX")
406 #endif
407 #if !defined PRIdPTR || PRI_MACROS_BROKEN
408 # undef PRIdPTR
409 # define PRIdPTR \
410 (sizeof (void *) == sizeof (long) ? "ld" : \
411 sizeof (void *) == sizeof (int) ? "d" : \
412 "lld")
413 #endif
414 #if !defined PRIiPTR || PRI_MACROS_BROKEN
415 # undef PRIiPTR
416 # define PRIiPTR \
417 (sizeof (void *) == sizeof (long) ? "li" : \
418 sizeof (void *) == sizeof (int) ? "i" : \
419 "lli")
420 #endif
421 #if !defined PRIoPTR || PRI_MACROS_BROKEN
422 # undef PRIoPTR
423 # define PRIoPTR \
424 (sizeof (void *) == sizeof (long) ? "lo" : \
425 sizeof (void *) == sizeof (int) ? "o" : \
426 "llo")
427 #endif
428 #if !defined PRIuPTR || PRI_MACROS_BROKEN
429 # undef PRIuPTR
430 # define PRIuPTR \
431 (sizeof (void *) == sizeof (long) ? "lu" : \
432 sizeof (void *) == sizeof (int) ? "u" : \
433 "llu")
434 #endif
435 #if !defined PRIxPTR || PRI_MACROS_BROKEN
436 # undef PRIxPTR
437 # define PRIxPTR \
438 (sizeof (void *) == sizeof (long) ? "lx" : \
439 sizeof (void *) == sizeof (int) ? "x" : \
440 "llx")
441 #endif
442 #if !defined PRIXPTR || PRI_MACROS_BROKEN
443 # undef PRIXPTR
444 # define PRIXPTR \
445 (sizeof (void *) == sizeof (long) ? "lX" : \
446 sizeof (void *) == sizeof (int) ? "X" : \
447 "llX")
448 #endif
450 /* @@ end of prolog @@ */
452 #ifdef _LIBC
453 /* Rename the non ISO C functions. This is required by the standard
454 because some ISO C functions will require linking with this object
455 file and the name space must not be polluted. */
456 # define open __open
457 # define close __close
458 # define read __read
459 # define mmap __mmap
460 # define munmap __munmap
461 #endif
463 /* For those losing systems which don't have `alloca' we have to add
464 some additional code emulating it. */
465 #ifdef HAVE_ALLOCA
466 # define freea(p) /* nothing */
467 #else
468 # define alloca(n) malloc (n)
469 # define freea(p) free (p)
470 #endif
473 /* Prototypes for local functions. Needed to ensure compiler checking of
474 function argument counts despite of K&R C function definition syntax. */
475 static const char *get_sysdep_segment_value PARAMS ((const char *name));
478 /* We need a sign, whether a new catalog was loaded, which can be associated
479 with all translations. This is important if the translations are
480 cached by one of GCC's features. */
481 int _nl_msg_cat_cntr;
484 /* Expand a system dependent string segment. Return NULL if unsupported. */
485 static const char *
486 get_sysdep_segment_value (name)
487 const char *name;
489 /* Test for an ISO C 99 section 7.8.1 format string directive.
490 Syntax:
491 P R I { d | i | o | u | x | X }
492 { { | LEAST | FAST } { 8 | 16 | 32 | 64 } | MAX | PTR } */
493 /* We don't use a table of 14 times 6 'const char *' strings here, because
494 data relocations cost startup time. */
495 if (name[0] == 'P' && name[1] == 'R' && name[2] == 'I')
497 if (name[3] == 'd' || name[3] == 'i' || name[3] == 'o' || name[3] == 'u'
498 || name[3] == 'x' || name[3] == 'X')
500 if (name[4] == '8' && name[5] == '\0')
502 if (name[3] == 'd')
503 return PRId8;
504 if (name[3] == 'i')
505 return PRIi8;
506 if (name[3] == 'o')
507 return PRIo8;
508 if (name[3] == 'u')
509 return PRIu8;
510 if (name[3] == 'x')
511 return PRIx8;
512 if (name[3] == 'X')
513 return PRIX8;
514 abort ();
516 if (name[4] == '1' && name[5] == '6' && name[6] == '\0')
518 if (name[3] == 'd')
519 return PRId16;
520 if (name[3] == 'i')
521 return PRIi16;
522 if (name[3] == 'o')
523 return PRIo16;
524 if (name[3] == 'u')
525 return PRIu16;
526 if (name[3] == 'x')
527 return PRIx16;
528 if (name[3] == 'X')
529 return PRIX16;
530 abort ();
532 if (name[4] == '3' && name[5] == '2' && name[6] == '\0')
534 if (name[3] == 'd')
535 return PRId32;
536 if (name[3] == 'i')
537 return PRIi32;
538 if (name[3] == 'o')
539 return PRIo32;
540 if (name[3] == 'u')
541 return PRIu32;
542 if (name[3] == 'x')
543 return PRIx32;
544 if (name[3] == 'X')
545 return PRIX32;
546 abort ();
548 if (name[4] == '6' && name[5] == '4' && name[6] == '\0')
550 if (name[3] == 'd')
551 return PRId64;
552 if (name[3] == 'i')
553 return PRIi64;
554 if (name[3] == 'o')
555 return PRIo64;
556 if (name[3] == 'u')
557 return PRIu64;
558 if (name[3] == 'x')
559 return PRIx64;
560 if (name[3] == 'X')
561 return PRIX64;
562 abort ();
564 if (name[4] == 'L' && name[5] == 'E' && name[6] == 'A'
565 && name[7] == 'S' && name[8] == 'T')
567 if (name[9] == '8' && name[10] == '\0')
569 if (name[3] == 'd')
570 return PRIdLEAST8;
571 if (name[3] == 'i')
572 return PRIiLEAST8;
573 if (name[3] == 'o')
574 return PRIoLEAST8;
575 if (name[3] == 'u')
576 return PRIuLEAST8;
577 if (name[3] == 'x')
578 return PRIxLEAST8;
579 if (name[3] == 'X')
580 return PRIXLEAST8;
581 abort ();
583 if (name[9] == '1' && name[10] == '6' && name[11] == '\0')
585 if (name[3] == 'd')
586 return PRIdLEAST16;
587 if (name[3] == 'i')
588 return PRIiLEAST16;
589 if (name[3] == 'o')
590 return PRIoLEAST16;
591 if (name[3] == 'u')
592 return PRIuLEAST16;
593 if (name[3] == 'x')
594 return PRIxLEAST16;
595 if (name[3] == 'X')
596 return PRIXLEAST16;
597 abort ();
599 if (name[9] == '3' && name[10] == '2' && name[11] == '\0')
601 if (name[3] == 'd')
602 return PRIdLEAST32;
603 if (name[3] == 'i')
604 return PRIiLEAST32;
605 if (name[3] == 'o')
606 return PRIoLEAST32;
607 if (name[3] == 'u')
608 return PRIuLEAST32;
609 if (name[3] == 'x')
610 return PRIxLEAST32;
611 if (name[3] == 'X')
612 return PRIXLEAST32;
613 abort ();
615 if (name[9] == '6' && name[10] == '4' && name[11] == '\0')
617 if (name[3] == 'd')
618 return PRIdLEAST64;
619 if (name[3] == 'i')
620 return PRIiLEAST64;
621 if (name[3] == 'o')
622 return PRIoLEAST64;
623 if (name[3] == 'u')
624 return PRIuLEAST64;
625 if (name[3] == 'x')
626 return PRIxLEAST64;
627 if (name[3] == 'X')
628 return PRIXLEAST64;
629 abort ();
632 if (name[4] == 'F' && name[5] == 'A' && name[6] == 'S'
633 && name[7] == 'T')
635 if (name[8] == '8' && name[9] == '\0')
637 if (name[3] == 'd')
638 return PRIdFAST8;
639 if (name[3] == 'i')
640 return PRIiFAST8;
641 if (name[3] == 'o')
642 return PRIoFAST8;
643 if (name[3] == 'u')
644 return PRIuFAST8;
645 if (name[3] == 'x')
646 return PRIxFAST8;
647 if (name[3] == 'X')
648 return PRIXFAST8;
649 abort ();
651 if (name[8] == '1' && name[9] == '6' && name[10] == '\0')
653 if (name[3] == 'd')
654 return PRIdFAST16;
655 if (name[3] == 'i')
656 return PRIiFAST16;
657 if (name[3] == 'o')
658 return PRIoFAST16;
659 if (name[3] == 'u')
660 return PRIuFAST16;
661 if (name[3] == 'x')
662 return PRIxFAST16;
663 if (name[3] == 'X')
664 return PRIXFAST16;
665 abort ();
667 if (name[8] == '3' && name[9] == '2' && name[10] == '\0')
669 if (name[3] == 'd')
670 return PRIdFAST32;
671 if (name[3] == 'i')
672 return PRIiFAST32;
673 if (name[3] == 'o')
674 return PRIoFAST32;
675 if (name[3] == 'u')
676 return PRIuFAST32;
677 if (name[3] == 'x')
678 return PRIxFAST32;
679 if (name[3] == 'X')
680 return PRIXFAST32;
681 abort ();
683 if (name[8] == '6' && name[9] == '4' && name[10] == '\0')
685 if (name[3] == 'd')
686 return PRIdFAST64;
687 if (name[3] == 'i')
688 return PRIiFAST64;
689 if (name[3] == 'o')
690 return PRIoFAST64;
691 if (name[3] == 'u')
692 return PRIuFAST64;
693 if (name[3] == 'x')
694 return PRIxFAST64;
695 if (name[3] == 'X')
696 return PRIXFAST64;
697 abort ();
700 if (name[4] == 'M' && name[5] == 'A' && name[6] == 'X'
701 && name[7] == '\0')
703 if (name[3] == 'd')
704 return PRIdMAX;
705 if (name[3] == 'i')
706 return PRIiMAX;
707 if (name[3] == 'o')
708 return PRIoMAX;
709 if (name[3] == 'u')
710 return PRIuMAX;
711 if (name[3] == 'x')
712 return PRIxMAX;
713 if (name[3] == 'X')
714 return PRIXMAX;
715 abort ();
717 if (name[4] == 'P' && name[5] == 'T' && name[6] == 'R'
718 && name[7] == '\0')
720 if (name[3] == 'd')
721 return PRIdPTR;
722 if (name[3] == 'i')
723 return PRIiPTR;
724 if (name[3] == 'o')
725 return PRIoPTR;
726 if (name[3] == 'u')
727 return PRIuPTR;
728 if (name[3] == 'x')
729 return PRIxPTR;
730 if (name[3] == 'X')
731 return PRIXPTR;
732 abort ();
736 /* Other system dependent strings are not valid. */
737 return NULL;
740 /* Initialize the codeset dependent parts of an opened message catalog.
741 Return the header entry. */
742 const char *
743 internal_function
744 _nl_init_domain_conv (domain_file, domain, domainbinding)
745 struct loaded_l10nfile *domain_file;
746 struct loaded_domain *domain;
747 struct binding *domainbinding;
749 /* Find out about the character set the file is encoded with.
750 This can be found (in textual form) in the entry "". If this
751 entry does not exist or if this does not contain the `charset='
752 information, we will assume the charset matches the one the
753 current locale and we don't have to perform any conversion. */
754 char *nullentry;
755 size_t nullentrylen;
757 /* Preinitialize fields, to avoid recursion during _nl_find_msg. */
758 domain->codeset_cntr =
759 (domainbinding != NULL ? domainbinding->codeset_cntr : 0);
760 #ifdef _LIBC
761 domain->conv = (__gconv_t) -1;
762 #else
763 # if HAVE_ICONV
764 domain->conv = (iconv_t) -1;
765 # endif
766 #endif
767 domain->conv_tab = NULL;
769 /* Get the header entry. */
770 nullentry = _nl_find_msg (domain_file, domainbinding, "", &nullentrylen);
772 if (nullentry != NULL)
774 #if defined _LIBC || HAVE_ICONV
775 const char *charsetstr;
777 charsetstr = strstr (nullentry, "charset=");
778 if (charsetstr != NULL)
780 size_t len;
781 char *charset;
782 const char *outcharset;
784 charsetstr += strlen ("charset=");
785 len = strcspn (charsetstr, " \t\n");
787 charset = (char *) alloca (len + 1);
788 # if defined _LIBC || HAVE_MEMPCPY
789 *((char *) mempcpy (charset, charsetstr, len)) = '\0';
790 # else
791 memcpy (charset, charsetstr, len);
792 charset[len] = '\0';
793 # endif
795 /* The output charset should normally be determined by the
796 locale. But sometimes the locale is not used or not correctly
797 set up, so we provide a possibility for the user to override
798 this. Moreover, the value specified through
799 bind_textdomain_codeset overrides both. */
800 if (domainbinding != NULL && domainbinding->codeset != NULL)
801 outcharset = domainbinding->codeset;
802 else
804 outcharset = getenv ("OUTPUT_CHARSET");
805 if (outcharset == NULL || outcharset[0] == '\0')
807 # ifdef _LIBC
808 outcharset = _NL_CURRENT (LC_CTYPE, CODESET);
809 # else
810 # if HAVE_ICONV
811 extern const char *locale_charset (void);
812 outcharset = locale_charset ();
813 # endif
814 # endif
818 # ifdef _LIBC
819 /* We always want to use transliteration. */
820 outcharset = norm_add_slashes (outcharset, "TRANSLIT");
821 charset = norm_add_slashes (charset, NULL);
822 if (__gconv_open (outcharset, charset, &domain->conv,
823 GCONV_AVOID_NOCONV)
824 != __GCONV_OK)
825 domain->conv = (__gconv_t) -1;
826 # else
827 # if HAVE_ICONV
828 /* When using GNU libc >= 2.2 or GNU libiconv >= 1.5,
829 we want to use transliteration. */
830 # if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2 \
831 || _LIBICONV_VERSION >= 0x0105
832 len = strlen (outcharset);
833 if (len < 10 || strcmp (outcharset + len - 9, "/TRANSLIT") != 0)
835 char *tmp = (char *) alloca (len + 10 + 1);
836 memcpy (tmp, outcharset, len);
837 memcpy (tmp + len, "//TRANSLIT", 10 + 1);
838 outcharset = tmp;
840 # endif
841 domain->conv = iconv_open (outcharset, charset);
842 # if (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) || __GLIBC__ > 2 \
843 || _LIBICONV_VERSION >= 0x0105
844 freea (outcharset);
845 # endif
846 # endif
847 # endif
849 freea (charset);
851 #endif /* _LIBC || HAVE_ICONV */
854 return nullentry;
857 /* Frees the codeset dependent parts of an opened message catalog. */
858 void
859 internal_function
860 _nl_free_domain_conv (domain)
861 struct loaded_domain *domain;
863 if (domain->conv_tab != NULL && domain->conv_tab != (char **) -1)
864 free (domain->conv_tab);
866 #ifdef _LIBC
867 if (domain->conv != (__gconv_t) -1)
868 __gconv_close (domain->conv);
869 #else
870 # if HAVE_ICONV
871 if (domain->conv != (iconv_t) -1)
872 iconv_close (domain->conv);
873 # endif
874 #endif
877 /* Load the message catalogs specified by FILENAME. If it is no valid
878 message catalog do nothing. */
879 void
880 internal_function
881 _nl_load_domain (domain_file, domainbinding)
882 struct loaded_l10nfile *domain_file;
883 struct binding *domainbinding;
885 int fd;
886 size_t size;
887 #ifdef _LIBC
888 struct stat64 st;
889 #else
890 struct stat st;
891 #endif
892 struct mo_file_header *data = (struct mo_file_header *) -1;
893 int use_mmap = 0;
894 struct loaded_domain *domain;
895 int revision;
896 const char *nullentry;
898 domain_file->decided = 1;
899 domain_file->data = NULL;
901 /* Note that it would be useless to store domainbinding in domain_file
902 because domainbinding might be == NULL now but != NULL later (after
903 a call to bind_textdomain_codeset). */
905 /* If the record does not represent a valid locale the FILENAME
906 might be NULL. This can happen when according to the given
907 specification the locale file name is different for XPG and CEN
908 syntax. */
909 if (domain_file->filename == NULL)
910 return;
912 /* Try to open the addressed file. */
913 fd = open (domain_file->filename, O_RDONLY);
914 if (fd == -1)
915 return;
917 /* We must know about the size of the file. */
918 if (
919 #ifdef _LIBC
920 __builtin_expect (fstat64 (fd, &st) != 0, 0)
921 #else
922 __builtin_expect (fstat (fd, &st) != 0, 0)
923 #endif
924 || __builtin_expect ((size = (size_t) st.st_size) != st.st_size, 0)
925 || __builtin_expect (size < sizeof (struct mo_file_header), 0))
927 /* Something went wrong. */
928 close (fd);
929 return;
932 #ifdef HAVE_MMAP
933 /* Now we are ready to load the file. If mmap() is available we try
934 this first. If not available or it failed we try to load it. */
935 data = (struct mo_file_header *) mmap (NULL, size, PROT_READ,
936 MAP_PRIVATE, fd, 0);
938 if (__builtin_expect (data != (struct mo_file_header *) -1, 1))
940 /* mmap() call was successful. */
941 close (fd);
942 use_mmap = 1;
944 #endif
946 /* If the data is not yet available (i.e. mmap'ed) we try to load
947 it manually. */
948 if (data == (struct mo_file_header *) -1)
950 size_t to_read;
951 char *read_ptr;
953 data = (struct mo_file_header *) malloc (size);
954 if (data == NULL)
955 return;
957 to_read = size;
958 read_ptr = (char *) data;
961 long int nb = (long int) read (fd, read_ptr, to_read);
962 if (nb <= 0)
964 #ifdef EINTR
965 if (nb == -1 && errno == EINTR)
966 continue;
967 #endif
968 close (fd);
969 return;
971 read_ptr += nb;
972 to_read -= nb;
974 while (to_read > 0);
976 close (fd);
979 /* Using the magic number we can test whether it really is a message
980 catalog file. */
981 if (__builtin_expect (data->magic != _MAGIC && data->magic != _MAGIC_SWAPPED,
984 /* The magic number is wrong: not a message catalog file. */
985 #ifdef HAVE_MMAP
986 if (use_mmap)
987 munmap ((caddr_t) data, size);
988 else
989 #endif
990 free (data);
991 return;
994 domain = (struct loaded_domain *) malloc (sizeof (struct loaded_domain));
995 if (domain == NULL)
996 return;
997 domain_file->data = domain;
999 domain->data = (char *) data;
1000 domain->use_mmap = use_mmap;
1001 domain->mmap_size = size;
1002 domain->must_swap = data->magic != _MAGIC;
1003 domain->malloced = NULL;
1005 /* Fill in the information about the available tables. */
1006 revision = W (domain->must_swap, data->revision);
1007 /* We support only the major revision 0. */
1008 switch (revision >> 16)
1010 case 0:
1011 domain->nstrings = W (domain->must_swap, data->nstrings);
1012 domain->orig_tab = (const struct string_desc *)
1013 ((char *) data + W (domain->must_swap, data->orig_tab_offset));
1014 domain->trans_tab = (const struct string_desc *)
1015 ((char *) data + W (domain->must_swap, data->trans_tab_offset));
1016 domain->hash_size = W (domain->must_swap, data->hash_tab_size);
1017 domain->hash_tab =
1018 (domain->hash_size > 2
1019 ? (const nls_uint32 *)
1020 ((char *) data + W (domain->must_swap, data->hash_tab_offset))
1021 : NULL);
1022 domain->must_swap_hash_tab = domain->must_swap;
1024 /* Now dispatch on the minor revision. */
1025 switch (revision & 0xffff)
1027 case 0:
1028 domain->n_sysdep_strings = 0;
1029 domain->orig_sysdep_tab = NULL;
1030 domain->trans_sysdep_tab = NULL;
1031 break;
1032 case 1:
1033 default:
1035 nls_uint32 n_sysdep_strings;
1037 if (domain->hash_tab == NULL)
1038 /* This is invalid. These minor revisions need a hash table. */
1039 goto invalid;
1041 n_sysdep_strings =
1042 W (domain->must_swap, data->n_sysdep_strings);
1043 if (n_sysdep_strings > 0)
1045 nls_uint32 n_sysdep_segments;
1046 const struct sysdep_segment *sysdep_segments;
1047 const char **sysdep_segment_values;
1048 const nls_uint32 *orig_sysdep_tab;
1049 const nls_uint32 *trans_sysdep_tab;
1050 size_t memneed;
1051 char *mem;
1052 struct sysdep_string_desc *inmem_orig_sysdep_tab;
1053 struct sysdep_string_desc *inmem_trans_sysdep_tab;
1054 nls_uint32 *inmem_hash_tab;
1055 unsigned int i;
1057 /* Get the values of the system dependent segments. */
1058 n_sysdep_segments =
1059 W (domain->must_swap, data->n_sysdep_segments);
1060 sysdep_segments = (const struct sysdep_segment *)
1061 ((char *) data
1062 + W (domain->must_swap, data->sysdep_segments_offset));
1063 sysdep_segment_values =
1064 alloca (n_sysdep_segments * sizeof (const char *));
1065 for (i = 0; i < n_sysdep_segments; i++)
1067 const char *name =
1068 (char *) data
1069 + W (domain->must_swap, sysdep_segments[i].offset);
1070 nls_uint32 namelen =
1071 W (domain->must_swap, sysdep_segments[i].length);
1073 if (!(namelen > 0 && name[namelen - 1] == '\0'))
1075 freea (sysdep_segment_values);
1076 goto invalid;
1079 sysdep_segment_values[i] = get_sysdep_segment_value (name);
1082 orig_sysdep_tab = (const nls_uint32 *)
1083 ((char *) data
1084 + W (domain->must_swap, data->orig_sysdep_tab_offset));
1085 trans_sysdep_tab = (const nls_uint32 *)
1086 ((char *) data
1087 + W (domain->must_swap, data->trans_sysdep_tab_offset));
1089 /* Compute the amount of additional memory needed for the
1090 system dependent strings and the augmented hash table. */
1091 memneed = 2 * n_sysdep_strings
1092 * sizeof (struct sysdep_string_desc)
1093 + domain->hash_size * sizeof (nls_uint32);
1094 for (i = 0; i < 2 * n_sysdep_strings; i++)
1096 const struct sysdep_string *sysdep_string =
1097 (const struct sysdep_string *)
1098 ((char *) data
1099 + W (domain->must_swap,
1100 i < n_sysdep_strings
1101 ? orig_sysdep_tab[i]
1102 : trans_sysdep_tab[i - n_sysdep_strings]));
1103 size_t need = 0;
1104 const struct segment_pair *p = sysdep_string->segments;
1106 if (W (domain->must_swap, p->sysdepref) != SEGMENTS_END)
1107 for (p = sysdep_string->segments;; p++)
1109 nls_uint32 sysdepref;
1111 need += W (domain->must_swap, p->segsize);
1113 sysdepref = W (domain->must_swap, p->sysdepref);
1114 if (sysdepref == SEGMENTS_END)
1115 break;
1117 if (sysdepref >= n_sysdep_segments)
1119 /* Invalid. */
1120 freea (sysdep_segment_values);
1121 goto invalid;
1124 need += strlen (sysdep_segment_values[sysdepref]);
1127 memneed += need;
1130 /* Allocate additional memory. */
1131 mem = (char *) malloc (memneed);
1132 if (mem == NULL)
1133 goto invalid;
1135 domain->malloced = mem;
1136 inmem_orig_sysdep_tab = (struct sysdep_string_desc *) mem;
1137 mem += n_sysdep_strings * sizeof (struct sysdep_string_desc);
1138 inmem_trans_sysdep_tab = (struct sysdep_string_desc *) mem;
1139 mem += n_sysdep_strings * sizeof (struct sysdep_string_desc);
1140 inmem_hash_tab = (nls_uint32 *) mem;
1141 mem += domain->hash_size * sizeof (nls_uint32);
1143 /* Compute the system dependent strings. */
1144 for (i = 0; i < 2 * n_sysdep_strings; i++)
1146 const struct sysdep_string *sysdep_string =
1147 (const struct sysdep_string *)
1148 ((char *) data
1149 + W (domain->must_swap,
1150 i < n_sysdep_strings
1151 ? orig_sysdep_tab[i]
1152 : trans_sysdep_tab[i - n_sysdep_strings]));
1153 const char *static_segments =
1154 (char *) data
1155 + W (domain->must_swap, sysdep_string->offset);
1156 const struct segment_pair *p = sysdep_string->segments;
1158 /* Concatenate the segments, and fill
1159 inmem_orig_sysdep_tab[i] (for i < n_sysdep_strings) and
1160 inmem_trans_sysdep_tab[i-n_sysdep_strings] (for
1161 i >= n_sysdep_strings). */
1163 if (W (domain->must_swap, p->sysdepref) == SEGMENTS_END)
1165 /* Only one static segment. */
1166 inmem_orig_sysdep_tab[i].length =
1167 W (domain->must_swap, p->segsize);
1168 inmem_orig_sysdep_tab[i].pointer = static_segments;
1170 else
1172 inmem_orig_sysdep_tab[i].pointer = mem;
1174 for (p = sysdep_string->segments;; p++)
1176 nls_uint32 segsize =
1177 W (domain->must_swap, p->segsize);
1178 nls_uint32 sysdepref =
1179 W (domain->must_swap, p->sysdepref);
1180 size_t n;
1182 if (segsize > 0)
1184 memcpy (mem, static_segments, segsize);
1185 mem += segsize;
1186 static_segments += segsize;
1189 if (sysdepref == SEGMENTS_END)
1190 break;
1192 n = strlen (sysdep_segment_values[sysdepref]);
1193 memcpy (mem, sysdep_segment_values[sysdepref], n);
1194 mem += n;
1197 inmem_orig_sysdep_tab[i].length =
1198 mem - inmem_orig_sysdep_tab[i].pointer;
1202 /* Compute the augmented hash table. */
1203 for (i = 0; i < domain->hash_size; i++)
1204 inmem_hash_tab[i] =
1205 W (domain->must_swap_hash_tab, domain->hash_tab[i]);
1206 for (i = 0; i < n_sysdep_strings; i++)
1208 const char *msgid = inmem_orig_sysdep_tab[i].pointer;
1209 nls_uint32 hash_val = hash_string (msgid);
1210 nls_uint32 idx = hash_val % domain->hash_size;
1211 nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
1213 for (;;)
1215 if (inmem_hash_tab[idx] == 0)
1217 /* Hash table entry is empty. Use it. */
1218 inmem_hash_tab[idx] = 1 + domain->nstrings + i;
1219 break;
1222 if (idx >= domain->hash_size - incr)
1223 idx -= domain->hash_size - incr;
1224 else
1225 idx += incr;
1229 freea (sysdep_segment_values);
1231 domain->n_sysdep_strings = n_sysdep_strings;
1232 domain->orig_sysdep_tab = inmem_orig_sysdep_tab;
1233 domain->trans_sysdep_tab = inmem_trans_sysdep_tab;
1235 domain->hash_tab = inmem_hash_tab;
1236 domain->must_swap_hash_tab = 0;
1238 else
1240 domain->n_sysdep_strings = 0;
1241 domain->orig_sysdep_tab = NULL;
1242 domain->trans_sysdep_tab = NULL;
1245 break;
1247 break;
1248 default:
1249 /* This is an invalid revision. */
1250 invalid:
1251 /* This is an invalid .mo file. */
1252 if (domain->malloced)
1253 free (domain->malloced);
1254 #ifdef HAVE_MMAP
1255 if (use_mmap)
1256 munmap ((caddr_t) data, size);
1257 else
1258 #endif
1259 free (data);
1260 free (domain);
1261 domain_file->data = NULL;
1262 return;
1265 /* Now initialize the character set converter from the character set
1266 the file is encoded with (found in the header entry) to the domain's
1267 specified character set or the locale's character set. */
1268 nullentry = _nl_init_domain_conv (domain_file, domain, domainbinding);
1270 /* Also look for a plural specification. */
1271 EXTRACT_PLURAL_EXPRESSION (nullentry, &domain->plural, &domain->nplurals);
1275 #ifdef _LIBC
1276 void
1277 internal_function
1278 _nl_unload_domain (domain)
1279 struct loaded_domain *domain;
1281 if (domain->plural != &__gettext_germanic_plural)
1282 __gettext_free_exp (domain->plural);
1284 _nl_free_domain_conv (domain);
1286 if (domain->malloced)
1287 free (domain->malloced);
1289 # ifdef _POSIX_MAPPED_FILES
1290 if (domain->use_mmap)
1291 munmap ((caddr_t) domain->data, domain->mmap_size);
1292 else
1293 # endif /* _POSIX_MAPPED_FILES */
1294 free ((void *) domain->data);
1296 free (domain);
1298 #endif