]> git.ipfire.org Git - thirdparty/glibc.git/blob - locale/programs/localedef.c
Update.
[thirdparty/glibc.git] / locale / programs / localedef.c
1 /* Copyright (C) 1995,1996,1997,1998,1999,2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1995.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
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 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
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>
36
37 #include "error.h"
38 #include "charmap.h"
39 #include "locfile.h"
40
41 /* Undefine the following line in the production version. */
42 /* #define NDEBUG 1 */
43 #include <assert.h>
44
45
46 /* List of copied locales. */
47 struct copy_def_list_t *copy_list;
48
49 /* If this is defined be POSIX conform. */
50 int posix_conformance;
51
52 /* If not zero give a lot more messages. */
53 int verbose;
54
55 /* If not zero suppress warnings and information messages. */
56 int be_quiet;
57
58 /* If not zero, produce old-style hash table instead of 3-level access tables. */
59 int oldstyle_tables;
60
61 /* If not zero force output even if warning were issued. */
62 static int force_output;
63
64 /* Name of the character map file. */
65 static const char *charmap_file;
66
67 /* Name of the locale definition file. */
68 static const char *input_file;
69
70 /* Name of the repertoire map file. */
71 const char *repertoire_global;
72
73 /* List of all locales. */
74 static struct localedef_t *locales;
75
76
77 /* Name and version of program. */
78 static void print_version (FILE *stream, struct argp_state *state);
79 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
80
81 #define OPT_POSIX 1
82 #define OPT_QUIET 2
83 #define OPT_OLDSTYLE 3
84
85 /* Definitions of arguments for argp functions. */
86 static const struct argp_option options[] =
87 {
88 { NULL, 0, NULL, 0, N_("Input Files:") },
89 { "charmap", 'f', "FILE", 0,
90 N_("Symbolic character names defined in FILE") },
91 { "inputfile", 'i', "FILE", 0, N_("Source definitions are found in FILE") },
92 { "repertoire-map", 'u', "FILE", 0,
93 N_("FILE contains mapping from symbolic names to UCS4 values") },
94
95 { NULL, 0, NULL, 0, N_("Output control:") },
96 { "force", 'c', NULL, 0,
97 N_("Create output even if warning messages were issued") },
98 { "old-style", OPT_OLDSTYLE, NULL, 0, N_("Create old-style tables") },
99 { "posix", OPT_POSIX, NULL, 0, N_("Be strictly POSIX conform") },
100 { "quiet", OPT_QUIET, NULL, 0,
101 N_("Suppress warnings and information messages") },
102 { "verbose", 'v', NULL, 0, N_("Print more messages") },
103 { NULL, 0, NULL, 0, NULL }
104 };
105
106 /* Short description of program. */
107 static const char doc[] = N_("Compile locale specification");
108
109 /* Strings for arguments in help texts. */
110 static const char args_doc[] = N_("NAME");
111
112 /* Prototype for option handler. */
113 static error_t parse_opt (int key, char *arg, struct argp_state *state);
114
115 /* Function to print some extra text in the help message. */
116 static char *more_help (int key, const char *text, void *input);
117
118 /* Data structure to communicate with argp functions. */
119 static struct argp argp =
120 {
121 options, parse_opt, args_doc, doc, NULL, more_help
122 };
123
124
125 /* Prototypes for global functions. */
126 extern void *xmalloc (size_t __n);
127
128 /* Prototypes for local functions. */
129 static void error_print (void);
130 static const char *construct_output_path (char *path);
131 static const char *normalize_codeset (const char *codeset, size_t name_len);
132
133
134 int
135 main (int argc, char *argv[])
136 {
137 const char *output_path;
138 int cannot_write_why;
139 struct charmap_t *charmap;
140 struct localedef_t global;
141 int remaining;
142
143 /* Set initial values for global variables. */
144 copy_list = NULL;
145 posix_conformance = getenv ("POSIXLY_CORRECT") != NULL;
146 error_print_progname = error_print;
147
148 /* Set locale. Do not set LC_ALL because the other categories must
149 not be affected (according to POSIX.2). */
150 setlocale (LC_MESSAGES, "");
151 setlocale (LC_CTYPE, "");
152
153 /* Initialize the message catalog. */
154 textdomain (_libc_intl_domainname);
155
156 /* Parse and process arguments. */
157 argp_err_exit_status = 4;
158 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
159
160 /* POSIX.2 requires to be verbose about missing characters in the
161 character map. */
162 verbose |= posix_conformance;
163
164 if (argc - remaining != 1)
165 {
166 /* We need exactly one non-option parameter. */
167 argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
168 program_invocation_short_name);
169 exit (4);
170 }
171
172 /* The parameter describes the output path of the constructed files.
173 If the described files cannot be written return a NULL pointer. */
174 output_path = construct_output_path (argv[remaining]);
175 cannot_write_why = errno;
176
177 /* Now that the parameters are processed we have to reset the local
178 ctype locale. (P1003.2 4.35.5.2) */
179 setlocale (LC_CTYPE, "POSIX");
180
181 /* Look whether the system really allows locale definitions. POSIX
182 defines error code 3 for this situation so I think it must be
183 a fatal error (see P1003.2 4.35.8). */
184 if (sysconf (_SC_2_LOCALEDEF) < 0)
185 error (3, 0, _("FATAL: system does not define `_POSIX2_LOCALEDEF'"));
186
187 /* Process charmap file. */
188 charmap = charmap_read (charmap_file);
189
190 /* Add the first entry in the locale list. */
191 memset (&global, '\0', sizeof (struct localedef_t));
192 global.name = input_file;
193 global.needed = ALL_LOCALES;
194 locales = &global;
195
196 /* Now read the locale file. */
197 if (locfile_read (&global, charmap) != 0)
198 error (4, errno, _("cannot open locale definition file `%s'"), input_file);
199
200 /* Perhaps we saw some `copy' instructions. */
201 while (1)
202 {
203 struct localedef_t *runp = locales;
204
205 while (runp != NULL && (runp->needed & runp->avail) == runp->needed)
206 runp = runp->next;
207
208 if (runp == NULL)
209 /* Everything read. */
210 break;
211
212 if (locfile_read (runp, charmap) != 0)
213 error (4, errno, _("cannot open locale definition file `%s'"),
214 runp->name);
215 }
216
217 /* Check the categories we processed in source form. */
218 check_all_categories (locales, charmap);
219
220 /* We are now able to write the data files. If warning were given we
221 do it only if it is explicitly requested (--force). */
222 if (error_message_count == 0 || force_output != 0)
223 {
224 if (cannot_write_why != 0)
225 error (4, cannot_write_why, _("cannot write output files to `%s'"),
226 output_path);
227 else
228 write_all_categories (locales, charmap, output_path);
229 }
230 else
231 error (4, 0, _("no output file produced because warning were issued"));
232
233 /* This exit status is prescribed by POSIX.2 4.35.7. */
234 exit (error_message_count != 0);
235 }
236
237
238 /* Handle program arguments. */
239 static error_t
240 parse_opt (int key, char *arg, struct argp_state *state)
241 {
242 switch (key)
243 {
244 case OPT_QUIET:
245 be_quiet = 1;
246 break;
247 case OPT_POSIX:
248 posix_conformance = 1;
249 break;
250 case OPT_OLDSTYLE:
251 oldstyle_tables = 1;
252 break;
253 case 'c':
254 force_output = 1;
255 break;
256 case 'f':
257 charmap_file = arg;
258 break;
259 case 'i':
260 input_file = arg;
261 break;
262 case 'u':
263 repertoire_global = arg;
264 break;
265 case 'v':
266 verbose = 1;
267 break;
268 default:
269 return ARGP_ERR_UNKNOWN;
270 }
271 return 0;
272 }
273
274
275 static char *
276 more_help (int key, const char *text, void *input)
277 {
278 char *cp;
279
280 switch (key)
281 {
282 case ARGP_KEY_HELP_EXTRA:
283 /* We print some extra information. */
284 asprintf (&cp, gettext ("\
285 System's directory for character maps : %s\n\
286 repertoire maps: %s\n\
287 locale path : %s\n\
288 %s"),
289 CHARMAP_PATH, REPERTOIREMAP_PATH, LOCALE_PATH, gettext ("\
290 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
291 return cp;
292 default:
293 break;
294 }
295 return (char *) text;
296 }
297
298 /* Print the version information. */
299 static void
300 print_version (FILE *stream, struct argp_state *state)
301 {
302 fprintf (stream, "localedef (GNU %s) %s\n", PACKAGE, VERSION);
303 fprintf (stream, gettext ("\
304 Copyright (C) %s Free Software Foundation, Inc.\n\
305 This is free software; see the source for copying conditions. There is NO\n\
306 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
307 "), "2000");
308 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
309 }
310
311
312 /* The address of this function will be assigned to the hook in the error
313 functions. */
314 static void
315 error_print (void)
316 {
317 }
318
319
320 /* The parameter to localedef describes the output path. If it does
321 contain a '/' character it is a relative path. Otherwise it names the
322 locale this definition is for. */
323 static const char *
324 construct_output_path (char *path)
325 {
326 const char *normal = NULL;
327 char *result;
328 char *endp;
329
330 if (strchr (path, '/') == NULL)
331 {
332 /* This is a system path. First examine whether the locale name
333 contains a reference to the codeset. This should be
334 normalized. */
335 char *startp;
336 size_t n;
337
338 startp = path;
339 /* We must be prepared for finding a CEN name or a location of
340 the introducing `.' where it is not possible anymore. */
341 while (*startp != '\0' && *startp != '@' && *startp != '.'
342 && *startp != '+' && *startp != ',')
343 ++startp;
344 if (*startp == '.')
345 {
346 /* We found a codeset specification. Now find the end. */
347 endp = ++startp;
348 while (*endp != '\0' && *endp != '@')
349 ++endp;
350
351 if (endp > startp)
352 normal = normalize_codeset (startp, endp - startp);
353 }
354 else
355 /* This is to keep gcc quiet. */
356 endp = NULL;
357
358 /* We put an additional '\0' at the end of the string because at
359 the end of the function we need another byte for the trailing
360 '/'. */
361 if (normal == NULL)
362 n = asprintf (&result, "%s/%s%c", LOCALEDIR, path, '\0');
363 else
364 n = asprintf (&result, "%s/%.*s%s%s%c", LOCALEDIR,
365 (int) (startp - path), path, normal, endp, '\0');
366
367 endp = result + n;
368 }
369 else
370 {
371 /* This is a user path. Please note the additional byte in the
372 memory allocation. */
373 size_t len = strlen (path) + 1;
374 result = xmalloc (len + 1);
375 endp = mempcpy (result, path, len) - 1;
376 }
377
378 errno = 0;
379
380 if (euidaccess (result, W_OK) == -1)
381 /* Perhaps the directory does not exist now. Try to create it. */
382 if (errno == ENOENT)
383 {
384 errno = 0;
385 mkdir (result, 0777);
386 }
387
388 *endp++ = '/';
389 *endp = '\0';
390
391 return result;
392 }
393
394
395 /* Normalize codeset name. There is no standard for the codeset
396 names. Normalization allows the user to use any of the common
397 names. */
398 static const char *
399 normalize_codeset (codeset, name_len)
400 const char *codeset;
401 size_t name_len;
402 {
403 int len = 0;
404 int only_digit = 1;
405 char *retval;
406 char *wp;
407 size_t cnt;
408
409 for (cnt = 0; cnt < name_len; ++cnt)
410 if (isalnum (codeset[cnt]))
411 {
412 ++len;
413
414 if (isalpha (codeset[cnt]))
415 only_digit = 0;
416 }
417
418 retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
419
420 if (retval != NULL)
421 {
422 if (only_digit)
423 wp = stpcpy (retval, "iso");
424 else
425 wp = retval;
426
427 for (cnt = 0; cnt < name_len; ++cnt)
428 if (isalpha (codeset[cnt]))
429 *wp++ = tolower (codeset[cnt]);
430 else if (isdigit (codeset[cnt]))
431 *wp++ = codeset[cnt];
432
433 *wp = '\0';
434 }
435
436 return (const char *) retval;
437 }
438
439
440 struct localedef_t *
441 add_to_readlist (int locale, const char *name, const char *repertoire_name,
442 int generate, struct localedef_t *copy_locale)
443 {
444 struct localedef_t *runp = locales;
445
446 while (runp != NULL && strcmp (name, runp->name) != 0)
447 runp = runp->next;
448
449 if (runp == NULL)
450 {
451 /* Add a new entry at the end. */
452 struct localedef_t *newp;
453
454 assert (generate == 1);
455
456 newp = xcalloc (1, sizeof (struct localedef_t));
457 newp->name = name;
458 newp->repertoire_name = repertoire_name;
459
460 if (locales == NULL)
461 runp = locales = newp;
462 else
463 {
464 runp = locales;
465 while (runp->next != NULL)
466 runp = runp->next;
467 runp = runp->next = newp;
468 }
469 }
470
471 if (generate && (runp->needed & (1 << locale)) != 0)
472 error (5, 0, _("circular dependencies between locale definitions"));
473
474 if (copy_locale != NULL)
475 {
476 if (runp->categories[locale].generic != NULL)
477 error (5, 0, _("cannot add already read locale `%s' a second time"),
478 name);
479 else
480 runp->categories[locale].generic =
481 copy_locale->categories[locale].generic;
482 }
483
484 runp->needed |= 1 << locale;
485
486 return runp;
487 }
488
489
490 struct localedef_t *
491 find_locale (int locale, const char *name, const char *repertoire_name,
492 struct charmap_t *charmap)
493 {
494 struct localedef_t *result;
495
496 /* Find the locale, but do not generate it since this would be a bug. */
497 result = add_to_readlist (locale, name, repertoire_name, 0, NULL);
498
499 assert (result != NULL);
500
501 if ((result->avail & (1 << locale)) == 0
502 && locfile_read (result, charmap) != 0)
503 error (4, errno, _("cannot open locale definition file `%s'"),
504 result->name);
505
506 return result;
507 }
508
509
510 struct localedef_t *
511 load_locale (int locale, const char *name, const char *repertoire_name,
512 struct charmap_t *charmap, struct localedef_t *copy_locale)
513 {
514 struct localedef_t *result;
515
516 /* Generate the locale if it does not exist. */
517 result = add_to_readlist (locale, name, repertoire_name, 1, copy_locale);
518
519 assert (result != NULL);
520
521 if ((result->avail & (1 << locale)) == 0
522 && locfile_read (result, charmap) != 0)
523 error (4, errno, _("cannot open locale definition file `%s'"),
524 result->name);
525
526 return result;
527 }
528
529 static void
530 turn_on_mcheck (void)
531 {
532 /* Enable `malloc' debugging. */
533 mcheck (NULL);
534 /* Use the following line for a more thorough but much slower testing. */
535 /* mcheck_pedantic (NULL); */
536 }
537
538 void (*__malloc_initialize_hook) (void) = turn_on_mcheck;