]> git.ipfire.org Git - thirdparty/glibc.git/blame - locale/programs/localedef.c
update from main archive
[thirdparty/glibc.git] / locale / programs / localedef.c
CommitLineData
19bc17a9
RM
1/* Copyright (C) 1995, 1996 Free Software Foundation, Inc.
2This file is part of the GNU C Library.
7a12c6bb 3Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
19bc17a9
RM
4
5The GNU C Library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Library General Public License as
7published by the Free Software Foundation; either version 2 of the
8License, or (at your option) any later version.
9
10The GNU C Library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Library General Public License for more details.
14
15You should have received a copy of the GNU Library General Public
16License along with the GNU C Library; see the file COPYING.LIB. If
17not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18Boston, MA 02111-1307, USA. */
19
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
23
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>
35
36#include "error.h"
37#include "charset.h"
38#include "locfile.h"
7a12c6bb 39#include "../intl/loadinfo.h"
19bc17a9
RM
40
41/* Undefine the following line in the production version. */
42/* #define NDEBUG 1 */
43#include <assert.h>
44
45
46/* List of locale definition files which are used in `copy' instructions. */
47struct copy_def_list_t
48{
49 struct copy_def_list_t *next;
50
51 const char *name;
52 int mask;
53
54 struct localedef_t *locale;
55
56 struct
57 {
58 void *data;
59 size_t len;
60 } binary[6];
61};
62
63
64/* List of copied locales. */
65struct copy_def_list_t *copy_list;
66
67/* If this is defined be POSIX conform. */
68int posix_conformance;
69
19bc17a9
RM
70/* If not zero give a lot more messages. */
71int verbose;
72
73
74
75/* Long options. */
76static const struct option long_options[] =
77{
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 }
87};
88
89
90/* Prototypes for global functions. */
91void *xmalloc (size_t __n);
92
93/* Prototypes for local functions. */
94static void usage (int status) __attribute__ ((noreturn));
95static void error_print (void);
7a12c6bb 96static const char *construct_output_path (char *path);
19bc17a9
RM
97
98
99int
100main (int argc, char *argv[])
101{
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;
114
115 /* Set initial values for global varaibles. */
116 copy_list = NULL;
117 posix_conformance = getenv ("POSIXLY_CORRECT") != NULL;
19bc17a9
RM
118 error_print_progname = error_print;
119 verbose = 0;
120
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, "");
125
126 /* Initialize the message catalog. */
19bc17a9 127 textdomain (_libc_intl_domainname);
19bc17a9
RM
128
129 while ((optchar = getopt_long (argc, argv, "cf:hi:u:vV", long_options, NULL))
130 != EOF)
131 switch (optchar)
132 {
133 case '\0': /* Long option. */
134 break;
135
136 case 'c':
137 force_output = 1;
138 break;
139
140 case 'f':
141 charmap_file = optarg;
142 break;
143
144 case 'h':
145 do_help = 1;
146 break;
147
148 case 'i':
149 input_file = optarg;
150 break;
151
152 case 'u':
153 ucs_csn = optarg;
154 break;
155
156 case 'v':
157 verbose = 1;
158 break;
159
160 case 'V':
161 do_version = 1;
162 break;
163
164 default:
165 usage (4); /* A value >3 is forced by POSIX. */
166 break;
167 }
168
169 /* POSIX.2 requires to be verbose about missing characters in the
170 character map. */
171 verbose |= posix_conformance;
172
173 /* Version information is requested. */
174 if (do_version)
175 {
c4029823 176 fprintf (stderr, "localedef (GNU %s) %s\n", PACKAGE, VERSION);
e320cbc9
UD
177 fprintf (stderr, _("\
178Copyright (C) %s Free Software Foundation, Inc.\n\
179This is free software; see the source for copying conditions. There is NO\n\
180warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
181"), "1995, 1996");
c4029823
UD
182 fprintf (stderr, _("Written by %s.\n"),
183 "Ulrich Drepper");
e320cbc9 184
19bc17a9
RM
185 exit (0);
186 }
187
188 /* Help is requested. */
189 if (do_help)
190 /* Possible violation: POSIX.2 4.35.8 defines the return value 0 as
191 "No errors occured and the locale(s) were successfully created."
192 But giving a other value than 0 does not make sense here. It
193 is perhaps not that important because POSIX does not specify the
194 -h option for localedef. */
195 usage (0);
196
197 if (argc - optind != 1)
198 /* We need exactly one non-option parameter. */
199 usage (4);
200
201 /* The parameter describes the output path of the constructed files.
202 If the described files cannot be written return a NULL pointer. */
203 output_path = construct_output_path (argv[optind]);
204 cannot_write_why = errno;
205
206 /* Now that the parameters are processed we have to reset the local
207 ctype locale. (P1003.2 4.35.5.2) */
208 setlocale (LC_CTYPE, "POSIX");
209
210 /* Look whether the system really allows locale definitions. POSIX
211 defines error code 3 for this situation so I think it must be
212 a fatal error (see P1003.2 4.35.8). */
213 if (sysconf (_SC_2_LOCALEDEF) < 0)
214 error (3, 0, _("FATAL: system does not define `_POSIX2_LOCALEDEF'"));
215
216 /* Process charmap file. */
217 charset = charmap_read (charmap_file);
218
219 /* Now read the locale file. */
220 localedef = locfile_read (input_file, charset);
221 if (localedef->failed != 0)
222 error (4, errno, _("cannot open locale definition file `%s'"), input_file);
223
224 /* Perhaps we saw some `copy' instructions. Process the given list.
225 We use a very simple algorithm: we look up the list from the
226 beginning every time. */
227 do
228 {
229 int cat;
230
231 for (act_add_locdef = copy_list; act_add_locdef != NULL;
232 act_add_locdef = act_add_locdef->next)
233 {
75cd5204 234 for (cat = LC_CTYPE; cat <= LC_MESSAGES; ++cat)
19bc17a9
RM
235 if ((act_add_locdef->mask & (1 << cat)) != 0)
236 {
237 act_add_locdef->mask &= ~(1 << cat);
238 break;
239 }
240 if (cat <= LC_MESSAGES)
241 break;
242 }
243
244 if (act_add_locdef != NULL)
245 {
246 int avail = 0;
247
248 if (act_add_locdef->locale == NULL)
249 act_add_locdef->locale = locfile_read (act_add_locdef->name,
250 charset);
251
252 if (! act_add_locdef->locale->failed)
253 {
254 avail = act_add_locdef->locale->categories[cat].generic != NULL;
255 if (avail)
256 localedef->categories[cat].generic
257 = act_add_locdef->locale->categories[cat].generic;
258 }
259
260 if (! avail)
261 {
262 const char *locale_names[] = { "LC_COLLATE", "LC_CTYPE",
263 "LC_MONETARY", "LC_NUMERIC",
264 "LC_TIME", "LC_MESSAGES" };
265 char *fname;
266 int fd;
267 struct stat st;
268
269 asprintf (&fname, LOCALE_PATH "/%s/%s", act_add_locdef->name,
270 locale_names[cat]);
271 fd = open (fname, O_RDONLY);
272 if (fd == -1)
273 {
274 free (fname);
275
276 asprintf (&fname, LOCALE_PATH "/%s/%s/SYS_%s",
277 act_add_locdef->name, locale_names[cat],
278 locale_names[cat]);
279
280 fd = open (fname, O_RDONLY);
281 if (fd == -1)
282 error (5, 0, _("\
283locale file `%s', used in `copy' statement, not found"),
284 act_add_locdef->name);
285 }
286
287 if (fstat (fd, &st) < 0)
288 error (5, errno, _("\
289cannot `stat' locale file `%s'"),
290 fname);
291
292 localedef->len[cat] = st.st_size;
293 localedef->categories[cat].generic
294 = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
295
296 if (localedef->categories[cat].generic == (void *) -1)
297 {
298 size_t left = st.st_size;
299 void *read_ptr;
300
301 localedef->categories[cat].generic
302 = xmalloc (st.st_size);
303 read_ptr = localedef->categories[cat].generic;
304
305 do
306 {
307 long int n;
308 n = read (fd, read_ptr, left);
309 if (n == 1)
310 error (5, errno, _("cannot read locale file `%s'"),
311 fname);
312 read_ptr += n;
313 left -= n;
314 }
315 while (left > 0);
316 }
317
318 close (fd);
319 free (fname);
320
321 localedef->binary |= 1 << cat;
322 }
323 }
324 }
325 while (act_add_locdef != NULL);
326
327 /* Check the categories we processed in source form. */
328 check_all_categories (localedef, charset);
329
330 /* We are now able to write the data files. If warning were given we
331 do it only if it is explicitly requested (--force). */
332 if (error_message_count == 0 || force_output != 0)
333 {
334 if (cannot_write_why != 0)
335 error (4, cannot_write_why, _("cannot write output files to `%s'"),
336 output_path);
337 else
75cd5204 338 write_all_categories (localedef, charset, output_path);
19bc17a9
RM
339 }
340 else
341 error (4, 0, _("no output file produced because warning were issued"));
342
343 /* This exit status is prescribed by POSIX.2 4.35.7. */
344 exit (error_message_count != 0);
345}
346
347
348void
349def_to_process (const char *name, int category)
350{
351 struct copy_def_list_t *new, **rp;
352
353 for (rp = &copy_list; *rp != NULL; rp = &(*rp)->next)
354 if (strcmp (name, (*rp)->name) == 0)
355 break;
356
357 if (*rp == NULL)
358 {
359 size_t cnt;
360
361 *rp = (struct copy_def_list_t *) xmalloc (sizeof (**rp));
362
363 (*rp)->next = NULL;
364 (*rp)->name = name;
365 (*rp)->mask = 0;
366 (*rp)->locale = NULL;
367
368 for (cnt = 0; cnt < 6; ++cnt)
369 {
370 (*rp)->binary[cnt].data = NULL;
371 (*rp)->binary[cnt].len = 0;
372 }
373 }
374 new = *rp;
375
376 if ((new->mask & category) != 0)
377 /* We already have the information. This cannot happen. */
378 error (5, 0, _("\
379category data requested more than once: should not happen"));
380
381 new->mask |= category;
382}
383
384
385/* Display usage information and exit. */
386static void
387usage (int status)
388{
389 if (status != 0)
390 fprintf (stderr, _("Try `%s --help' for more information.\n"),
036cc82f 391 program_invocation_name);
19bc17a9 392 else
fafaa44e
UD
393 {
394 printf (_("\
19bc17a9
RM
395Usage: %s [OPTION]... name\n\
396Mandatory 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\
405\n\
406System's directory for character maps: %s\n\
fafaa44e
UD
407 locale files : %s\n"),
408 program_invocation_name, CHARMAP_PATH, LOCALE_PATH);
c4029823
UD
409 fputs (gettext ("Report bugs to <bug-glibc@prep.ai.mit.edu>.\n"),
410 stdout);
fafaa44e 411 }
19bc17a9
RM
412
413 exit (status);
414}
415
416
417/* The address of this function will be assigned to the hook in the error
418 functions. */
419static void
420error_print ()
421{
422 /* We don't want the program name to be printed in messages. Emacs'
423 compile.el does not like this. */
424}
425
426
427/* The parameter to localedef describes the output path. If it does
428 contain a '/' character it is a relativ path. Otherwise it names the
429 locale this definition is for. */
430static const char *
7a12c6bb 431construct_output_path (char *path)
19bc17a9 432{
c4029823 433 const char *normal = NULL;
19bc17a9
RM
434 char *result;
435
436 if (strchr (path, '/') == NULL)
437 {
7a12c6bb
RM
438 /* This is a system path. First examine whether the locale name
439 contains a reference to the codeset. This should be
440 normalized. */
441 char *startp, *endp;
442
443 startp = path;
444 /* We must be prepared for finding a CEN name or a location of
445 the introducing `.' where it is not possible anymore. */
446 while (*startp != '\0' && *startp != '@' && *startp != '.'
447 && *startp != '+' && *startp != ',')
448 ++startp;
449 if (*startp == '.')
450 {
451 /* We found a codeset specification. Now find the end. */
452 endp = ++startp;
453 while (*endp != '\0' && *endp != '@')
454 ++endp;
455
456 if (endp > startp)
457 normal = _nl_normalize_codeset (startp, endp - startp);
458 }
c4029823
UD
459 else
460 /* This is to keep gcc quiet. */
461 endp = NULL;
19bc17a9 462
7a12c6bb
RM
463 /* We put an additional '\0' at the end of the string because at
464 the end of the function we need another byte for the trailing
465 '/'. */
466 if (normal == NULL)
467 asprintf (&result, "%s/%s\0", LOCALE_PATH, path);
468 else
469 asprintf (&result, "%s/%.*s%s%s\0", LOCALE_PATH, startp - path, path,
470 normal, endp);
19bc17a9
RM
471 }
472 else
473 {
7a12c6bb
RM
474 /* This is a user path. Please note the additional byte in the
475 memory allocation. */
19bc17a9 476 result = xmalloc (strlen (path) + 2);
7a12c6bb 477 strcpy (result, path);
19bc17a9
RM
478 }
479
480 errno = 0;
481
482 if (euidaccess (result, W_OK) == -1)
483 /* Perhaps the directory does not exist now. Try to create it. */
484 if (errno == ENOENT)
485 {
486 errno = 0;
487 mkdir (result, 0777);
488 }
489
490 strcat (result, "/");
491
492 return result;
493}