ia64: drop HAVE_CPP_ASM_DEBUGINFO
[glibc.git] / locale / programs / localedef.c
blob5dc35ca695c7a5bf2eda3b1d211ee15e3c0c2ed6
1 /* Copyright (C) 1995-2011, 2012 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, see <http://www.gnu.org/licenses/>. */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include <argp.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <libintl.h>
26 #include <locale.h>
27 #include <mcheck.h>
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <error.h>
34 #include <sys/mman.h>
35 #include <sys/stat.h>
37 #include "localedef.h"
38 #include "charmap.h"
39 #include "locfile.h"
41 /* Undefine the following line in the production version. */
42 /* #define NDEBUG 1 */
43 #include <assert.h>
46 /* List of copied locales. */
47 struct copy_def_list_t *copy_list;
49 /* If this is defined be POSIX conform. */
50 int posix_conformance;
52 /* If not zero give a lot more messages. */
53 int verbose;
55 /* If not zero suppress warnings and information messages. */
56 int be_quiet;
58 /* If not zero, produce old-style hash table instead of 3-level access
59 tables. */
60 int oldstyle_tables;
62 /* If not zero force output even if warning were issued. */
63 static int force_output;
65 /* Prefix for output files. */
66 const char *output_prefix;
68 /* Name of the character map file. */
69 static const char *charmap_file;
71 /* Name of the locale definition file. */
72 static const char *input_file;
74 /* Name of the repertoire map file. */
75 const char *repertoire_global;
77 /* Name of the locale.alias file. */
78 const char *alias_file;
80 /* List of all locales. */
81 static struct localedef_t *locales;
83 /* If true don't add locale data to archive. */
84 bool no_archive;
86 /* If true add named locales to archive. */
87 static bool add_to_archive;
89 /* If true delete named locales from archive. */
90 static bool delete_from_archive;
92 /* If true replace archive content when adding. */
93 static bool replace_archive;
95 /* If true list archive content. */
96 static bool list_archive;
98 /* Maximum number of retries when opening the locale archive. */
99 int max_locarchive_open_retry = 10;
102 /* Name and version of program. */
103 static void print_version (FILE *stream, struct argp_state *state);
104 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
106 #define OPT_POSIX 301
107 #define OPT_QUIET 302
108 #define OPT_OLDSTYLE 303
109 #define OPT_PREFIX 304
110 #define OPT_NO_ARCHIVE 305
111 #define OPT_ADD_TO_ARCHIVE 306
112 #define OPT_REPLACE 307
113 #define OPT_DELETE_FROM_ARCHIVE 308
114 #define OPT_LIST_ARCHIVE 309
116 /* Definitions of arguments for argp functions. */
117 static const struct argp_option options[] =
119 { NULL, 0, NULL, 0, N_("Input Files:") },
120 { "charmap", 'f', "FILE", 0,
121 N_("Symbolic character names defined in FILE") },
122 { "inputfile", 'i', "FILE", 0, N_("Source definitions are found in FILE") },
123 { "repertoire-map", 'u', "FILE", 0,
124 N_("FILE contains mapping from symbolic names to UCS4 values") },
126 { NULL, 0, NULL, 0, N_("Output control:") },
127 { "force", 'c', NULL, 0,
128 N_("Create output even if warning messages were issued") },
129 { "old-style", OPT_OLDSTYLE, NULL, 0, N_("Create old-style tables") },
130 { "prefix", OPT_PREFIX, "PATH", 0, N_("Optional output file prefix") },
131 { "posix", OPT_POSIX, NULL, 0, N_("Be strictly POSIX conform") },
132 { "quiet", OPT_QUIET, NULL, 0,
133 N_("Suppress warnings and information messages") },
134 { "verbose", 'v', NULL, 0, N_("Print more messages") },
135 { NULL, 0, NULL, 0, N_("Archive control:") },
136 { "no-archive", OPT_NO_ARCHIVE, NULL, 0,
137 N_("Don't add new data to archive") },
138 { "add-to-archive", OPT_ADD_TO_ARCHIVE, NULL, 0,
139 N_("Add locales named by parameters to archive") },
140 { "replace", OPT_REPLACE, NULL, 0, N_("Replace existing archive content") },
141 { "delete-from-archive", OPT_DELETE_FROM_ARCHIVE, NULL, 0,
142 N_("Remove locales named by parameters from archive") },
143 { "list-archive", OPT_LIST_ARCHIVE, NULL, 0, N_("List content of archive") },
144 { "alias-file", 'A', "FILE", 0,
145 N_("locale.alias file to consult when making archive")},
146 { NULL, 0, NULL, 0, NULL }
149 /* Short description of program. */
150 static const char doc[] = N_("Compile locale specification");
152 /* Strings for arguments in help texts. */
153 static const char args_doc[] = N_("\
154 NAME\n\
155 [--add-to-archive|--delete-from-archive] FILE...\n\
156 --list-archive [FILE]");
158 /* Prototype for option handler. */
159 static error_t parse_opt (int key, char *arg, struct argp_state *state);
161 /* Function to print some extra text in the help message. */
162 static char *more_help (int key, const char *text, void *input);
164 /* Data structure to communicate with argp functions. */
165 static struct argp argp =
167 options, parse_opt, args_doc, doc, NULL, more_help
171 /* Prototypes for local functions. */
172 static void error_print (void);
173 static const char *construct_output_path (char *path);
174 static const char *normalize_codeset (const char *codeset, size_t name_len);
178 main (int argc, char *argv[])
180 const char *output_path;
181 int cannot_write_why;
182 struct charmap_t *charmap;
183 struct localedef_t global;
184 int remaining;
186 /* Set initial values for global variables. */
187 copy_list = NULL;
188 posix_conformance = getenv ("POSIXLY_CORRECT") != NULL;
189 error_print_progname = error_print;
191 /* Set locale. Do not set LC_ALL because the other categories must
192 not be affected (according to POSIX.2). */
193 setlocale (LC_MESSAGES, "");
194 setlocale (LC_CTYPE, "");
196 /* Initialize the message catalog. */
197 textdomain (_libc_intl_domainname);
199 /* Parse and process arguments. */
200 argp_err_exit_status = 4;
201 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
203 /* Handle a few special cases. */
204 if (list_archive)
205 show_archive_content (verbose);
206 if (add_to_archive)
207 return add_locales_to_archive (argc - remaining, &argv[remaining],
208 replace_archive);
209 if (delete_from_archive)
210 return delete_locales_from_archive (argc - remaining, &argv[remaining]);
212 /* POSIX.2 requires to be verbose about missing characters in the
213 character map. */
214 verbose |= posix_conformance;
216 if (argc - remaining != 1)
218 /* We need exactly one non-option parameter. */
219 argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
220 program_invocation_short_name);
221 exit (4);
224 /* The parameter describes the output path of the constructed files.
225 If the described files cannot be written return a NULL pointer. */
226 output_path = construct_output_path (argv[remaining]);
227 if (output_path == NULL && ! no_archive)
228 error (4, errno, _("cannot create directory for output files"));
229 cannot_write_why = errno;
231 /* Now that the parameters are processed we have to reset the local
232 ctype locale. (P1003.2 4.35.5.2) */
233 setlocale (LC_CTYPE, "POSIX");
235 /* Look whether the system really allows locale definitions. POSIX
236 defines error code 3 for this situation so I think it must be
237 a fatal error (see P1003.2 4.35.8). */
238 if (sysconf (_SC_2_LOCALEDEF) < 0)
239 WITH_CUR_LOCALE (error (3, 0, _("\
240 FATAL: system does not define `_POSIX2_LOCALEDEF'")));
242 /* Process charmap file. */
243 charmap = charmap_read (charmap_file, verbose, 1, be_quiet, 1);
245 /* Add the first entry in the locale list. */
246 memset (&global, '\0', sizeof (struct localedef_t));
247 global.name = input_file ?: "/dev/stdin";
248 global.needed = ALL_LOCALES;
249 locales = &global;
251 /* Now read the locale file. */
252 if (locfile_read (&global, charmap) != 0)
253 WITH_CUR_LOCALE (error (4, errno, _("\
254 cannot open locale definition file `%s'"), input_file));
256 /* Perhaps we saw some `copy' instructions. */
257 while (1)
259 struct localedef_t *runp = locales;
261 while (runp != NULL && (runp->needed & runp->avail) == runp->needed)
262 runp = runp->next;
264 if (runp == NULL)
265 /* Everything read. */
266 break;
268 if (locfile_read (runp, charmap) != 0)
269 WITH_CUR_LOCALE (error (4, errno, _("\
270 cannot open locale definition file `%s'"), runp->name));
273 /* Check the categories we processed in source form. */
274 check_all_categories (locales, charmap);
276 /* We are now able to write the data files. If warning were given we
277 do it only if it is explicitly requested (--force). */
278 if (error_message_count == 0 || force_output != 0)
280 if (cannot_write_why != 0)
281 WITH_CUR_LOCALE (error (4, cannot_write_why, _("\
282 cannot write output files to `%s'"), output_path));
283 else
284 write_all_categories (locales, charmap, argv[remaining], output_path);
286 else
287 WITH_CUR_LOCALE (error (4, 0, _("\
288 no output file produced because warnings were issued")));
290 /* This exit status is prescribed by POSIX.2 4.35.7. */
291 exit (error_message_count != 0);
295 /* Handle program arguments. */
296 static error_t
297 parse_opt (int key, char *arg, struct argp_state *state)
299 switch (key)
301 case OPT_QUIET:
302 be_quiet = 1;
303 break;
304 case OPT_POSIX:
305 posix_conformance = 1;
306 break;
307 case OPT_OLDSTYLE:
308 oldstyle_tables = 1;
309 break;
310 case OPT_PREFIX:
311 output_prefix = arg;
312 break;
313 case OPT_NO_ARCHIVE:
314 no_archive = true;
315 break;
316 case OPT_ADD_TO_ARCHIVE:
317 add_to_archive = true;
318 break;
319 case OPT_REPLACE:
320 replace_archive = true;
321 break;
322 case OPT_DELETE_FROM_ARCHIVE:
323 delete_from_archive = true;
324 break;
325 case OPT_LIST_ARCHIVE:
326 list_archive = true;
327 break;
328 case 'c':
329 force_output = 1;
330 break;
331 case 'f':
332 charmap_file = arg;
333 break;
334 case 'A':
335 alias_file = arg;
336 break;
337 case 'i':
338 input_file = arg;
339 break;
340 case 'u':
341 repertoire_global = arg;
342 break;
343 case 'v':
344 verbose = 1;
345 break;
346 default:
347 return ARGP_ERR_UNKNOWN;
349 return 0;
353 static char *
354 more_help (int key, const char *text, void *input)
356 char *cp;
358 switch (key)
360 case ARGP_KEY_HELP_EXTRA:
361 /* We print some extra information. */
362 if (asprintf (&cp, gettext ("\
363 System's directory for character maps : %s\n\
364 repertoire maps: %s\n\
365 locale path : %s\n\
366 %s"),
367 CHARMAP_PATH, REPERTOIREMAP_PATH, LOCALE_PATH, gettext ("\
368 For bug reporting instructions, please see:\n\
369 <http://www.gnu.org/software/libc/bugs.html>.\n")) < 0)
370 return NULL;
371 return cp;
372 default:
373 break;
375 return (char *) text;
378 /* Print the version information. */
379 static void
380 print_version (FILE *stream, struct argp_state *state)
382 fprintf (stream, "localedef (GNU %s) %s\n", PACKAGE, VERSION);
383 fprintf (stream, gettext ("\
384 Copyright (C) %s Free Software Foundation, Inc.\n\
385 This is free software; see the source for copying conditions. There is NO\n\
386 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
387 "), "2012");
388 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
392 /* The address of this function will be assigned to the hook in the error
393 functions. */
394 static void
395 error_print (void)
400 /* The parameter to localedef describes the output path. If it does
401 contain a '/' character it is a relative path. Otherwise it names the
402 locale this definition is for. */
403 static const char *
404 construct_output_path (char *path)
406 const char *normal = NULL;
407 char *result;
408 char *endp;
410 if (strchr (path, '/') == NULL)
412 /* This is a system path. First examine whether the locale name
413 contains a reference to the codeset. This should be
414 normalized. */
415 char *startp;
417 startp = path;
418 /* We must be prepared for finding a CEN name or a location of
419 the introducing `.' where it is not possible anymore. */
420 while (*startp != '\0' && *startp != '@' && *startp != '.')
421 ++startp;
422 if (*startp == '.')
424 /* We found a codeset specification. Now find the end. */
425 endp = ++startp;
426 while (*endp != '\0' && *endp != '@')
427 ++endp;
429 if (endp > startp)
430 normal = normalize_codeset (startp, endp - startp);
432 else
433 /* This is to keep gcc quiet. */
434 endp = NULL;
436 /* We put an additional '\0' at the end of the string because at
437 the end of the function we need another byte for the trailing
438 '/'. */
439 ssize_t n;
440 if (normal == NULL)
441 n = asprintf (&result, "%s%s/%s%c",
442 output_prefix ?: "", LOCALEDIR, path, '\0');
443 else
444 n = asprintf (&result, "%s%s/%.*s%s%s%c",
445 output_prefix ?: "", LOCALEDIR,
446 (int) (startp - path), path, normal, endp, '\0');
448 if (n < 0)
449 return NULL;
451 endp = result + n - 1;
453 else
455 /* This is a user path. Please note the additional byte in the
456 memory allocation. */
457 size_t len = strlen (path) + 1;
458 result = xmalloc (len + 1);
459 endp = mempcpy (result, path, len) - 1;
461 /* If the user specified an output path we cannot add the output
462 to the archive. */
463 no_archive = true;
466 errno = 0;
468 if (no_archive && euidaccess (result, W_OK) == -1)
469 /* Perhaps the directory does not exist now. Try to create it. */
470 if (errno == ENOENT)
472 errno = 0;
473 if (mkdir (result, 0777) < 0)
474 return NULL;
477 *endp++ = '/';
478 *endp = '\0';
480 return result;
484 /* Normalize codeset name. There is no standard for the codeset
485 names. Normalization allows the user to use any of the common
486 names. */
487 static const char *
488 normalize_codeset (codeset, name_len)
489 const char *codeset;
490 size_t name_len;
492 int len = 0;
493 int only_digit = 1;
494 char *retval;
495 char *wp;
496 size_t cnt;
498 for (cnt = 0; cnt < name_len; ++cnt)
499 if (isalnum (codeset[cnt]))
501 ++len;
503 if (isalpha (codeset[cnt]))
504 only_digit = 0;
507 retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
509 if (retval != NULL)
511 if (only_digit)
512 wp = stpcpy (retval, "iso");
513 else
514 wp = retval;
516 for (cnt = 0; cnt < name_len; ++cnt)
517 if (isalpha (codeset[cnt]))
518 *wp++ = tolower (codeset[cnt]);
519 else if (isdigit (codeset[cnt]))
520 *wp++ = codeset[cnt];
522 *wp = '\0';
525 return (const char *) retval;
529 struct localedef_t *
530 add_to_readlist (int category, const char *name, const char *repertoire_name,
531 int generate, struct localedef_t *copy_locale)
533 struct localedef_t *runp = locales;
535 while (runp != NULL && strcmp (name, runp->name) != 0)
536 runp = runp->next;
538 if (runp == NULL)
540 /* Add a new entry at the end. */
541 struct localedef_t *newp;
543 assert (generate == 1);
545 newp = xcalloc (1, sizeof (struct localedef_t));
546 newp->name = name;
547 newp->repertoire_name = repertoire_name;
549 if (locales == NULL)
550 runp = locales = newp;
551 else
553 runp = locales;
554 while (runp->next != NULL)
555 runp = runp->next;
556 runp = runp->next = newp;
560 if (generate
561 && (runp->needed & (1 << category)) != 0
562 && (runp->avail & (1 << category)) == 0)
563 WITH_CUR_LOCALE (error (5, 0, _("\
564 circular dependencies between locale definitions")));
566 if (copy_locale != NULL)
568 if (runp->categories[category].generic != NULL)
569 WITH_CUR_LOCALE (error (5, 0, _("\
570 cannot add already read locale `%s' a second time"), name));
571 else
572 runp->categories[category].generic =
573 copy_locale->categories[category].generic;
576 runp->needed |= 1 << category;
578 return runp;
582 struct localedef_t *
583 find_locale (int category, const char *name, const char *repertoire_name,
584 const struct charmap_t *charmap)
586 struct localedef_t *result;
588 /* Find the locale, but do not generate it since this would be a bug. */
589 result = add_to_readlist (category, name, repertoire_name, 0, NULL);
591 assert (result != NULL);
593 if ((result->avail & (1 << category)) == 0
594 && locfile_read (result, charmap) != 0)
595 WITH_CUR_LOCALE (error (4, errno, _("\
596 cannot open locale definition file `%s'"), result->name));
598 return result;
602 struct localedef_t *
603 load_locale (int category, const char *name, const char *repertoire_name,
604 const struct charmap_t *charmap, struct localedef_t *copy_locale)
606 struct localedef_t *result;
608 /* Generate the locale if it does not exist. */
609 result = add_to_readlist (category, name, repertoire_name, 1, copy_locale);
611 assert (result != NULL);
613 if ((result->avail & (1 << category)) == 0
614 && locfile_read (result, charmap) != 0)
615 WITH_CUR_LOCALE (error (4, errno, _("\
616 cannot open locale definition file `%s'"), result->name));
618 return result;
621 static void
622 turn_on_mcheck (void)
624 /* Enable `malloc' debugging. */
625 mcheck (NULL);
626 /* Use the following line for a more thorough but much slower testing. */
627 /* mcheck_pedantic (NULL); */
630 void (*__malloc_initialize_hook) (void) = turn_on_mcheck;