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. */
39 #include "../intl/loadinfo.h"
41 /* Undefine the following line in the production version. */
42 /* #define NDEBUG 1 */
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;
54 struct localedef_t *locale;
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. */
73 /* If not zero suppress warnings and information messages. */
76 /* If not zero force output even if warning were issued. */
77 static int force_output;
79 /* Name of the character map file. */
80 static const char *charmap_file;
82 /* Name of the locale definition file. */
83 static const char *input_file;
85 /* Name of the UCS file. */
86 static const char *ucs_csn;
89 /* Name and version of program. */
90 static void print_version (FILE *stream, struct argp_state *state);
91 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
96 /* Definitions of arguments for argp functions. */
97 static const struct argp_option options[] =
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") },
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 }
116 /* Short description of program. */
117 static const char doc[] = N_("Compile locale specification");
119 /* Strings for arguments in help texts. */
120 static const char args_doc[] = N_("NAME");
122 /* Prototype for option handler. */
123 static error_t parse_opt (int key, char *arg, struct argp_state *state);
125 /* Function to print some extra text in the help message. */
126 static char *more_help (int key, const char *text, void *input);
128 /* Data structure to communicate with argp functions. */
129 static struct argp argp =
131 options, parse_opt, args_doc, doc, NULL, more_help
135 /* Prototypes for global functions. */
136 void *xmalloc (size_t __n);
138 /* Prototypes for local functions. */
139 static void error_print (void);
140 static const char *construct_output_path (char *path);
144 main (int argc, char *argv[])
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;
152 /* Set initial values for global variables. */
154 posix_conformance = getenv ("POSIXLY_CORRECT") != NULL;
155 error_print_progname = error_print;
157 /* Set locale. Do not set LC_ALL because the other categories must
158 not be affected (according to POSIX.2). */
159 setlocale (LC_MESSAGES, "");
160 setlocale (LC_CTYPE, "");
162 /* Initialize the message catalog. */
163 textdomain (_libc_intl_domainname);
165 /* Parse and process arguments. */
166 argp_parse (&argp, argc, argv, 0, 0, NULL);
168 /* XXX POSIX is violated since for unknown option a exit value > 3
171 /* POSIX.2 requires to be verbose about missing characters in the
173 verbose |= posix_conformance;
175 if (argc - optind != 1)
177 /* We need exactly one non-option parameter. */
178 argp_help (&argp, stdout, ARGP_HELP_SEE,
179 program_invocation_short_name);
181 /* XXX Currently POSIX is violated. We must exit with code 4
182 but the argp_help function currently does not allow this. */
186 /* The parameter describes the output path of the constructed files.
187 If the described files cannot be written return a NULL pointer. */
188 output_path = construct_output_path (argv[optind]);
189 cannot_write_why = errno;
191 /* Now that the parameters are processed we have to reset the local
192 ctype locale. (P1003.2 4.35.5.2) */
193 setlocale (LC_CTYPE, "POSIX");
195 /* Look whether the system really allows locale definitions. POSIX
196 defines error code 3 for this situation so I think it must be
197 a fatal error (see P1003.2 4.35.8). */
198 if (sysconf (_SC_2_LOCALEDEF) < 0)
199 error (3, 0, _("FATAL: system does not define `_POSIX2_LOCALEDEF'"));
201 /* Process charmap file. */
202 charset = charmap_read (charmap_file);
204 /* Now read the locale file. */
205 localedef = locfile_read (input_file, charset);
206 if (localedef->failed != 0)
207 error (4, errno, _("cannot open locale definition file `%s'"), input_file);
209 /* Perhaps we saw some `copy' instructions. Process the given list.
210 We use a very simple algorithm: we look up the list from the
211 beginning every time. */
216 for (act_add_locdef = copy_list; act_add_locdef != NULL;
217 act_add_locdef = act_add_locdef->next)
219 for (cat = LC_CTYPE; cat <= LC_MESSAGES; ++cat)
220 if ((act_add_locdef->mask & (1 << cat)) != 0)
222 act_add_locdef->mask &= ~(1 << cat);
225 if (cat <= LC_MESSAGES)
229 if (act_add_locdef != NULL)
233 if (act_add_locdef->locale == NULL)
234 act_add_locdef->locale = locfile_read (act_add_locdef->name,
237 if (! act_add_locdef->locale->failed)
239 avail = act_add_locdef->locale->categories[cat].generic != NULL;
241 localedef->categories[cat].generic
242 = act_add_locdef->locale->categories[cat].generic;
247 const char *locale_names[] = { "LC_COLLATE", "LC_CTYPE",
248 "LC_MONETARY", "LC_NUMERIC",
249 "LC_TIME", "LC_MESSAGES" };
254 asprintf (&fname, LOCALE_PATH "/%s/%s", act_add_locdef->name,
256 fd = open (fname, O_RDONLY);
261 asprintf (&fname, LOCALE_PATH "/%s/%s/SYS_%s",
262 act_add_locdef->name, locale_names[cat],
265 fd = open (fname, O_RDONLY);
268 locale file `%s', used in `copy' statement, not found"),
269 act_add_locdef->name);
272 if (fstat (fd, &st) < 0)
273 error (5, errno, _("\
274 cannot `stat' locale file `%s'"),
277 localedef->len[cat] = st.st_size;
278 localedef->categories[cat].generic
279 = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
281 if (localedef->categories[cat].generic == (void *) -1)
283 size_t left = st.st_size;
286 localedef->categories[cat].generic
287 = xmalloc (st.st_size);
288 read_ptr = localedef->categories[cat].generic;
293 n = read (fd, read_ptr, left);
295 error (5, errno, _("cannot read locale file `%s'"),
306 localedef->binary |= 1 << cat;
310 while (act_add_locdef != NULL);
312 /* Check the categories we processed in source form. */
313 check_all_categories (localedef, charset);
315 /* We are now able to write the data files. If warning were given we
316 do it only if it is explicitly requested (--force). */
317 if (error_message_count == 0 || force_output != 0)
319 if (cannot_write_why != 0)
320 error (4, cannot_write_why, _("cannot write output files to `%s'"),
323 write_all_categories (localedef, charset, output_path);
326 error (4, 0, _("no output file produced because warning were issued"));
328 /* This exit status is prescribed by POSIX.2 4.35.7. */
329 exit (error_message_count != 0);
333 /* Handle program arguments. */
335 parse_opt (int key, char *arg, struct argp_state *state)
343 posix_conformance = 1;
361 return ARGP_ERR_UNKNOWN;
368 more_help (int key, const char *text, void *input)
374 case ARGP_KEY_HELP_EXTRA:
375 /* We print some extra information. */
376 asprintf (&cp, gettext ("\
377 System's directory for character maps: %s\n\
380 CHARMAP_PATH, LOCALE_PATH, gettext ("\
381 Report bugs using the `glibcbug' script to <bugs@gnu.ai.mit.edu>.\n"));
386 return (char *) text;
389 /* Print the version information. */
391 print_version (FILE *stream, struct argp_state *state)
393 fprintf (stream, "localedef (GNU %s) %s\n", PACKAGE, VERSION);
394 fprintf (stream, gettext ("\
395 Copyright (C) %s Free Software Foundation, Inc.\n\
396 This is free software; see the source for copying conditions. There is NO\n\
397 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
398 "), "1995, 1996, 1997");
399 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
404 def_to_process (const char *name, int category)
406 struct copy_def_list_t *new, **rp;
408 for (rp = ©_list; *rp != NULL; rp = &(*rp)->next)
409 if (strcmp (name, (*rp)->name) == 0)
416 *rp = (struct copy_def_list_t *) xmalloc (sizeof (**rp));
421 (*rp)->locale = NULL;
423 for (cnt = 0; cnt < 6; ++cnt)
425 (*rp)->binary[cnt].data = NULL;
426 (*rp)->binary[cnt].len = 0;
431 if ((new->mask & category) != 0)
432 /* We already have the information. This cannot happen. */
434 category data requested more than once: should not happen"));
436 new->mask |= category;
440 /* The address of this function will be assigned to the hook in the error
445 /* We don't want the program name to be printed in messages. Emacs'
446 compile.el does not like this. */
450 /* The parameter to localedef describes the output path. If it does
451 contain a '/' character it is a relative path. Otherwise it names the
452 locale this definition is for. */
454 construct_output_path (char *path)
456 const char *normal = NULL;
459 if (strchr (path, '/') == NULL)
461 /* This is a system path. First examine whether the locale name
462 contains a reference to the codeset. This should be
467 /* We must be prepared for finding a CEN name or a location of
468 the introducing `.' where it is not possible anymore. */
469 while (*startp != '\0' && *startp != '@' && *startp != '.'
470 && *startp != '+' && *startp != ',')
474 /* We found a codeset specification. Now find the end. */
476 while (*endp != '\0' && *endp != '@')
480 normal = _nl_normalize_codeset (startp, endp - startp);
483 /* This is to keep gcc quiet. */
486 /* We put an additional '\0' at the end of the string because at
487 the end of the function we need another byte for the trailing
490 asprintf (&result, "%s/%s%c", LOCALEDIR, path, '\0');
492 asprintf (&result, "%s/%.*s%s%s%c", LOCALEDIR, startp - path, path,
497 /* This is a user path. Please note the additional byte in the
498 memory allocation. */
499 result = xmalloc (strlen (path) + 2);
500 strcpy (result, path);
505 if (euidaccess (result, W_OK) == -1)
506 /* Perhaps the directory does not exist now. Try to create it. */
510 mkdir (result, 0777);
513 strcat (result, "/");