]> git.ipfire.org Git - thirdparty/glibc.git/blob - locale/programs/localedef.c
cc948286ab314336e93fab4687ecf11546f66d0c
[thirdparty/glibc.git] / locale / programs / localedef.c
1 /* Copyright (C) 1995-2013 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1995.
4
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.
9
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.
14
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/>. */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
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>
36
37 #include "localedef.h"
38 #include "charmap.h"
39 #include "locfile.h"
40
41 /* Undefine the following line in the production version. */
42 /* #define NDEBUG 1 */
43 #include <assert.h>
44
45
46 /* List of copied locales. */
47 struct copy_def_list_t *copy_list;
48
49 /* If this is defined be POSIX conform. */
50 int posix_conformance;
51
52 /* If not zero give a lot more messages. */
53 int verbose;
54
55 /* If not zero suppress warnings and information messages. */
56 int be_quiet;
57
58 /* If not zero, produce old-style hash table instead of 3-level access
59 tables. */
60 int oldstyle_tables;
61
62 /* If not zero force output even if warning were issued. */
63 static int force_output;
64
65 /* Prefix for output files. */
66 const char *output_prefix;
67
68 /* Name of the character map file. */
69 static const char *charmap_file;
70
71 /* Name of the locale definition file. */
72 static const char *input_file;
73
74 /* Name of the repertoire map file. */
75 const char *repertoire_global;
76
77 /* Name of the locale.alias file. */
78 const char *alias_file;
79
80 /* List of all locales. */
81 static struct localedef_t *locales;
82
83 /* If true don't add locale data to archive. */
84 bool no_archive;
85
86 /* If true add named locales to archive. */
87 static bool add_to_archive;
88
89 /* If true delete named locales from archive. */
90 static bool delete_from_archive;
91
92 /* If true replace archive content when adding. */
93 static bool replace_archive;
94
95 /* If true list archive content. */
96 static bool list_archive;
97
98 /* Maximum number of retries when opening the locale archive. */
99 int max_locarchive_open_retry = 10;
100
101
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;
105
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
116 /* Definitions of arguments for argp functions. */
117 static const struct argp_option options[] =
118 {
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") },
125
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 }
147 };
148
149 /* Short description of program. */
150 static const char doc[] = N_("Compile locale specification");
151
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]");
157
158 /* Prototype for option handler. */
159 static error_t parse_opt (int key, char *arg, struct argp_state *state);
160
161 /* Function to print some extra text in the help message. */
162 static char *more_help (int key, const char *text, void *input);
163
164 /* Data structure to communicate with argp functions. */
165 static struct argp argp =
166 {
167 options, parse_opt, args_doc, doc, NULL, more_help
168 };
169
170
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);
175
176
177 int
178 main (int argc, char *argv[])
179 {
180 const char *output_path;
181 int cannot_write_why;
182 struct charmap_t *charmap;
183 struct localedef_t global;
184 int remaining;
185
186 /* Set initial values for global variables. */
187 copy_list = NULL;
188 posix_conformance = getenv ("POSIXLY_CORRECT") != NULL;
189 error_print_progname = error_print;
190
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, "");
195
196 /* Initialize the message catalog. */
197 textdomain (_libc_intl_domainname);
198
199 /* Parse and process arguments. */
200 argp_err_exit_status = 4;
201 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
202
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]);
211
212 /* POSIX.2 requires to be verbose about missing characters in the
213 character map. */
214 verbose |= posix_conformance;
215
216 if (argc - remaining != 1)
217 {
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);
222 }
223
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;
230
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");
234
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'")));
241
242 /* Process charmap file. */
243 charmap = charmap_read (charmap_file, verbose, 1, be_quiet, 1);
244
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;
250
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));
255
256 /* Perhaps we saw some `copy' instructions. */
257 while (1)
258 {
259 struct localedef_t *runp = locales;
260
261 while (runp != NULL && (runp->needed & runp->avail) == runp->needed)
262 runp = runp->next;
263
264 if (runp == NULL)
265 /* Everything read. */
266 break;
267
268 if (locfile_read (runp, charmap) != 0)
269 WITH_CUR_LOCALE (error (4, errno, _("\
270 cannot open locale definition file `%s'"), runp->name));
271 }
272
273 /* Check the categories we processed in source form. */
274 check_all_categories (locales, charmap);
275
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)
279 {
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);
285 }
286 else
287 WITH_CUR_LOCALE (error (4, 0, _("\
288 no output file produced because warnings were issued")));
289
290 /* This exit status is prescribed by POSIX.2 4.35.7. */
291 exit (error_message_count != 0);
292 }
293
294
295 /* Handle program arguments. */
296 static error_t
297 parse_opt (int key, char *arg, struct argp_state *state)
298 {
299 switch (key)
300 {
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;
348 }
349 return 0;
350 }
351
352
353 static char *
354 more_help (int key, const char *text, void *input)
355 {
356 char *cp;
357 char *tp;
358
359 switch (key)
360 {
361 case ARGP_KEY_HELP_EXTRA:
362 /* We print some extra information. */
363 if (asprintf (&tp, gettext ("\
364 For bug reporting instructions, please see:\n\
365 %s.\n"), REPORT_BUGS_TO) < 0)
366 return NULL;
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, tp) < 0)
373 {
374 free (tp);
375 return NULL;
376 }
377 return cp;
378 default:
379 break;
380 }
381 return (char *) text;
382 }
383
384 /* Print the version information. */
385 static void
386 print_version (FILE *stream, struct argp_state *state)
387 {
388 fprintf (stream, "localedef %s%s\n", PKGVERSION, VERSION);
389 fprintf (stream, gettext ("\
390 Copyright (C) %s Free Software Foundation, Inc.\n\
391 This is free software; see the source for copying conditions. There is NO\n\
392 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
393 "), "2013");
394 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
395 }
396
397
398 /* The address of this function will be assigned to the hook in the error
399 functions. */
400 static void
401 error_print (void)
402 {
403 }
404
405
406 /* The parameter to localedef describes the output path. If it does
407 contain a '/' character it is a relative path. Otherwise it names the
408 locale this definition is for. */
409 static const char *
410 construct_output_path (char *path)
411 {
412 const char *normal = NULL;
413 char *result;
414 char *endp;
415
416 if (strchr (path, '/') == NULL)
417 {
418 /* This is a system path. First examine whether the locale name
419 contains a reference to the codeset. This should be
420 normalized. */
421 char *startp;
422
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 == '.')
429 {
430 /* We found a codeset specification. Now find the end. */
431 endp = ++startp;
432 while (*endp != '\0' && *endp != '@')
433 ++endp;
434
435 if (endp > startp)
436 normal = normalize_codeset (startp, endp - startp);
437 }
438 else
439 /* This is to keep gcc quiet. */
440 endp = NULL;
441
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 ssize_t n;
446 if (normal == NULL)
447 n = asprintf (&result, "%s%s/%s%c",
448 output_prefix ?: "", LOCALEDIR, path, '\0');
449 else
450 n = asprintf (&result, "%s%s/%.*s%s%s%c",
451 output_prefix ?: "", LOCALEDIR,
452 (int) (startp - path), path, normal, endp, '\0');
453
454 if (n < 0)
455 return NULL;
456
457 endp = result + n - 1;
458 }
459 else
460 {
461 /* This is a user path. Please note the additional byte in the
462 memory allocation. */
463 size_t len = strlen (path) + 1;
464 result = xmalloc (len + 1);
465 endp = mempcpy (result, path, len) - 1;
466
467 /* If the user specified an output path we cannot add the output
468 to the archive. */
469 no_archive = true;
470 }
471
472 errno = 0;
473
474 if (no_archive && euidaccess (result, W_OK) == -1)
475 /* Perhaps the directory does not exist now. Try to create it. */
476 if (errno == ENOENT)
477 {
478 errno = 0;
479 if (mkdir (result, 0777) < 0)
480 return NULL;
481 }
482
483 *endp++ = '/';
484 *endp = '\0';
485
486 return result;
487 }
488
489
490 /* Normalize codeset name. There is no standard for the codeset
491 names. Normalization allows the user to use any of the common
492 names. */
493 static const char *
494 normalize_codeset (codeset, name_len)
495 const char *codeset;
496 size_t name_len;
497 {
498 int len = 0;
499 int only_digit = 1;
500 char *retval;
501 char *wp;
502 size_t cnt;
503
504 for (cnt = 0; cnt < name_len; ++cnt)
505 if (isalnum (codeset[cnt]))
506 {
507 ++len;
508
509 if (isalpha (codeset[cnt]))
510 only_digit = 0;
511 }
512
513 retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
514
515 if (retval != NULL)
516 {
517 if (only_digit)
518 wp = stpcpy (retval, "iso");
519 else
520 wp = retval;
521
522 for (cnt = 0; cnt < name_len; ++cnt)
523 if (isalpha (codeset[cnt]))
524 *wp++ = tolower (codeset[cnt]);
525 else if (isdigit (codeset[cnt]))
526 *wp++ = codeset[cnt];
527
528 *wp = '\0';
529 }
530
531 return (const char *) retval;
532 }
533
534
535 struct localedef_t *
536 add_to_readlist (int category, const char *name, const char *repertoire_name,
537 int generate, struct localedef_t *copy_locale)
538 {
539 struct localedef_t *runp = locales;
540
541 while (runp != NULL && strcmp (name, runp->name) != 0)
542 runp = runp->next;
543
544 if (runp == NULL)
545 {
546 /* Add a new entry at the end. */
547 struct localedef_t *newp;
548
549 assert (generate == 1);
550
551 newp = xcalloc (1, sizeof (struct localedef_t));
552 newp->name = name;
553 newp->repertoire_name = repertoire_name;
554
555 if (locales == NULL)
556 runp = locales = newp;
557 else
558 {
559 runp = locales;
560 while (runp->next != NULL)
561 runp = runp->next;
562 runp = runp->next = newp;
563 }
564 }
565
566 if (generate
567 && (runp->needed & (1 << category)) != 0
568 && (runp->avail & (1 << category)) == 0)
569 WITH_CUR_LOCALE (error (5, 0, _("\
570 circular dependencies between locale definitions")));
571
572 if (copy_locale != NULL)
573 {
574 if (runp->categories[category].generic != NULL)
575 WITH_CUR_LOCALE (error (5, 0, _("\
576 cannot add already read locale `%s' a second time"), name));
577 else
578 runp->categories[category].generic =
579 copy_locale->categories[category].generic;
580 }
581
582 runp->needed |= 1 << category;
583
584 return runp;
585 }
586
587
588 struct localedef_t *
589 find_locale (int category, const char *name, const char *repertoire_name,
590 const struct charmap_t *charmap)
591 {
592 struct localedef_t *result;
593
594 /* Find the locale, but do not generate it since this would be a bug. */
595 result = add_to_readlist (category, name, repertoire_name, 0, NULL);
596
597 assert (result != NULL);
598
599 if ((result->avail & (1 << category)) == 0
600 && locfile_read (result, charmap) != 0)
601 WITH_CUR_LOCALE (error (4, errno, _("\
602 cannot open locale definition file `%s'"), result->name));
603
604 return result;
605 }
606
607
608 struct localedef_t *
609 load_locale (int category, const char *name, const char *repertoire_name,
610 const struct charmap_t *charmap, struct localedef_t *copy_locale)
611 {
612 struct localedef_t *result;
613
614 /* Generate the locale if it does not exist. */
615 result = add_to_readlist (category, name, repertoire_name, 1, copy_locale);
616
617 assert (result != NULL);
618
619 if ((result->avail & (1 << category)) == 0
620 && locfile_read (result, charmap) != 0)
621 WITH_CUR_LOCALE (error (4, errno, _("\
622 cannot open locale definition file `%s'"), result->name));
623
624 return result;
625 }
626
627 static void
628 turn_on_mcheck (void)
629 {
630 /* Enable `malloc' debugging. */
631 mcheck (NULL);
632 /* Use the following line for a more thorough but much slower testing. */
633 /* mcheck_pedantic (NULL); */
634 }
635
636 void (*__malloc_initialize_hook) (void) = turn_on_mcheck;