1 /* Copyright (C) 1995-2008, 2009 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, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
38 #include "localedef.h"
42 /* Undefine the following line in the production version. */
43 /* #define NDEBUG 1 */
47 /* List of copied locales. */
48 struct copy_def_list_t
*copy_list
;
50 /* If this is defined be POSIX conform. */
51 int posix_conformance
;
53 /* If not zero give a lot more messages. */
56 /* If not zero suppress warnings and information messages. */
59 /* If not zero, produce old-style hash table instead of 3-level access
63 /* If not zero force output even if warning were issued. */
64 static int force_output
;
66 /* Prefix for output files. */
67 const char *output_prefix
;
69 /* Name of the character map file. */
70 static const char *charmap_file
;
72 /* Name of the locale definition file. */
73 static const char *input_file
;
75 /* Name of the repertoire map file. */
76 const char *repertoire_global
;
78 /* Name of the locale.alias file. */
79 const char *alias_file
;
81 /* List of all locales. */
82 static struct localedef_t
*locales
;
84 /* If true don't add locale data to archive. */
87 /* If true add named locales to archive. */
88 static bool add_to_archive
;
90 /* If true delete named locales from archive. */
91 static bool delete_from_archive
;
93 /* If true replace archive content when adding. */
94 static bool replace_archive
;
96 /* If true list archive content. */
97 static bool list_archive
;
99 /* Maximum number of retries when opening the locale archive. */
100 int max_locarchive_open_retry
= 10;
103 /* Name and version of program. */
104 static void print_version (FILE *stream
, struct argp_state
*state
);
105 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
107 #define OPT_POSIX 301
108 #define OPT_QUIET 302
109 #define OPT_OLDSTYLE 303
110 #define OPT_PREFIX 304
111 #define OPT_NO_ARCHIVE 305
112 #define OPT_ADD_TO_ARCHIVE 306
113 #define OPT_REPLACE 307
114 #define OPT_DELETE_FROM_ARCHIVE 308
115 #define OPT_LIST_ARCHIVE 309
117 /* Definitions of arguments for argp functions. */
118 static const struct argp_option options
[] =
120 { NULL
, 0, NULL
, 0, N_("Input Files:") },
121 { "charmap", 'f', "FILE", 0,
122 N_("Symbolic character names defined in FILE") },
123 { "inputfile", 'i', "FILE", 0, N_("Source definitions are found in FILE") },
124 { "repertoire-map", 'u', "FILE", 0,
125 N_("FILE contains mapping from symbolic names to UCS4 values") },
127 { NULL
, 0, NULL
, 0, N_("Output control:") },
128 { "force", 'c', NULL
, 0,
129 N_("Create output even if warning messages were issued") },
130 { "old-style", OPT_OLDSTYLE
, NULL
, 0, N_("Create old-style tables") },
131 { "prefix", OPT_PREFIX
, "PATH", 0, N_("Optional output file prefix") },
132 { "posix", OPT_POSIX
, NULL
, 0, N_("Be strictly POSIX conform") },
133 { "quiet", OPT_QUIET
, NULL
, 0,
134 N_("Suppress warnings and information messages") },
135 { "verbose", 'v', NULL
, 0, N_("Print more messages") },
136 { NULL
, 0, NULL
, 0, N_("Archive control:") },
137 { "no-archive", OPT_NO_ARCHIVE
, NULL
, 0,
138 N_("Don't add new data to archive") },
139 { "add-to-archive", OPT_ADD_TO_ARCHIVE
, NULL
, 0,
140 N_("Add locales named by parameters to archive") },
141 { "replace", OPT_REPLACE
, NULL
, 0, N_("Replace existing archive content") },
142 { "delete-from-archive", OPT_DELETE_FROM_ARCHIVE
, NULL
, 0,
143 N_("Remove locales named by parameters from archive") },
144 { "list-archive", OPT_LIST_ARCHIVE
, NULL
, 0, N_("List content of archive") },
145 { "alias-file", 'A', "FILE", 0,
146 N_("locale.alias file to consult when making archive")},
147 { NULL
, 0, NULL
, 0, NULL
}
150 /* Short description of program. */
151 static const char doc
[] = N_("Compile locale specification");
153 /* Strings for arguments in help texts. */
154 static const char args_doc
[] = N_("\
156 [--add-to-archive|--delete-from-archive] FILE...\n\
157 --list-archive [FILE]");
159 /* Prototype for option handler. */
160 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
162 /* Function to print some extra text in the help message. */
163 static char *more_help (int key
, const char *text
, void *input
);
165 /* Data structure to communicate with argp functions. */
166 static struct argp argp
=
168 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
172 /* Prototypes for global functions. */
173 extern void *xmalloc (size_t __n
);
175 /* Prototypes for local functions. */
176 static void error_print (void);
177 static const char *construct_output_path (char *path
);
178 static const char *normalize_codeset (const char *codeset
, size_t name_len
);
182 main (int argc
, char *argv
[])
184 const char *output_path
;
185 int cannot_write_why
;
186 struct charmap_t
*charmap
;
187 struct localedef_t global
;
190 /* Set initial values for global variables. */
192 posix_conformance
= getenv ("POSIXLY_CORRECT") != NULL
;
193 error_print_progname
= error_print
;
195 /* Set locale. Do not set LC_ALL because the other categories must
196 not be affected (according to POSIX.2). */
197 setlocale (LC_MESSAGES
, "");
198 setlocale (LC_CTYPE
, "");
200 /* Initialize the message catalog. */
201 textdomain (_libc_intl_domainname
);
203 /* Parse and process arguments. */
204 argp_err_exit_status
= 4;
205 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
207 /* Handle a few special cases. */
209 show_archive_content (verbose
);
211 return add_locales_to_archive (argc
- remaining
, &argv
[remaining
],
213 if (delete_from_archive
)
214 return delete_locales_from_archive (argc
- remaining
, &argv
[remaining
]);
216 /* POSIX.2 requires to be verbose about missing characters in the
218 verbose
|= posix_conformance
;
220 if (argc
- remaining
!= 1)
222 /* We need exactly one non-option parameter. */
223 argp_help (&argp
, stdout
, ARGP_HELP_SEE
| ARGP_HELP_EXIT_ERR
,
224 program_invocation_short_name
);
228 /* The parameter describes the output path of the constructed files.
229 If the described files cannot be written return a NULL pointer. */
230 output_path
= construct_output_path (argv
[remaining
]);
231 if (output_path
== NULL
&& ! no_archive
)
232 error (4, errno
, _("cannot create directory for output files"));
233 cannot_write_why
= errno
;
235 /* Now that the parameters are processed we have to reset the local
236 ctype locale. (P1003.2 4.35.5.2) */
237 setlocale (LC_CTYPE
, "POSIX");
239 /* Look whether the system really allows locale definitions. POSIX
240 defines error code 3 for this situation so I think it must be
241 a fatal error (see P1003.2 4.35.8). */
242 if (sysconf (_SC_2_LOCALEDEF
) < 0)
243 WITH_CUR_LOCALE (error (3, 0, _("\
244 FATAL: system does not define `_POSIX2_LOCALEDEF'")));
246 /* Process charmap file. */
247 charmap
= charmap_read (charmap_file
, verbose
, 1, be_quiet
, 1);
249 /* Add the first entry in the locale list. */
250 memset (&global
, '\0', sizeof (struct localedef_t
));
251 global
.name
= input_file
?: "/dev/stdin";
252 global
.needed
= ALL_LOCALES
;
255 /* Now read the locale file. */
256 if (locfile_read (&global
, charmap
) != 0)
257 WITH_CUR_LOCALE (error (4, errno
, _("\
258 cannot open locale definition file `%s'"), input_file
));
260 /* Perhaps we saw some `copy' instructions. */
263 struct localedef_t
*runp
= locales
;
265 while (runp
!= NULL
&& (runp
->needed
& runp
->avail
) == runp
->needed
)
269 /* Everything read. */
272 if (locfile_read (runp
, charmap
) != 0)
273 WITH_CUR_LOCALE (error (4, errno
, _("\
274 cannot open locale definition file `%s'"), runp
->name
));
277 /* Check the categories we processed in source form. */
278 check_all_categories (locales
, charmap
);
280 /* We are now able to write the data files. If warning were given we
281 do it only if it is explicitly requested (--force). */
282 if (error_message_count
== 0 || force_output
!= 0)
284 if (cannot_write_why
!= 0)
285 WITH_CUR_LOCALE (error (4, cannot_write_why
, _("\
286 cannot write output files to `%s'"), output_path
));
288 write_all_categories (locales
, charmap
, argv
[remaining
], output_path
);
291 WITH_CUR_LOCALE (error (4, 0, _("\
292 no output file produced because warnings were issued")));
294 /* This exit status is prescribed by POSIX.2 4.35.7. */
295 exit (error_message_count
!= 0);
299 /* Handle program arguments. */
301 parse_opt (int key
, char *arg
, struct argp_state
*state
)
309 posix_conformance
= 1;
320 case OPT_ADD_TO_ARCHIVE
:
321 add_to_archive
= true;
324 replace_archive
= true;
326 case OPT_DELETE_FROM_ARCHIVE
:
327 delete_from_archive
= true;
329 case OPT_LIST_ARCHIVE
:
345 repertoire_global
= arg
;
351 return ARGP_ERR_UNKNOWN
;
358 more_help (int key
, const char *text
, void *input
)
364 case ARGP_KEY_HELP_EXTRA
:
365 /* We print some extra information. */
366 if (asprintf (&cp
, gettext ("\
367 System's directory for character maps : %s\n\
368 repertoire maps: %s\n\
371 CHARMAP_PATH
, REPERTOIREMAP_PATH
, LOCALE_PATH
, gettext ("\
372 For bug reporting instructions, please see:\n\
373 <http://www.gnu.org/software/libc/bugs.html>.\n")) < 0)
379 return (char *) text
;
382 /* Print the version information. */
384 print_version (FILE *stream
, struct argp_state
*state
)
386 fprintf (stream
, "localedef (GNU %s) %s\n", PACKAGE
, VERSION
);
387 fprintf (stream
, gettext ("\
388 Copyright (C) %s Free Software Foundation, Inc.\n\
389 This is free software; see the source for copying conditions. There is NO\n\
390 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
392 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
396 /* The address of this function will be assigned to the hook in the error
404 /* The parameter to localedef describes the output path. If it does
405 contain a '/' character it is a relative path. Otherwise it names the
406 locale this definition is for. */
408 construct_output_path (char *path
)
410 const char *normal
= NULL
;
414 if (strchr (path
, '/') == NULL
)
416 /* This is a system path. First examine whether the locale name
417 contains a reference to the codeset. This should be
423 /* We must be prepared for finding a CEN name or a location of
424 the introducing `.' where it is not possible anymore. */
425 while (*startp
!= '\0' && *startp
!= '@' && *startp
!= '.')
429 /* We found a codeset specification. Now find the end. */
431 while (*endp
!= '\0' && *endp
!= '@')
435 normal
= normalize_codeset (startp
, endp
- startp
);
438 /* This is to keep gcc quiet. */
441 /* We put an additional '\0' at the end of the string because at
442 the end of the function we need another byte for the trailing
445 n
= asprintf (&result
, "%s%s/%s%c",
446 output_prefix
?: "", LOCALEDIR
, path
, '\0');
448 n
= asprintf (&result
, "%s%s/%.*s%s%s%c",
449 output_prefix
?: "", LOCALEDIR
,
450 (int) (startp
- path
), path
, normal
, endp
, '\0');
455 endp
= result
+ n
- 1;
459 /* This is a user path. Please note the additional byte in the
460 memory allocation. */
461 size_t len
= strlen (path
) + 1;
462 result
= xmalloc (len
+ 1);
463 endp
= mempcpy (result
, path
, len
) - 1;
465 /* If the user specified an output path we cannot add the output
472 if (no_archive
&& euidaccess (result
, W_OK
) == -1)
473 /* Perhaps the directory does not exist now. Try to create it. */
477 if (mkdir (result
, 0777) < 0)
488 /* Normalize codeset name. There is no standard for the codeset
489 names. Normalization allows the user to use any of the common
492 normalize_codeset (codeset
, name_len
)
502 for (cnt
= 0; cnt
< name_len
; ++cnt
)
503 if (isalnum (codeset
[cnt
]))
507 if (isalpha (codeset
[cnt
]))
511 retval
= (char *) malloc ((only_digit
? 3 : 0) + len
+ 1);
516 wp
= stpcpy (retval
, "iso");
520 for (cnt
= 0; cnt
< name_len
; ++cnt
)
521 if (isalpha (codeset
[cnt
]))
522 *wp
++ = tolower (codeset
[cnt
]);
523 else if (isdigit (codeset
[cnt
]))
524 *wp
++ = codeset
[cnt
];
529 return (const char *) retval
;
534 add_to_readlist (int category
, const char *name
, const char *repertoire_name
,
535 int generate
, struct localedef_t
*copy_locale
)
537 struct localedef_t
*runp
= locales
;
539 while (runp
!= NULL
&& strcmp (name
, runp
->name
) != 0)
544 /* Add a new entry at the end. */
545 struct localedef_t
*newp
;
547 assert (generate
== 1);
549 newp
= xcalloc (1, sizeof (struct localedef_t
));
551 newp
->repertoire_name
= repertoire_name
;
554 runp
= locales
= newp
;
558 while (runp
->next
!= NULL
)
560 runp
= runp
->next
= newp
;
565 && (runp
->needed
& (1 << category
)) != 0
566 && (runp
->avail
& (1 << category
)) == 0)
567 WITH_CUR_LOCALE (error (5, 0, _("\
568 circular dependencies between locale definitions")));
570 if (copy_locale
!= NULL
)
572 if (runp
->categories
[category
].generic
!= NULL
)
573 WITH_CUR_LOCALE (error (5, 0, _("\
574 cannot add already read locale `%s' a second time"), name
));
576 runp
->categories
[category
].generic
=
577 copy_locale
->categories
[category
].generic
;
580 runp
->needed
|= 1 << category
;
587 find_locale (int category
, const char *name
, const char *repertoire_name
,
588 const struct charmap_t
*charmap
)
590 struct localedef_t
*result
;
592 /* Find the locale, but do not generate it since this would be a bug. */
593 result
= add_to_readlist (category
, name
, repertoire_name
, 0, NULL
);
595 assert (result
!= NULL
);
597 if ((result
->avail
& (1 << category
)) == 0
598 && locfile_read (result
, charmap
) != 0)
599 WITH_CUR_LOCALE (error (4, errno
, _("\
600 cannot open locale definition file `%s'"), result
->name
));
607 load_locale (int category
, const char *name
, const char *repertoire_name
,
608 const struct charmap_t
*charmap
, struct localedef_t
*copy_locale
)
610 struct localedef_t
*result
;
612 /* Generate the locale if it does not exist. */
613 result
= add_to_readlist (category
, name
, repertoire_name
, 1, copy_locale
);
615 assert (result
!= NULL
);
617 if ((result
->avail
& (1 << category
)) == 0
618 && locfile_read (result
, charmap
) != 0)
619 WITH_CUR_LOCALE (error (4, errno
, _("\
620 cannot open locale definition file `%s'"), result
->name
));
626 turn_on_mcheck (void)
628 /* Enable `malloc' debugging. */
630 /* Use the following line for a more thorough but much slower testing. */
631 /* mcheck_pedantic (NULL); */
634 void (*__malloc_initialize_hook
) (void) = turn_on_mcheck
;