1 /* Copyright (C) 1995,1996,1997,1998,1999,2000 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 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. */
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 force output even if warning were issued. */
59 static int force_output
;
61 /* Name of the character map file. */
62 static const char *charmap_file
;
64 /* Name of the locale definition file. */
65 static const char *input_file
;
67 /* Name of the repertoire map file. */
68 const char *repertoire_global
;
70 /* List of all locales. */
71 static struct localedef_t
*locales
;
74 /* Name and version of program. */
75 static void print_version (FILE *stream
, struct argp_state
*state
);
76 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
81 /* Definitions of arguments for argp functions. */
82 static const struct argp_option options
[] =
84 { NULL
, 0, NULL
, 0, N_("Input Files:") },
85 { "charmap", 'f', "FILE", 0,
86 N_("Symbolic character names defined in FILE") },
87 { "inputfile", 'i', "FILE", 0, N_("Source definitions are found in FILE") },
88 { "repertoire-map", 'u', "FILE", 0,
89 N_("FILE contains mapping from symbolic names to UCS4 values") },
91 { NULL
, 0, NULL
, 0, N_("Output control:") },
92 { "force", 'c', NULL
, 0,
93 N_("Create output even if warning messages were issued") },
94 { "posix", OPT_POSIX
, NULL
, 0, N_("Be strictly POSIX conform") },
95 { "quiet", OPT_QUIET
, NULL
, 0,
96 N_("Suppress warnings and information messages") },
97 { "verbose", 'v', NULL
, 0, N_("Print more messages") },
98 { NULL
, 0, NULL
, 0, NULL
}
101 /* Short description of program. */
102 static const char doc
[] = N_("Compile locale specification");
104 /* Strings for arguments in help texts. */
105 static const char args_doc
[] = N_("NAME");
107 /* Prototype for option handler. */
108 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
110 /* Function to print some extra text in the help message. */
111 static char *more_help (int key
, const char *text
, void *input
);
113 /* Data structure to communicate with argp functions. */
114 static struct argp argp
=
116 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
120 /* Prototypes for global functions. */
121 extern void *xmalloc (size_t __n
);
123 /* Prototypes for local functions. */
124 static void error_print (void);
125 static const char *construct_output_path (char *path
);
126 static const char *normalize_codeset (const char *codeset
, size_t name_len
);
130 main (int argc
, char *argv
[])
132 const char *output_path
;
133 int cannot_write_why
;
134 struct charmap_t
*charmap
;
135 struct localedef_t global
;
138 /* Set initial values for global variables. */
140 posix_conformance
= getenv ("POSIXLY_CORRECT") != NULL
;
141 error_print_progname
= error_print
;
143 /* Set locale. Do not set LC_ALL because the other categories must
144 not be affected (according to POSIX.2). */
145 setlocale (LC_MESSAGES
, "");
146 setlocale (LC_CTYPE
, "");
148 /* Initialize the message catalog. */
149 textdomain (_libc_intl_domainname
);
151 /* Parse and process arguments. */
152 argp_err_exit_status
= 4;
153 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
155 /* POSIX.2 requires to be verbose about missing characters in the
157 verbose
|= posix_conformance
;
159 if (argc
- remaining
!= 1)
161 /* We need exactly one non-option parameter. */
162 argp_help (&argp
, stdout
, ARGP_HELP_SEE
| ARGP_HELP_EXIT_ERR
,
163 program_invocation_short_name
);
167 /* The parameter describes the output path of the constructed files.
168 If the described files cannot be written return a NULL pointer. */
169 output_path
= construct_output_path (argv
[remaining
]);
170 cannot_write_why
= errno
;
172 /* Now that the parameters are processed we have to reset the local
173 ctype locale. (P1003.2 4.35.5.2) */
174 setlocale (LC_CTYPE
, "POSIX");
176 /* Look whether the system really allows locale definitions. POSIX
177 defines error code 3 for this situation so I think it must be
178 a fatal error (see P1003.2 4.35.8). */
179 if (sysconf (_SC_2_LOCALEDEF
) < 0)
180 error (3, 0, _("FATAL: system does not define `_POSIX2_LOCALEDEF'"));
182 /* Process charmap file. */
183 charmap
= charmap_read (charmap_file
);
185 /* Add the first entry in the locale list. */
186 memset (&global
, '\0', sizeof (struct localedef_t
));
187 global
.name
= input_file
;
188 global
.needed
= ALL_LOCALES
;
191 /* Now read the locale file. */
192 if (locfile_read (&global
, charmap
) != 0)
193 error (4, errno
, _("cannot open locale definition file `%s'"), input_file
);
195 /* Perhaps we saw some `copy' instructions. */
198 struct localedef_t
*runp
= locales
;
200 while (runp
!= NULL
&& (runp
->needed
& runp
->avail
) == runp
->needed
)
204 /* Everything read. */
207 if (locfile_read (runp
, charmap
) != 0)
208 error (4, errno
, _("cannot open locale definition file `%s'"),
212 /* Check the categories we processed in source form. */
213 check_all_categories (locales
, charmap
);
215 /* We are now able to write the data files. If warning were given we
216 do it only if it is explicitly requested (--force). */
217 if (error_message_count
== 0 || force_output
!= 0)
219 if (cannot_write_why
!= 0)
220 error (4, cannot_write_why
, _("cannot write output files to `%s'"),
223 write_all_categories (locales
, charmap
, output_path
);
226 error (4, 0, _("no output file produced because warning were issued"));
228 /* This exit status is prescribed by POSIX.2 4.35.7. */
229 exit (error_message_count
!= 0);
233 /* Handle program arguments. */
235 parse_opt (int key
, char *arg
, struct argp_state
*state
)
243 posix_conformance
= 1;
255 repertoire_global
= arg
;
261 return ARGP_ERR_UNKNOWN
;
268 more_help (int key
, const char *text
, void *input
)
274 case ARGP_KEY_HELP_EXTRA
:
275 /* We print some extra information. */
276 asprintf (&cp
, gettext ("\
277 System's directory for character maps : %s\n\
278 repertoire maps: %s\n\
281 CHARMAP_PATH
, REPERTOIREMAP_PATH
, LOCALE_PATH
, gettext ("\
282 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
287 return (char *) text
;
290 /* Print the version information. */
292 print_version (FILE *stream
, struct argp_state
*state
)
294 fprintf (stream
, "localedef (GNU %s) %s\n", PACKAGE
, VERSION
);
295 fprintf (stream
, gettext ("\
296 Copyright (C) %s Free Software Foundation, Inc.\n\
297 This is free software; see the source for copying conditions. There is NO\n\
298 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
300 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
304 /* The address of this function will be assigned to the hook in the error
312 /* The parameter to localedef describes the output path. If it does
313 contain a '/' character it is a relative path. Otherwise it names the
314 locale this definition is for. */
316 construct_output_path (char *path
)
318 const char *normal
= NULL
;
322 if (strchr (path
, '/') == NULL
)
324 /* This is a system path. First examine whether the locale name
325 contains a reference to the codeset. This should be
331 /* We must be prepared for finding a CEN name or a location of
332 the introducing `.' where it is not possible anymore. */
333 while (*startp
!= '\0' && *startp
!= '@' && *startp
!= '.'
334 && *startp
!= '+' && *startp
!= ',')
338 /* We found a codeset specification. Now find the end. */
340 while (*endp
!= '\0' && *endp
!= '@')
344 normal
= normalize_codeset (startp
, endp
- startp
);
347 /* This is to keep gcc quiet. */
350 /* We put an additional '\0' at the end of the string because at
351 the end of the function we need another byte for the trailing
354 n
= asprintf (&result
, "%s/%s%c", LOCALEDIR
, path
, '\0');
356 n
= asprintf (&result
, "%s/%.*s%s%s%c", LOCALEDIR
,
357 (int) (startp
- path
), path
, normal
, endp
, '\0');
363 /* This is a user path. Please note the additional byte in the
364 memory allocation. */
365 size_t len
= strlen (path
) + 1;
366 result
= xmalloc (len
+ 1);
367 endp
= mempcpy (result
, path
, len
) - 1;
372 if (euidaccess (result
, W_OK
) == -1)
373 /* Perhaps the directory does not exist now. Try to create it. */
377 mkdir (result
, 0777);
387 /* Normalize codeset name. There is no standard for the codeset
388 names. Normalization allows the user to use any of the common
391 normalize_codeset (codeset
, name_len
)
401 for (cnt
= 0; cnt
< name_len
; ++cnt
)
402 if (isalnum (codeset
[cnt
]))
406 if (isalpha (codeset
[cnt
]))
410 retval
= (char *) malloc ((only_digit
? 3 : 0) + len
+ 1);
415 wp
= stpcpy (retval
, "iso");
419 for (cnt
= 0; cnt
< name_len
; ++cnt
)
420 if (isalpha (codeset
[cnt
]))
421 *wp
++ = tolower (codeset
[cnt
]);
422 else if (isdigit (codeset
[cnt
]))
423 *wp
++ = codeset
[cnt
];
428 return (const char *) retval
;
433 add_to_readlist (int locale
, const char *name
, const char *repertoire_name
,
434 int generate
, struct localedef_t
*copy_locale
)
436 struct localedef_t
*runp
= locales
;
438 while (runp
!= NULL
&& strcmp (name
, runp
->name
) != 0)
443 /* Add a new entry at the end. */
444 struct localedef_t
*newp
;
446 assert (generate
== 1);
448 newp
= xcalloc (1, sizeof (struct localedef_t
));
450 newp
->repertoire_name
= repertoire_name
;
453 runp
= locales
= newp
;
457 while (runp
->next
!= NULL
)
459 runp
= runp
->next
= newp
;
463 if (generate
&& (runp
->needed
& (1 << locale
)) != 0)
464 error (5, 0, _("circular dependencies between locale definitions"));
466 if (copy_locale
!= NULL
)
468 if (runp
->categories
[locale
].generic
!= NULL
)
469 error (5, 0, _("cannot add already read locale `%s' a second time"),
472 runp
->categories
[locale
].generic
=
473 copy_locale
->categories
[locale
].generic
;
476 runp
->needed
|= 1 << locale
;
483 find_locale (int locale
, const char *name
, const char *repertoire_name
,
484 struct charmap_t
*charmap
)
486 struct localedef_t
*result
;
488 /* Find the locale, but do not generate it since this would be a bug. */
489 result
= add_to_readlist (locale
, name
, repertoire_name
, 0, NULL
);
491 assert (result
!= NULL
);
493 if ((result
->avail
& (1 << locale
)) == 0
494 && locfile_read (result
, charmap
) != 0)
495 error (4, errno
, _("cannot open locale definition file `%s'"),
503 load_locale (int locale
, const char *name
, const char *repertoire_name
,
504 struct charmap_t
*charmap
, struct localedef_t
*copy_locale
)
506 struct localedef_t
*result
;
508 /* Generate the locale if it does not exist. */
509 result
= add_to_readlist (locale
, name
, repertoire_name
, 1, copy_locale
);
511 assert (result
!= NULL
);
513 if ((result
->avail
& (1 << locale
)) == 0
514 && locfile_read (result
, charmap
) != 0)
515 error (4, errno
, _("cannot open locale definition file `%s'"),
522 turn_on_mcheck (void)
524 /* Enable `malloc' debugging. */
526 /* Use the following line for a more thorough but much slower testing. */
527 /* mcheck_pedantic (NULL); */
530 void (*__malloc_initialize_hook
) (void) = turn_on_mcheck
;