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