Update.
[glibc.git] / locale / programs / localedef.c
blob28cb7b316e1e62923bb2ee218b45ca57a91bbc5c
1 /* Copyright (C) 1995-2002, 2003, 2004 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 if (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 For bug reporting instructions, please see:\n\
374 <http://www.gnu.org/software/libc/bugs.html>.\n")) < 0)
375 return NULL;
376 return cp;
377 default:
378 break;
380 return (char *) text;
383 /* Print the version information. */
384 static void
385 print_version (FILE *stream, struct argp_state *state)
387 fprintf (stream, "localedef (GNU %s) %s\n", PACKAGE, VERSION);
388 fprintf (stream, gettext ("\
389 Copyright (C) %s Free Software Foundation, Inc.\n\
390 This is free software; see the source for copying conditions. There is NO\n\
391 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
392 "), "2004");
393 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
397 /* The address of this function will be assigned to the hook in the error
398 functions. */
399 static void
400 error_print (void)
405 /* The parameter to localedef describes the output path. If it does
406 contain a '/' character it is a relative path. Otherwise it names the
407 locale this definition is for. */
408 static const char *
409 construct_output_path (char *path)
411 const char *normal = NULL;
412 char *result;
413 char *endp;
415 if (strchr (path, '/') == NULL)
417 /* This is a system path. First examine whether the locale name
418 contains a reference to the codeset. This should be
419 normalized. */
420 char *startp;
421 size_t n;
423 startp = path;
424 /* We must be prepared for finding a CEN name or a location of
425 the introducing `.' where it is not possible anymore. */
426 while (*startp != '\0' && *startp != '@' && *startp != '.')
427 ++startp;
428 if (*startp == '.')
430 /* We found a codeset specification. Now find the end. */
431 endp = ++startp;
432 while (*endp != '\0' && *endp != '@')
433 ++endp;
435 if (endp > startp)
436 normal = normalize_codeset (startp, endp - startp);
438 else
439 /* This is to keep gcc quiet. */
440 endp = NULL;
442 /* We put an additional '\0' at the end of the string because at
443 the end of the function we need another byte for the trailing
444 '/'. */
445 if (normal == NULL)
446 n = asprintf (&result, "%s%s/%s%c",
447 output_prefix ?: "", LOCALEDIR, path, '\0');
448 else
449 n = asprintf (&result, "%s%s/%.*s%s%s%c",
450 output_prefix ?: "", LOCALEDIR,
451 (int) (startp - path), path, normal, endp, '\0');
453 if (n < 0)
454 return NULL;
456 endp = result + n - 1;
458 else
460 /* This is a user path. Please note the additional byte in the
461 memory allocation. */
462 size_t len = strlen (path) + 1;
463 result = xmalloc (len + 1);
464 endp = mempcpy (result, path, len) - 1;
466 /* If the user specified an output path we cannot add the output
467 to the archive. */
468 no_archive = true;
471 errno = 0;
473 if (no_archive && euidaccess (result, W_OK) == -1)
474 /* Perhaps the directory does not exist now. Try to create it. */
475 if (errno == ENOENT)
477 errno = 0;
478 if (mkdir (result, 0777) < 0)
479 return NULL;
482 *endp++ = '/';
483 *endp = '\0';
485 return result;
489 /* Normalize codeset name. There is no standard for the codeset
490 names. Normalization allows the user to use any of the common
491 names. */
492 static const char *
493 normalize_codeset (codeset, name_len)
494 const char *codeset;
495 size_t name_len;
497 int len = 0;
498 int only_digit = 1;
499 char *retval;
500 char *wp;
501 size_t cnt;
503 for (cnt = 0; cnt < name_len; ++cnt)
504 if (isalnum (codeset[cnt]))
506 ++len;
508 if (isalpha (codeset[cnt]))
509 only_digit = 0;
512 retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
514 if (retval != NULL)
516 if (only_digit)
517 wp = stpcpy (retval, "iso");
518 else
519 wp = retval;
521 for (cnt = 0; cnt < name_len; ++cnt)
522 if (isalpha (codeset[cnt]))
523 *wp++ = tolower (codeset[cnt]);
524 else if (isdigit (codeset[cnt]))
525 *wp++ = codeset[cnt];
527 *wp = '\0';
530 return (const char *) retval;
534 struct localedef_t *
535 add_to_readlist (int locale, const char *name, const char *repertoire_name,
536 int generate, struct localedef_t *copy_locale)
538 struct localedef_t *runp = locales;
540 while (runp != NULL && strcmp (name, runp->name) != 0)
541 runp = runp->next;
543 if (runp == NULL)
545 /* Add a new entry at the end. */
546 struct localedef_t *newp;
548 assert (generate == 1);
550 newp = xcalloc (1, sizeof (struct localedef_t));
551 newp->name = name;
552 newp->repertoire_name = repertoire_name;
554 if (locales == NULL)
555 runp = locales = newp;
556 else
558 runp = locales;
559 while (runp->next != NULL)
560 runp = runp->next;
561 runp = runp->next = newp;
565 if (generate
566 && (runp->needed & (1 << locale)) != 0
567 && (runp->avail & (1 << locale)) == 0)
568 WITH_CUR_LOCALE (error (5, 0, _("\
569 circular dependencies between locale definitions")));
571 if (copy_locale != NULL)
573 if (runp->categories[locale].generic != NULL)
574 WITH_CUR_LOCALE (error (5, 0, _("\
575 cannot add already read locale `%s' a second time"), name));
576 else
577 runp->categories[locale].generic =
578 copy_locale->categories[locale].generic;
581 runp->needed |= 1 << locale;
583 return runp;
587 struct localedef_t *
588 find_locale (int locale, const char *name, const char *repertoire_name,
589 const struct charmap_t *charmap)
591 struct localedef_t *result;
593 /* Find the locale, but do not generate it since this would be a bug. */
594 result = add_to_readlist (locale, name, repertoire_name, 0, NULL);
596 assert (result != NULL);
598 if ((result->avail & (1 << locale)) == 0
599 && locfile_read (result, charmap) != 0)
600 WITH_CUR_LOCALE (error (4, errno, _("\
601 cannot open locale definition file `%s'"), result->name));
603 return result;
607 struct localedef_t *
608 load_locale (int locale, const char *name, const char *repertoire_name,
609 const struct charmap_t *charmap, struct localedef_t *copy_locale)
611 struct localedef_t *result;
613 /* Generate the locale if it does not exist. */
614 result = add_to_readlist (locale, name, repertoire_name, 1, copy_locale);
616 assert (result != NULL);
618 if ((result->avail & (1 << locale)) == 0
619 && locfile_read (result, charmap) != 0)
620 WITH_CUR_LOCALE (error (4, errno, _("\
621 cannot open locale definition file `%s'"), result->name));
623 return result;
626 static void
627 turn_on_mcheck (void)
629 /* Enable `malloc' debugging. */
630 mcheck (NULL);
631 /* Use the following line for a more thorough but much slower testing. */
632 /* mcheck_pedantic (NULL); */
635 void (*__malloc_initialize_hook) (void) = turn_on_mcheck;