]> git.ipfire.org Git - thirdparty/glibc.git/blame - locale/programs/localedef.c
Initial revision
[thirdparty/glibc.git] / locale / programs / localedef.c
CommitLineData
df4ef2ab 1/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
e4cf5070
UD
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
19bc17a9 4
e4cf5070
UD
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.
19bc17a9 9
e4cf5070
UD
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.
19bc17a9 14
e4cf5070
UD
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. */
19bc17a9
RM
19
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
23
5a97622d 24#include <argp.h>
19bc17a9
RM
25#include <errno.h>
26#include <fcntl.h>
19bc17a9
RM
27#include <libintl.h>
28#include <locale.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33#include <sys/mman.h>
34#include <sys/stat.h>
35
36#include "error.h"
37#include "charset.h"
38#include "locfile.h"
7a12c6bb 39#include "../intl/loadinfo.h"
19bc17a9
RM
40
41/* Undefine the following line in the production version. */
42/* #define NDEBUG 1 */
43#include <assert.h>
44
45
46/* List of locale definition files which are used in `copy' instructions. */
47struct copy_def_list_t
48{
49 struct copy_def_list_t *next;
50
51 const char *name;
52 int mask;
53
54 struct localedef_t *locale;
55
56 struct
57 {
58 void *data;
59 size_t len;
60 } binary[6];
61};
62
63
64/* List of copied locales. */
65struct copy_def_list_t *copy_list;
66
67/* If this is defined be POSIX conform. */
68int posix_conformance;
69
19bc17a9
RM
70/* If not zero give a lot more messages. */
71int verbose;
72
c84142e8
UD
73/* If not zero suppress warnings and information messages. */
74int be_quiet;
19bc17a9 75
5a97622d
UD
76/* If not zero force output even if warning were issued. */
77static int force_output;
19bc17a9 78
5a97622d
UD
79/* Name of the character map file. */
80static const char *charmap_file;
81
82/* Name of the locale definition file. */
83static const char *input_file;
84
85/* Name of the UCS file. */
86static const char *ucs_csn;
87
88
89/* Name and version of program. */
90static void print_version (FILE *stream, struct argp_state *state);
91void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
92
93#define OPT_POSIX 1
94#define OPT_QUIET 2
95
96/* Definitions of arguments for argp functions. */
97static const struct argp_option options[] =
98{
99 { NULL, 0, NULL, 0, N_("Input Files:") },
100 { "charmap", 'f', "FILE", 0,
101 N_("Symbolic character names defined in FILE") },
102 { "inputfile", 'i', "FILE", 0, N_("Source definitions are found in FILE") },
103 { "code-set-name", 'u', "NAME", OPTION_HIDDEN,
104 N_("Specify code set for mapping ISO 10646 elements") },
105
106 { NULL, 0, NULL, 0, N_("Output control:") },
107 { "force", 'c', NULL, 0,
108 N_("Create output even if warning messages were issued") },
109 { "posix", OPT_POSIX, NULL, 0, N_("Be strictly POSIX conform") },
110 { "quiet", OPT_QUIET, NULL, 0,
111 N_("Suppress warnings and information messages") },
112 { "verbose", 'V', NULL, 0, N_("print more messages") },
113 { NULL, 0, NULL, 0, NULL }
114};
115
116/* Short description of program. */
117static const char doc[] = N_("Compile locale specification");
118
119/* Strings for arguments in help texts. */
120static const char args_doc[] = N_("NAME");
121
122/* Prototype for option handler. */
123static error_t parse_opt (int key, char *arg, struct argp_state *state);
124
125/* Function to print some extra text in the help message. */
126static char *more_help (int key, const char *text, void *input);
127
128/* Data structure to communicate with argp functions. */
129static struct argp argp =
19bc17a9 130{
5a97622d 131 options, parse_opt, args_doc, doc, NULL, more_help
19bc17a9
RM
132};
133
134
135/* Prototypes for global functions. */
136void *xmalloc (size_t __n);
137
138/* Prototypes for local functions. */
19bc17a9 139static void error_print (void);
7a12c6bb 140static const char *construct_output_path (char *path);
19bc17a9
RM
141
142
143int
144main (int argc, char *argv[])
145{
19bc17a9
RM
146 const char *output_path;
147 int cannot_write_why;
148 struct charset_t *charset;
149 struct localedef_t *localedef;
150 struct copy_def_list_t *act_add_locdef;
2f6d1f1b 151 int remaining;
19bc17a9 152
6d52618b 153 /* Set initial values for global variables. */
19bc17a9
RM
154 copy_list = NULL;
155 posix_conformance = getenv ("POSIXLY_CORRECT") != NULL;
19bc17a9 156 error_print_progname = error_print;
19bc17a9
RM
157
158 /* Set locale. Do not set LC_ALL because the other categories must
6d52618b 159 not be affected (according to POSIX.2). */
19bc17a9
RM
160 setlocale (LC_MESSAGES, "");
161 setlocale (LC_CTYPE, "");
162
163 /* Initialize the message catalog. */
19bc17a9 164 textdomain (_libc_intl_domainname);
19bc17a9 165
5a97622d 166 /* Parse and process arguments. */
2f6d1f1b 167 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
c84142e8 168
5a97622d
UD
169 /* XXX POSIX is violated since for unknown option a exit value > 3
170 must be used. */
19bc17a9
RM
171
172 /* POSIX.2 requires to be verbose about missing characters in the
173 character map. */
174 verbose |= posix_conformance;
175
2f6d1f1b 176 if (argc - remaining != 1)
19bc17a9 177 {
5a97622d
UD
178 /* We need exactly one non-option parameter. */
179 argp_help (&argp, stdout, ARGP_HELP_SEE,
180 program_invocation_short_name);
e320cbc9 181
5a97622d
UD
182 /* XXX Currently POSIX is violated. We must exit with code 4
183 but the argp_help function currently does not allow this. */
184 exit (4);
19bc17a9
RM
185 }
186
19bc17a9
RM
187 /* The parameter describes the output path of the constructed files.
188 If the described files cannot be written return a NULL pointer. */
2f6d1f1b 189 output_path = construct_output_path (argv[remaining]);
19bc17a9
RM
190 cannot_write_why = errno;
191
192 /* Now that the parameters are processed we have to reset the local
193 ctype locale. (P1003.2 4.35.5.2) */
194 setlocale (LC_CTYPE, "POSIX");
195
196 /* Look whether the system really allows locale definitions. POSIX
197 defines error code 3 for this situation so I think it must be
198 a fatal error (see P1003.2 4.35.8). */
199 if (sysconf (_SC_2_LOCALEDEF) < 0)
200 error (3, 0, _("FATAL: system does not define `_POSIX2_LOCALEDEF'"));
201
202 /* Process charmap file. */
203 charset = charmap_read (charmap_file);
204
205 /* Now read the locale file. */
206 localedef = locfile_read (input_file, charset);
207 if (localedef->failed != 0)
208 error (4, errno, _("cannot open locale definition file `%s'"), input_file);
209
210 /* Perhaps we saw some `copy' instructions. Process the given list.
211 We use a very simple algorithm: we look up the list from the
212 beginning every time. */
213 do
214 {
215 int cat;
216
217 for (act_add_locdef = copy_list; act_add_locdef != NULL;
218 act_add_locdef = act_add_locdef->next)
219 {
75cd5204 220 for (cat = LC_CTYPE; cat <= LC_MESSAGES; ++cat)
19bc17a9
RM
221 if ((act_add_locdef->mask & (1 << cat)) != 0)
222 {
223 act_add_locdef->mask &= ~(1 << cat);
224 break;
225 }
226 if (cat <= LC_MESSAGES)
227 break;
228 }
229
230 if (act_add_locdef != NULL)
231 {
232 int avail = 0;
233
234 if (act_add_locdef->locale == NULL)
235 act_add_locdef->locale = locfile_read (act_add_locdef->name,
236 charset);
237
238 if (! act_add_locdef->locale->failed)
239 {
240 avail = act_add_locdef->locale->categories[cat].generic != NULL;
241 if (avail)
242 localedef->categories[cat].generic
243 = act_add_locdef->locale->categories[cat].generic;
244 }
245
246 if (! avail)
247 {
248 const char *locale_names[] = { "LC_COLLATE", "LC_CTYPE",
249 "LC_MONETARY", "LC_NUMERIC",
250 "LC_TIME", "LC_MESSAGES" };
251 char *fname;
252 int fd;
253 struct stat st;
254
255 asprintf (&fname, LOCALE_PATH "/%s/%s", act_add_locdef->name,
256 locale_names[cat]);
257 fd = open (fname, O_RDONLY);
258 if (fd == -1)
259 {
260 free (fname);
261
262 asprintf (&fname, LOCALE_PATH "/%s/%s/SYS_%s",
263 act_add_locdef->name, locale_names[cat],
264 locale_names[cat]);
265
266 fd = open (fname, O_RDONLY);
267 if (fd == -1)
268 error (5, 0, _("\
269locale file `%s', used in `copy' statement, not found"),
270 act_add_locdef->name);
271 }
272
273 if (fstat (fd, &st) < 0)
274 error (5, errno, _("\
275cannot `stat' locale file `%s'"),
276 fname);
277
278 localedef->len[cat] = st.st_size;
279 localedef->categories[cat].generic
280 = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
281
282 if (localedef->categories[cat].generic == (void *) -1)
283 {
284 size_t left = st.st_size;
285 void *read_ptr;
286
287 localedef->categories[cat].generic
288 = xmalloc (st.st_size);
289 read_ptr = localedef->categories[cat].generic;
290
291 do
292 {
293 long int n;
294 n = read (fd, read_ptr, left);
0d204b0a 295 if (n == -1)
19bc17a9
RM
296 error (5, errno, _("cannot read locale file `%s'"),
297 fname);
298 read_ptr += n;
299 left -= n;
300 }
301 while (left > 0);
302 }
303
304 close (fd);
305 free (fname);
306
307 localedef->binary |= 1 << cat;
308 }
309 }
310 }
311 while (act_add_locdef != NULL);
312
313 /* Check the categories we processed in source form. */
314 check_all_categories (localedef, charset);
315
316 /* We are now able to write the data files. If warning were given we
317 do it only if it is explicitly requested (--force). */
318 if (error_message_count == 0 || force_output != 0)
319 {
320 if (cannot_write_why != 0)
321 error (4, cannot_write_why, _("cannot write output files to `%s'"),
322 output_path);
323 else
75cd5204 324 write_all_categories (localedef, charset, output_path);
19bc17a9
RM
325 }
326 else
327 error (4, 0, _("no output file produced because warning were issued"));
328
329 /* This exit status is prescribed by POSIX.2 4.35.7. */
330 exit (error_message_count != 0);
331}
332
333
5a97622d
UD
334/* Handle program arguments. */
335static error_t
336parse_opt (int key, char *arg, struct argp_state *state)
337{
338 switch (key)
339 {
340 case OPT_QUIET:
341 be_quiet = 1;
342 break;
343 case OPT_POSIX:
344 posix_conformance = 1;
345 break;
346 case 'c':
347 force_output = 1;
348 break;
349 case 'f':
350 charmap_file = arg;
351 break;
352 case 'i':
353 input_file = arg;
354 break;
355 case 'u':
356 ucs_csn = arg;
357 break;
358 case 'v':
359 verbose = 1;
360 break;
361 default:
362 return ARGP_ERR_UNKNOWN;
363 }
364 return 0;
365}
366
367
368static char *
369more_help (int key, const char *text, void *input)
370{
371 char *cp;
372
373 switch (key)
374 {
375 case ARGP_KEY_HELP_EXTRA:
376 /* We print some extra information. */
377 asprintf (&cp, gettext ("\
378System's directory for character maps: %s\n\
379 locale files : %s\n\
380%s"),
381 CHARMAP_PATH, LOCALE_PATH, gettext ("\
382Report bugs using the `glibcbug' script to <bugs@gnu.ai.mit.edu>.\n"));
383 return cp;
384 default:
385 break;
386 }
387 return (char *) text;
388}
389
390/* Print the version information. */
391static void
392print_version (FILE *stream, struct argp_state *state)
393{
394 fprintf (stream, "localedef (GNU %s) %s\n", PACKAGE, VERSION);
395 fprintf (stream, gettext ("\
396Copyright (C) %s Free Software Foundation, Inc.\n\
397This is free software; see the source for copying conditions. There is NO\n\
398warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
399"), "1995, 1996, 1997");
400 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
401}
402
403
19bc17a9
RM
404void
405def_to_process (const char *name, int category)
406{
407 struct copy_def_list_t *new, **rp;
408
409 for (rp = &copy_list; *rp != NULL; rp = &(*rp)->next)
410 if (strcmp (name, (*rp)->name) == 0)
411 break;
412
413 if (*rp == NULL)
414 {
415 size_t cnt;
416
417 *rp = (struct copy_def_list_t *) xmalloc (sizeof (**rp));
418
419 (*rp)->next = NULL;
420 (*rp)->name = name;
421 (*rp)->mask = 0;
422 (*rp)->locale = NULL;
423
424 for (cnt = 0; cnt < 6; ++cnt)
425 {
426 (*rp)->binary[cnt].data = NULL;
427 (*rp)->binary[cnt].len = 0;
428 }
429 }
430 new = *rp;
431
432 if ((new->mask & category) != 0)
433 /* We already have the information. This cannot happen. */
434 error (5, 0, _("\
435category data requested more than once: should not happen"));
436
437 new->mask |= category;
438}
439
440
19bc17a9
RM
441/* The address of this function will be assigned to the hook in the error
442 functions. */
443static void
444error_print ()
445{
446 /* We don't want the program name to be printed in messages. Emacs'
447 compile.el does not like this. */
448}
449
450
451/* The parameter to localedef describes the output path. If it does
6d52618b 452 contain a '/' character it is a relative path. Otherwise it names the
19bc17a9
RM
453 locale this definition is for. */
454static const char *
7a12c6bb 455construct_output_path (char *path)
19bc17a9 456{
c4029823 457 const char *normal = NULL;
19bc17a9
RM
458 char *result;
459
460 if (strchr (path, '/') == NULL)
461 {
7a12c6bb
RM
462 /* This is a system path. First examine whether the locale name
463 contains a reference to the codeset. This should be
464 normalized. */
465 char *startp, *endp;
466
467 startp = path;
468 /* We must be prepared for finding a CEN name or a location of
469 the introducing `.' where it is not possible anymore. */
470 while (*startp != '\0' && *startp != '@' && *startp != '.'
471 && *startp != '+' && *startp != ',')
472 ++startp;
473 if (*startp == '.')
474 {
475 /* We found a codeset specification. Now find the end. */
476 endp = ++startp;
477 while (*endp != '\0' && *endp != '@')
478 ++endp;
479
480 if (endp > startp)
481 normal = _nl_normalize_codeset (startp, endp - startp);
482 }
c4029823
UD
483 else
484 /* This is to keep gcc quiet. */
485 endp = NULL;
19bc17a9 486
7a12c6bb
RM
487 /* We put an additional '\0' at the end of the string because at
488 the end of the function we need another byte for the trailing
489 '/'. */
490 if (normal == NULL)
e4cf5070 491 asprintf (&result, "%s/%s%c", LOCALEDIR, path, '\0');
7a12c6bb 492 else
e4cf5070 493 asprintf (&result, "%s/%.*s%s%s%c", LOCALEDIR, startp - path, path,
ba1ffaa1 494 normal, endp, '\0');
19bc17a9
RM
495 }
496 else
497 {
7a12c6bb
RM
498 /* This is a user path. Please note the additional byte in the
499 memory allocation. */
19bc17a9 500 result = xmalloc (strlen (path) + 2);
7a12c6bb 501 strcpy (result, path);
19bc17a9
RM
502 }
503
504 errno = 0;
505
506 if (euidaccess (result, W_OK) == -1)
507 /* Perhaps the directory does not exist now. Try to create it. */
508 if (errno == ENOENT)
509 {
510 errno = 0;
511 mkdir (result, 0777);
512 }
513
514 strcat (result, "/");
515
516 return result;
517}