1 /* Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1995.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
41 /* Undefine the following line in the production version. */
42 /* #define NDEBUG 1 */
46 /* List of copied locales. */
47 struct copy_def_list_t
*copy_list
;
49 /* If this is defined be POSIX conform. */
50 int posix_conformance
;
52 /* If not zero give a lot more messages. */
55 /* If not zero suppress warnings and information messages. */
58 /* If not zero, produce old-style hash table instead of 3-level access tables. */
61 /* If not zero force output even if warning were issued. */
62 static int force_output
;
64 /* Prefix for output files. */
65 const char *output_prefix
;
67 /* Name of the character map file. */
68 static const char *charmap_file
;
70 /* Name of the locale definition file. */
71 static const char *input_file
;
73 /* Name of the repertoire map file. */
74 const char *repertoire_global
;
76 /* List of all locales. */
77 static struct localedef_t
*locales
;
80 /* Name and version of program. */
81 static void print_version (FILE *stream
, struct argp_state
*state
);
82 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
86 #define OPT_OLDSTYLE 3
89 /* Definitions of arguments for argp functions. */
90 static const struct argp_option options
[] =
92 { NULL
, 0, NULL
, 0, N_("Input Files:") },
93 { "charmap", 'f', "FILE", 0,
94 N_("Symbolic character names defined in FILE") },
95 { "inputfile", 'i', "FILE", 0, N_("Source definitions are found in FILE") },
96 { "repertoire-map", 'u', "FILE", 0,
97 N_("FILE contains mapping from symbolic names to UCS4 values") },
99 { NULL
, 0, NULL
, 0, N_("Output control:") },
100 { "force", 'c', NULL
, 0,
101 N_("Create output even if warning messages were issued") },
102 { "old-style", OPT_OLDSTYLE
, NULL
, 0, N_("Create old-style tables") },
103 { "prefix", OPT_PREFIX
, "PATH", 0, N_("Optional output file prefix") },
104 { "posix", OPT_POSIX
, NULL
, 0, N_("Be strictly POSIX conform") },
105 { "quiet", OPT_QUIET
, NULL
, 0,
106 N_("Suppress warnings and information messages") },
107 { "verbose", 'v', NULL
, 0, N_("Print more messages") },
108 { NULL
, 0, NULL
, 0, NULL
}
111 /* Short description of program. */
112 static const char doc
[] = N_("Compile locale specification");
114 /* Strings for arguments in help texts. */
115 static const char args_doc
[] = N_("NAME");
117 /* Prototype for option handler. */
118 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
120 /* Function to print some extra text in the help message. */
121 static char *more_help (int key
, const char *text
, void *input
);
123 /* Data structure to communicate with argp functions. */
124 static struct argp argp
=
126 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
130 /* Prototypes for global functions. */
131 extern void *xmalloc (size_t __n
);
133 /* Prototypes for local functions. */
134 static void error_print (void);
135 static const char *construct_output_path (char *path
);
136 static const char *normalize_codeset (const char *codeset
, size_t name_len
);
140 main (int argc
, char *argv
[])
142 const char *output_path
;
143 int cannot_write_why
;
144 struct charmap_t
*charmap
;
145 struct localedef_t global
;
148 /* Set initial values for global variables. */
150 posix_conformance
= getenv ("POSIXLY_CORRECT") != NULL
;
151 error_print_progname
= error_print
;
153 /* Set locale. Do not set LC_ALL because the other categories must
154 not be affected (according to POSIX.2). */
155 setlocale (LC_MESSAGES
, "");
156 setlocale (LC_CTYPE
, "");
158 /* Initialize the message catalog. */
159 textdomain (_libc_intl_domainname
);
161 /* Parse and process arguments. */
162 argp_err_exit_status
= 4;
163 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
165 /* POSIX.2 requires to be verbose about missing characters in the
167 verbose
|= posix_conformance
;
169 if (argc
- remaining
!= 1)
171 /* We need exactly one non-option parameter. */
172 argp_help (&argp
, stdout
, ARGP_HELP_SEE
| ARGP_HELP_EXIT_ERR
,
173 program_invocation_short_name
);
177 /* The parameter describes the output path of the constructed files.
178 If the described files cannot be written return a NULL pointer. */
179 output_path
= construct_output_path (argv
[remaining
]);
180 cannot_write_why
= errno
;
182 /* Now that the parameters are processed we have to reset the local
183 ctype locale. (P1003.2 4.35.5.2) */
184 setlocale (LC_CTYPE
, "POSIX");
186 /* Look whether the system really allows locale definitions. POSIX
187 defines error code 3 for this situation so I think it must be
188 a fatal error (see P1003.2 4.35.8). */
189 if (sysconf (_SC_2_LOCALEDEF
) < 0)
190 error (3, 0, _("FATAL: system does not define `_POSIX2_LOCALEDEF'"));
192 /* Process charmap file. */
193 charmap
= charmap_read (charmap_file
, verbose
, be_quiet
, 1);
195 /* Add the first entry in the locale list. */
196 memset (&global
, '\0', sizeof (struct localedef_t
));
197 global
.name
= input_file
?: "/dev/stdin";
198 global
.needed
= ALL_LOCALES
;
201 /* Now read the locale file. */
202 if (locfile_read (&global
, charmap
) != 0)
203 error (4, errno
, _("cannot open locale definition file `%s'"), input_file
);
205 /* Perhaps we saw some `copy' instructions. */
208 struct localedef_t
*runp
= locales
;
210 while (runp
!= NULL
&& (runp
->needed
& runp
->avail
) == runp
->needed
)
214 /* Everything read. */
217 if (locfile_read (runp
, charmap
) != 0)
218 error (4, errno
, _("cannot open locale definition file `%s'"),
222 /* Check the categories we processed in source form. */
223 check_all_categories (locales
, charmap
);
225 /* We are now able to write the data files. If warning were given we
226 do it only if it is explicitly requested (--force). */
227 if (error_message_count
== 0 || force_output
!= 0)
229 if (cannot_write_why
!= 0)
230 error (4, cannot_write_why
, _("cannot write output files to `%s'"),
233 write_all_categories (locales
, charmap
, output_path
);
236 error (4, 0, _("no output file produced because warning were issued"));
238 /* This exit status is prescribed by POSIX.2 4.35.7. */
239 exit (error_message_count
!= 0);
243 /* Handle program arguments. */
245 parse_opt (int key
, char *arg
, struct argp_state
*state
)
253 posix_conformance
= 1;
271 repertoire_global
= arg
;
277 return ARGP_ERR_UNKNOWN
;
284 more_help (int key
, const char *text
, void *input
)
290 case ARGP_KEY_HELP_EXTRA
:
291 /* We print some extra information. */
292 asprintf (&cp
, gettext ("\
293 System's directory for character maps : %s\n\
294 repertoire maps: %s\n\
297 CHARMAP_PATH
, REPERTOIREMAP_PATH
, LOCALE_PATH
, gettext ("\
298 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
303 return (char *) text
;
306 /* Print the version information. */
308 print_version (FILE *stream
, struct argp_state
*state
)
310 fprintf (stream
, "localedef (GNU %s) %s\n", PACKAGE
, VERSION
);
311 fprintf (stream
, gettext ("\
312 Copyright (C) %s Free Software Foundation, Inc.\n\
313 This is free software; see the source for copying conditions. There is NO\n\
314 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
316 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
320 /* The address of this function will be assigned to the hook in the error
328 /* The parameter to localedef describes the output path. If it does
329 contain a '/' character it is a relative path. Otherwise it names the
330 locale this definition is for. */
332 construct_output_path (char *path
)
334 const char *normal
= NULL
;
338 if (strchr (path
, '/') == NULL
)
340 /* This is a system path. First examine whether the locale name
341 contains a reference to the codeset. This should be
347 /* We must be prepared for finding a CEN name or a location of
348 the introducing `.' where it is not possible anymore. */
349 while (*startp
!= '\0' && *startp
!= '@' && *startp
!= '.'
350 && *startp
!= '+' && *startp
!= ',')
354 /* We found a codeset specification. Now find the end. */
356 while (*endp
!= '\0' && *endp
!= '@')
360 normal
= normalize_codeset (startp
, endp
- startp
);
363 /* This is to keep gcc quiet. */
366 /* We put an additional '\0' at the end of the string because at
367 the end of the function we need another byte for the trailing
370 n
= asprintf (&result
, "%s%s/%s%c",
371 output_prefix
?: "", LOCALEDIR
, path
, '\0');
373 n
= asprintf (&result
, "%s%s/%.*s%s%s%c",
374 output_prefix
?: "", LOCALEDIR
,
375 (int) (startp
- path
), path
, normal
, endp
, '\0');
377 endp
= result
+ n
- 1;
381 /* This is a user path. Please note the additional byte in the
382 memory allocation. */
383 size_t len
= strlen (path
) + 1;
384 result
= xmalloc (len
+ 1);
385 endp
= mempcpy (result
, path
, len
) - 1;
390 if (euidaccess (result
, W_OK
) == -1)
391 /* Perhaps the directory does not exist now. Try to create it. */
395 mkdir (result
, 0777);
405 /* Normalize codeset name. There is no standard for the codeset
406 names. Normalization allows the user to use any of the common
409 normalize_codeset (codeset
, name_len
)
419 for (cnt
= 0; cnt
< name_len
; ++cnt
)
420 if (isalnum (codeset
[cnt
]))
424 if (isalpha (codeset
[cnt
]))
428 retval
= (char *) malloc ((only_digit
? 3 : 0) + len
+ 1);
433 wp
= stpcpy (retval
, "iso");
437 for (cnt
= 0; cnt
< name_len
; ++cnt
)
438 if (isalpha (codeset
[cnt
]))
439 *wp
++ = tolower (codeset
[cnt
]);
440 else if (isdigit (codeset
[cnt
]))
441 *wp
++ = codeset
[cnt
];
446 return (const char *) retval
;
451 add_to_readlist (int locale
, const char *name
, const char *repertoire_name
,
452 int generate
, struct localedef_t
*copy_locale
)
454 struct localedef_t
*runp
= locales
;
456 while (runp
!= NULL
&& strcmp (name
, runp
->name
) != 0)
461 /* Add a new entry at the end. */
462 struct localedef_t
*newp
;
464 assert (generate
== 1);
466 newp
= xcalloc (1, sizeof (struct localedef_t
));
468 newp
->repertoire_name
= repertoire_name
;
471 runp
= locales
= newp
;
475 while (runp
->next
!= NULL
)
477 runp
= runp
->next
= newp
;
481 if (generate
&& (runp
->needed
& (1 << locale
)) != 0)
482 error (5, 0, _("circular dependencies between locale definitions"));
484 if (copy_locale
!= NULL
)
486 if (runp
->categories
[locale
].generic
!= NULL
)
487 error (5, 0, _("cannot add already read locale `%s' a second time"),
490 runp
->categories
[locale
].generic
=
491 copy_locale
->categories
[locale
].generic
;
494 runp
->needed
|= 1 << locale
;
501 find_locale (int locale
, const char *name
, const char *repertoire_name
,
502 struct charmap_t
*charmap
)
504 struct localedef_t
*result
;
506 /* Find the locale, but do not generate it since this would be a bug. */
507 result
= add_to_readlist (locale
, name
, repertoire_name
, 0, NULL
);
509 assert (result
!= NULL
);
511 if ((result
->avail
& (1 << locale
)) == 0
512 && locfile_read (result
, charmap
) != 0)
513 error (4, errno
, _("cannot open locale definition file `%s'"),
521 load_locale (int locale
, const char *name
, const char *repertoire_name
,
522 struct charmap_t
*charmap
, struct localedef_t
*copy_locale
)
524 struct localedef_t
*result
;
526 /* Generate the locale if it does not exist. */
527 result
= add_to_readlist (locale
, name
, repertoire_name
, 1, copy_locale
);
529 assert (result
!= NULL
);
531 if ((result
->avail
& (1 << locale
)) == 0
532 && locfile_read (result
, charmap
) != 0)
533 error (4, errno
, _("cannot open locale definition file `%s'"),
540 turn_on_mcheck (void)
542 /* Enable `malloc' debugging. */
544 /* Use the following line for a more thorough but much slower testing. */
545 /* mcheck_pedantic (NULL); */
548 void (*__malloc_initialize_hook
) (void) = turn_on_mcheck
;