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. */
40 /* Undefine the following line in the production version. */
41 /* #define NDEBUG 1 */
45 /* List of locale definition files which are used in `copy' instructions. */
46 struct copy_def_list_t
48 struct copy_def_list_t *next;
53 struct localedef_t *locale;
63 /* List of copied locales. */
64 struct copy_def_list_t *copy_list;
66 /* If this is defined be POSIX conform. */
67 int posix_conformance;
69 /* If not zero give a lot more messages. */
72 /* If not zero suppress warnings and information messages. */
75 /* If not zero force output even if warning were issued. */
76 static int force_output;
78 /* Name of the character map file. */
79 static const char *charmap_file;
81 /* Name of the locale definition file. */
82 static const char *input_file;
84 /* Name of the UCS file. */
85 static const char *ucs_csn;
88 /* Name and version of program. */
89 static void print_version (FILE *stream, struct argp_state *state);
90 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
95 /* Definitions of arguments for argp functions. */
96 static const struct argp_option options[] =
98 { NULL, 0, NULL, 0, N_("Input Files:") },
99 { "charmap", 'f', "FILE", 0,
100 N_("Symbolic character names defined in FILE") },
101 { "inputfile", 'i', "FILE", 0, N_("Source definitions are found in FILE") },
102 { "code-set-name", 'u', "NAME", OPTION_HIDDEN,
103 N_("Specify code set for mapping ISO 10646 elements") },
105 { NULL, 0, NULL, 0, N_("Output control:") },
106 { "force", 'c', NULL, 0,
107 N_("Create output even if warning messages were issued") },
108 { "posix", OPT_POSIX, NULL, 0, N_("Be strictly POSIX conform") },
109 { "quiet", OPT_QUIET, NULL, 0,
110 N_("Suppress warnings and information messages") },
111 { "verbose", 'V', NULL, 0, N_("Print more messages") },
112 { NULL, 0, NULL, 0, NULL }
115 /* Short description of program. */
116 static const char doc[] = N_("Compile locale specification");
118 /* Strings for arguments in help texts. */
119 static const char args_doc[] = N_("NAME");
121 /* Prototype for option handler. */
122 static error_t parse_opt (int key, char *arg, struct argp_state *state);
124 /* Function to print some extra text in the help message. */
125 static char *more_help (int key, const char *text, void *input);
127 /* Data structure to communicate with argp functions. */
128 static struct argp argp =
130 options, parse_opt, args_doc, doc, NULL, more_help
134 /* Prototypes for global functions. */
135 void *xmalloc (size_t __n);
137 /* Prototypes for local functions. */
138 static void error_print (void);
139 static const char *construct_output_path (char *path);
140 static const char *normalize_codeset (const char *codeset, size_t name_len);
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;
153 /* Set initial values for global variables. */
155 posix_conformance = getenv ("POSIXLY_CORRECT") != NULL;
156 error_print_progname = error_print;
158 /* Set locale. Do not set LC_ALL because the other categories must
159 not be affected (according to POSIX.2). */
160 setlocale (LC_MESSAGES, "");
161 setlocale (LC_CTYPE, "");
163 /* Initialize the message catalog. */
164 textdomain (_libc_intl_domainname);
166 /* Parse and process arguments. */
167 argp_err_exit_status = 4;
168 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
170 /* POSIX.2 requires to be verbose about missing characters in the
172 verbose |= posix_conformance;
174 if (argc - remaining != 1)
176 /* We need exactly one non-option parameter. */
177 argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR,
178 program_invocation_short_name);
182 /* The parameter describes the output path of the constructed files.
183 If the described files cannot be written return a NULL pointer. */
184 output_path = construct_output_path (argv[remaining]);
185 cannot_write_why = errno;
187 /* Now that the parameters are processed we have to reset the local
188 ctype locale. (P1003.2 4.35.5.2) */
189 setlocale (LC_CTYPE, "POSIX");
191 /* Look whether the system really allows locale definitions. POSIX
192 defines error code 3 for this situation so I think it must be
193 a fatal error (see P1003.2 4.35.8). */
194 if (sysconf (_SC_2_LOCALEDEF) < 0)
195 error (3, 0, _("FATAL: system does not define `_POSIX2_LOCALEDEF'"));
197 /* Process charmap file. */
198 charset = charmap_read (charmap_file);
200 /* Now read the locale file. */
201 localedef = locfile_read (input_file, charset);
202 if (localedef->failed != 0)
203 error (4, errno, _("cannot open locale definition file `%s'"), input_file);
205 /* Perhaps we saw some `copy' instructions. Process the given list.
206 We use a very simple algorithm: we look up the list from the
207 beginning every time. */
212 for (act_add_locdef = copy_list; act_add_locdef != NULL;
213 act_add_locdef = act_add_locdef->next)
215 for (cat = LC_CTYPE; cat <= LC_MESSAGES; ++cat)
216 if ((act_add_locdef->mask & (1 << cat)) != 0)
218 act_add_locdef->mask &= ~(1 << cat);
221 if (cat <= LC_MESSAGES)
225 if (act_add_locdef != NULL)
229 if (act_add_locdef->locale == NULL)
230 act_add_locdef->locale = locfile_read (act_add_locdef->name,
233 if (! act_add_locdef->locale->failed)
235 avail = act_add_locdef->locale->categories[cat].generic != NULL;
237 localedef->categories[cat].generic
238 = act_add_locdef->locale->categories[cat].generic;
243 static const char *locale_names[] =
245 "LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
246 "LC_NUMERIC", "LC_TIME", "LC_MESSAGES"
252 asprintf (&fname, LOCALEDIR "/%s/%s", act_add_locdef->name,
254 fd = open (fname, O_RDONLY);
259 asprintf (&fname, LOCALEDIR "/%s/%s/SYS_%s",
260 act_add_locdef->name, locale_names[cat],
263 fd = open (fname, O_RDONLY);
266 locale file `%s', used in `copy' statement, not found"),
267 act_add_locdef->name);
270 if (fstat (fd, &st) < 0)
271 error (5, errno, _("\
272 cannot `stat' locale file `%s'"),
275 localedef->len[cat] = st.st_size;
276 localedef->categories[cat].generic
277 = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
279 if (localedef->categories[cat].generic == (void *) -1)
281 size_t left = st.st_size;
284 localedef->categories[cat].generic
285 = xmalloc (st.st_size);
286 read_ptr = localedef->categories[cat].generic;
291 n = read (fd, read_ptr, left);
293 error (5, errno, _("cannot read locale file `%s'"),
304 localedef->binary |= 1 << cat;
308 while (act_add_locdef != NULL);
310 /* Check the categories we processed in source form. */
311 check_all_categories (localedef, charset);
313 /* We are now able to write the data files. If warning were given we
314 do it only if it is explicitly requested (--force). */
315 if (error_message_count == 0 || force_output != 0)
317 if (cannot_write_why != 0)
318 error (4, cannot_write_why, _("cannot write output files to `%s'"),
321 write_all_categories (localedef, charset, output_path);
324 error (4, 0, _("no output file produced because warning were issued"));
326 /* This exit status is prescribed by POSIX.2 4.35.7. */
327 exit (error_message_count != 0);
331 /* Handle program arguments. */
333 parse_opt (int key, char *arg, struct argp_state *state)
341 posix_conformance = 1;
359 return ARGP_ERR_UNKNOWN;
366 more_help (int key, const char *text, void *input)
372 case ARGP_KEY_HELP_EXTRA:
373 /* We print some extra information. */
374 asprintf (&cp, gettext ("\
375 System's directory for character maps: %s\n\
378 CHARMAP_PATH, LOCALE_PATH, gettext ("\
379 Report bugs using the `glibcbug' script to <bugs@gnu.ai.mit.edu>.\n"));
384 return (char *) text;
387 /* Print the version information. */
389 print_version (FILE *stream, struct argp_state *state)
391 fprintf (stream, "localedef (GNU %s) %s\n", PACKAGE, VERSION);
392 fprintf (stream, gettext ("\
393 Copyright (C) %s Free Software Foundation, Inc.\n\
394 This is free software; see the source for copying conditions. There is NO\n\
395 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
396 "), "1995, 1996, 1997");
397 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
402 def_to_process (const char *name, int category)
404 struct copy_def_list_t *new, **rp;
406 for (rp = ©_list; *rp != NULL; rp = &(*rp)->next)
407 if (strcmp (name, (*rp)->name) == 0)
414 *rp = (struct copy_def_list_t *) xmalloc (sizeof (**rp));
419 (*rp)->locale = NULL;
421 for (cnt = 0; cnt < 6; ++cnt)
423 (*rp)->binary[cnt].data = NULL;
424 (*rp)->binary[cnt].len = 0;
429 if ((new->mask & category) != 0)
430 /* We already have the information. This cannot happen. */
432 category data requested more than once: should not happen"));
434 new->mask |= category;
438 /* The address of this function will be assigned to the hook in the error
443 /* We don't want the program name to be printed in messages. Emacs'
444 compile.el does not like this. */
448 /* The parameter to localedef describes the output path. If it does
449 contain a '/' character it is a relative path. Otherwise it names the
450 locale this definition is for. */
452 construct_output_path (char *path)
454 const char *normal = NULL;
457 if (strchr (path, '/') == NULL)
459 /* This is a system path. First examine whether the locale name
460 contains a reference to the codeset. This should be
465 /* We must be prepared for finding a CEN name or a location of
466 the introducing `.' where it is not possible anymore. */
467 while (*startp != '\0' && *startp != '@' && *startp != '.'
468 && *startp != '+' && *startp != ',')
472 /* We found a codeset specification. Now find the end. */
474 while (*endp != '\0' && *endp != '@')
478 normal = normalize_codeset (startp, endp - startp);
481 /* This is to keep gcc quiet. */
484 /* We put an additional '\0' at the end of the string because at
485 the end of the function we need another byte for the trailing
488 asprintf (&result, "%s/%s%c", LOCALEDIR, path, '\0');
490 asprintf (&result, "%s/%.*s%s%s%c", LOCALEDIR, startp - path, path,
495 /* This is a user path. Please note the additional byte in the
496 memory allocation. */
497 result = xmalloc (strlen (path) + 2);
498 strcpy (result, path);
503 if (euidaccess (result, W_OK) == -1)
504 /* Perhaps the directory does not exist now. Try to create it. */
508 mkdir (result, 0777);
511 strcat (result, "/");
516 /* Normalize codeset name. There is no standard for the codeset
517 names. Normalization allows the user to use any of the common
520 normalize_codeset (codeset, name_len)
530 for (cnt = 0; cnt < name_len; ++cnt)
531 if (isalnum (codeset[cnt]))
535 if (isalpha (codeset[cnt]))
539 retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
544 wp = stpcpy (retval, "iso");
548 for (cnt = 0; cnt < name_len; ++cnt)
549 if (isalpha (codeset[cnt]))
550 *wp++ = tolower (codeset[cnt]);
551 else if (isdigit (codeset[cnt]))
552 *wp++ = codeset[cnt];
557 return (const char *) retval;