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.
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/>. */
36 #include "localedef.h"
40 /* Undefine the following line in the production version. */
41 /* #define NDEBUG 1 */
45 /* List of copied locales. */
46 struct copy_def_list_t
*copy_list
;
48 /* If this is defined be POSIX conform. */
49 int posix_conformance
;
51 /* If not zero give a lot more messages. */
54 /* If not zero suppress warnings and information messages. */
57 /* If not zero force output even if warning were issued. */
58 static int force_output
;
60 /* Prefix for output files. */
61 const char *output_prefix
;
63 /* Name of the character map file. */
64 static const char *charmap_file
;
66 /* Name of the locale definition file. */
67 static const char *input_file
;
69 /* Name of the repertoire map file. */
70 const char *repertoire_global
;
72 /* Name of the locale.alias file. */
73 const char *alias_file
;
75 /* List of all locales. */
76 static struct localedef_t
*locales
;
78 /* If true don't add locale data to archive. */
81 /* If true add named locales to archive. */
82 static bool add_to_archive
;
84 /* If true delete named locales from archive. */
85 static bool delete_from_archive
;
87 /* If true replace archive content when adding. */
88 static bool replace_archive
;
90 /* If true list archive content. */
91 static bool list_archive
;
93 /* Maximum number of retries when opening the locale archive. */
94 int max_locarchive_open_retry
= 10;
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
;
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
112 /* Definitions of arguments for argp functions. */
113 static const struct argp_option options
[] =
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") },
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
}
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_("\
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
;
186 /* Set initial values for global variables. */
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. */
205 show_archive_content (remaining
> 1 ? argv
[remaining
] : NULL
, verbose
);
207 return add_locales_to_archive (argc
- remaining
, &argv
[remaining
],
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
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
);
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
;
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. */
259 struct localedef_t
*runp
= locales
;
261 while (runp
!= NULL
&& (runp
->needed
& runp
->avail
) == runp
->needed
)
265 /* Everything read. */
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
? : argv
[remaining
]));
284 write_all_categories (locales
, charmap
, argv
[remaining
], output_path
);
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. */
297 parse_opt (int key
, char *arg
, struct argp_state
*state
)
305 posix_conformance
= 1;
313 case OPT_ADD_TO_ARCHIVE
:
314 add_to_archive
= true;
317 replace_archive
= true;
319 case OPT_DELETE_FROM_ARCHIVE
:
320 delete_from_archive
= true;
322 case OPT_LIST_ARCHIVE
:
325 case OPT_LITTLE_ENDIAN
:
326 set_big_endian (false);
329 set_big_endian (true);
344 repertoire_global
= arg
;
350 return ARGP_ERR_UNKNOWN
;
357 more_help (int key
, const char *text
, void *input
)
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)
370 if (asprintf (&cp
, gettext ("\
371 System's directory for character maps : %s\n\
372 repertoire maps: %s\n\
375 CHARMAP_PATH
, REPERTOIREMAP_PATH
, LOCALE_PATH
, tp
) < 0)
384 return (char *) text
;
387 /* Print the version information. */
389 print_version (FILE *stream
, struct argp_state
*state
)
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\
397 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
401 /* The address of this function will be assigned to the hook in the error
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. */
413 construct_output_path (char *path
)
415 const char *normal
= NULL
;
419 if (strchr (path
, '/') == NULL
)
421 /* This is a system path. First examine whether the locale name
422 contains a reference to the codeset. This should be
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
!= '.')
433 /* We found a codeset specification. Now find the end. */
435 while (*endp
!= '\0' && *endp
!= '@')
439 normal
= normalize_codeset (startp
, endp
- startp
);
442 /* This is to keep gcc quiet. */
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
450 n
= asprintf (&result
, "%s%s/%s%c", output_prefix
?: "",
451 COMPLOCALEDIR
, path
, '\0');
453 n
= asprintf (&result
, "%s%s/%.*s%s%s%c",
454 output_prefix
?: "", COMPLOCALEDIR
,
455 (int) (startp
- path
), path
, normal
, endp
, '\0');
460 endp
= result
+ n
- 1;
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;
470 /* If the user specified an output path we cannot add the output
477 if (no_archive
&& euidaccess (result
, W_OK
) == -1)
478 /* Perhaps the directory does not exist now. Try to create it. */
482 if (mkdir (result
, 0777) < 0)
493 /* Normalize codeset name. There is no standard for the codeset
494 names. Normalization allows the user to use any of the common
497 normalize_codeset (const char *codeset
, size_t name_len
)
505 for (cnt
= 0; cnt
< name_len
; ++cnt
)
506 if (isalnum (codeset
[cnt
]))
510 if (isalpha (codeset
[cnt
]))
514 retval
= (char *) malloc ((only_digit
? 3 : 0) + len
+ 1);
519 wp
= stpcpy (retval
, "iso");
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
];
532 return (const char *) retval
;
537 add_to_readlist (int category
, const char *name
, const char *repertoire_name
,
538 int generate
, struct localedef_t
*copy_locale
)
540 struct localedef_t
*runp
= locales
;
542 while (runp
!= NULL
&& strcmp (name
, runp
->name
) != 0)
547 /* Add a new entry at the end. */
548 struct localedef_t
*newp
;
550 assert (generate
== 1);
552 newp
= xcalloc (1, sizeof (struct localedef_t
));
554 newp
->repertoire_name
= repertoire_name
;
557 runp
= locales
= newp
;
561 while (runp
->next
!= NULL
)
563 runp
= runp
->next
= newp
;
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")));
573 if (copy_locale
!= NULL
)
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
));
579 runp
->categories
[category
].generic
=
580 copy_locale
->categories
[category
].generic
;
583 runp
->needed
|= 1 << category
;
590 find_locale (int category
, const char *name
, const char *repertoire_name
,
591 const struct charmap_t
*charmap
)
593 struct localedef_t
*result
;
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
);
598 assert (result
!= NULL
);
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
));
610 load_locale (int category
, const char *name
, const char *repertoire_name
,
611 const struct charmap_t
*charmap
, struct localedef_t
*copy_locale
)
613 struct localedef_t
*result
;
615 /* Generate the locale if it does not exist. */
616 result
= add_to_readlist (category
, name
, repertoire_name
, 1, copy_locale
);
618 assert (result
!= NULL
);
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
));