Update to 2.1.x development version
[glibc.git] / locale / programs / localedef.c
blobda01d4c9743914cf8c9c2460db9e01bd2aa2f005
1 /* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
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.
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.
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. */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <getopt.h>
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>
36 #include "error.h"
37 #include "charset.h"
38 #include "locfile.h"
39 #include "../intl/loadinfo.h"
41 /* Undefine the following line in the production version. */
42 /* #define NDEBUG 1 */
43 #include <assert.h>
46 /* List of locale definition files which are used in `copy' instructions. */
47 struct copy_def_list_t
49 struct copy_def_list_t *next;
51 const char *name;
52 int mask;
54 struct localedef_t *locale;
56 struct
58 void *data;
59 size_t len;
60 } binary[6];
64 /* List of copied locales. */
65 struct copy_def_list_t *copy_list;
67 /* If this is defined be POSIX conform. */
68 int posix_conformance;
70 /* If not zero give a lot more messages. */
71 int verbose;
73 /* If not zero suppress warnings and information messages. */
74 int be_quiet;
77 /* Long options. */
78 static const struct option long_options[] =
80 { "charmap", required_argument, NULL, 'f' },
81 { "code-set-name", required_argument, NULL, 'u' },
82 { "help", no_argument, NULL, 'h' },
83 { "force", no_argument, NULL, 'c' },
84 { "inputfile", required_argument, NULL, 'i' },
85 { "posix", no_argument, &posix_conformance, 1 },
86 { "quiet", no_argument, NULL, 'q' },
87 { "verbose", no_argument, &verbose, 1},
88 { "version", no_argument, NULL, 'V' },
89 { NULL, 0, NULL, 0 }
93 /* Prototypes for global functions. */
94 void *xmalloc (size_t __n);
96 /* Prototypes for local functions. */
97 static void usage (int status) __attribute__ ((noreturn));
98 static void error_print (void);
99 static const char *construct_output_path (char *path);
103 main (int argc, char *argv[])
105 int optchar;
106 int do_help = 0;
107 int do_version = 0;
108 int force_output = 0;
109 const char *charmap_file = NULL;
110 const char *input_file = NULL;
111 const char *ucs_csn = NULL;
112 const char *output_path;
113 int cannot_write_why;
114 struct charset_t *charset;
115 struct localedef_t *localedef;
116 struct copy_def_list_t *act_add_locdef;
118 /* Set initial values for global variables. */
119 copy_list = NULL;
120 posix_conformance = getenv ("POSIXLY_CORRECT") != NULL;
121 error_print_progname = error_print;
122 verbose = 0;
124 /* Set locale. Do not set LC_ALL because the other categories must
125 not be affected (according to POSIX.2). */
126 setlocale (LC_MESSAGES, "");
127 setlocale (LC_CTYPE, "");
129 /* Initialize the message catalog. */
130 textdomain (_libc_intl_domainname);
132 while ((optchar = getopt_long (argc, argv, "cf:hi:u:vV", long_options, NULL))
133 != -1)
134 switch (optchar)
136 case '\0': /* Long option. */
137 break;
139 case 'c':
140 force_output = 1;
141 break;
143 case 'f':
144 charmap_file = optarg;
145 break;
147 case 'h':
148 do_help = 1;
149 break;
151 case 'i':
152 input_file = optarg;
153 break;
155 case 'q':
156 be_quiet = 1;
157 verbose = 0;
158 break;
160 case 'u':
161 ucs_csn = optarg;
162 break;
164 case 'v':
165 verbose = 1;
166 be_quiet = 0;
167 break;
169 case 'V':
170 do_version = 1;
171 break;
173 default:
174 usage (4); /* A value >3 is forced by POSIX. */
175 break;
178 /* POSIX.2 requires to be verbose about missing characters in the
179 character map. */
180 verbose |= posix_conformance;
182 /* Version information is requested. */
183 if (do_version)
185 printf ("localedef (GNU %s) %s\n", PACKAGE, VERSION);
186 printf (_("\
187 Copyright (C) %s Free Software Foundation, Inc.\n\
188 This is free software; see the source for copying conditions. There is NO\n\
189 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
190 "), "1995, 1996, 1997");
191 printf (_("Written by %s.\n"), "Ulrich Drepper");
193 exit (0);
196 /* Help is requested. */
197 if (do_help)
198 /* Possible violation: POSIX.2 4.35.8 defines the return value 0 as
199 "No errors occurred and the locale(s) were successfully created."
200 But giving a other value than 0 does not make sense here. It
201 is perhaps not that important because POSIX does not specify the
202 -h option for localedef. */
203 usage (0);
205 if (argc - optind != 1)
206 /* We need exactly one non-option parameter. */
207 usage (4);
209 /* The parameter describes the output path of the constructed files.
210 If the described files cannot be written return a NULL pointer. */
211 output_path = construct_output_path (argv[optind]);
212 cannot_write_why = errno;
214 /* Now that the parameters are processed we have to reset the local
215 ctype locale. (P1003.2 4.35.5.2) */
216 setlocale (LC_CTYPE, "POSIX");
218 /* Look whether the system really allows locale definitions. POSIX
219 defines error code 3 for this situation so I think it must be
220 a fatal error (see P1003.2 4.35.8). */
221 if (sysconf (_SC_2_LOCALEDEF) < 0)
222 error (3, 0, _("FATAL: system does not define `_POSIX2_LOCALEDEF'"));
224 /* Process charmap file. */
225 charset = charmap_read (charmap_file);
227 /* Now read the locale file. */
228 localedef = locfile_read (input_file, charset);
229 if (localedef->failed != 0)
230 error (4, errno, _("cannot open locale definition file `%s'"), input_file);
232 /* Perhaps we saw some `copy' instructions. Process the given list.
233 We use a very simple algorithm: we look up the list from the
234 beginning every time. */
237 int cat;
239 for (act_add_locdef = copy_list; act_add_locdef != NULL;
240 act_add_locdef = act_add_locdef->next)
242 for (cat = LC_CTYPE; cat <= LC_MESSAGES; ++cat)
243 if ((act_add_locdef->mask & (1 << cat)) != 0)
245 act_add_locdef->mask &= ~(1 << cat);
246 break;
248 if (cat <= LC_MESSAGES)
249 break;
252 if (act_add_locdef != NULL)
254 int avail = 0;
256 if (act_add_locdef->locale == NULL)
257 act_add_locdef->locale = locfile_read (act_add_locdef->name,
258 charset);
260 if (! act_add_locdef->locale->failed)
262 avail = act_add_locdef->locale->categories[cat].generic != NULL;
263 if (avail)
264 localedef->categories[cat].generic
265 = act_add_locdef->locale->categories[cat].generic;
268 if (! avail)
270 const char *locale_names[] = { "LC_COLLATE", "LC_CTYPE",
271 "LC_MONETARY", "LC_NUMERIC",
272 "LC_TIME", "LC_MESSAGES" };
273 char *fname;
274 int fd;
275 struct stat st;
277 asprintf (&fname, LOCALE_PATH "/%s/%s", act_add_locdef->name,
278 locale_names[cat]);
279 fd = open (fname, O_RDONLY);
280 if (fd == -1)
282 free (fname);
284 asprintf (&fname, LOCALE_PATH "/%s/%s/SYS_%s",
285 act_add_locdef->name, locale_names[cat],
286 locale_names[cat]);
288 fd = open (fname, O_RDONLY);
289 if (fd == -1)
290 error (5, 0, _("\
291 locale file `%s', used in `copy' statement, not found"),
292 act_add_locdef->name);
295 if (fstat (fd, &st) < 0)
296 error (5, errno, _("\
297 cannot `stat' locale file `%s'"),
298 fname);
300 localedef->len[cat] = st.st_size;
301 localedef->categories[cat].generic
302 = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
304 if (localedef->categories[cat].generic == (void *) -1)
306 size_t left = st.st_size;
307 void *read_ptr;
309 localedef->categories[cat].generic
310 = xmalloc (st.st_size);
311 read_ptr = localedef->categories[cat].generic;
315 long int n;
316 n = read (fd, read_ptr, left);
317 if (n == -1)
318 error (5, errno, _("cannot read locale file `%s'"),
319 fname);
320 read_ptr += n;
321 left -= n;
323 while (left > 0);
326 close (fd);
327 free (fname);
329 localedef->binary |= 1 << cat;
333 while (act_add_locdef != NULL);
335 /* Check the categories we processed in source form. */
336 check_all_categories (localedef, charset);
338 /* We are now able to write the data files. If warning were given we
339 do it only if it is explicitly requested (--force). */
340 if (error_message_count == 0 || force_output != 0)
342 if (cannot_write_why != 0)
343 error (4, cannot_write_why, _("cannot write output files to `%s'"),
344 output_path);
345 else
346 write_all_categories (localedef, charset, output_path);
348 else
349 error (4, 0, _("no output file produced because warning were issued"));
351 /* This exit status is prescribed by POSIX.2 4.35.7. */
352 exit (error_message_count != 0);
356 void
357 def_to_process (const char *name, int category)
359 struct copy_def_list_t *new, **rp;
361 for (rp = &copy_list; *rp != NULL; rp = &(*rp)->next)
362 if (strcmp (name, (*rp)->name) == 0)
363 break;
365 if (*rp == NULL)
367 size_t cnt;
369 *rp = (struct copy_def_list_t *) xmalloc (sizeof (**rp));
371 (*rp)->next = NULL;
372 (*rp)->name = name;
373 (*rp)->mask = 0;
374 (*rp)->locale = NULL;
376 for (cnt = 0; cnt < 6; ++cnt)
378 (*rp)->binary[cnt].data = NULL;
379 (*rp)->binary[cnt].len = 0;
382 new = *rp;
384 if ((new->mask & category) != 0)
385 /* We already have the information. This cannot happen. */
386 error (5, 0, _("\
387 category data requested more than once: should not happen"));
389 new->mask |= category;
393 /* Display usage information and exit. */
394 static void
395 usage (int status)
397 if (status != 0)
398 fprintf (stderr, _("Try `%s --help' for more information.\n"),
399 program_invocation_name);
400 else
402 printf (_("\
403 Usage: %s [OPTION]... name\n\
404 Mandatory arguments to long options are mandatory for short options too.\n\
405 -c, --force create output even if warning messages were issued\n\
406 -h, --help display this help and exit\n\
407 -f, --charmap=FILE symbolic character names defined in FILE\n\
408 -i, --inputfile=FILE source definitions are found in FILE\n\
409 --quiet Suppress warnings and information messages\n\
410 -u, --code-set-name=NAME specify code set for mapping ISO 10646 elements\n\
411 -v, --verbose print more messages\n\
412 -V, --version output version information and exit\n\
413 --posix be strictly POSIX conform\n\
415 System's directory for character maps: %s\n\
416 locale files : %s\n"),
417 program_invocation_name, CHARMAP_PATH, LOCALE_PATH);
418 fputs (gettext ("\
419 Report bugs using the `glibcbug' script to <bugs@gnu.ai.mit.edu>.\n"),
420 stdout);
423 exit (status);
427 /* The address of this function will be assigned to the hook in the error
428 functions. */
429 static void
430 error_print ()
432 /* We don't want the program name to be printed in messages. Emacs'
433 compile.el does not like this. */
437 /* The parameter to localedef describes the output path. If it does
438 contain a '/' character it is a relative path. Otherwise it names the
439 locale this definition is for. */
440 static const char *
441 construct_output_path (char *path)
443 const char *normal = NULL;
444 char *result;
446 if (strchr (path, '/') == NULL)
448 /* This is a system path. First examine whether the locale name
449 contains a reference to the codeset. This should be
450 normalized. */
451 char *startp, *endp;
453 startp = path;
454 /* We must be prepared for finding a CEN name or a location of
455 the introducing `.' where it is not possible anymore. */
456 while (*startp != '\0' && *startp != '@' && *startp != '.'
457 && *startp != '+' && *startp != ',')
458 ++startp;
459 if (*startp == '.')
461 /* We found a codeset specification. Now find the end. */
462 endp = ++startp;
463 while (*endp != '\0' && *endp != '@')
464 ++endp;
466 if (endp > startp)
467 normal = _nl_normalize_codeset (startp, endp - startp);
469 else
470 /* This is to keep gcc quiet. */
471 endp = NULL;
473 /* We put an additional '\0' at the end of the string because at
474 the end of the function we need another byte for the trailing
475 '/'. */
476 if (normal == NULL)
477 asprintf (&result, "%s/%s%c", LOCALEDIR, path, '\0');
478 else
479 asprintf (&result, "%s/%.*s%s%s%c", LOCALEDIR, startp - path, path,
480 normal, endp, '\0');
482 else
484 /* This is a user path. Please note the additional byte in the
485 memory allocation. */
486 result = xmalloc (strlen (path) + 2);
487 strcpy (result, path);
490 errno = 0;
492 if (euidaccess (result, W_OK) == -1)
493 /* Perhaps the directory does not exist now. Try to create it. */
494 if (errno == ENOENT)
496 errno = 0;
497 mkdir (result, 0777);
500 strcat (result, "/");
502 return result;