update from main archive 961001
[glibc.git] / locale / programs / localedef.c
blob6d37a0e3988f5993977095082ac7e34416463443
1 /* Copyright (C) 1995, 1996 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
17 not, 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;
75 /* Long options. */
76 static const struct option long_options[] =
78 { "charmap", required_argument, NULL, 'f' },
79 { "code-set-name", required_argument, NULL, 'u' },
80 { "help", no_argument, NULL, 'h' },
81 { "force", no_argument, NULL, 'c' },
82 { "inputfile", required_argument, NULL, 'i' },
83 { "posix", no_argument, &posix_conformance, 1 },
84 { "verbose", no_argument, &verbose, 1},
85 { "version", no_argument, NULL, 'V' },
86 { NULL, 0, NULL, 0 }
90 /* Prototypes for global functions. */
91 void *xmalloc (size_t __n);
93 /* Prototypes for local functions. */
94 static void usage (int status) __attribute__ ((noreturn));
95 static void error_print (void);
96 static const char *construct_output_path (char *path);
99 int
100 main (int argc, char *argv[])
102 int optchar;
103 int do_help = 0;
104 int do_version = 0;
105 int force_output = 0;
106 const char *charmap_file = NULL;
107 const char *input_file = NULL;
108 const char *ucs_csn = NULL;
109 const char *output_path;
110 int cannot_write_why;
111 struct charset_t *charset;
112 struct localedef_t *localedef;
113 struct copy_def_list_t *act_add_locdef;
115 /* Set initial values for global varaibles. */
116 copy_list = NULL;
117 posix_conformance = getenv ("POSIXLY_CORRECT") != NULL;
118 error_print_progname = error_print;
119 verbose = 0;
121 /* Set locale. Do not set LC_ALL because the other categories must
122 not be affected (acccording to POSIX.2). */
123 setlocale (LC_MESSAGES, "");
124 setlocale (LC_CTYPE, "");
126 /* Initialize the message catalog. */
127 textdomain (_libc_intl_domainname);
129 while ((optchar = getopt_long (argc, argv, "cf:hi:u:vV", long_options, NULL))
130 != EOF)
131 switch (optchar)
133 case '\0': /* Long option. */
134 break;
136 case 'c':
137 force_output = 1;
138 break;
140 case 'f':
141 charmap_file = optarg;
142 break;
144 case 'h':
145 do_help = 1;
146 break;
148 case 'i':
149 input_file = optarg;
150 break;
152 case 'u':
153 ucs_csn = optarg;
154 break;
156 case 'v':
157 verbose = 1;
158 break;
160 case 'V':
161 do_version = 1;
162 break;
164 default:
165 usage (4); /* A value >3 is forced by POSIX. */
166 break;
169 /* POSIX.2 requires to be verbose about missing characters in the
170 character map. */
171 verbose |= posix_conformance;
173 /* Version information is requested. */
174 if (do_version)
176 printf ("localedef (GNU %s) %s\n", PACKAGE, VERSION);
177 printf (_("\
178 Copyright (C) %s Free Software Foundation, Inc.\n\
179 This is free software; see the source for copying conditions. There is NO\n\
180 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
181 "), "1995, 1996");
182 printf (_("Written by %s.\n"), "Ulrich Drepper");
184 exit (0);
187 /* Help is requested. */
188 if (do_help)
189 /* Possible violation: POSIX.2 4.35.8 defines the return value 0 as
190 "No errors occured and the locale(s) were successfully created."
191 But giving a other value than 0 does not make sense here. It
192 is perhaps not that important because POSIX does not specify the
193 -h option for localedef. */
194 usage (0);
196 if (argc - optind != 1)
197 /* We need exactly one non-option parameter. */
198 usage (4);
200 /* The parameter describes the output path of the constructed files.
201 If the described files cannot be written return a NULL pointer. */
202 output_path = construct_output_path (argv[optind]);
203 cannot_write_why = errno;
205 /* Now that the parameters are processed we have to reset the local
206 ctype locale. (P1003.2 4.35.5.2) */
207 setlocale (LC_CTYPE, "POSIX");
209 /* Look whether the system really allows locale definitions. POSIX
210 defines error code 3 for this situation so I think it must be
211 a fatal error (see P1003.2 4.35.8). */
212 if (sysconf (_SC_2_LOCALEDEF) < 0)
213 error (3, 0, _("FATAL: system does not define `_POSIX2_LOCALEDEF'"));
215 /* Process charmap file. */
216 charset = charmap_read (charmap_file);
218 /* Now read the locale file. */
219 localedef = locfile_read (input_file, charset);
220 if (localedef->failed != 0)
221 error (4, errno, _("cannot open locale definition file `%s'"), input_file);
223 /* Perhaps we saw some `copy' instructions. Process the given list.
224 We use a very simple algorithm: we look up the list from the
225 beginning every time. */
228 int cat;
230 for (act_add_locdef = copy_list; act_add_locdef != NULL;
231 act_add_locdef = act_add_locdef->next)
233 for (cat = LC_CTYPE; cat <= LC_MESSAGES; ++cat)
234 if ((act_add_locdef->mask & (1 << cat)) != 0)
236 act_add_locdef->mask &= ~(1 << cat);
237 break;
239 if (cat <= LC_MESSAGES)
240 break;
243 if (act_add_locdef != NULL)
245 int avail = 0;
247 if (act_add_locdef->locale == NULL)
248 act_add_locdef->locale = locfile_read (act_add_locdef->name,
249 charset);
251 if (! act_add_locdef->locale->failed)
253 avail = act_add_locdef->locale->categories[cat].generic != NULL;
254 if (avail)
255 localedef->categories[cat].generic
256 = act_add_locdef->locale->categories[cat].generic;
259 if (! avail)
261 const char *locale_names[] = { "LC_COLLATE", "LC_CTYPE",
262 "LC_MONETARY", "LC_NUMERIC",
263 "LC_TIME", "LC_MESSAGES" };
264 char *fname;
265 int fd;
266 struct stat st;
268 asprintf (&fname, LOCALE_PATH "/%s/%s", act_add_locdef->name,
269 locale_names[cat]);
270 fd = open (fname, O_RDONLY);
271 if (fd == -1)
273 free (fname);
275 asprintf (&fname, LOCALE_PATH "/%s/%s/SYS_%s",
276 act_add_locdef->name, locale_names[cat],
277 locale_names[cat]);
279 fd = open (fname, O_RDONLY);
280 if (fd == -1)
281 error (5, 0, _("\
282 locale file `%s', used in `copy' statement, not found"),
283 act_add_locdef->name);
286 if (fstat (fd, &st) < 0)
287 error (5, errno, _("\
288 cannot `stat' locale file `%s'"),
289 fname);
291 localedef->len[cat] = st.st_size;
292 localedef->categories[cat].generic
293 = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
295 if (localedef->categories[cat].generic == (void *) -1)
297 size_t left = st.st_size;
298 void *read_ptr;
300 localedef->categories[cat].generic
301 = xmalloc (st.st_size);
302 read_ptr = localedef->categories[cat].generic;
306 long int n;
307 n = read (fd, read_ptr, left);
308 if (n == 1)
309 error (5, errno, _("cannot read locale file `%s'"),
310 fname);
311 read_ptr += n;
312 left -= n;
314 while (left > 0);
317 close (fd);
318 free (fname);
320 localedef->binary |= 1 << cat;
324 while (act_add_locdef != NULL);
326 /* Check the categories we processed in source form. */
327 check_all_categories (localedef, charset);
329 /* We are now able to write the data files. If warning were given we
330 do it only if it is explicitly requested (--force). */
331 if (error_message_count == 0 || force_output != 0)
333 if (cannot_write_why != 0)
334 error (4, cannot_write_why, _("cannot write output files to `%s'"),
335 output_path);
336 else
337 write_all_categories (localedef, charset, output_path);
339 else
340 error (4, 0, _("no output file produced because warning were issued"));
342 /* This exit status is prescribed by POSIX.2 4.35.7. */
343 exit (error_message_count != 0);
347 void
348 def_to_process (const char *name, int category)
350 struct copy_def_list_t *new, **rp;
352 for (rp = &copy_list; *rp != NULL; rp = &(*rp)->next)
353 if (strcmp (name, (*rp)->name) == 0)
354 break;
356 if (*rp == NULL)
358 size_t cnt;
360 *rp = (struct copy_def_list_t *) xmalloc (sizeof (**rp));
362 (*rp)->next = NULL;
363 (*rp)->name = name;
364 (*rp)->mask = 0;
365 (*rp)->locale = NULL;
367 for (cnt = 0; cnt < 6; ++cnt)
369 (*rp)->binary[cnt].data = NULL;
370 (*rp)->binary[cnt].len = 0;
373 new = *rp;
375 if ((new->mask & category) != 0)
376 /* We already have the information. This cannot happen. */
377 error (5, 0, _("\
378 category data requested more than once: should not happen"));
380 new->mask |= category;
384 /* Display usage information and exit. */
385 static void
386 usage (int status)
388 if (status != 0)
389 fprintf (stderr, _("Try `%s --help' for more information.\n"),
390 program_invocation_name);
391 else
393 printf (_("\
394 Usage: %s [OPTION]... name\n\
395 Mandatory arguments to long options are mandatory for short options too.\n\
396 -c, --force create output even if warning messages were issued\n\
397 -h, --help display this help and exit\n\
398 -f, --charmap=FILE symbolic character names defined in FILE\n\
399 -i, --inputfile=FILE source definitions are found in FILE\n\
400 -u, --code-set-name=NAME specify code set for mapping ISO 10646 elements\n\
401 -v, --verbose print more messages\n\
402 -V, --version output version information and exit\n\
403 --posix be strictly POSIX conform\n\
405 System's directory for character maps: %s\n\
406 locale files : %s\n"),
407 program_invocation_name, CHARMAP_PATH, LOCALE_PATH);
408 fputs (gettext ("Report bugs to <bug-glibc@prep.ai.mit.edu>.\n"),
409 stdout);
412 exit (status);
416 /* The address of this function will be assigned to the hook in the error
417 functions. */
418 static void
419 error_print ()
421 /* We don't want the program name to be printed in messages. Emacs'
422 compile.el does not like this. */
426 /* The parameter to localedef describes the output path. If it does
427 contain a '/' character it is a relativ path. Otherwise it names the
428 locale this definition is for. */
429 static const char *
430 construct_output_path (char *path)
432 const char *normal = NULL;
433 char *result;
435 if (strchr (path, '/') == NULL)
437 /* This is a system path. First examine whether the locale name
438 contains a reference to the codeset. This should be
439 normalized. */
440 char *startp, *endp;
442 startp = path;
443 /* We must be prepared for finding a CEN name or a location of
444 the introducing `.' where it is not possible anymore. */
445 while (*startp != '\0' && *startp != '@' && *startp != '.'
446 && *startp != '+' && *startp != ',')
447 ++startp;
448 if (*startp == '.')
450 /* We found a codeset specification. Now find the end. */
451 endp = ++startp;
452 while (*endp != '\0' && *endp != '@')
453 ++endp;
455 if (endp > startp)
456 normal = _nl_normalize_codeset (startp, endp - startp);
458 else
459 /* This is to keep gcc quiet. */
460 endp = NULL;
462 /* We put an additional '\0' at the end of the string because at
463 the end of the function we need another byte for the trailing
464 '/'. */
465 if (normal == NULL)
466 asprintf (&result, "%s/%s\0", LOCALE_PATH, path);
467 else
468 asprintf (&result, "%s/%.*s%s%s\0", LOCALE_PATH, startp - path, path,
469 normal, endp);
471 else
473 /* This is a user path. Please note the additional byte in the
474 memory allocation. */
475 result = xmalloc (strlen (path) + 2);
476 strcpy (result, path);
479 errno = 0;
481 if (euidaccess (result, W_OK) == -1)
482 /* Perhaps the directory does not exist now. Try to create it. */
483 if (errno == ENOENT)
485 errno = 0;
486 mkdir (result, 0777);
489 strcat (result, "/");
491 return result;