Update.
[glibc.git] / locale / programs / localedef.c
blob64b1d446372b627bb52545bd11c63ba70e369649
1 /* Copyright (C) 1995-1999, 2000, 2001, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1995.
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 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <argp.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <libintl.h>
28 #include <locale.h>
29 #include <mcheck.h>
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <error.h>
36 #include <sys/mman.h>
37 #include <sys/stat.h>
39 #include "localedef.h"
40 #include "charmap.h"
41 #include "locfile.h"
43 /* Undefine the following line in the production version. */
44 /* #define NDEBUG 1 */
45 #include <assert.h>
48 /* List of copied locales. */
49 struct copy_def_list_t *copy_list;
51 /* If this is defined be POSIX conform. */
52 int posix_conformance;
54 /* If not zero give a lot more messages. */
55 int verbose;
57 /* If not zero suppress warnings and information messages. */
58 int be_quiet;
60 /* If not zero, produce old-style hash table instead of 3-level access
61 tables. */
62 int oldstyle_tables;
64 /* If not zero force output even if warning were issued. */
65 static int force_output;
67 /* Prefix for output files. */
68 const char *output_prefix;
70 /* Name of the character map file. */
71 static const char *charmap_file;
73 /* Name of the locale definition file. */
74 static const char *input_file;
76 /* Name of the repertoire map file. */
77 const char *repertoire_global;
79 /* Name of the locale.alias file. */
80 const char *alias_file;
82 /* List of all locales. */
83 static struct localedef_t *locales;
85 /* If true don't add locale data to archive. */
86 bool no_archive;
88 /* If true add named locales to archive. */
89 static bool add_to_archive;
91 /* If true delete named locales from archive. */
92 static bool delete_from_archive;
94 /* If true replace archive content when adding. */
95 static bool replace_archive;
97 /* If true list archive content. */
98 static bool list_archive;
100 /* Maximum number of retries when opening the locale archive. */
101 int max_locarchive_open_retry = 10;
104 /* Name and version of program. */
105 static void print_version (FILE *stream, struct argp_state *state);
106 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
108 #define OPT_POSIX 301
109 #define OPT_QUIET 302
110 #define OPT_OLDSTYLE 303
111 #define OPT_PREFIX 304
112 #define OPT_NO_ARCHIVE 305
113 #define OPT_ADD_TO_ARCHIVE 306
114 #define OPT_REPLACE 307
115 #define OPT_DELETE_FROM_ARCHIVE 308
116 #define OPT_LIST_ARCHIVE 309
118 /* Definitions of arguments for argp functions. */
119 static const struct argp_option options[] =
121 { NULL, 0, NULL, 0, N_("Input Files:") },
122 { "charmap", 'f', "FILE", 0,
123 N_("Symbolic character names defined in FILE") },
124 { "inputfile", 'i', "FILE", 0, N_("Source definitions are found in FILE") },
125 { "repertoire-map", 'u', "FILE", 0,
126 N_("FILE contains mapping from symbolic names to UCS4 values") },
128 { NULL, 0, NULL, 0, N_("Output control:") },
129 { "force", 'c', NULL, 0,
130 N_("Create output even if warning messages were issued") },
131 { "old-style", OPT_OLDSTYLE, NULL, 0, N_("Create old-style tables") },
132 { "prefix", OPT_PREFIX, "PATH", 0, N_("Optional output file prefix") },
133 { "posix", OPT_POSIX, NULL, 0, N_("Be strictly POSIX conform") },
134 { "quiet", OPT_QUIET, NULL, 0,
135 N_("Suppress warnings and information messages") },
136 { "verbose", 'v', NULL, 0, N_("Print more messages") },
137 { NULL, 0, NULL, 0, N_("Archive control:") },
138 { "no-archive", OPT_NO_ARCHIVE, NULL, 0,
139 N_("Don't add new data to archive") },
140 { "add-to-archive", OPT_ADD_TO_ARCHIVE, NULL, 0,
141 N_("Add locales named by parameters to archive") },
142 { "replace", OPT_REPLACE, NULL, 0, N_("Replace existing archive content") },
143 { "delete-from-archive", OPT_DELETE_FROM_ARCHIVE, NULL, 0,
144 N_("Remove locales named by parameters from archive") },
145 { "list-archive", OPT_LIST_ARCHIVE, NULL, 0, N_("List content of archive") },
146 { "alias-file", 'A', "FILE", 0,
147 N_("locale.alias file to consult when making archive")},
148 { NULL, 0, NULL, 0, NULL }
151 /* Short description of program. */
152 static const char doc[] = N_("Compile locale specification");
154 /* Strings for arguments in help texts. */
155 static const char args_doc[] = N_("\
156 NAME\n\
157 [--add-to-archive|--delete-from-archive] FILE...\n\
158 --list-archive [FILE]");
160 /* Prototype for option handler. */
161 static error_t parse_opt (int key, char *arg, struct argp_state *state);
163 /* Function to print some extra text in the help message. */
164 static char *more_help (int key, const char *text, void *input);
166 /* Data structure to communicate with argp functions. */
167 static struct argp argp =
169 options, parse_opt, args_doc, doc, NULL, more_help
173 /* Prototypes for global functions. */
174 extern void *xmalloc (size_t __n);
176 /* Prototypes for local functions. */
177 static void error_print (void);
178 static const char *construct_output_path (char *path);
179 static const char *normalize_codeset (const char *codeset, size_t name_len);
183 main (int argc, char *argv[])
185 const char *output_path;
186 int cannot_write_why;
187 struct charmap_t *charmap;
188 struct localedef_t global;
189 int remaining;
191 /* Set initial values for global variables. */
192 copy_list = NULL;
193 posix_conformance = getenv ("POSIXLY_CORRECT") != NULL;
194 error_print_progname = error_print;
196 /* Set locale. Do not set LC_ALL because the other categories must
197 not be affected (according to POSIX.2). */
198 setlocale (LC_MESSAGES, "");
199 setlocale (LC_CTYPE, "");
201 /* Initialize the message catalog. */
202 textdomain (_libc_intl_domainname);
204 /* Parse and process arguments. */
205 argp_err_exit_status = 4;
206 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
208 /* Handle a few special cases. */
209 if (list_archive)
210 show_archive_content (verbose);
211 if (add_to_archive)
212 return add_locales_to_archive (argc - remaining, &argv[remaining],
213 replace_archive);
214 if (delete_from_archive)
215 return delete_locales_from_archive (argc - remaining, &argv[remaining]);
217 /* POSIX.2 requires to be verbose about missing characters in the
218 character map. */
219 verbose |= posix_conformance;
221 if (argc - remaining != 1)
223 /* We need exactly one non-option parameter. */
224 argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
225 program_invocation_short_name);
226 exit (4);
229 /* The parameter describes the output path of the constructed files.
230 If the described files cannot be written return a NULL pointer. */
231 output_path = construct_output_path (argv[remaining]);
232 if (output_path == NULL && ! no_archive)
233 error (4, errno, _("cannot create directory for output files"));
234 cannot_write_why = errno;
236 /* Now that the parameters are processed we have to reset the local
237 ctype locale. (P1003.2 4.35.5.2) */
238 setlocale (LC_CTYPE, "POSIX");
240 /* Look whether the system really allows locale definitions. POSIX
241 defines error code 3 for this situation so I think it must be
242 a fatal error (see P1003.2 4.35.8). */
243 if (sysconf (_SC_2_LOCALEDEF) < 0)
244 WITH_CUR_LOCALE (error (3, 0, _("\
245 FATAL: system does not define `_POSIX2_LOCALEDEF'")));
247 /* Process charmap file. */
248 charmap = charmap_read (charmap_file, verbose, be_quiet, 1);
250 /* Add the first entry in the locale list. */
251 memset (&global, '\0', sizeof (struct localedef_t));
252 global.name = input_file ?: "/dev/stdin";
253 global.needed = ALL_LOCALES;
254 locales = &global;
256 /* Now read the locale file. */
257 if (locfile_read (&global, charmap) != 0)
258 WITH_CUR_LOCALE (error (4, errno, _("\
259 cannot open locale definition file `%s'"), input_file));
261 /* Perhaps we saw some `copy' instructions. */
262 while (1)
264 struct localedef_t *runp = locales;
266 while (runp != NULL && (runp->needed & runp->avail) == runp->needed)
267 runp = runp->next;
269 if (runp == NULL)
270 /* Everything read. */
271 break;
273 if (locfile_read (runp, charmap) != 0)
274 WITH_CUR_LOCALE (error (4, errno, _("\
275 cannot open locale definition file `%s'"), runp->name));
278 /* Check the categories we processed in source form. */
279 check_all_categories (locales, charmap);
281 /* We are now able to write the data files. If warning were given we
282 do it only if it is explicitly requested (--force). */
283 if (error_message_count == 0 || force_output != 0)
285 if (cannot_write_why != 0)
286 WITH_CUR_LOCALE (error (4, cannot_write_why, _("\
287 cannot write output files to `%s'"), output_path));
288 else
289 write_all_categories (locales, charmap, argv[remaining], output_path);
291 else
292 WITH_CUR_LOCALE (error (4, 0, _("\
293 no output file produced because warning were issued")));
295 /* This exit status is prescribed by POSIX.2 4.35.7. */
296 exit (error_message_count != 0);
300 /* Handle program arguments. */
301 static error_t
302 parse_opt (int key, char *arg, struct argp_state *state)
304 switch (key)
306 case OPT_QUIET:
307 be_quiet = 1;
308 break;
309 case OPT_POSIX:
310 posix_conformance = 1;
311 break;
312 case OPT_OLDSTYLE:
313 oldstyle_tables = 1;
314 break;
315 case OPT_PREFIX:
316 output_prefix = arg;
317 break;
318 case OPT_NO_ARCHIVE:
319 no_archive = true;
320 break;
321 case OPT_ADD_TO_ARCHIVE:
322 add_to_archive = true;
323 break;
324 case OPT_REPLACE:
325 replace_archive = true;
326 break;
327 case OPT_DELETE_FROM_ARCHIVE:
328 delete_from_archive = true;
329 break;
330 case OPT_LIST_ARCHIVE:
331 list_archive = true;
332 break;
333 case 'c':
334 force_output = 1;
335 break;
336 case 'f':
337 charmap_file = arg;
338 break;
339 case 'A':
340 alias_file = arg;
341 break;
342 case 'i':
343 input_file = arg;
344 break;
345 case 'u':
346 repertoire_global = arg;
347 break;
348 case 'v':
349 verbose = 1;
350 break;
351 default:
352 return ARGP_ERR_UNKNOWN;
354 return 0;
358 static char *
359 more_help (int key, const char *text, void *input)
361 char *cp;
363 switch (key)
365 case ARGP_KEY_HELP_EXTRA:
366 /* We print some extra information. */
367 asprintf (&cp, gettext ("\
368 System's directory for character maps : %s\n\
369 repertoire maps: %s\n\
370 locale path : %s\n\
371 %s"),
372 CHARMAP_PATH, REPERTOIREMAP_PATH, LOCALE_PATH, gettext ("\
373 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
374 return cp;
375 default:
376 break;
378 return (char *) text;
381 /* Print the version information. */
382 static void
383 print_version (FILE *stream, struct argp_state *state)
385 fprintf (stream, "localedef (GNU %s) %s\n", PACKAGE, VERSION);
386 fprintf (stream, gettext ("\
387 Copyright (C) %s Free Software Foundation, Inc.\n\
388 This is free software; see the source for copying conditions. There is NO\n\
389 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
390 "), "2002");
391 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
395 /* The address of this function will be assigned to the hook in the error
396 functions. */
397 static void
398 error_print (void)
403 /* The parameter to localedef describes the output path. If it does
404 contain a '/' character it is a relative path. Otherwise it names the
405 locale this definition is for. */
406 static const char *
407 construct_output_path (char *path)
409 const char *normal = NULL;
410 char *result;
411 char *endp;
413 if (strchr (path, '/') == NULL)
415 /* This is a system path. First examine whether the locale name
416 contains a reference to the codeset. This should be
417 normalized. */
418 char *startp;
419 size_t n;
421 startp = path;
422 /* We must be prepared for finding a CEN name or a location of
423 the introducing `.' where it is not possible anymore. */
424 while (*startp != '\0' && *startp != '@' && *startp != '.'
425 && *startp != '+' && *startp != ',')
426 ++startp;
427 if (*startp == '.')
429 /* We found a codeset specification. Now find the end. */
430 endp = ++startp;
431 while (*endp != '\0' && *endp != '@')
432 ++endp;
434 if (endp > startp)
435 normal = normalize_codeset (startp, endp - startp);
437 else
438 /* This is to keep gcc quiet. */
439 endp = NULL;
441 /* We put an additional '\0' at the end of the string because at
442 the end of the function we need another byte for the trailing
443 '/'. */
444 if (normal == NULL)
445 n = asprintf (&result, "%s%s/%s%c",
446 output_prefix ?: "", LOCALEDIR, path, '\0');
447 else
448 n = asprintf (&result, "%s%s/%.*s%s%s%c",
449 output_prefix ?: "", LOCALEDIR,
450 (int) (startp - path), path, normal, endp, '\0');
452 if (n < 0)
453 return NULL;
455 endp = result + n - 1;
457 else
459 /* This is a user path. Please note the additional byte in the
460 memory allocation. */
461 size_t len = strlen (path) + 1;
462 result = xmalloc (len + 1);
463 endp = mempcpy (result, path, len) - 1;
465 /* If the user specified an output path we cannot add the output
466 to the archive. */
467 no_archive = true;
470 errno = 0;
472 if (no_archive && euidaccess (result, W_OK) == -1)
473 /* Perhaps the directory does not exist now. Try to create it. */
474 if (errno == ENOENT)
476 errno = 0;
477 if (mkdir (result, 0777) < 0)
478 return NULL;
481 *endp++ = '/';
482 *endp = '\0';
484 return result;
488 /* Normalize codeset name. There is no standard for the codeset
489 names. Normalization allows the user to use any of the common
490 names. */
491 static const char *
492 normalize_codeset (codeset, name_len)
493 const char *codeset;
494 size_t name_len;
496 int len = 0;
497 int only_digit = 1;
498 char *retval;
499 char *wp;
500 size_t cnt;
502 for (cnt = 0; cnt < name_len; ++cnt)
503 if (isalnum (codeset[cnt]))
505 ++len;
507 if (isalpha (codeset[cnt]))
508 only_digit = 0;
511 retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
513 if (retval != NULL)
515 if (only_digit)
516 wp = stpcpy (retval, "iso");
517 else
518 wp = retval;
520 for (cnt = 0; cnt < name_len; ++cnt)
521 if (isalpha (codeset[cnt]))
522 *wp++ = tolower (codeset[cnt]);
523 else if (isdigit (codeset[cnt]))
524 *wp++ = codeset[cnt];
526 *wp = '\0';
529 return (const char *) retval;
533 struct localedef_t *
534 add_to_readlist (int locale, const char *name, const char *repertoire_name,
535 int generate, struct localedef_t *copy_locale)
537 struct localedef_t *runp = locales;
539 while (runp != NULL && strcmp (name, runp->name) != 0)
540 runp = runp->next;
542 if (runp == NULL)
544 /* Add a new entry at the end. */
545 struct localedef_t *newp;
547 assert (generate == 1);
549 newp = xcalloc (1, sizeof (struct localedef_t));
550 newp->name = name;
551 newp->repertoire_name = repertoire_name;
553 if (locales == NULL)
554 runp = locales = newp;
555 else
557 runp = locales;
558 while (runp->next != NULL)
559 runp = runp->next;
560 runp = runp->next = newp;
564 if (generate
565 && (runp->needed & (1 << locale)) != 0
566 && (runp->avail & (1 << locale)) == 0)
567 WITH_CUR_LOCALE (error (5, 0, _("\
568 circular dependencies between locale definitions")));
570 if (copy_locale != NULL)
572 if (runp->categories[locale].generic != NULL)
573 WITH_CUR_LOCALE (error (5, 0, _("\
574 cannot add already read locale `%s' a second time"), name));
575 else
576 runp->categories[locale].generic =
577 copy_locale->categories[locale].generic;
580 runp->needed |= 1 << locale;
582 return runp;
586 struct localedef_t *
587 find_locale (int locale, const char *name, const char *repertoire_name,
588 const struct charmap_t *charmap)
590 struct localedef_t *result;
592 /* Find the locale, but do not generate it since this would be a bug. */
593 result = add_to_readlist (locale, name, repertoire_name, 0, NULL);
595 assert (result != NULL);
597 if ((result->avail & (1 << locale)) == 0
598 && locfile_read (result, charmap) != 0)
599 WITH_CUR_LOCALE (error (4, errno, _("\
600 cannot open locale definition file `%s'"), result->name));
602 return result;
606 struct localedef_t *
607 load_locale (int locale, const char *name, const char *repertoire_name,
608 const struct charmap_t *charmap, struct localedef_t *copy_locale)
610 struct localedef_t *result;
612 /* Generate the locale if it does not exist. */
613 result = add_to_readlist (locale, name, repertoire_name, 1, copy_locale);
615 assert (result != NULL);
617 if ((result->avail & (1 << locale)) == 0
618 && locfile_read (result, charmap) != 0)
619 WITH_CUR_LOCALE (error (4, errno, _("\
620 cannot open locale definition file `%s'"), result->name));
622 return result;
625 static void
626 turn_on_mcheck (void)
628 /* Enable `malloc' debugging. */
629 mcheck (NULL);
630 /* Use the following line for a more thorough but much slower testing. */
631 /* mcheck_pedantic (NULL); */
634 void (*__malloc_initialize_hook) (void) = turn_on_mcheck;