1 | /* Copyright (C) 1995-2016 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 | #define OPT_LITTLE_ENDIAN 400 |
116 | #define OPT_BIG_ENDIAN 401 |
117 | |
118 | /* Definitions of arguments for argp functions. */ |
119 | static const struct argp_option options[] = |
120 | { |
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" ) }, |
128 | |
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 } |
154 | }; |
155 | |
156 | /* Short description of program. */ |
157 | static const char doc[] = N_("Compile locale specification" ); |
158 | |
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]" ); |
164 | |
165 | /* Prototype for option handler. */ |
166 | static error_t parse_opt (int key, char *arg, struct argp_state *state); |
167 | |
168 | /* Function to print some extra text in the help message. */ |
169 | static char *more_help (int key, const char *text, void *input); |
170 | |
171 | /* Data structure to communicate with argp functions. */ |
172 | static struct argp argp = |
173 | { |
174 | options, parse_opt, args_doc, doc, NULL, more_help |
175 | }; |
176 | |
177 | |
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); |
182 | |
183 | |
184 | int |
185 | main (int argc, char *argv[]) |
186 | { |
187 | const char *output_path; |
188 | int cannot_write_why; |
189 | struct charmap_t *charmap; |
190 | struct localedef_t global; |
191 | int remaining; |
192 | |
193 | /* Set initial values for global variables. */ |
194 | copy_list = NULL; |
195 | posix_conformance = getenv ("POSIXLY_CORRECT" ) != NULL; |
196 | error_print_progname = error_print; |
197 | |
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, "" ); |
202 | |
203 | /* Initialize the message catalog. */ |
204 | textdomain (_libc_intl_domainname); |
205 | |
206 | /* Parse and process arguments. */ |
207 | argp_err_exit_status = 4; |
208 | argp_parse (&argp, argc, argv, 0, &remaining, NULL); |
209 | |
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]); |
218 | |
219 | /* POSIX.2 requires to be verbose about missing characters in the |
220 | character map. */ |
221 | verbose |= posix_conformance; |
222 | |
223 | if (argc - remaining != 1) |
224 | { |
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); |
229 | } |
230 | |
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; |
237 | |
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" ); |
241 | |
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'" ))); |
248 | |
249 | /* Process charmap file. */ |
250 | charmap = charmap_read (charmap_file, verbose, 1, be_quiet, 1); |
251 | |
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; |
257 | |
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)); |
262 | |
263 | /* Perhaps we saw some `copy' instructions. */ |
264 | while (1) |
265 | { |
266 | struct localedef_t *runp = locales; |
267 | |
268 | while (runp != NULL && (runp->needed & runp->avail) == runp->needed) |
269 | runp = runp->next; |
270 | |
271 | if (runp == NULL) |
272 | /* Everything read. */ |
273 | break; |
274 | |
275 | if (locfile_read (runp, charmap) != 0) |
276 | WITH_CUR_LOCALE (error (4, errno, _("\ |
277 | cannot open locale definition file `%s'" ), runp->name)); |
278 | } |
279 | |
280 | /* Check the categories we processed in source form. */ |
281 | check_all_categories (locales, charmap); |
282 | |
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) |
286 | { |
287 | if (cannot_write_why != 0) |
288 | WITH_CUR_LOCALE (error (4, cannot_write_why, _("\ |
289 | cannot write output files to `%s'" ), output_path ? : argv[remaining])); |
290 | else |
291 | write_all_categories (locales, charmap, argv[remaining], output_path); |
292 | } |
293 | else |
294 | WITH_CUR_LOCALE (error (4, 0, _("\ |
295 | no output file produced because warnings were issued" ))); |
296 | |
297 | /* This exit status is prescribed by POSIX.2 4.35.7. */ |
298 | exit (error_message_count != 0); |
299 | } |
300 | |
301 | |
302 | /* Handle program arguments. */ |
303 | static error_t |
304 | parse_opt (int key, char *arg, struct argp_state *state) |
305 | { |
306 | switch (key) |
307 | { |
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; |
361 | } |
362 | return 0; |
363 | } |
364 | |
365 | |
366 | static char * |
367 | more_help (int key, const char *text, void *input) |
368 | { |
369 | char *cp; |
370 | char *tp; |
371 | |
372 | switch (key) |
373 | { |
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) |
386 | { |
387 | free (tp); |
388 | return NULL; |
389 | } |
390 | return cp; |
391 | default: |
392 | break; |
393 | } |
394 | return (char *) text; |
395 | } |
396 | |
397 | /* Print the version information. */ |
398 | static void |
399 | print_version (FILE *stream, struct argp_state *state) |
400 | { |
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 | " ), "2016" ); |
407 | fprintf (stream, gettext ("Written by %s.\n" ), "Ulrich Drepper" ); |
408 | } |
409 | |
410 | |
411 | /* The address of this function will be assigned to the hook in the error |
412 | functions. */ |
413 | static void |
414 | error_print (void) |
415 | { |
416 | } |
417 | |
418 | |
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) |
424 | { |
425 | const char *normal = NULL; |
426 | char *result; |
427 | char *endp; |
428 | |
429 | if (strchr (path, '/') == NULL) |
430 | { |
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; |
435 | |
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 == '.') |
442 | { |
443 | /* We found a codeset specification. Now find the end. */ |
444 | endp = ++startp; |
445 | while (*endp != '\0' && *endp != '@') |
446 | ++endp; |
447 | |
448 | if (endp > startp) |
449 | normal = normalize_codeset (startp, endp - startp); |
450 | } |
451 | else |
452 | /* This is to keep gcc quiet. */ |
453 | endp = NULL; |
454 | |
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" , output_prefix ?: "" , |
461 | COMPLOCALEDIR, path, '\0'); |
462 | else |
463 | n = asprintf (&result, "%s%s/%.*s%s%s%c" , |
464 | output_prefix ?: "" , COMPLOCALEDIR, |
465 | (int) (startp - path), path, normal, endp, '\0'); |
466 | |
467 | if (n < 0) |
468 | return NULL; |
469 | |
470 | endp = result + n - 1; |
471 | } |
472 | else |
473 | { |
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; |
479 | |
480 | /* If the user specified an output path we cannot add the output |
481 | to the archive. */ |
482 | no_archive = true; |
483 | } |
484 | |
485 | errno = 0; |
486 | |
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) |
490 | { |
491 | errno = 0; |
492 | if (mkdir (result, 0777) < 0) |
493 | return NULL; |
494 | } |
495 | |
496 | *endp++ = '/'; |
497 | *endp = '\0'; |
498 | |
499 | return result; |
500 | } |
501 | |
502 | |
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 (const char *codeset, size_t name_len) |
508 | { |
509 | int len = 0; |
510 | int only_digit = 1; |
511 | char *retval; |
512 | char *wp; |
513 | size_t cnt; |
514 | |
515 | for (cnt = 0; cnt < name_len; ++cnt) |
516 | if (isalnum (codeset[cnt])) |
517 | { |
518 | ++len; |
519 | |
520 | if (isalpha (codeset[cnt])) |
521 | only_digit = 0; |
522 | } |
523 | |
524 | retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1); |
525 | |
526 | if (retval != NULL) |
527 | { |
528 | if (only_digit) |
529 | wp = stpcpy (retval, "iso" ); |
530 | else |
531 | wp = retval; |
532 | |
533 | for (cnt = 0; cnt < name_len; ++cnt) |
534 | if (isalpha (codeset[cnt])) |
535 | *wp++ = tolower (codeset[cnt]); |
536 | else if (isdigit (codeset[cnt])) |
537 | *wp++ = codeset[cnt]; |
538 | |
539 | *wp = '\0'; |
540 | } |
541 | |
542 | return (const char *) retval; |
543 | } |
544 | |
545 | |
546 | struct localedef_t * |
547 | add_to_readlist (int category, const char *name, const char *repertoire_name, |
548 | int generate, struct localedef_t *copy_locale) |
549 | { |
550 | struct localedef_t *runp = locales; |
551 | |
552 | while (runp != NULL && strcmp (name, runp->name) != 0) |
553 | runp = runp->next; |
554 | |
555 | if (runp == NULL) |
556 | { |
557 | /* Add a new entry at the end. */ |
558 | struct localedef_t *newp; |
559 | |
560 | assert (generate == 1); |
561 | |
562 | newp = xcalloc (1, sizeof (struct localedef_t)); |
563 | newp->name = name; |
564 | newp->repertoire_name = repertoire_name; |
565 | |
566 | if (locales == NULL) |
567 | runp = locales = newp; |
568 | else |
569 | { |
570 | runp = locales; |
571 | while (runp->next != NULL) |
572 | runp = runp->next; |
573 | runp = runp->next = newp; |
574 | } |
575 | } |
576 | |
577 | if (generate |
578 | && (runp->needed & (1 << category)) != 0 |
579 | && (runp->avail & (1 << category)) == 0) |
580 | WITH_CUR_LOCALE (error (5, 0, _("\ |
581 | circular dependencies between locale definitions" ))); |
582 | |
583 | if (copy_locale != NULL) |
584 | { |
585 | if (runp->categories[category].generic != NULL) |
586 | WITH_CUR_LOCALE (error (5, 0, _("\ |
587 | cannot add already read locale `%s' a second time" ), name)); |
588 | else |
589 | runp->categories[category].generic = |
590 | copy_locale->categories[category].generic; |
591 | } |
592 | |
593 | runp->needed |= 1 << category; |
594 | |
595 | return runp; |
596 | } |
597 | |
598 | |
599 | struct localedef_t * |
600 | find_locale (int category, const char *name, const char *repertoire_name, |
601 | const struct charmap_t *charmap) |
602 | { |
603 | struct localedef_t *result; |
604 | |
605 | /* Find the locale, but do not generate it since this would be a bug. */ |
606 | result = add_to_readlist (category, name, repertoire_name, 0, NULL); |
607 | |
608 | assert (result != NULL); |
609 | |
610 | if ((result->avail & (1 << category)) == 0 |
611 | && locfile_read (result, charmap) != 0) |
612 | WITH_CUR_LOCALE (error (4, errno, _("\ |
613 | cannot open locale definition file `%s'" ), result->name)); |
614 | |
615 | return result; |
616 | } |
617 | |
618 | |
619 | struct localedef_t * |
620 | load_locale (int category, const char *name, const char *repertoire_name, |
621 | const struct charmap_t *charmap, struct localedef_t *copy_locale) |
622 | { |
623 | struct localedef_t *result; |
624 | |
625 | /* Generate the locale if it does not exist. */ |
626 | result = add_to_readlist (category, name, repertoire_name, 1, copy_locale); |
627 | |
628 | assert (result != NULL); |
629 | |
630 | if ((result->avail & (1 << category)) == 0 |
631 | && locfile_read (result, charmap) != 0) |
632 | WITH_CUR_LOCALE (error (4, errno, _("\ |
633 | cannot open locale definition file `%s'" ), result->name)); |
634 | |
635 | return result; |
636 | } |
637 | |
638 | static void |
639 | turn_on_mcheck (void) |
640 | { |
641 | /* Enable `malloc' debugging. */ |
642 | mcheck (NULL); |
643 | /* Use the following line for a more thorough but much slower testing. */ |
644 | /* mcheck_pedantic (NULL); */ |
645 | } |
646 | |
647 | void (*__malloc_initialize_hook) (void) = turn_on_mcheck; |
648 | |