1 /* Copyright (C) 1995-2002, 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1995.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
39 #include "localedef.h"
43 /* Undefine the following line in the production version. */
44 /* #define NDEBUG 1 */
48 /* List of copied locales. */
49 struct copy_def_list_t
*copy_list
;
51 /* If this is defined be POSIX conform. */
52 int posix_conformance
;
54 /* If not zero give a lot more messages. */
57 /* If not zero suppress warnings and information messages. */
60 /* If not zero, produce old-style hash table instead of 3-level access
64 /* If not zero force output even if warning were issued. */
65 static int force_output
;
67 /* Prefix for output files. */
68 const char *output_prefix
;
70 /* Name of the character map file. */
71 static const char *charmap_file
;
73 /* Name of the locale definition file. */
74 static const char *input_file
;
76 /* Name of the repertoire map file. */
77 const char *repertoire_global
;
79 /* Name of the locale.alias file. */
80 const char *alias_file
;
82 /* List of all locales. */
83 static struct localedef_t
*locales
;
85 /* If true don't add locale data to archive. */
88 /* If true add named locales to archive. */
89 static bool add_to_archive
;
91 /* If true delete named locales from archive. */
92 static bool delete_from_archive
;
94 /* If true replace archive content when adding. */
95 static bool replace_archive
;
97 /* If true list archive content. */
98 static bool list_archive
;
100 /* Maximum number of retries when opening the locale archive. */
101 int max_locarchive_open_retry
= 10;
104 /* Name and version of program. */
105 static void print_version (FILE *stream
, struct argp_state
*state
);
106 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
108 #define OPT_POSIX 301
109 #define OPT_QUIET 302
110 #define OPT_OLDSTYLE 303
111 #define OPT_PREFIX 304
112 #define OPT_NO_ARCHIVE 305
113 #define OPT_ADD_TO_ARCHIVE 306
114 #define OPT_REPLACE 307
115 #define OPT_DELETE_FROM_ARCHIVE 308
116 #define OPT_LIST_ARCHIVE 309
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', "FILE", 0,
123 N_("Symbolic character names defined in FILE") },
124 { "inputfile", 'i', "FILE", 0, N_("Source definitions are found in FILE") },
125 { "repertoire-map", 'u', "FILE", 0,
126 N_("FILE contains mapping from symbolic names to UCS4 values") },
128 { NULL
, 0, NULL
, 0, N_("Output control:") },
129 { "force", 'c', NULL
, 0,
130 N_("Create output even if warning messages were issued") },
131 { "old-style", OPT_OLDSTYLE
, NULL
, 0, N_("Create old-style tables") },
132 { "prefix", OPT_PREFIX
, "PATH", 0, N_("Optional output file prefix") },
133 { "posix", OPT_POSIX
, NULL
, 0, N_("Be strictly POSIX conform") },
134 { "quiet", OPT_QUIET
, NULL
, 0,
135 N_("Suppress warnings and information messages") },
136 { "verbose", 'v', NULL
, 0, N_("Print more messages") },
137 { NULL
, 0, NULL
, 0, N_("Archive control:") },
138 { "no-archive", OPT_NO_ARCHIVE
, NULL
, 0,
139 N_("Don't add new data to archive") },
140 { "add-to-archive", OPT_ADD_TO_ARCHIVE
, NULL
, 0,
141 N_("Add locales named by parameters to archive") },
142 { "replace", OPT_REPLACE
, NULL
, 0, N_("Replace existing archive content") },
143 { "delete-from-archive", OPT_DELETE_FROM_ARCHIVE
, NULL
, 0,
144 N_("Remove locales named by parameters from archive") },
145 { "list-archive", OPT_LIST_ARCHIVE
, NULL
, 0, N_("List content of archive") },
146 { "alias-file", 'A', "FILE", 0,
147 N_("locale.alias file to consult when making archive")},
148 { NULL
, 0, NULL
, 0, NULL
}
151 /* Short description of program. */
152 static const char doc
[] = N_("Compile locale specification");
154 /* Strings for arguments in help texts. */
155 static const char args_doc
[] = N_("\
157 [--add-to-archive|--delete-from-archive] FILE...\n\
158 --list-archive [FILE]");
160 /* Prototype for option handler. */
161 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
163 /* Function to print some extra text in the help message. */
164 static char *more_help (int key
, const char *text
, void *input
);
166 /* Data structure to communicate with argp functions. */
167 static struct argp argp
=
169 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
173 /* Prototypes for global functions. */
174 extern void *xmalloc (size_t __n
);
176 /* Prototypes for local functions. */
177 static void error_print (void);
178 static const char *construct_output_path (char *path
);
179 static const char *normalize_codeset (const char *codeset
, size_t name_len
);
183 main (int argc
, char *argv
[])
185 const char *output_path
;
186 int cannot_write_why
;
187 struct charmap_t
*charmap
;
188 struct localedef_t global
;
191 /* Set initial values for global variables. */
193 posix_conformance
= getenv ("POSIXLY_CORRECT") != NULL
;
194 error_print_progname
= error_print
;
196 /* Set locale. Do not set LC_ALL because the other categories must
197 not be affected (according to POSIX.2). */
198 setlocale (LC_MESSAGES
, "");
199 setlocale (LC_CTYPE
, "");
201 /* Initialize the message catalog. */
202 textdomain (_libc_intl_domainname
);
204 /* Parse and process arguments. */
205 argp_err_exit_status
= 4;
206 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
208 /* Handle a few special cases. */
210 show_archive_content (verbose
);
212 return add_locales_to_archive (argc
- remaining
, &argv
[remaining
],
214 if (delete_from_archive
)
215 return delete_locales_from_archive (argc
- remaining
, &argv
[remaining
]);
217 /* POSIX.2 requires to be verbose about missing characters in the
219 verbose
|= posix_conformance
;
221 if (argc
- remaining
!= 1)
223 /* We need exactly one non-option parameter. */
224 argp_help (&argp
, stdout
, ARGP_HELP_SEE
| ARGP_HELP_EXIT_ERR
,
225 program_invocation_short_name
);
229 /* The parameter describes the output path of the constructed files.
230 If the described files cannot be written return a NULL pointer. */
231 output_path
= construct_output_path (argv
[remaining
]);
232 if (output_path
== NULL
&& ! no_archive
)
233 error (4, errno
, _("cannot create directory for output files"));
234 cannot_write_why
= errno
;
236 /* Now that the parameters are processed we have to reset the local
237 ctype locale. (P1003.2 4.35.5.2) */
238 setlocale (LC_CTYPE
, "POSIX");
240 /* Look whether the system really allows locale definitions. POSIX
241 defines error code 3 for this situation so I think it must be
242 a fatal error (see P1003.2 4.35.8). */
243 if (sysconf (_SC_2_LOCALEDEF
) < 0)
244 WITH_CUR_LOCALE (error (3, 0, _("\
245 FATAL: system does not define `_POSIX2_LOCALEDEF'")));
247 /* Process charmap file. */
248 charmap
= charmap_read (charmap_file
, verbose
, be_quiet
, 1);
250 /* Add the first entry in the locale list. */
251 memset (&global
, '\0', sizeof (struct localedef_t
));
252 global
.name
= input_file
?: "/dev/stdin";
253 global
.needed
= ALL_LOCALES
;
256 /* Now read the locale file. */
257 if (locfile_read (&global
, charmap
) != 0)
258 WITH_CUR_LOCALE (error (4, errno
, _("\
259 cannot open locale definition file `%s'"), input_file
));
261 /* Perhaps we saw some `copy' instructions. */
264 struct localedef_t
*runp
= locales
;
266 while (runp
!= NULL
&& (runp
->needed
& runp
->avail
) == runp
->needed
)
270 /* Everything read. */
273 if (locfile_read (runp
, charmap
) != 0)
274 WITH_CUR_LOCALE (error (4, errno
, _("\
275 cannot open locale definition file `%s'"), runp
->name
));
278 /* Check the categories we processed in source form. */
279 check_all_categories (locales
, charmap
);
281 /* We are now able to write the data files. If warning were given we
282 do it only if it is explicitly requested (--force). */
283 if (error_message_count
== 0 || force_output
!= 0)
285 if (cannot_write_why
!= 0)
286 WITH_CUR_LOCALE (error (4, cannot_write_why
, _("\
287 cannot write output files to `%s'"), output_path
));
289 write_all_categories (locales
, charmap
, argv
[remaining
], output_path
);
292 WITH_CUR_LOCALE (error (4, 0, _("\
293 no output file produced because warning were issued")));
295 /* This exit status is prescribed by POSIX.2 4.35.7. */
296 exit (error_message_count
!= 0);
300 /* Handle program arguments. */
302 parse_opt (int key
, char *arg
, struct argp_state
*state
)
310 posix_conformance
= 1;
321 case OPT_ADD_TO_ARCHIVE
:
322 add_to_archive
= true;
325 replace_archive
= true;
327 case OPT_DELETE_FROM_ARCHIVE
:
328 delete_from_archive
= true;
330 case OPT_LIST_ARCHIVE
:
346 repertoire_global
= arg
;
352 return ARGP_ERR_UNKNOWN
;
359 more_help (int key
, const char *text
, void *input
)
365 case ARGP_KEY_HELP_EXTRA
:
366 /* We print some extra information. */
367 if (asprintf (&cp
, gettext ("\
368 System's directory for character maps : %s\n\
369 repertoire maps: %s\n\
372 CHARMAP_PATH
, REPERTOIREMAP_PATH
, LOCALE_PATH
, gettext ("\
373 For bug reporting instructions, please see:\n\
374 <http://www.gnu.org/software/libc/bugs.html>.\n")) < 0)
380 return (char *) text
;
383 /* Print the version information. */
385 print_version (FILE *stream
, struct argp_state
*state
)
387 fprintf (stream
, "localedef (GNU %s) %s\n", PACKAGE
, VERSION
);
388 fprintf (stream
, gettext ("\
389 Copyright (C) %s Free Software Foundation, Inc.\n\
390 This is free software; see the source for copying conditions. There is NO\n\
391 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
393 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
397 /* The address of this function will be assigned to the hook in the error
405 /* The parameter to localedef describes the output path. If it does
406 contain a '/' character it is a relative path. Otherwise it names the
407 locale this definition is for. */
409 construct_output_path (char *path
)
411 const char *normal
= NULL
;
415 if (strchr (path
, '/') == NULL
)
417 /* This is a system path. First examine whether the locale name
418 contains a reference to the codeset. This should be
424 /* We must be prepared for finding a CEN name or a location of
425 the introducing `.' where it is not possible anymore. */
426 while (*startp
!= '\0' && *startp
!= '@' && *startp
!= '.')
430 /* We found a codeset specification. Now find the end. */
432 while (*endp
!= '\0' && *endp
!= '@')
436 normal
= normalize_codeset (startp
, endp
- startp
);
439 /* This is to keep gcc quiet. */
442 /* We put an additional '\0' at the end of the string because at
443 the end of the function we need another byte for the trailing
446 n
= asprintf (&result
, "%s%s/%s%c",
447 output_prefix
?: "", LOCALEDIR
, path
, '\0');
449 n
= asprintf (&result
, "%s%s/%.*s%s%s%c",
450 output_prefix
?: "", LOCALEDIR
,
451 (int) (startp
- path
), path
, normal
, endp
, '\0');
456 endp
= result
+ n
- 1;
460 /* This is a user path. Please note the additional byte in the
461 memory allocation. */
462 size_t len
= strlen (path
) + 1;
463 result
= xmalloc (len
+ 1);
464 endp
= mempcpy (result
, path
, len
) - 1;
466 /* If the user specified an output path we cannot add the output
473 if (no_archive
&& euidaccess (result
, W_OK
) == -1)
474 /* Perhaps the directory does not exist now. Try to create it. */
478 if (mkdir (result
, 0777) < 0)
489 /* Normalize codeset name. There is no standard for the codeset
490 names. Normalization allows the user to use any of the common
493 normalize_codeset (codeset
, name_len
)
503 for (cnt
= 0; cnt
< name_len
; ++cnt
)
504 if (isalnum (codeset
[cnt
]))
508 if (isalpha (codeset
[cnt
]))
512 retval
= (char *) malloc ((only_digit
? 3 : 0) + len
+ 1);
517 wp
= stpcpy (retval
, "iso");
521 for (cnt
= 0; cnt
< name_len
; ++cnt
)
522 if (isalpha (codeset
[cnt
]))
523 *wp
++ = tolower (codeset
[cnt
]);
524 else if (isdigit (codeset
[cnt
]))
525 *wp
++ = codeset
[cnt
];
530 return (const char *) retval
;
535 add_to_readlist (int locale
, const char *name
, const char *repertoire_name
,
536 int generate
, struct localedef_t
*copy_locale
)
538 struct localedef_t
*runp
= locales
;
540 while (runp
!= NULL
&& strcmp (name
, runp
->name
) != 0)
545 /* Add a new entry at the end. */
546 struct localedef_t
*newp
;
548 assert (generate
== 1);
550 newp
= xcalloc (1, sizeof (struct localedef_t
));
552 newp
->repertoire_name
= repertoire_name
;
555 runp
= locales
= newp
;
559 while (runp
->next
!= NULL
)
561 runp
= runp
->next
= newp
;
566 && (runp
->needed
& (1 << locale
)) != 0
567 && (runp
->avail
& (1 << locale
)) == 0)
568 WITH_CUR_LOCALE (error (5, 0, _("\
569 circular dependencies between locale definitions")));
571 if (copy_locale
!= NULL
)
573 if (runp
->categories
[locale
].generic
!= NULL
)
574 WITH_CUR_LOCALE (error (5, 0, _("\
575 cannot add already read locale `%s' a second time"), name
));
577 runp
->categories
[locale
].generic
=
578 copy_locale
->categories
[locale
].generic
;
581 runp
->needed
|= 1 << locale
;
588 find_locale (int locale
, const char *name
, const char *repertoire_name
,
589 const struct charmap_t
*charmap
)
591 struct localedef_t
*result
;
593 /* Find the locale, but do not generate it since this would be a bug. */
594 result
= add_to_readlist (locale
, name
, repertoire_name
, 0, NULL
);
596 assert (result
!= NULL
);
598 if ((result
->avail
& (1 << locale
)) == 0
599 && locfile_read (result
, charmap
) != 0)
600 WITH_CUR_LOCALE (error (4, errno
, _("\
601 cannot open locale definition file `%s'"), result
->name
));
608 load_locale (int locale
, const char *name
, const char *repertoire_name
,
609 const struct charmap_t
*charmap
, struct localedef_t
*copy_locale
)
611 struct localedef_t
*result
;
613 /* Generate the locale if it does not exist. */
614 result
= add_to_readlist (locale
, name
, repertoire_name
, 1, copy_locale
);
616 assert (result
!= NULL
);
618 if ((result
->avail
& (1 << locale
)) == 0
619 && locfile_read (result
, charmap
) != 0)
620 WITH_CUR_LOCALE (error (4, errno
, _("\
621 cannot open locale definition file `%s'"), result
->name
));
627 turn_on_mcheck (void)
629 /* Enable `malloc' debugging. */
631 /* Use the following line for a more thorough but much slower testing. */
632 /* mcheck_pedantic (NULL); */
635 void (*__malloc_initialize_hook
) (void) = turn_on_mcheck
;