Merge remote branch 'origin/roland/hwcap_mask'
[glibc.git] / locale / programs / localedef.c
blob4cb0d862ca698e502529399b32f4880cc0fa920d
1 /* Copyright (C) 1995-2008, 2009, 2010 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1995.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; version 2 of the License, or
8 (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <argp.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <libintl.h>
27 #include <locale.h>
28 #include <mcheck.h>
29 #include <stdbool.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <error.h>
35 #include <sys/mman.h>
36 #include <sys/stat.h>
38 #include "localedef.h"
39 #include "charmap.h"
40 #include "locfile.h"
42 /* Undefine the following line in the production version. */
43 /* #define NDEBUG 1 */
44 #include <assert.h>
47 /* List of copied locales. */
48 struct copy_def_list_t *copy_list;
50 /* If this is defined be POSIX conform. */
51 int posix_conformance;
53 /* If not zero give a lot more messages. */
54 int verbose;
56 /* If not zero suppress warnings and information messages. */
57 int be_quiet;
59 /* If not zero, produce old-style hash table instead of 3-level access
60 tables. */
61 int oldstyle_tables;
63 /* If not zero force output even if warning were issued. */
64 static int force_output;
66 /* Prefix for output files. */
67 const char *output_prefix;
69 /* Name of the character map file. */
70 static const char *charmap_file;
72 /* Name of the locale definition file. */
73 static const char *input_file;
75 /* Name of the repertoire map file. */
76 const char *repertoire_global;
78 /* Name of the locale.alias file. */
79 const char *alias_file;
81 /* List of all locales. */
82 static struct localedef_t *locales;
84 /* If true don't add locale data to archive. */
85 bool no_archive;
87 /* If true add named locales to archive. */
88 static bool add_to_archive;
90 /* If true delete named locales from archive. */
91 static bool delete_from_archive;
93 /* If true replace archive content when adding. */
94 static bool replace_archive;
96 /* If true list archive content. */
97 static bool list_archive;
99 /* Maximum number of retries when opening the locale archive. */
100 int max_locarchive_open_retry = 10;
103 /* Name and version of program. */
104 static void print_version (FILE *stream, struct argp_state *state);
105 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
107 #define OPT_POSIX 301
108 #define OPT_QUIET 302
109 #define OPT_OLDSTYLE 303
110 #define OPT_PREFIX 304
111 #define OPT_NO_ARCHIVE 305
112 #define OPT_ADD_TO_ARCHIVE 306
113 #define OPT_REPLACE 307
114 #define OPT_DELETE_FROM_ARCHIVE 308
115 #define OPT_LIST_ARCHIVE 309
117 /* Definitions of arguments for argp functions. */
118 static const struct argp_option options[] =
120 { NULL, 0, NULL, 0, N_("Input Files:") },
121 { "charmap", 'f', "FILE", 0,
122 N_("Symbolic character names defined in FILE") },
123 { "inputfile", 'i', "FILE", 0, N_("Source definitions are found in FILE") },
124 { "repertoire-map", 'u', "FILE", 0,
125 N_("FILE contains mapping from symbolic names to UCS4 values") },
127 { NULL, 0, NULL, 0, N_("Output control:") },
128 { "force", 'c', NULL, 0,
129 N_("Create output even if warning messages were issued") },
130 { "old-style", OPT_OLDSTYLE, NULL, 0, N_("Create old-style tables") },
131 { "prefix", OPT_PREFIX, "PATH", 0, N_("Optional output file prefix") },
132 { "posix", OPT_POSIX, NULL, 0, N_("Be strictly POSIX conform") },
133 { "quiet", OPT_QUIET, NULL, 0,
134 N_("Suppress warnings and information messages") },
135 { "verbose", 'v', NULL, 0, N_("Print more messages") },
136 { NULL, 0, NULL, 0, N_("Archive control:") },
137 { "no-archive", OPT_NO_ARCHIVE, NULL, 0,
138 N_("Don't add new data to archive") },
139 { "add-to-archive", OPT_ADD_TO_ARCHIVE, NULL, 0,
140 N_("Add locales named by parameters to archive") },
141 { "replace", OPT_REPLACE, NULL, 0, N_("Replace existing archive content") },
142 { "delete-from-archive", OPT_DELETE_FROM_ARCHIVE, NULL, 0,
143 N_("Remove locales named by parameters from archive") },
144 { "list-archive", OPT_LIST_ARCHIVE, NULL, 0, N_("List content of archive") },
145 { "alias-file", 'A', "FILE", 0,
146 N_("locale.alias file to consult when making archive")},
147 { NULL, 0, NULL, 0, NULL }
150 /* Short description of program. */
151 static const char doc[] = N_("Compile locale specification");
153 /* Strings for arguments in help texts. */
154 static const char args_doc[] = N_("\
155 NAME\n\
156 [--add-to-archive|--delete-from-archive] FILE...\n\
157 --list-archive [FILE]");
159 /* Prototype for option handler. */
160 static error_t parse_opt (int key, char *arg, struct argp_state *state);
162 /* Function to print some extra text in the help message. */
163 static char *more_help (int key, const char *text, void *input);
165 /* Data structure to communicate with argp functions. */
166 static struct argp argp =
168 options, parse_opt, args_doc, doc, NULL, more_help
172 /* Prototypes for global functions. */
173 extern void *xmalloc (size_t __n);
175 /* Prototypes for local functions. */
176 static void error_print (void);
177 static const char *construct_output_path (char *path);
178 static const char *normalize_codeset (const char *codeset, size_t name_len);
182 main (int argc, char *argv[])
184 const char *output_path;
185 int cannot_write_why;
186 struct charmap_t *charmap;
187 struct localedef_t global;
188 int remaining;
190 /* Set initial values for global variables. */
191 copy_list = NULL;
192 posix_conformance = getenv ("POSIXLY_CORRECT") != NULL;
193 error_print_progname = error_print;
195 /* Set locale. Do not set LC_ALL because the other categories must
196 not be affected (according to POSIX.2). */
197 setlocale (LC_MESSAGES, "");
198 setlocale (LC_CTYPE, "");
200 /* Initialize the message catalog. */
201 textdomain (_libc_intl_domainname);
203 /* Parse and process arguments. */
204 argp_err_exit_status = 4;
205 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
207 /* Handle a few special cases. */
208 if (list_archive)
209 show_archive_content (verbose);
210 if (add_to_archive)
211 return add_locales_to_archive (argc - remaining, &argv[remaining],
212 replace_archive);
213 if (delete_from_archive)
214 return delete_locales_from_archive (argc - remaining, &argv[remaining]);
216 /* POSIX.2 requires to be verbose about missing characters in the
217 character map. */
218 verbose |= posix_conformance;
220 if (argc - remaining != 1)
222 /* We need exactly one non-option parameter. */
223 argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
224 program_invocation_short_name);
225 exit (4);
228 /* The parameter describes the output path of the constructed files.
229 If the described files cannot be written return a NULL pointer. */
230 output_path = construct_output_path (argv[remaining]);
231 if (output_path == NULL && ! no_archive)
232 error (4, errno, _("cannot create directory for output files"));
233 cannot_write_why = errno;
235 /* Now that the parameters are processed we have to reset the local
236 ctype locale. (P1003.2 4.35.5.2) */
237 setlocale (LC_CTYPE, "POSIX");
239 /* Look whether the system really allows locale definitions. POSIX
240 defines error code 3 for this situation so I think it must be
241 a fatal error (see P1003.2 4.35.8). */
242 if (sysconf (_SC_2_LOCALEDEF) < 0)
243 WITH_CUR_LOCALE (error (3, 0, _("\
244 FATAL: system does not define `_POSIX2_LOCALEDEF'")));
246 /* Process charmap file. */
247 charmap = charmap_read (charmap_file, verbose, 1, be_quiet, 1);
249 /* Add the first entry in the locale list. */
250 memset (&global, '\0', sizeof (struct localedef_t));
251 global.name = input_file ?: "/dev/stdin";
252 global.needed = ALL_LOCALES;
253 locales = &global;
255 /* Now read the locale file. */
256 if (locfile_read (&global, charmap) != 0)
257 WITH_CUR_LOCALE (error (4, errno, _("\
258 cannot open locale definition file `%s'"), input_file));
260 /* Perhaps we saw some `copy' instructions. */
261 while (1)
263 struct localedef_t *runp = locales;
265 while (runp != NULL && (runp->needed & runp->avail) == runp->needed)
266 runp = runp->next;
268 if (runp == NULL)
269 /* Everything read. */
270 break;
272 if (locfile_read (runp, charmap) != 0)
273 WITH_CUR_LOCALE (error (4, errno, _("\
274 cannot open locale definition file `%s'"), runp->name));
277 /* Check the categories we processed in source form. */
278 check_all_categories (locales, charmap);
280 /* We are now able to write the data files. If warning were given we
281 do it only if it is explicitly requested (--force). */
282 if (error_message_count == 0 || force_output != 0)
284 if (cannot_write_why != 0)
285 WITH_CUR_LOCALE (error (4, cannot_write_why, _("\
286 cannot write output files to `%s'"), output_path));
287 else
288 write_all_categories (locales, charmap, argv[remaining], output_path);
290 else
291 WITH_CUR_LOCALE (error (4, 0, _("\
292 no output file produced because warnings were issued")));
294 /* This exit status is prescribed by POSIX.2 4.35.7. */
295 exit (error_message_count != 0);
299 /* Handle program arguments. */
300 static error_t
301 parse_opt (int key, char *arg, struct argp_state *state)
303 switch (key)
305 case OPT_QUIET:
306 be_quiet = 1;
307 break;
308 case OPT_POSIX:
309 posix_conformance = 1;
310 break;
311 case OPT_OLDSTYLE:
312 oldstyle_tables = 1;
313 break;
314 case OPT_PREFIX:
315 output_prefix = arg;
316 break;
317 case OPT_NO_ARCHIVE:
318 no_archive = true;
319 break;
320 case OPT_ADD_TO_ARCHIVE:
321 add_to_archive = true;
322 break;
323 case OPT_REPLACE:
324 replace_archive = true;
325 break;
326 case OPT_DELETE_FROM_ARCHIVE:
327 delete_from_archive = true;
328 break;
329 case OPT_LIST_ARCHIVE:
330 list_archive = true;
331 break;
332 case 'c':
333 force_output = 1;
334 break;
335 case 'f':
336 charmap_file = arg;
337 break;
338 case 'A':
339 alias_file = arg;
340 break;
341 case 'i':
342 input_file = arg;
343 break;
344 case 'u':
345 repertoire_global = arg;
346 break;
347 case 'v':
348 verbose = 1;
349 break;
350 default:
351 return ARGP_ERR_UNKNOWN;
353 return 0;
357 static char *
358 more_help (int key, const char *text, void *input)
360 char *cp;
362 switch (key)
364 case ARGP_KEY_HELP_EXTRA:
365 /* We print some extra information. */
366 if (asprintf (&cp, gettext ("\
367 System's directory for character maps : %s\n\
368 repertoire maps: %s\n\
369 locale path : %s\n\
370 %s"),
371 CHARMAP_PATH, REPERTOIREMAP_PATH, LOCALE_PATH, gettext ("\
372 For bug reporting instructions, please see:\n\
373 <http://www.gnu.org/software/libc/bugs.html>.\n")) < 0)
374 return NULL;
375 return cp;
376 default:
377 break;
379 return (char *) text;
382 /* Print the version information. */
383 static void
384 print_version (FILE *stream, struct argp_state *state)
386 fprintf (stream, "localedef (GNU %s) %s\n", PACKAGE, VERSION);
387 fprintf (stream, gettext ("\
388 Copyright (C) %s Free Software Foundation, Inc.\n\
389 This is free software; see the source for copying conditions. There is NO\n\
390 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
391 "), "2010");
392 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
396 /* The address of this function will be assigned to the hook in the error
397 functions. */
398 static void
399 error_print (void)
404 /* The parameter to localedef describes the output path. If it does
405 contain a '/' character it is a relative path. Otherwise it names the
406 locale this definition is for. */
407 static const char *
408 construct_output_path (char *path)
410 const char *normal = NULL;
411 char *result;
412 char *endp;
414 if (strchr (path, '/') == NULL)
416 /* This is a system path. First examine whether the locale name
417 contains a reference to the codeset. This should be
418 normalized. */
419 char *startp;
420 size_t n;
422 startp = path;
423 /* We must be prepared for finding a CEN name or a location of
424 the introducing `.' where it is not possible anymore. */
425 while (*startp != '\0' && *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 category, 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 << category)) != 0
566 && (runp->avail & (1 << category)) == 0)
567 WITH_CUR_LOCALE (error (5, 0, _("\
568 circular dependencies between locale definitions")));
570 if (copy_locale != NULL)
572 if (runp->categories[category].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[category].generic =
577 copy_locale->categories[category].generic;
580 runp->needed |= 1 << category;
582 return runp;
586 struct localedef_t *
587 find_locale (int category, 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 (category, name, repertoire_name, 0, NULL);
595 assert (result != NULL);
597 if ((result->avail & (1 << category)) == 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 category, 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 (category, name, repertoire_name, 1, copy_locale);
615 assert (result != NULL);
617 if ((result->avail & (1 << category)) == 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;