1 /* Copyright (C) 1995-2015 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/>. */
37 #include "localedef.h"
41 /* Undefine the following line in the production version. */
42 /* #define NDEBUG 1 */
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. */
55 /* If not zero suppress warnings and information messages. */
58 /* If not zero, produce old-style hash table instead of 3-level access
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. */
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_("\
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
;
193 /* Set initial values for global variables. */
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. */
212 show_archive_content (remaining
> 1 ? argv
[remaining
] : NULL
, verbose
);
214 return add_locales_to_archive (argc
- remaining
, &argv
[remaining
],
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
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
);
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
;
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. */
266 struct localedef_t
*runp
= locales
;
268 while (runp
!= NULL
&& (runp
->needed
& runp
->avail
) == runp
->needed
)
272 /* Everything read. */
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
));
291 write_all_categories (locales
, charmap
, argv
[remaining
], output_path
);
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. */
304 parse_opt (int key
, char *arg
, struct argp_state
*state
)
312 posix_conformance
= 1;
323 case OPT_ADD_TO_ARCHIVE
:
324 add_to_archive
= true;
327 replace_archive
= true;
329 case OPT_DELETE_FROM_ARCHIVE
:
330 delete_from_archive
= true;
332 case OPT_LIST_ARCHIVE
:
335 case OPT_LITTLE_ENDIAN
:
336 set_big_endian (false);
339 set_big_endian (true);
354 repertoire_global
= arg
;
360 return ARGP_ERR_UNKNOWN
;
367 more_help (int key
, const char *text
, void *input
)
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)
380 if (asprintf (&cp
, gettext ("\
381 System's directory for character maps : %s\n\
382 repertoire maps: %s\n\
385 CHARMAP_PATH
, REPERTOIREMAP_PATH
, LOCALE_PATH
, tp
) < 0)
394 return (char *) text
;
397 /* Print the version information. */
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\
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
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. */
423 construct_output_path (char *path
)
425 const char *normal
= NULL
;
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
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
!= '.')
443 /* We found a codeset specification. Now find the end. */
445 while (*endp
!= '\0' && *endp
!= '@')
449 normal
= normalize_codeset (startp
, endp
- startp
);
452 /* This is to keep gcc quiet. */
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
460 n
= asprintf (&result
, "%s%s/%s%c",
461 output_prefix
?: "", LOCALEDIR
, path
, '\0');
463 n
= asprintf (&result
, "%s%s/%.*s%s%s%c",
464 output_prefix
?: "", LOCALEDIR
,
465 (int) (startp
- path
), path
, normal
, endp
, '\0');
470 endp
= result
+ n
- 1;
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
487 if (no_archive
&& euidaccess (result
, W_OK
) == -1)
488 /* Perhaps the directory does not exist now. Try to create it. */
492 if (mkdir (result
, 0777) < 0)
503 /* Normalize codeset name. There is no standard for the codeset
504 names. Normalization allows the user to use any of the common
507 normalize_codeset (codeset
, name_len
)
517 for (cnt
= 0; cnt
< name_len
; ++cnt
)
518 if (isalnum (codeset
[cnt
]))
522 if (isalpha (codeset
[cnt
]))
526 retval
= (char *) malloc ((only_digit
? 3 : 0) + len
+ 1);
531 wp
= stpcpy (retval
, "iso");
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
];
544 return (const char *) retval
;
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)
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
));
566 newp
->repertoire_name
= repertoire_name
;
569 runp
= locales
= newp
;
573 while (runp
->next
!= NULL
)
575 runp
= runp
->next
= newp
;
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
));
591 runp
->categories
[category
].generic
=
592 copy_locale
->categories
[category
].generic
;
595 runp
->needed
|= 1 << category
;
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
));
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
));
641 turn_on_mcheck (void)
643 /* Enable `malloc' debugging. */
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
;