1 | /* Copyright (C) 1995-2017 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 <stdbool.h> |
28 | #include <stdio.h> |
29 | #include <stdlib.h> |
30 | #include <string.h> |
31 | #include <unistd.h> |
32 | #include <error.h> |
33 | #include <sys/mman.h> |
34 | #include <sys/stat.h> |
35 | |
36 | #include "localedef.h" |
37 | #include "charmap.h" |
38 | #include "locfile.h" |
39 | |
40 | /* Undefine the following line in the production version. */ |
41 | /* #define NDEBUG 1 */ |
42 | #include <assert.h> |
43 | |
44 | |
45 | /* List of copied locales. */ |
46 | struct copy_def_list_t *copy_list; |
47 | |
48 | /* If this is defined be POSIX conform. */ |
49 | int posix_conformance; |
50 | |
51 | /* If not zero give a lot more messages. */ |
52 | int verbose; |
53 | |
54 | /* If not zero suppress warnings and information messages. */ |
55 | int be_quiet; |
56 | |
57 | /* If not zero force output even if warning were issued. */ |
58 | static int force_output; |
59 | |
60 | /* Prefix for output files. */ |
61 | const char *output_prefix; |
62 | |
63 | /* Name of the character map file. */ |
64 | static const char *charmap_file; |
65 | |
66 | /* Name of the locale definition file. */ |
67 | static const char *input_file; |
68 | |
69 | /* Name of the repertoire map file. */ |
70 | const char *repertoire_global; |
71 | |
72 | /* Name of the locale.alias file. */ |
73 | const char *alias_file; |
74 | |
75 | /* List of all locales. */ |
76 | static struct localedef_t *locales; |
77 | |
78 | /* If true don't add locale data to archive. */ |
79 | bool no_archive; |
80 | |
81 | /* If true add named locales to archive. */ |
82 | static bool add_to_archive; |
83 | |
84 | /* If true delete named locales from archive. */ |
85 | static bool delete_from_archive; |
86 | |
87 | /* If true replace archive content when adding. */ |
88 | static bool replace_archive; |
89 | |
90 | /* If true list archive content. */ |
91 | static bool list_archive; |
92 | |
93 | /* Maximum number of retries when opening the locale archive. */ |
94 | int max_locarchive_open_retry = 10; |
95 | |
96 | |
97 | /* Name and version of program. */ |
98 | static void print_version (FILE *stream, struct argp_state *state); |
99 | void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version; |
100 | |
101 | #define OPT_POSIX 301 |
102 | #define OPT_QUIET 302 |
103 | #define OPT_PREFIX 304 |
104 | #define OPT_NO_ARCHIVE 305 |
105 | #define OPT_ADD_TO_ARCHIVE 306 |
106 | #define OPT_REPLACE 307 |
107 | #define OPT_DELETE_FROM_ARCHIVE 308 |
108 | #define OPT_LIST_ARCHIVE 309 |
109 | #define OPT_LITTLE_ENDIAN 400 |
110 | #define OPT_BIG_ENDIAN 401 |
111 | |
112 | /* Definitions of arguments for argp functions. */ |
113 | static const struct argp_option options[] = |
114 | { |
115 | { NULL, 0, NULL, 0, N_("Input Files:" ) }, |
116 | { "charmap" , 'f', N_("FILE" ), 0, |
117 | N_("Symbolic character names defined in FILE" ) }, |
118 | { "inputfile" , 'i', N_("FILE" ), 0, |
119 | N_("Source definitions are found in FILE" ) }, |
120 | { "repertoire-map" , 'u', N_("FILE" ), 0, |
121 | N_("FILE contains mapping from symbolic names to UCS4 values" ) }, |
122 | |
123 | { NULL, 0, NULL, 0, N_("Output control:" ) }, |
124 | { "force" , 'c', NULL, 0, |
125 | N_("Create output even if warning messages were issued" ) }, |
126 | { "prefix" , OPT_PREFIX, N_("PATH" ), 0, N_("Optional output file prefix" ) }, |
127 | { "posix" , OPT_POSIX, NULL, 0, N_("Strictly conform to POSIX" ) }, |
128 | { "quiet" , OPT_QUIET, NULL, 0, |
129 | N_("Suppress warnings and information messages" ) }, |
130 | { "verbose" , 'v', NULL, 0, N_("Print more messages" ) }, |
131 | { NULL, 0, NULL, 0, N_("Archive control:" ) }, |
132 | { "no-archive" , OPT_NO_ARCHIVE, NULL, 0, |
133 | N_("Don't add new data to archive" ) }, |
134 | { "add-to-archive" , OPT_ADD_TO_ARCHIVE, NULL, 0, |
135 | N_("Add locales named by parameters to archive" ) }, |
136 | { "replace" , OPT_REPLACE, NULL, 0, N_("Replace existing archive content" ) }, |
137 | { "delete-from-archive" , OPT_DELETE_FROM_ARCHIVE, NULL, 0, |
138 | N_("Remove locales named by parameters from archive" ) }, |
139 | { "list-archive" , OPT_LIST_ARCHIVE, NULL, 0, N_("List content of archive" ) }, |
140 | { "alias-file" , 'A', N_("FILE" ), 0, |
141 | N_("locale.alias file to consult when making archive" )}, |
142 | { "little-endian" , OPT_LITTLE_ENDIAN, NULL, 0, |
143 | N_("Generate little-endian output" ) }, |
144 | { "big-endian" , OPT_BIG_ENDIAN, NULL, 0, |
145 | N_("Generate big-endian output" ) }, |
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 (remaining > 1 ? argv[remaining] : NULL, 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 ? : argv[remaining])); |
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_PREFIX: |
308 | output_prefix = arg; |
309 | break; |
310 | case OPT_NO_ARCHIVE: |
311 | no_archive = true; |
312 | break; |
313 | case OPT_ADD_TO_ARCHIVE: |
314 | add_to_archive = true; |
315 | break; |
316 | case OPT_REPLACE: |
317 | replace_archive = true; |
318 | break; |
319 | case OPT_DELETE_FROM_ARCHIVE: |
320 | delete_from_archive = true; |
321 | break; |
322 | case OPT_LIST_ARCHIVE: |
323 | list_archive = true; |
324 | break; |
325 | case OPT_LITTLE_ENDIAN: |
326 | set_big_endian (false); |
327 | break; |
328 | case OPT_BIG_ENDIAN: |
329 | set_big_endian (true); |
330 | break; |
331 | case 'c': |
332 | force_output = 1; |
333 | break; |
334 | case 'f': |
335 | charmap_file = arg; |
336 | break; |
337 | case 'A': |
338 | alias_file = arg; |
339 | break; |
340 | case 'i': |
341 | input_file = arg; |
342 | break; |
343 | case 'u': |
344 | repertoire_global = arg; |
345 | break; |
346 | case 'v': |
347 | verbose = 1; |
348 | break; |
349 | default: |
350 | return ARGP_ERR_UNKNOWN; |
351 | } |
352 | return 0; |
353 | } |
354 | |
355 | |
356 | static char * |
357 | more_help (int key, const char *text, void *input) |
358 | { |
359 | char *cp; |
360 | char *tp; |
361 | |
362 | switch (key) |
363 | { |
364 | case ARGP_KEY_HELP_EXTRA: |
365 | /* We print some extra information. */ |
366 | if (asprintf (&tp, gettext ("\ |
367 | For bug reporting instructions, please see:\n\ |
368 | %s.\n" ), REPORT_BUGS_TO) < 0) |
369 | return NULL; |
370 | if (asprintf (&cp, gettext ("\ |
371 | System's directory for character maps : %s\n\ |
372 | repertoire maps: %s\n\ |
373 | locale path : %s\n\ |
374 | %s" ), |
375 | CHARMAP_PATH, REPERTOIREMAP_PATH, LOCALE_PATH, tp) < 0) |
376 | { |
377 | free (tp); |
378 | return NULL; |
379 | } |
380 | return cp; |
381 | default: |
382 | break; |
383 | } |
384 | return (char *) text; |
385 | } |
386 | |
387 | /* Print the version information. */ |
388 | static void |
389 | print_version (FILE *stream, struct argp_state *state) |
390 | { |
391 | fprintf (stream, "localedef %s%s\n" , PKGVERSION, VERSION); |
392 | fprintf (stream, gettext ("\ |
393 | Copyright (C) %s Free Software Foundation, Inc.\n\ |
394 | This is free software; see the source for copying conditions. There is NO\n\ |
395 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\ |
396 | " ), "2017" ); |
397 | fprintf (stream, gettext ("Written by %s.\n" ), "Ulrich Drepper" ); |
398 | } |
399 | |
400 | |
401 | /* The address of this function will be assigned to the hook in the error |
402 | functions. */ |
403 | static void |
404 | error_print (void) |
405 | { |
406 | } |
407 | |
408 | |
409 | /* The parameter to localedef describes the output path. If it does |
410 | contain a '/' character it is a relative path. Otherwise it names the |
411 | locale this definition is for. */ |
412 | static const char * |
413 | construct_output_path (char *path) |
414 | { |
415 | const char *normal = NULL; |
416 | char *result; |
417 | char *endp; |
418 | |
419 | if (strchr (path, '/') == NULL) |
420 | { |
421 | /* This is a system path. First examine whether the locale name |
422 | contains a reference to the codeset. This should be |
423 | normalized. */ |
424 | char *startp; |
425 | |
426 | startp = path; |
427 | /* We must be prepared for finding a CEN name or a location of |
428 | the introducing `.' where it is not possible anymore. */ |
429 | while (*startp != '\0' && *startp != '@' && *startp != '.') |
430 | ++startp; |
431 | if (*startp == '.') |
432 | { |
433 | /* We found a codeset specification. Now find the end. */ |
434 | endp = ++startp; |
435 | while (*endp != '\0' && *endp != '@') |
436 | ++endp; |
437 | |
438 | if (endp > startp) |
439 | normal = normalize_codeset (startp, endp - startp); |
440 | } |
441 | else |
442 | /* This is to keep gcc quiet. */ |
443 | endp = NULL; |
444 | |
445 | /* We put an additional '\0' at the end of the string because at |
446 | the end of the function we need another byte for the trailing |
447 | '/'. */ |
448 | ssize_t n; |
449 | if (normal == NULL) |
450 | n = asprintf (&result, "%s%s/%s%c" , output_prefix ?: "" , |
451 | COMPLOCALEDIR, path, '\0'); |
452 | else |
453 | n = asprintf (&result, "%s%s/%.*s%s%s%c" , |
454 | output_prefix ?: "" , COMPLOCALEDIR, |
455 | (int) (startp - path), path, normal, endp, '\0'); |
456 | |
457 | if (n < 0) |
458 | return NULL; |
459 | |
460 | endp = result + n - 1; |
461 | } |
462 | else |
463 | { |
464 | /* This is a user path. Please note the additional byte in the |
465 | memory allocation. */ |
466 | size_t len = strlen (path) + 1; |
467 | result = xmalloc (len + 1); |
468 | endp = mempcpy (result, path, len) - 1; |
469 | |
470 | /* If the user specified an output path we cannot add the output |
471 | to the archive. */ |
472 | no_archive = true; |
473 | } |
474 | |
475 | errno = 0; |
476 | |
477 | if (no_archive && euidaccess (result, W_OK) == -1) |
478 | /* Perhaps the directory does not exist now. Try to create it. */ |
479 | if (errno == ENOENT) |
480 | { |
481 | errno = 0; |
482 | if (mkdir (result, 0777) < 0) |
483 | return NULL; |
484 | } |
485 | |
486 | *endp++ = '/'; |
487 | *endp = '\0'; |
488 | |
489 | return result; |
490 | } |
491 | |
492 | |
493 | /* Normalize codeset name. There is no standard for the codeset |
494 | names. Normalization allows the user to use any of the common |
495 | names. */ |
496 | static const char * |
497 | normalize_codeset (const char *codeset, size_t name_len) |
498 | { |
499 | int len = 0; |
500 | int only_digit = 1; |
501 | char *retval; |
502 | char *wp; |
503 | size_t cnt; |
504 | |
505 | for (cnt = 0; cnt < name_len; ++cnt) |
506 | if (isalnum (codeset[cnt])) |
507 | { |
508 | ++len; |
509 | |
510 | if (isalpha (codeset[cnt])) |
511 | only_digit = 0; |
512 | } |
513 | |
514 | retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1); |
515 | |
516 | if (retval != NULL) |
517 | { |
518 | if (only_digit) |
519 | wp = stpcpy (retval, "iso" ); |
520 | else |
521 | wp = retval; |
522 | |
523 | for (cnt = 0; cnt < name_len; ++cnt) |
524 | if (isalpha (codeset[cnt])) |
525 | *wp++ = tolower (codeset[cnt]); |
526 | else if (isdigit (codeset[cnt])) |
527 | *wp++ = codeset[cnt]; |
528 | |
529 | *wp = '\0'; |
530 | } |
531 | |
532 | return (const char *) retval; |
533 | } |
534 | |
535 | |
536 | struct localedef_t * |
537 | add_to_readlist (int category, const char *name, const char *repertoire_name, |
538 | int generate, struct localedef_t *copy_locale) |
539 | { |
540 | struct localedef_t *runp = locales; |
541 | |
542 | while (runp != NULL && strcmp (name, runp->name) != 0) |
543 | runp = runp->next; |
544 | |
545 | if (runp == NULL) |
546 | { |
547 | /* Add a new entry at the end. */ |
548 | struct localedef_t *newp; |
549 | |
550 | assert (generate == 1); |
551 | |
552 | newp = xcalloc (1, sizeof (struct localedef_t)); |
553 | newp->name = name; |
554 | newp->repertoire_name = repertoire_name; |
555 | |
556 | if (locales == NULL) |
557 | runp = locales = newp; |
558 | else |
559 | { |
560 | runp = locales; |
561 | while (runp->next != NULL) |
562 | runp = runp->next; |
563 | runp = runp->next = newp; |
564 | } |
565 | } |
566 | |
567 | if (generate |
568 | && (runp->needed & (1 << category)) != 0 |
569 | && (runp->avail & (1 << category)) == 0) |
570 | WITH_CUR_LOCALE (error (5, 0, _("\ |
571 | circular dependencies between locale definitions" ))); |
572 | |
573 | if (copy_locale != NULL) |
574 | { |
575 | if (runp->categories[category].generic != NULL) |
576 | WITH_CUR_LOCALE (error (5, 0, _("\ |
577 | cannot add already read locale `%s' a second time" ), name)); |
578 | else |
579 | runp->categories[category].generic = |
580 | copy_locale->categories[category].generic; |
581 | } |
582 | |
583 | runp->needed |= 1 << category; |
584 | |
585 | return runp; |
586 | } |
587 | |
588 | |
589 | struct localedef_t * |
590 | find_locale (int category, const char *name, const char *repertoire_name, |
591 | const struct charmap_t *charmap) |
592 | { |
593 | struct localedef_t *result; |
594 | |
595 | /* Find the locale, but do not generate it since this would be a bug. */ |
596 | result = add_to_readlist (category, name, repertoire_name, 0, NULL); |
597 | |
598 | assert (result != NULL); |
599 | |
600 | if ((result->avail & (1 << category)) == 0 |
601 | && locfile_read (result, charmap) != 0) |
602 | WITH_CUR_LOCALE (error (4, errno, _("\ |
603 | cannot open locale definition file `%s'" ), result->name)); |
604 | |
605 | return result; |
606 | } |
607 | |
608 | |
609 | struct localedef_t * |
610 | load_locale (int category, const char *name, const char *repertoire_name, |
611 | const struct charmap_t *charmap, struct localedef_t *copy_locale) |
612 | { |
613 | struct localedef_t *result; |
614 | |
615 | /* Generate the locale if it does not exist. */ |
616 | result = add_to_readlist (category, name, repertoire_name, 1, copy_locale); |
617 | |
618 | assert (result != NULL); |
619 | |
620 | if ((result->avail & (1 << category)) == 0 |
621 | && locfile_read (result, charmap) != 0) |
622 | WITH_CUR_LOCALE (error (4, errno, _("\ |
623 | cannot open locale definition file `%s'" ), result->name)); |
624 | |
625 | return result; |
626 | } |
627 | |