2003-08-18 H.J. Lu <hongjiu.lu@intel.com>
[glibc.git] / intl / loadmsgcat.c
blob55dab143d31cdf927cf1419188e2afb793ed944e
1 /* Load needed message catalogs.
2 Copyright (C) 1995-2002, 2003 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 #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 PARAMS ((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 if (strchr (outcharset, '/') == NULL)
834 char *tmp;
836 len = strlen (outcharset);
837 tmp = (char *) alloca (len + 10 + 1);
838 memcpy (tmp, outcharset, len);
839 memcpy (tmp + len, "//TRANSLIT", 10 + 1);
840 outcharset = tmp;
842 domain->conv = iconv_open (outcharset, charset);
844 freea (outcharset);
846 else
847 # endif
848 domain->conv = iconv_open (outcharset, charset);
849 # endif
850 # endif
852 freea (charset);
854 #endif /* _LIBC || HAVE_ICONV */
857 return nullentry;
860 /* Frees the codeset dependent parts of an opened message catalog. */
861 void
862 internal_function
863 _nl_free_domain_conv (domain)
864 struct loaded_domain *domain;
866 if (domain->conv_tab != NULL && domain->conv_tab != (char **) -1)
867 free (domain->conv_tab);
869 #ifdef _LIBC
870 if (domain->conv != (__gconv_t) -1)
871 __gconv_close (domain->conv);
872 #else
873 # if HAVE_ICONV
874 if (domain->conv != (iconv_t) -1)
875 iconv_close (domain->conv);
876 # endif
877 #endif
880 /* Load the message catalogs specified by FILENAME. If it is no valid
881 message catalog do nothing. */
882 void
883 internal_function
884 _nl_load_domain (domain_file, domainbinding)
885 struct loaded_l10nfile *domain_file;
886 struct binding *domainbinding;
888 int fd;
889 size_t size;
890 #ifdef _LIBC
891 struct stat64 st;
892 #else
893 struct stat st;
894 #endif
895 struct mo_file_header *data = (struct mo_file_header *) -1;
896 int use_mmap = 0;
897 struct loaded_domain *domain;
898 int revision;
899 const char *nullentry;
901 domain_file->decided = 1;
902 domain_file->data = NULL;
904 /* Note that it would be useless to store domainbinding in domain_file
905 because domainbinding might be == NULL now but != NULL later (after
906 a call to bind_textdomain_codeset). */
908 /* If the record does not represent a valid locale the FILENAME
909 might be NULL. This can happen when according to the given
910 specification the locale file name is different for XPG and CEN
911 syntax. */
912 if (domain_file->filename == NULL)
913 return;
915 /* Try to open the addressed file. */
916 fd = open (domain_file->filename, O_RDONLY);
917 if (fd == -1)
918 return;
920 /* We must know about the size of the file. */
921 if (
922 #ifdef _LIBC
923 __builtin_expect (fstat64 (fd, &st) != 0, 0)
924 #else
925 __builtin_expect (fstat (fd, &st) != 0, 0)
926 #endif
927 || __builtin_expect ((size = (size_t) st.st_size) != st.st_size, 0)
928 || __builtin_expect (size < sizeof (struct mo_file_header), 0))
930 /* Something went wrong. */
931 close (fd);
932 return;
935 #ifdef HAVE_MMAP
936 /* Now we are ready to load the file. If mmap() is available we try
937 this first. If not available or it failed we try to load it. */
938 data = (struct mo_file_header *) mmap (NULL, size, PROT_READ,
939 MAP_PRIVATE, fd, 0);
941 if (__builtin_expect (data != (struct mo_file_header *) -1, 1))
943 /* mmap() call was successful. */
944 close (fd);
945 use_mmap = 1;
947 #endif
949 /* If the data is not yet available (i.e. mmap'ed) we try to load
950 it manually. */
951 if (data == (struct mo_file_header *) -1)
953 size_t to_read;
954 char *read_ptr;
956 data = (struct mo_file_header *) malloc (size);
957 if (data == NULL)
958 return;
960 to_read = size;
961 read_ptr = (char *) data;
964 long int nb = (long int) read (fd, read_ptr, to_read);
965 if (nb <= 0)
967 #ifdef EINTR
968 if (nb == -1 && errno == EINTR)
969 continue;
970 #endif
971 close (fd);
972 return;
974 read_ptr += nb;
975 to_read -= nb;
977 while (to_read > 0);
979 close (fd);
982 /* Using the magic number we can test whether it really is a message
983 catalog file. */
984 if (__builtin_expect (data->magic != _MAGIC && data->magic != _MAGIC_SWAPPED,
987 /* The magic number is wrong: not a message catalog file. */
988 #ifdef HAVE_MMAP
989 if (use_mmap)
990 munmap ((caddr_t) data, size);
991 else
992 #endif
993 free (data);
994 return;
997 domain = (struct loaded_domain *) malloc (sizeof (struct loaded_domain));
998 if (domain == NULL)
999 return;
1000 domain_file->data = domain;
1002 domain->data = (char *) data;
1003 domain->use_mmap = use_mmap;
1004 domain->mmap_size = size;
1005 domain->must_swap = data->magic != _MAGIC;
1006 domain->malloced = NULL;
1008 /* Fill in the information about the available tables. */
1009 revision = W (domain->must_swap, data->revision);
1010 /* We support only the major revision 0. */
1011 switch (revision >> 16)
1013 case 0:
1014 domain->nstrings = W (domain->must_swap, data->nstrings);
1015 domain->orig_tab = (const struct string_desc *)
1016 ((char *) data + W (domain->must_swap, data->orig_tab_offset));
1017 domain->trans_tab = (const struct string_desc *)
1018 ((char *) data + W (domain->must_swap, data->trans_tab_offset));
1019 domain->hash_size = W (domain->must_swap, data->hash_tab_size);
1020 domain->hash_tab =
1021 (domain->hash_size > 2
1022 ? (const nls_uint32 *)
1023 ((char *) data + W (domain->must_swap, data->hash_tab_offset))
1024 : NULL);
1025 domain->must_swap_hash_tab = domain->must_swap;
1027 /* Now dispatch on the minor revision. */
1028 switch (revision & 0xffff)
1030 case 0:
1031 domain->n_sysdep_strings = 0;
1032 domain->orig_sysdep_tab = NULL;
1033 domain->trans_sysdep_tab = NULL;
1034 break;
1035 case 1:
1036 default:
1038 nls_uint32 n_sysdep_strings;
1040 if (domain->hash_tab == NULL)
1041 /* This is invalid. These minor revisions need a hash table. */
1042 goto invalid;
1044 n_sysdep_strings =
1045 W (domain->must_swap, data->n_sysdep_strings);
1046 if (n_sysdep_strings > 0)
1048 nls_uint32 n_sysdep_segments;
1049 const struct sysdep_segment *sysdep_segments;
1050 const char **sysdep_segment_values;
1051 const nls_uint32 *orig_sysdep_tab;
1052 const nls_uint32 *trans_sysdep_tab;
1053 size_t memneed;
1054 char *mem;
1055 struct sysdep_string_desc *inmem_orig_sysdep_tab;
1056 struct sysdep_string_desc *inmem_trans_sysdep_tab;
1057 nls_uint32 *inmem_hash_tab;
1058 unsigned int i;
1060 /* Get the values of the system dependent segments. */
1061 n_sysdep_segments =
1062 W (domain->must_swap, data->n_sysdep_segments);
1063 sysdep_segments = (const struct sysdep_segment *)
1064 ((char *) data
1065 + W (domain->must_swap, data->sysdep_segments_offset));
1066 sysdep_segment_values =
1067 alloca (n_sysdep_segments * sizeof (const char *));
1068 for (i = 0; i < n_sysdep_segments; i++)
1070 const char *name =
1071 (char *) data
1072 + W (domain->must_swap, sysdep_segments[i].offset);
1073 nls_uint32 namelen =
1074 W (domain->must_swap, sysdep_segments[i].length);
1076 if (!(namelen > 0 && name[namelen - 1] == '\0'))
1078 freea (sysdep_segment_values);
1079 goto invalid;
1082 sysdep_segment_values[i] = get_sysdep_segment_value (name);
1085 orig_sysdep_tab = (const nls_uint32 *)
1086 ((char *) data
1087 + W (domain->must_swap, data->orig_sysdep_tab_offset));
1088 trans_sysdep_tab = (const nls_uint32 *)
1089 ((char *) data
1090 + W (domain->must_swap, data->trans_sysdep_tab_offset));
1092 /* Compute the amount of additional memory needed for the
1093 system dependent strings and the augmented hash table. */
1094 memneed = 2 * n_sysdep_strings
1095 * sizeof (struct sysdep_string_desc)
1096 + domain->hash_size * sizeof (nls_uint32);
1097 for (i = 0; i < 2 * n_sysdep_strings; i++)
1099 const struct sysdep_string *sysdep_string =
1100 (const struct sysdep_string *)
1101 ((char *) data
1102 + W (domain->must_swap,
1103 i < n_sysdep_strings
1104 ? orig_sysdep_tab[i]
1105 : trans_sysdep_tab[i - n_sysdep_strings]));
1106 size_t need = 0;
1107 const struct segment_pair *p = sysdep_string->segments;
1109 if (W (domain->must_swap, p->sysdepref) != SEGMENTS_END)
1110 for (p = sysdep_string->segments;; p++)
1112 nls_uint32 sysdepref;
1114 need += W (domain->must_swap, p->segsize);
1116 sysdepref = W (domain->must_swap, p->sysdepref);
1117 if (sysdepref == SEGMENTS_END)
1118 break;
1120 if (sysdepref >= n_sysdep_segments)
1122 /* Invalid. */
1123 freea (sysdep_segment_values);
1124 goto invalid;
1127 need += strlen (sysdep_segment_values[sysdepref]);
1130 memneed += need;
1133 /* Allocate additional memory. */
1134 mem = (char *) malloc (memneed);
1135 if (mem == NULL)
1136 goto invalid;
1138 domain->malloced = mem;
1139 inmem_orig_sysdep_tab = (struct sysdep_string_desc *) mem;
1140 mem += n_sysdep_strings * sizeof (struct sysdep_string_desc);
1141 inmem_trans_sysdep_tab = (struct sysdep_string_desc *) mem;
1142 mem += n_sysdep_strings * sizeof (struct sysdep_string_desc);
1143 inmem_hash_tab = (nls_uint32 *) mem;
1144 mem += domain->hash_size * sizeof (nls_uint32);
1146 /* Compute the system dependent strings. */
1147 for (i = 0; i < 2 * n_sysdep_strings; i++)
1149 const struct sysdep_string *sysdep_string =
1150 (const struct sysdep_string *)
1151 ((char *) data
1152 + W (domain->must_swap,
1153 i < n_sysdep_strings
1154 ? orig_sysdep_tab[i]
1155 : trans_sysdep_tab[i - n_sysdep_strings]));
1156 const char *static_segments =
1157 (char *) data
1158 + W (domain->must_swap, sysdep_string->offset);
1159 const struct segment_pair *p = sysdep_string->segments;
1161 /* Concatenate the segments, and fill
1162 inmem_orig_sysdep_tab[i] (for i < n_sysdep_strings) and
1163 inmem_trans_sysdep_tab[i-n_sysdep_strings] (for
1164 i >= n_sysdep_strings). */
1166 if (W (domain->must_swap, p->sysdepref) == SEGMENTS_END)
1168 /* Only one static segment. */
1169 inmem_orig_sysdep_tab[i].length =
1170 W (domain->must_swap, p->segsize);
1171 inmem_orig_sysdep_tab[i].pointer = static_segments;
1173 else
1175 inmem_orig_sysdep_tab[i].pointer = mem;
1177 for (p = sysdep_string->segments;; p++)
1179 nls_uint32 segsize =
1180 W (domain->must_swap, p->segsize);
1181 nls_uint32 sysdepref =
1182 W (domain->must_swap, p->sysdepref);
1183 size_t n;
1185 if (segsize > 0)
1187 memcpy (mem, static_segments, segsize);
1188 mem += segsize;
1189 static_segments += segsize;
1192 if (sysdepref == SEGMENTS_END)
1193 break;
1195 n = strlen (sysdep_segment_values[sysdepref]);
1196 memcpy (mem, sysdep_segment_values[sysdepref], n);
1197 mem += n;
1200 inmem_orig_sysdep_tab[i].length =
1201 mem - inmem_orig_sysdep_tab[i].pointer;
1205 /* Compute the augmented hash table. */
1206 for (i = 0; i < domain->hash_size; i++)
1207 inmem_hash_tab[i] =
1208 W (domain->must_swap_hash_tab, domain->hash_tab[i]);
1209 for (i = 0; i < n_sysdep_strings; i++)
1211 const char *msgid = inmem_orig_sysdep_tab[i].pointer;
1212 nls_uint32 hash_val = __hash_string (msgid);
1213 nls_uint32 idx = hash_val % domain->hash_size;
1214 nls_uint32 incr = 1 + (hash_val % (domain->hash_size - 2));
1216 for (;;)
1218 if (inmem_hash_tab[idx] == 0)
1220 /* Hash table entry is empty. Use it. */
1221 inmem_hash_tab[idx] = 1 + domain->nstrings + i;
1222 break;
1225 if (idx >= domain->hash_size - incr)
1226 idx -= domain->hash_size - incr;
1227 else
1228 idx += incr;
1232 freea (sysdep_segment_values);
1234 domain->n_sysdep_strings = n_sysdep_strings;
1235 domain->orig_sysdep_tab = inmem_orig_sysdep_tab;
1236 domain->trans_sysdep_tab = inmem_trans_sysdep_tab;
1238 domain->hash_tab = inmem_hash_tab;
1239 domain->must_swap_hash_tab = 0;
1241 else
1243 domain->n_sysdep_strings = 0;
1244 domain->orig_sysdep_tab = NULL;
1245 domain->trans_sysdep_tab = NULL;
1248 break;
1250 break;
1251 default:
1252 /* This is an invalid revision. */
1253 invalid:
1254 /* This is an invalid .mo file. */
1255 if (domain->malloced)
1256 free (domain->malloced);
1257 #ifdef HAVE_MMAP
1258 if (use_mmap)
1259 munmap ((caddr_t) data, size);
1260 else
1261 #endif
1262 free (data);
1263 free (domain);
1264 domain_file->data = NULL;
1265 return;
1268 /* Now initialize the character set converter from the character set
1269 the file is encoded with (found in the header entry) to the domain's
1270 specified character set or the locale's character set. */
1271 nullentry = _nl_init_domain_conv (domain_file, domain, domainbinding);
1273 /* Also look for a plural specification. */
1274 EXTRACT_PLURAL_EXPRESSION (nullentry, &domain->plural, &domain->nplurals);
1278 #ifdef _LIBC
1279 void
1280 internal_function
1281 _nl_unload_domain (domain)
1282 struct loaded_domain *domain;
1284 if (domain->plural != &__gettext_germanic_plural)
1285 __gettext_free_exp (domain->plural);
1287 _nl_free_domain_conv (domain);
1289 if (domain->malloced)
1290 free (domain->malloced);
1292 # ifdef _POSIX_MAPPED_FILES
1293 if (domain->use_mmap)
1294 munmap ((caddr_t) domain->data, domain->mmap_size);
1295 else
1296 # endif /* _POSIX_MAPPED_FILES */
1297 free ((void *) domain->data);
1299 free (domain);
1301 #endif