Update.
[platform/upstream/glibc.git] / locale / programs / localedef.c
1 /* Copyright (C) 1995, 1996, 1997, 1998 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.
4
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.
9
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.
14
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.  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <argp.h>
25 #include <errno.h>
26 #include <fcntl.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>
35
36 #include "error.h"
37 #include "charset.h"
38 #include "locfile.h"
39
40 /* Undefine the following line in the production version.  */
41 /* #define NDEBUG 1 */
42 #include <assert.h>
43
44
45 /* List of locale definition files which are used in `copy' instructions.  */
46 struct copy_def_list_t
47 {
48   struct copy_def_list_t *next;
49
50   const char *name;
51   int mask;
52
53   struct localedef_t *locale;
54
55   struct
56   {
57     void *data;
58     size_t len;
59   } binary[6];
60 };
61
62
63 /* List of copied locales.  */
64 struct copy_def_list_t *copy_list;
65
66 /* If this is defined be POSIX conform.  */
67 int posix_conformance;
68
69 /* If not zero give a lot more messages.  */
70 int verbose;
71
72 /* If not zero suppress warnings and information messages.  */
73 int be_quiet;
74
75 /* If not zero force output even if warning were issued.  */
76 static int force_output;
77
78 /* Name of the character map file.  */
79 static const char *charmap_file;
80
81 /* Name of the locale definition file.  */
82 static const char *input_file;
83
84 /* Name of the repertoire map file.  */
85 const char *repertoiremap;
86
87
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;
91
92 #define OPT_POSIX 1
93 #define OPT_QUIET 2
94
95 /* Definitions of arguments for argp functions.  */
96 static const struct argp_option options[] =
97 {
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   { "repertoire-map", 'u', "FILE", 0,
103     N_("file containing mapping from symbolic names to UCS4 values") },
104
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 }
113 };
114
115 /* Short description of program.  */
116 static const char doc[] = N_("Compile locale specification");
117
118 /* Strings for arguments in help texts.  */
119 static const char args_doc[] = N_("NAME");
120
121 /* Prototype for option handler.  */
122 static error_t parse_opt (int key, char *arg, struct argp_state *state);
123
124 /* Function to print some extra text in the help message.  */
125 static char *more_help (int key, const char *text, void *input);
126
127 /* Data structure to communicate with argp functions.  */
128 static struct argp argp =
129 {
130   options, parse_opt, args_doc, doc, NULL, more_help
131 };
132
133
134 /* Prototypes for global functions.  */
135 void *xmalloc (size_t __n);
136
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);
141
142
143 int
144 main (int argc, char *argv[])
145 {
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;
151   int remaining;
152
153   /* Set initial values for global variables.  */
154   copy_list = NULL;
155   posix_conformance = getenv ("POSIXLY_CORRECT") != NULL;
156   error_print_progname = error_print;
157
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, "");
162
163   /* Initialize the message catalog.  */
164   textdomain (_libc_intl_domainname);
165
166   /* Parse and process arguments.  */
167   argp_err_exit_status = 4;
168   argp_parse (&argp, argc, argv, 0, &remaining, NULL);
169
170   /* POSIX.2 requires to be verbose about missing characters in the
171      character map.  */
172   verbose |= posix_conformance;
173
174   if (argc - remaining != 1)
175     {
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);
179       exit (4);
180     }
181
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;
186
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");
190
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'"));
196
197   /* Process charmap file.  */
198   charset = charmap_read (charmap_file);
199
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);
204
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.  */
208   do
209     {
210       int cat;
211
212       for (act_add_locdef = copy_list; act_add_locdef != NULL;
213            act_add_locdef = act_add_locdef->next)
214         {
215           for (cat = LC_CTYPE; cat <= LC_MESSAGES; ++cat)
216             if ((act_add_locdef->mask & (1 << cat)) != 0)
217               {
218                 act_add_locdef->mask &= ~(1 << cat);
219                 break;
220               }
221           if (cat <= LC_MESSAGES)
222             break;
223         }
224
225       if (act_add_locdef != NULL)
226         {
227           int avail = 0;
228
229           if (act_add_locdef->locale == NULL)
230             act_add_locdef->locale = locfile_read (act_add_locdef->name,
231                                                    charset);
232
233           if (! act_add_locdef->locale->failed)
234             {
235               avail = act_add_locdef->locale->categories[cat].generic != NULL;
236               if (avail)
237                 {
238                   localedef->categories[cat].generic
239                     = act_add_locdef->locale->categories[cat].generic;
240                   localedef->avail |= 1 << cat;
241                 }
242             }
243
244           if (! avail)
245             {
246               static const char *locale_names[] =
247               {
248                 "LC_COLLATE", "LC_CTYPE", "LC_MONETARY",
249                 "LC_NUMERIC", "LC_TIME", "LC_MESSAGES"
250               };
251               char *fname;
252               int fd;
253               struct stat st;
254
255               asprintf (&fname, LOCALEDIR "/%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, LOCALEDIR "/%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, _("\
269 locale 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, _("\
275 cannot `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 == MAP_FAILED)
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);
295                       if (n == -1)
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
324         write_all_categories (localedef, charset, output_path);
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
334 /* Handle program arguments.  */
335 static error_t
336 parse_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       repertoiremap = 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
368 static char *
369 more_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 ("\
378 System's directory for character maps : %s\n\
379                        repertoire maps: %s\n\
380                        locale path    : %s\n\
381 %s"),
382                 CHARMAP_PATH, REPERTOIREMAP_PATH, LOCALE_PATH, gettext ("\
383 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
384       return cp;
385     default:
386       break;
387     }
388   return (char *) text;
389 }
390
391 /* Print the version information.  */
392 static void
393 print_version (FILE *stream, struct argp_state *state)
394 {
395   fprintf (stream, "localedef (GNU %s) %s\n", PACKAGE, VERSION);
396   fprintf (stream, gettext ("\
397 Copyright (C) %s Free Software Foundation, Inc.\n\
398 This is free software; see the source for copying conditions.  There is NO\n\
399 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
400 "), "1995, 1996, 1997");
401   fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
402 }
403
404
405 void
406 def_to_process (const char *name, int category)
407 {
408   struct copy_def_list_t *new, **rp;
409
410   for (rp = &copy_list; *rp != NULL; rp = &(*rp)->next)
411     if (strcmp (name, (*rp)->name) == 0)
412       break;
413
414   if (*rp == NULL)
415     {
416       size_t cnt;
417
418       *rp = (struct copy_def_list_t *) xmalloc (sizeof (**rp));
419
420       (*rp)->next = NULL;
421       (*rp)->name = name;
422       (*rp)->mask = 0;
423       (*rp)->locale = NULL;
424
425       for (cnt = 0; cnt < 6; ++cnt)
426         {
427           (*rp)->binary[cnt].data = NULL;
428           (*rp)->binary[cnt].len = 0;
429         }
430     }
431   new = *rp;
432
433   if ((new->mask & category) != 0)
434     /* We already have the information.  This cannot happen.  */
435     error (5, 0, _("\
436 category data requested more than once: should not happen"));
437
438   new->mask |= category;
439 }
440
441
442 /* The address of this function will be assigned to the hook in the error
443    functions.  */
444 static void
445 error_print ()
446 {
447   /* We don't want the program name to be printed in messages.  Emacs'
448      compile.el does not like this.  */
449 }
450
451
452 /* The parameter to localedef describes the output path.  If it does
453    contain a '/' character it is a relative path.  Otherwise it names the
454    locale this definition is for.  */
455 static const char *
456 construct_output_path (char *path)
457 {
458   const char *normal = NULL;
459   char *result;
460
461   if (strchr (path, '/') == NULL)
462     {
463       /* This is a system path.  First examine whether the locale name
464          contains a reference to the codeset.  This should be
465          normalized.  */
466       char *startp, *endp;
467
468       startp = path;
469       /* We must be prepared for finding a CEN name or a location of
470          the introducing `.' where it is not possible anymore.  */
471       while (*startp != '\0' && *startp != '@' && *startp != '.'
472              && *startp != '+' && *startp != ',')
473         ++startp;
474       if (*startp == '.')
475         {
476           /* We found a codeset specification.  Now find the end.  */
477           endp = ++startp;
478           while (*endp != '\0' && *endp != '@')
479             ++endp;
480
481           if (endp > startp)
482             normal = normalize_codeset (startp, endp - startp);
483         }
484       else
485         /* This is to keep gcc quiet.  */
486         endp = NULL;
487
488       /* We put an additional '\0' at the end of the string because at
489          the end of the function we need another byte for the trailing
490          '/'.  */
491       if (normal == NULL)
492         asprintf (&result, "%s/%s%c", LOCALEDIR, path, '\0');
493       else
494         asprintf (&result, "%s/%.*s%s%s%c", LOCALEDIR, startp - path, path,
495                   normal, endp, '\0');
496     }
497   else
498     {
499       /* This is a user path.  Please note the additional byte in the
500          memory allocation.  */
501       result = xmalloc (strlen (path) + 2);
502       strcpy (result, path);
503     }
504
505   errno = 0;
506
507   if (euidaccess (result, W_OK) == -1)
508     /* Perhaps the directory does not exist now.  Try to create it.  */
509     if (errno == ENOENT)
510       {
511         errno = 0;
512         mkdir (result, 0777);
513       }
514
515   strcat (result, "/");
516
517   return result;
518 }
519
520 /* Normalize codeset name.  There is no standard for the codeset
521    names.  Normalization allows the user to use any of the common
522    names.  */
523 static const char *
524 normalize_codeset (codeset, name_len)
525      const char *codeset;
526      size_t name_len;
527 {
528   int len = 0;
529   int only_digit = 1;
530   char *retval;
531   char *wp;
532   size_t cnt;
533
534   for (cnt = 0; cnt < name_len; ++cnt)
535     if (isalnum (codeset[cnt]))
536       {
537         ++len;
538
539         if (isalpha (codeset[cnt]))
540           only_digit = 0;
541       }
542
543   retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
544
545   if (retval != NULL)
546     {
547       if (only_digit)
548         wp = stpcpy (retval, "iso");
549       else
550         wp = retval;
551
552       for (cnt = 0; cnt < name_len; ++cnt)
553         if (isalpha (codeset[cnt]))
554           *wp++ = tolower (codeset[cnt]);
555         else if (isdigit (codeset[cnt]))
556           *wp++ = codeset[cnt];
557
558       *wp = '\0';
559     }
560
561   return (const char *) retval;
562 }