Update copyright notices with scripts/update-copyrights
[glibc.git] / locale / programs / localedef.c
bloba275769b9b90e1d2e47e391b330df5d34479c426
1 /* Copyright (C) 1995-2014 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
115 #define OPT_LITTLE_ENDIAN 400
116 #define OPT_BIG_ENDIAN 401
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', N_("FILE"), 0,
123 N_("Symbolic character names defined in FILE") },
124 { "inputfile", 'i', N_("FILE"), 0,
125 N_("Source definitions are found in FILE") },
126 { "repertoire-map", 'u', N_("FILE"), 0,
127 N_("FILE contains mapping from symbolic names to UCS4 values") },
129 { NULL, 0, NULL, 0, N_("Output control:") },
130 { "force", 'c', NULL, 0,
131 N_("Create output even if warning messages were issued") },
132 { "old-style", OPT_OLDSTYLE, NULL, 0, N_("Create old-style tables") },
133 { "prefix", OPT_PREFIX, N_("PATH"), 0, N_("Optional output file prefix") },
134 { "posix", OPT_POSIX, NULL, 0, N_("Strictly conform to POSIX") },
135 { "quiet", OPT_QUIET, NULL, 0,
136 N_("Suppress warnings and information messages") },
137 { "verbose", 'v', NULL, 0, N_("Print more messages") },
138 { NULL, 0, NULL, 0, N_("Archive control:") },
139 { "no-archive", OPT_NO_ARCHIVE, NULL, 0,
140 N_("Don't add new data to archive") },
141 { "add-to-archive", OPT_ADD_TO_ARCHIVE, NULL, 0,
142 N_("Add locales named by parameters to archive") },
143 { "replace", OPT_REPLACE, NULL, 0, N_("Replace existing archive content") },
144 { "delete-from-archive", OPT_DELETE_FROM_ARCHIVE, NULL, 0,
145 N_("Remove locales named by parameters from archive") },
146 { "list-archive", OPT_LIST_ARCHIVE, NULL, 0, N_("List content of archive") },
147 { "alias-file", 'A', N_("FILE"), 0,
148 N_("locale.alias file to consult when making archive")},
149 { "little-endian", OPT_LITTLE_ENDIAN, NULL, 0,
150 N_("Generate little-endian output") },
151 { "big-endian", OPT_BIG_ENDIAN, NULL, 0,
152 N_("Generate big-endian output") },
153 { NULL, 0, NULL, 0, NULL }
156 /* Short description of program. */
157 static const char doc[] = N_("Compile locale specification");
159 /* Strings for arguments in help texts. */
160 static const char args_doc[] = N_("\
161 NAME\n\
162 [--add-to-archive|--delete-from-archive] FILE...\n\
163 --list-archive [FILE]");
165 /* Prototype for option handler. */
166 static error_t parse_opt (int key, char *arg, struct argp_state *state);
168 /* Function to print some extra text in the help message. */
169 static char *more_help (int key, const char *text, void *input);
171 /* Data structure to communicate with argp functions. */
172 static struct argp argp =
174 options, parse_opt, args_doc, doc, NULL, more_help
178 /* Prototypes for local functions. */
179 static void error_print (void);
180 static const char *construct_output_path (char *path);
181 static const char *normalize_codeset (const char *codeset, size_t name_len);
185 main (int argc, char *argv[])
187 const char *output_path;
188 int cannot_write_why;
189 struct charmap_t *charmap;
190 struct localedef_t global;
191 int remaining;
193 /* Set initial values for global variables. */
194 copy_list = NULL;
195 posix_conformance = getenv ("POSIXLY_CORRECT") != NULL;
196 error_print_progname = error_print;
198 /* Set locale. Do not set LC_ALL because the other categories must
199 not be affected (according to POSIX.2). */
200 setlocale (LC_MESSAGES, "");
201 setlocale (LC_CTYPE, "");
203 /* Initialize the message catalog. */
204 textdomain (_libc_intl_domainname);
206 /* Parse and process arguments. */
207 argp_err_exit_status = 4;
208 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
210 /* Handle a few special cases. */
211 if (list_archive)
212 show_archive_content (remaining > 1 ? argv[remaining] : NULL, verbose);
213 if (add_to_archive)
214 return add_locales_to_archive (argc - remaining, &argv[remaining],
215 replace_archive);
216 if (delete_from_archive)
217 return delete_locales_from_archive (argc - remaining, &argv[remaining]);
219 /* POSIX.2 requires to be verbose about missing characters in the
220 character map. */
221 verbose |= posix_conformance;
223 if (argc - remaining != 1)
225 /* We need exactly one non-option parameter. */
226 argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
227 program_invocation_short_name);
228 exit (4);
231 /* The parameter describes the output path of the constructed files.
232 If the described files cannot be written return a NULL pointer. */
233 output_path = construct_output_path (argv[remaining]);
234 if (output_path == NULL && ! no_archive)
235 error (4, errno, _("cannot create directory for output files"));
236 cannot_write_why = errno;
238 /* Now that the parameters are processed we have to reset the local
239 ctype locale. (P1003.2 4.35.5.2) */
240 setlocale (LC_CTYPE, "POSIX");
242 /* Look whether the system really allows locale definitions. POSIX
243 defines error code 3 for this situation so I think it must be
244 a fatal error (see P1003.2 4.35.8). */
245 if (sysconf (_SC_2_LOCALEDEF) < 0)
246 WITH_CUR_LOCALE (error (3, 0, _("\
247 FATAL: system does not define `_POSIX2_LOCALEDEF'")));
249 /* Process charmap file. */
250 charmap = charmap_read (charmap_file, verbose, 1, be_quiet, 1);
252 /* Add the first entry in the locale list. */
253 memset (&global, '\0', sizeof (struct localedef_t));
254 global.name = input_file ?: "/dev/stdin";
255 global.needed = ALL_LOCALES;
256 locales = &global;
258 /* Now read the locale file. */
259 if (locfile_read (&global, charmap) != 0)
260 WITH_CUR_LOCALE (error (4, errno, _("\
261 cannot open locale definition file `%s'"), input_file));
263 /* Perhaps we saw some `copy' instructions. */
264 while (1)
266 struct localedef_t *runp = locales;
268 while (runp != NULL && (runp->needed & runp->avail) == runp->needed)
269 runp = runp->next;
271 if (runp == NULL)
272 /* Everything read. */
273 break;
275 if (locfile_read (runp, charmap) != 0)
276 WITH_CUR_LOCALE (error (4, errno, _("\
277 cannot open locale definition file `%s'"), runp->name));
280 /* Check the categories we processed in source form. */
281 check_all_categories (locales, charmap);
283 /* We are now able to write the data files. If warning were given we
284 do it only if it is explicitly requested (--force). */
285 if (error_message_count == 0 || force_output != 0)
287 if (cannot_write_why != 0)
288 WITH_CUR_LOCALE (error (4, cannot_write_why, _("\
289 cannot write output files to `%s'"), output_path));
290 else
291 write_all_categories (locales, charmap, argv[remaining], output_path);
293 else
294 WITH_CUR_LOCALE (error (4, 0, _("\
295 no output file produced because warnings were issued")));
297 /* This exit status is prescribed by POSIX.2 4.35.7. */
298 exit (error_message_count != 0);
302 /* Handle program arguments. */
303 static error_t
304 parse_opt (int key, char *arg, struct argp_state *state)
306 switch (key)
308 case OPT_QUIET:
309 be_quiet = 1;
310 break;
311 case OPT_POSIX:
312 posix_conformance = 1;
313 break;
314 case OPT_OLDSTYLE:
315 oldstyle_tables = 1;
316 break;
317 case OPT_PREFIX:
318 output_prefix = arg;
319 break;
320 case OPT_NO_ARCHIVE:
321 no_archive = true;
322 break;
323 case OPT_ADD_TO_ARCHIVE:
324 add_to_archive = true;
325 break;
326 case OPT_REPLACE:
327 replace_archive = true;
328 break;
329 case OPT_DELETE_FROM_ARCHIVE:
330 delete_from_archive = true;
331 break;
332 case OPT_LIST_ARCHIVE:
333 list_archive = true;
334 break;
335 case OPT_LITTLE_ENDIAN:
336 set_big_endian (false);
337 break;
338 case OPT_BIG_ENDIAN:
339 set_big_endian (true);
340 break;
341 case 'c':
342 force_output = 1;
343 break;
344 case 'f':
345 charmap_file = arg;
346 break;
347 case 'A':
348 alias_file = arg;
349 break;
350 case 'i':
351 input_file = arg;
352 break;
353 case 'u':
354 repertoire_global = arg;
355 break;
356 case 'v':
357 verbose = 1;
358 break;
359 default:
360 return ARGP_ERR_UNKNOWN;
362 return 0;
366 static char *
367 more_help (int key, const char *text, void *input)
369 char *cp;
370 char *tp;
372 switch (key)
374 case ARGP_KEY_HELP_EXTRA:
375 /* We print some extra information. */
376 if (asprintf (&tp, gettext ("\
377 For bug reporting instructions, please see:\n\
378 %s.\n"), REPORT_BUGS_TO) < 0)
379 return NULL;
380 if (asprintf (&cp, gettext ("\
381 System's directory for character maps : %s\n\
382 repertoire maps: %s\n\
383 locale path : %s\n\
384 %s"),
385 CHARMAP_PATH, REPERTOIREMAP_PATH, LOCALE_PATH, tp) < 0)
387 free (tp);
388 return NULL;
390 return cp;
391 default:
392 break;
394 return (char *) text;
397 /* Print the version information. */
398 static void
399 print_version (FILE *stream, struct argp_state *state)
401 fprintf (stream, "localedef %s%s\n", PKGVERSION, VERSION);
402 fprintf (stream, gettext ("\
403 Copyright (C) %s Free Software Foundation, Inc.\n\
404 This is free software; see the source for copying conditions. There is NO\n\
405 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
406 "), "2013");
407 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
411 /* The address of this function will be assigned to the hook in the error
412 functions. */
413 static void
414 error_print (void)
419 /* The parameter to localedef describes the output path. If it does
420 contain a '/' character it is a relative path. Otherwise it names the
421 locale this definition is for. */
422 static const char *
423 construct_output_path (char *path)
425 const char *normal = NULL;
426 char *result;
427 char *endp;
429 if (strchr (path, '/') == NULL)
431 /* This is a system path. First examine whether the locale name
432 contains a reference to the codeset. This should be
433 normalized. */
434 char *startp;
436 startp = path;
437 /* We must be prepared for finding a CEN name or a location of
438 the introducing `.' where it is not possible anymore. */
439 while (*startp != '\0' && *startp != '@' && *startp != '.')
440 ++startp;
441 if (*startp == '.')
443 /* We found a codeset specification. Now find the end. */
444 endp = ++startp;
445 while (*endp != '\0' && *endp != '@')
446 ++endp;
448 if (endp > startp)
449 normal = normalize_codeset (startp, endp - startp);
451 else
452 /* This is to keep gcc quiet. */
453 endp = NULL;
455 /* We put an additional '\0' at the end of the string because at
456 the end of the function we need another byte for the trailing
457 '/'. */
458 ssize_t n;
459 if (normal == NULL)
460 n = asprintf (&result, "%s%s/%s%c",
461 output_prefix ?: "", LOCALEDIR, path, '\0');
462 else
463 n = asprintf (&result, "%s%s/%.*s%s%s%c",
464 output_prefix ?: "", LOCALEDIR,
465 (int) (startp - path), path, normal, endp, '\0');
467 if (n < 0)
468 return NULL;
470 endp = result + n - 1;
472 else
474 /* This is a user path. Please note the additional byte in the
475 memory allocation. */
476 size_t len = strlen (path) + 1;
477 result = xmalloc (len + 1);
478 endp = mempcpy (result, path, len) - 1;
480 /* If the user specified an output path we cannot add the output
481 to the archive. */
482 no_archive = true;
485 errno = 0;
487 if (no_archive && euidaccess (result, W_OK) == -1)
488 /* Perhaps the directory does not exist now. Try to create it. */
489 if (errno == ENOENT)
491 errno = 0;
492 if (mkdir (result, 0777) < 0)
493 return NULL;
496 *endp++ = '/';
497 *endp = '\0';
499 return result;
503 /* Normalize codeset name. There is no standard for the codeset
504 names. Normalization allows the user to use any of the common
505 names. */
506 static const char *
507 normalize_codeset (codeset, name_len)
508 const char *codeset;
509 size_t name_len;
511 int len = 0;
512 int only_digit = 1;
513 char *retval;
514 char *wp;
515 size_t cnt;
517 for (cnt = 0; cnt < name_len; ++cnt)
518 if (isalnum (codeset[cnt]))
520 ++len;
522 if (isalpha (codeset[cnt]))
523 only_digit = 0;
526 retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
528 if (retval != NULL)
530 if (only_digit)
531 wp = stpcpy (retval, "iso");
532 else
533 wp = retval;
535 for (cnt = 0; cnt < name_len; ++cnt)
536 if (isalpha (codeset[cnt]))
537 *wp++ = tolower (codeset[cnt]);
538 else if (isdigit (codeset[cnt]))
539 *wp++ = codeset[cnt];
541 *wp = '\0';
544 return (const char *) retval;
548 struct localedef_t *
549 add_to_readlist (int category, const char *name, const char *repertoire_name,
550 int generate, struct localedef_t *copy_locale)
552 struct localedef_t *runp = locales;
554 while (runp != NULL && strcmp (name, runp->name) != 0)
555 runp = runp->next;
557 if (runp == NULL)
559 /* Add a new entry at the end. */
560 struct localedef_t *newp;
562 assert (generate == 1);
564 newp = xcalloc (1, sizeof (struct localedef_t));
565 newp->name = name;
566 newp->repertoire_name = repertoire_name;
568 if (locales == NULL)
569 runp = locales = newp;
570 else
572 runp = locales;
573 while (runp->next != NULL)
574 runp = runp->next;
575 runp = runp->next = newp;
579 if (generate
580 && (runp->needed & (1 << category)) != 0
581 && (runp->avail & (1 << category)) == 0)
582 WITH_CUR_LOCALE (error (5, 0, _("\
583 circular dependencies between locale definitions")));
585 if (copy_locale != NULL)
587 if (runp->categories[category].generic != NULL)
588 WITH_CUR_LOCALE (error (5, 0, _("\
589 cannot add already read locale `%s' a second time"), name));
590 else
591 runp->categories[category].generic =
592 copy_locale->categories[category].generic;
595 runp->needed |= 1 << category;
597 return runp;
601 struct localedef_t *
602 find_locale (int category, const char *name, const char *repertoire_name,
603 const struct charmap_t *charmap)
605 struct localedef_t *result;
607 /* Find the locale, but do not generate it since this would be a bug. */
608 result = add_to_readlist (category, name, repertoire_name, 0, NULL);
610 assert (result != NULL);
612 if ((result->avail & (1 << category)) == 0
613 && locfile_read (result, charmap) != 0)
614 WITH_CUR_LOCALE (error (4, errno, _("\
615 cannot open locale definition file `%s'"), result->name));
617 return result;
621 struct localedef_t *
622 load_locale (int category, const char *name, const char *repertoire_name,
623 const struct charmap_t *charmap, struct localedef_t *copy_locale)
625 struct localedef_t *result;
627 /* Generate the locale if it does not exist. */
628 result = add_to_readlist (category, name, repertoire_name, 1, copy_locale);
630 assert (result != NULL);
632 if ((result->avail & (1 << category)) == 0
633 && locfile_read (result, charmap) != 0)
634 WITH_CUR_LOCALE (error (4, errno, _("\
635 cannot open locale definition file `%s'"), result->name));
637 return result;
640 static void
641 turn_on_mcheck (void)
643 /* Enable `malloc' debugging. */
644 mcheck (NULL);
645 /* Use the following line for a more thorough but much slower testing. */
646 /* mcheck_pedantic (NULL); */
649 void (*__malloc_initialize_hook) (void) = turn_on_mcheck;