* posix/glob.h (__glob_opendir_hook, __glob_readdir_hook,
[glibc.git] / locale / programs / localedef.c
blobe9fb6d57a6a52b21a60070be06e366c88c91242c
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 /* Name of the running program. */
71 const char *program_name;
73 /* If not zero give a lot more messages. */
74 int verbose;
78 /* Long options. */
79 static const struct option long_options[] =
81 { "charmap", required_argument, NULL, 'f' },
82 { "code-set-name", required_argument, NULL, 'u' },
83 { "help", no_argument, NULL, 'h' },
84 { "force", no_argument, NULL, 'c' },
85 { "inputfile", required_argument, NULL, 'i' },
86 { "posix", no_argument, &posix_conformance, 1 },
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 varaibles. */
119 copy_list = NULL;
120 posix_conformance = getenv ("POSIXLY_CORRECT") != NULL;
121 program_name = argv[0];
122 error_print_progname = error_print;
123 verbose = 0;
125 /* Set locale. Do not set LC_ALL because the other categories must
126 not be affected (acccording to POSIX.2). */
127 setlocale (LC_MESSAGES, "");
128 setlocale (LC_CTYPE, "");
130 /* Initialize the message catalog. */
131 #if 0
132 /* In the final version for glibc we can use the variable. */
133 textdomain (_libc_intl_domainname);
134 #else
135 textdomain ("SYS_libc");
136 #endif
138 while ((optchar = getopt_long (argc, argv, "cf:hi:u:vV", long_options, NULL))
139 != EOF)
140 switch (optchar)
142 case '\0': /* Long option. */
143 break;
145 case 'c':
146 force_output = 1;
147 break;
149 case 'f':
150 charmap_file = optarg;
151 break;
153 case 'h':
154 do_help = 1;
155 break;
157 case 'i':
158 input_file = optarg;
159 break;
161 case 'u':
162 ucs_csn = optarg;
163 break;
165 case 'v':
166 verbose = 1;
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 fprintf (stderr, "%s - GNU %s %s\n", program_name, PACKAGE, VERSION);
186 exit (0);
189 /* Help is requested. */
190 if (do_help)
191 /* Possible violation: POSIX.2 4.35.8 defines the return value 0 as
192 "No errors occured and the locale(s) were successfully created."
193 But giving a other value than 0 does not make sense here. It
194 is perhaps not that important because POSIX does not specify the
195 -h option for localedef. */
196 usage (0);
198 if (argc - optind != 1)
199 /* We need exactly one non-option parameter. */
200 usage (4);
202 /* The parameter describes the output path of the constructed files.
203 If the described files cannot be written return a NULL pointer. */
204 output_path = construct_output_path (argv[optind]);
205 cannot_write_why = errno;
207 /* Now that the parameters are processed we have to reset the local
208 ctype locale. (P1003.2 4.35.5.2) */
209 setlocale (LC_CTYPE, "POSIX");
211 /* Look whether the system really allows locale definitions. POSIX
212 defines error code 3 for this situation so I think it must be
213 a fatal error (see P1003.2 4.35.8). */
214 if (sysconf (_SC_2_LOCALEDEF) < 0)
215 error (3, 0, _("FATAL: system does not define `_POSIX2_LOCALEDEF'"));
217 /* Process charmap file. */
218 charset = charmap_read (charmap_file);
220 /* Now read the locale file. */
221 localedef = locfile_read (input_file, charset);
222 if (localedef->failed != 0)
223 error (4, errno, _("cannot open locale definition file `%s'"), input_file);
225 /* Perhaps we saw some `copy' instructions. Process the given list.
226 We use a very simple algorithm: we look up the list from the
227 beginning every time. */
230 int cat;
232 for (act_add_locdef = copy_list; act_add_locdef != NULL;
233 act_add_locdef = act_add_locdef->next)
235 for (cat = LC_CTYPE; cat <= LC_MESSAGES; ++cat)
236 if ((act_add_locdef->mask & (1 << cat)) != 0)
238 act_add_locdef->mask &= ~(1 << cat);
239 break;
241 if (cat <= LC_MESSAGES)
242 break;
245 if (act_add_locdef != NULL)
247 int avail = 0;
249 if (act_add_locdef->locale == NULL)
250 act_add_locdef->locale = locfile_read (act_add_locdef->name,
251 charset);
253 if (! act_add_locdef->locale->failed)
255 avail = act_add_locdef->locale->categories[cat].generic != NULL;
256 if (avail)
257 localedef->categories[cat].generic
258 = act_add_locdef->locale->categories[cat].generic;
261 if (! avail)
263 const char *locale_names[] = { "LC_COLLATE", "LC_CTYPE",
264 "LC_MONETARY", "LC_NUMERIC",
265 "LC_TIME", "LC_MESSAGES" };
266 char *fname;
267 int fd;
268 struct stat st;
270 asprintf (&fname, LOCALE_PATH "/%s/%s", act_add_locdef->name,
271 locale_names[cat]);
272 fd = open (fname, O_RDONLY);
273 if (fd == -1)
275 free (fname);
277 asprintf (&fname, LOCALE_PATH "/%s/%s/SYS_%s",
278 act_add_locdef->name, locale_names[cat],
279 locale_names[cat]);
281 fd = open (fname, O_RDONLY);
282 if (fd == -1)
283 error (5, 0, _("\
284 locale file `%s', used in `copy' statement, not found"),
285 act_add_locdef->name);
288 if (fstat (fd, &st) < 0)
289 error (5, errno, _("\
290 cannot `stat' locale file `%s'"),
291 fname);
293 localedef->len[cat] = st.st_size;
294 localedef->categories[cat].generic
295 = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
297 if (localedef->categories[cat].generic == (void *) -1)
299 size_t left = st.st_size;
300 void *read_ptr;
302 localedef->categories[cat].generic
303 = xmalloc (st.st_size);
304 read_ptr = localedef->categories[cat].generic;
308 long int n;
309 n = read (fd, read_ptr, left);
310 if (n == 1)
311 error (5, errno, _("cannot read locale file `%s'"),
312 fname);
313 read_ptr += n;
314 left -= n;
316 while (left > 0);
319 close (fd);
320 free (fname);
322 localedef->binary |= 1 << cat;
326 while (act_add_locdef != NULL);
328 /* Check the categories we processed in source form. */
329 check_all_categories (localedef, charset);
331 /* We are now able to write the data files. If warning were given we
332 do it only if it is explicitly requested (--force). */
333 if (error_message_count == 0 || force_output != 0)
335 if (cannot_write_why != 0)
336 error (4, cannot_write_why, _("cannot write output files to `%s'"),
337 output_path);
338 else
339 write_all_categories (localedef, charset, output_path);
341 else
342 error (4, 0, _("no output file produced because warning were issued"));
344 /* This exit status is prescribed by POSIX.2 4.35.7. */
345 exit (error_message_count != 0);
349 void
350 def_to_process (const char *name, int category)
352 struct copy_def_list_t *new, **rp;
354 for (rp = &copy_list; *rp != NULL; rp = &(*rp)->next)
355 if (strcmp (name, (*rp)->name) == 0)
356 break;
358 if (*rp == NULL)
360 size_t cnt;
362 *rp = (struct copy_def_list_t *) xmalloc (sizeof (**rp));
364 (*rp)->next = NULL;
365 (*rp)->name = name;
366 (*rp)->mask = 0;
367 (*rp)->locale = NULL;
369 for (cnt = 0; cnt < 6; ++cnt)
371 (*rp)->binary[cnt].data = NULL;
372 (*rp)->binary[cnt].len = 0;
375 new = *rp;
377 if ((new->mask & category) != 0)
378 /* We already have the information. This cannot happen. */
379 error (5, 0, _("\
380 category data requested more than once: should not happen"));
382 new->mask |= category;
386 /* Display usage information and exit. */
387 static void
388 usage (int status)
390 if (status != 0)
391 fprintf (stderr, _("Try `%s --help' for more information.\n"),
392 program_name);
393 else
394 printf (_("\
395 Usage: %s [OPTION]... name\n\
396 Mandatory arguments to long options are mandatory for short options too.\n\
397 -c, --force create output even if warning messages were issued\n\
398 -h, --help display this help and exit\n\
399 -f, --charmap=FILE symbolic character names defined in FILE\n\
400 -i, --inputfile=FILE source definitions are found in FILE\n\
401 -u, --code-set-name=NAME specify code set for mapping ISO 10646 elements\n\
402 -v, --verbose print more messages\n\
403 -V, --version output version information and exit\n\
404 --posix be strictly POSIX conform\n\
406 System's directory for character maps: %s\n\
407 locale files : %s\n"),
408 program_name, CHARMAP_PATH, LOCALE_PATH);
410 exit (status);
414 /* The address of this function will be assigned to the hook in the error
415 functions. */
416 static void
417 error_print ()
419 /* We don't want the program name to be printed in messages. Emacs'
420 compile.el does not like this. */
424 /* The parameter to localedef describes the output path. If it does
425 contain a '/' character it is a relativ path. Otherwise it names the
426 locale this definition is for. */
427 static const char *
428 construct_output_path (char *path)
430 char *normal = NULL;
431 char *result;
433 if (strchr (path, '/') == NULL)
435 /* This is a system path. First examine whether the locale name
436 contains a reference to the codeset. This should be
437 normalized. */
438 char *startp, *endp;
440 startp = path;
441 /* We must be prepared for finding a CEN name or a location of
442 the introducing `.' where it is not possible anymore. */
443 while (*startp != '\0' && *startp != '@' && *startp != '.'
444 && *startp != '+' && *startp != ',')
445 ++startp;
446 if (*startp == '.')
448 /* We found a codeset specification. Now find the end. */
449 endp = ++startp;
450 while (*endp != '\0' && *endp != '@')
451 ++endp;
453 if (endp > startp)
454 normal = _nl_normalize_codeset (startp, endp - startp);
457 /* We put an additional '\0' at the end of the string because at
458 the end of the function we need another byte for the trailing
459 '/'. */
460 if (normal == NULL)
461 asprintf (&result, "%s/%s\0", LOCALE_PATH, path);
462 else
463 asprintf (&result, "%s/%.*s%s%s\0", LOCALE_PATH, startp - path, path,
464 normal, endp);
466 else
468 /* This is a user path. Please note the additional byte in the
469 memory allocation. */
470 result = xmalloc (strlen (path) + 2);
471 strcpy (result, path);
474 errno = 0;
476 if (euidaccess (result, W_OK) == -1)
477 /* Perhaps the directory does not exist now. Try to create it. */
478 if (errno == ENOENT)
480 errno = 0;
481 mkdir (result, 0777);
484 strcat (result, "/");
486 return result;