Update.
[glibc.git] / locale / programs / localedef.c
blob1d09e8b46fb82b4fc75a163048ffdd2d73fc9af2
1 /* Copyright (C) 1995-1999, 2000, 2001, 2002 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
18 02111-1307 USA. */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <argp.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <libintl.h>
28 #include <locale.h>
29 #include <mcheck.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <sys/mman.h>
35 #include <sys/stat.h>
37 #include "error.h"
38 #include "charmap.h"
39 #include "locfile.h"
41 /* Undefine the following line in the production version. */
42 /* #define NDEBUG 1 */
43 #include <assert.h>
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. */
53 int verbose;
55 /* If not zero suppress warnings and information messages. */
56 int be_quiet;
58 /* If not zero, produce old-style hash table instead of 3-level access tables. */
59 int oldstyle_tables;
61 /* If not zero force output even if warning were issued. */
62 static int force_output;
64 /* Prefix for output files. */
65 const char *output_prefix;
67 /* Name of the character map file. */
68 static const char *charmap_file;
70 /* Name of the locale definition file. */
71 static const char *input_file;
73 /* Name of the repertoire map file. */
74 const char *repertoire_global;
76 /* List of all locales. */
77 static struct localedef_t *locales;
80 /* Name and version of program. */
81 static void print_version (FILE *stream, struct argp_state *state);
82 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
84 #define OPT_POSIX 1
85 #define OPT_QUIET 2
86 #define OPT_OLDSTYLE 3
87 #define OPT_PREFIX 4
89 /* Definitions of arguments for argp functions. */
90 static const struct argp_option options[] =
92 { NULL, 0, NULL, 0, N_("Input Files:") },
93 { "charmap", 'f', "FILE", 0,
94 N_("Symbolic character names defined in FILE") },
95 { "inputfile", 'i', "FILE", 0, N_("Source definitions are found in FILE") },
96 { "repertoire-map", 'u', "FILE", 0,
97 N_("FILE contains mapping from symbolic names to UCS4 values") },
99 { NULL, 0, NULL, 0, N_("Output control:") },
100 { "force", 'c', NULL, 0,
101 N_("Create output even if warning messages were issued") },
102 { "old-style", OPT_OLDSTYLE, NULL, 0, N_("Create old-style tables") },
103 { "prefix", OPT_PREFIX, "PATH", 0, N_("Optional output file prefix") },
104 { "posix", OPT_POSIX, NULL, 0, N_("Be strictly POSIX conform") },
105 { "quiet", OPT_QUIET, NULL, 0,
106 N_("Suppress warnings and information messages") },
107 { "verbose", 'v', NULL, 0, N_("Print more messages") },
108 { NULL, 0, NULL, 0, NULL }
111 /* Short description of program. */
112 static const char doc[] = N_("Compile locale specification");
114 /* Strings for arguments in help texts. */
115 static const char args_doc[] = N_("NAME");
117 /* Prototype for option handler. */
118 static error_t parse_opt (int key, char *arg, struct argp_state *state);
120 /* Function to print some extra text in the help message. */
121 static char *more_help (int key, const char *text, void *input);
123 /* Data structure to communicate with argp functions. */
124 static struct argp argp =
126 options, parse_opt, args_doc, doc, NULL, more_help
130 /* Prototypes for global functions. */
131 extern void *xmalloc (size_t __n);
133 /* Prototypes for local functions. */
134 static void error_print (void);
135 static const char *construct_output_path (char *path);
136 static const char *normalize_codeset (const char *codeset, size_t name_len);
140 main (int argc, char *argv[])
142 const char *output_path;
143 int cannot_write_why;
144 struct charmap_t *charmap;
145 struct localedef_t global;
146 int remaining;
148 /* Set initial values for global variables. */
149 copy_list = NULL;
150 posix_conformance = getenv ("POSIXLY_CORRECT") != NULL;
151 error_print_progname = error_print;
153 /* Set locale. Do not set LC_ALL because the other categories must
154 not be affected (according to POSIX.2). */
155 setlocale (LC_MESSAGES, "");
156 setlocale (LC_CTYPE, "");
158 /* Initialize the message catalog. */
159 textdomain (_libc_intl_domainname);
161 /* Parse and process arguments. */
162 argp_err_exit_status = 4;
163 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
165 /* POSIX.2 requires to be verbose about missing characters in the
166 character map. */
167 verbose |= posix_conformance;
169 if (argc - remaining != 1)
171 /* We need exactly one non-option parameter. */
172 argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
173 program_invocation_short_name);
174 exit (4);
177 /* The parameter describes the output path of the constructed files.
178 If the described files cannot be written return a NULL pointer. */
179 output_path = construct_output_path (argv[remaining]);
180 if (output_path == NULL)
181 error (4, errno, _("cannot create directory for output files"));
182 cannot_write_why = errno;
184 /* Now that the parameters are processed we have to reset the local
185 ctype locale. (P1003.2 4.35.5.2) */
186 setlocale (LC_CTYPE, "POSIX");
188 /* Look whether the system really allows locale definitions. POSIX
189 defines error code 3 for this situation so I think it must be
190 a fatal error (see P1003.2 4.35.8). */
191 if (sysconf (_SC_2_LOCALEDEF) < 0)
192 error (3, 0, _("FATAL: system does not define `_POSIX2_LOCALEDEF'"));
194 /* Process charmap file. */
195 charmap = charmap_read (charmap_file, verbose, be_quiet, 1);
197 /* Add the first entry in the locale list. */
198 memset (&global, '\0', sizeof (struct localedef_t));
199 global.name = input_file ?: "/dev/stdin";
200 global.needed = ALL_LOCALES;
201 locales = &global;
203 /* Now read the locale file. */
204 if (locfile_read (&global, charmap) != 0)
205 error (4, errno, _("cannot open locale definition file `%s'"), input_file);
207 /* Perhaps we saw some `copy' instructions. */
208 while (1)
210 struct localedef_t *runp = locales;
212 while (runp != NULL && (runp->needed & runp->avail) == runp->needed)
213 runp = runp->next;
215 if (runp == NULL)
216 /* Everything read. */
217 break;
219 if (locfile_read (runp, charmap) != 0)
220 error (4, errno, _("cannot open locale definition file `%s'"),
221 runp->name);
224 /* Check the categories we processed in source form. */
225 check_all_categories (locales, charmap);
227 /* We are now able to write the data files. If warning were given we
228 do it only if it is explicitly requested (--force). */
229 if (error_message_count == 0 || force_output != 0)
231 if (cannot_write_why != 0)
232 error (4, cannot_write_why, _("cannot write output files to `%s'"),
233 output_path);
234 else
235 write_all_categories (locales, charmap, output_path);
237 else
238 error (4, 0, _("no output file produced because warning were issued"));
240 /* This exit status is prescribed by POSIX.2 4.35.7. */
241 exit (error_message_count != 0);
245 /* Handle program arguments. */
246 static error_t
247 parse_opt (int key, char *arg, struct argp_state *state)
249 switch (key)
251 case OPT_QUIET:
252 be_quiet = 1;
253 break;
254 case OPT_POSIX:
255 posix_conformance = 1;
256 break;
257 case OPT_OLDSTYLE:
258 oldstyle_tables = 1;
259 break;
260 case OPT_PREFIX:
261 output_prefix = arg;
262 break;
263 case 'c':
264 force_output = 1;
265 break;
266 case 'f':
267 charmap_file = arg;
268 break;
269 case 'i':
270 input_file = arg;
271 break;
272 case 'u':
273 repertoire_global = arg;
274 break;
275 case 'v':
276 verbose = 1;
277 break;
278 default:
279 return ARGP_ERR_UNKNOWN;
281 return 0;
285 static char *
286 more_help (int key, const char *text, void *input)
288 char *cp;
290 switch (key)
292 case ARGP_KEY_HELP_EXTRA:
293 /* We print some extra information. */
294 asprintf (&cp, gettext ("\
295 System's directory for character maps : %s\n\
296 repertoire maps: %s\n\
297 locale path : %s\n\
298 %s"),
299 CHARMAP_PATH, REPERTOIREMAP_PATH, LOCALE_PATH, gettext ("\
300 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
301 return cp;
302 default:
303 break;
305 return (char *) text;
308 /* Print the version information. */
309 static void
310 print_version (FILE *stream, struct argp_state *state)
312 fprintf (stream, "localedef (GNU %s) %s\n", PACKAGE, VERSION);
313 fprintf (stream, gettext ("\
314 Copyright (C) %s Free Software Foundation, Inc.\n\
315 This is free software; see the source for copying conditions. There is NO\n\
316 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
317 "), "2002");
318 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
322 /* The address of this function will be assigned to the hook in the error
323 functions. */
324 static void
325 error_print (void)
330 /* The parameter to localedef describes the output path. If it does
331 contain a '/' character it is a relative path. Otherwise it names the
332 locale this definition is for. */
333 static const char *
334 construct_output_path (char *path)
336 const char *normal = NULL;
337 char *result;
338 char *endp;
340 if (strchr (path, '/') == NULL)
342 /* This is a system path. First examine whether the locale name
343 contains a reference to the codeset. This should be
344 normalized. */
345 char *startp;
346 size_t n;
348 startp = path;
349 /* We must be prepared for finding a CEN name or a location of
350 the introducing `.' where it is not possible anymore. */
351 while (*startp != '\0' && *startp != '@' && *startp != '.'
352 && *startp != '+' && *startp != ',')
353 ++startp;
354 if (*startp == '.')
356 /* We found a codeset specification. Now find the end. */
357 endp = ++startp;
358 while (*endp != '\0' && *endp != '@')
359 ++endp;
361 if (endp > startp)
362 normal = normalize_codeset (startp, endp - startp);
364 else
365 /* This is to keep gcc quiet. */
366 endp = NULL;
368 /* We put an additional '\0' at the end of the string because at
369 the end of the function we need another byte for the trailing
370 '/'. */
371 if (normal == NULL)
372 n = asprintf (&result, "%s%s/%s%c",
373 output_prefix ?: "", LOCALEDIR, path, '\0');
374 else
375 n = asprintf (&result, "%s%s/%.*s%s%s%c",
376 output_prefix ?: "", LOCALEDIR,
377 (int) (startp - path), path, normal, endp, '\0');
379 if (n < 0)
380 return NULL;
382 endp = result + n - 1;
384 else
386 /* This is a user path. Please note the additional byte in the
387 memory allocation. */
388 size_t len = strlen (path) + 1;
389 result = xmalloc (len + 1);
390 endp = mempcpy (result, path, len) - 1;
393 errno = 0;
395 if (euidaccess (result, W_OK) == -1)
396 /* Perhaps the directory does not exist now. Try to create it. */
397 if (errno == ENOENT)
399 errno = 0;
400 if (mkdir (result, 0777) < 0)
401 return NULL;
404 *endp++ = '/';
405 *endp = '\0';
407 return result;
411 /* Normalize codeset name. There is no standard for the codeset
412 names. Normalization allows the user to use any of the common
413 names. */
414 static const char *
415 normalize_codeset (codeset, name_len)
416 const char *codeset;
417 size_t name_len;
419 int len = 0;
420 int only_digit = 1;
421 char *retval;
422 char *wp;
423 size_t cnt;
425 for (cnt = 0; cnt < name_len; ++cnt)
426 if (isalnum (codeset[cnt]))
428 ++len;
430 if (isalpha (codeset[cnt]))
431 only_digit = 0;
434 retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
436 if (retval != NULL)
438 if (only_digit)
439 wp = stpcpy (retval, "iso");
440 else
441 wp = retval;
443 for (cnt = 0; cnt < name_len; ++cnt)
444 if (isalpha (codeset[cnt]))
445 *wp++ = tolower (codeset[cnt]);
446 else if (isdigit (codeset[cnt]))
447 *wp++ = codeset[cnt];
449 *wp = '\0';
452 return (const char *) retval;
456 struct localedef_t *
457 add_to_readlist (int locale, const char *name, const char *repertoire_name,
458 int generate, struct localedef_t *copy_locale)
460 struct localedef_t *runp = locales;
462 while (runp != NULL && strcmp (name, runp->name) != 0)
463 runp = runp->next;
465 if (runp == NULL)
467 /* Add a new entry at the end. */
468 struct localedef_t *newp;
470 assert (generate == 1);
472 newp = xcalloc (1, sizeof (struct localedef_t));
473 newp->name = name;
474 newp->repertoire_name = repertoire_name;
476 if (locales == NULL)
477 runp = locales = newp;
478 else
480 runp = locales;
481 while (runp->next != NULL)
482 runp = runp->next;
483 runp = runp->next = newp;
487 if (generate && (runp->needed & (1 << locale)) != 0)
488 error (5, 0, _("circular dependencies between locale definitions"));
490 if (copy_locale != NULL)
492 if (runp->categories[locale].generic != NULL)
493 error (5, 0, _("cannot add already read locale `%s' a second time"),
494 name);
495 else
496 runp->categories[locale].generic =
497 copy_locale->categories[locale].generic;
500 runp->needed |= 1 << locale;
502 return runp;
506 struct localedef_t *
507 find_locale (int locale, const char *name, const char *repertoire_name,
508 const struct charmap_t *charmap)
510 struct localedef_t *result;
512 /* Find the locale, but do not generate it since this would be a bug. */
513 result = add_to_readlist (locale, name, repertoire_name, 0, NULL);
515 assert (result != NULL);
517 if ((result->avail & (1 << locale)) == 0
518 && locfile_read (result, charmap) != 0)
519 error (4, errno, _("cannot open locale definition file `%s'"),
520 result->name);
522 return result;
526 struct localedef_t *
527 load_locale (int locale, const char *name, const char *repertoire_name,
528 const struct charmap_t *charmap, struct localedef_t *copy_locale)
530 struct localedef_t *result;
532 /* Generate the locale if it does not exist. */
533 result = add_to_readlist (locale, name, repertoire_name, 1, copy_locale);
535 assert (result != NULL);
537 if ((result->avail & (1 << locale)) == 0
538 && locfile_read (result, charmap) != 0)
539 error (4, errno, _("cannot open locale definition file `%s'"),
540 result->name);
542 return result;
545 static void
546 turn_on_mcheck (void)
548 /* Enable `malloc' debugging. */
549 mcheck (NULL);
550 /* Use the following line for a more thorough but much slower testing. */
551 /* mcheck_pedantic (NULL); */
554 void (*__malloc_initialize_hook) (void) = turn_on_mcheck;