1 /* Copyright (C) 1995-2004, 2005, 2006, 2007 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1995.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
37 #include "localedef.h"
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
62 /* If not zero force output even if warning were issued. */
63 static int force_output
;
65 /* Prefix for output files. */
66 const char *output_prefix
;
68 /* Name of the character map file. */
69 static const char *charmap_file
;
71 /* Name of the locale definition file. */
72 static const char *input_file
;
74 /* Name of the repertoire map file. */
75 const char *repertoire_global
;
77 /* Name of the locale.alias file. */
78 const char *alias_file
;
80 /* List of all locales. */
81 static struct localedef_t
*locales
;
83 /* If true don't add locale data to archive. */
86 /* If true add named locales to archive. */
87 static bool add_to_archive
;
89 /* If true delete named locales from archive. */
90 static bool delete_from_archive
;
92 /* If true replace archive content when adding. */
93 static bool replace_archive
;
95 /* If true list archive content. */
96 static bool list_archive
;
98 /* Maximum number of retries when opening the locale archive. */
99 int max_locarchive_open_retry
= 10;
102 /* Name and version of program. */
103 static void print_version (FILE *stream
, struct argp_state
*state
);
104 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
106 #define OPT_POSIX 301
107 #define OPT_QUIET 302
108 #define OPT_OLDSTYLE 303
109 #define OPT_PREFIX 304
110 #define OPT_NO_ARCHIVE 305
111 #define OPT_ADD_TO_ARCHIVE 306
112 #define OPT_REPLACE 307
113 #define OPT_DELETE_FROM_ARCHIVE 308
114 #define OPT_LIST_ARCHIVE 309
116 /* Definitions of arguments for argp functions. */
117 static const struct argp_option options
[] =
119 { NULL
, 0, NULL
, 0, N_("Input Files:") },
120 { "charmap", 'f', "FILE", 0,
121 N_("Symbolic character names defined in FILE") },
122 { "inputfile", 'i', "FILE", 0, N_("Source definitions are found in FILE") },
123 { "repertoire-map", 'u', "FILE", 0,
124 N_("FILE contains mapping from symbolic names to UCS4 values") },
126 { NULL
, 0, NULL
, 0, N_("Output control:") },
127 { "force", 'c', NULL
, 0,
128 N_("Create output even if warning messages were issued") },
129 { "old-style", OPT_OLDSTYLE
, NULL
, 0, N_("Create old-style tables") },
130 { "prefix", OPT_PREFIX
, "PATH", 0, N_("Optional output file prefix") },
131 { "posix", OPT_POSIX
, NULL
, 0, N_("Be strictly POSIX conform") },
132 { "quiet", OPT_QUIET
, NULL
, 0,
133 N_("Suppress warnings and information messages") },
134 { "verbose", 'v', NULL
, 0, N_("Print more messages") },
135 { NULL
, 0, NULL
, 0, N_("Archive control:") },
136 { "no-archive", OPT_NO_ARCHIVE
, NULL
, 0,
137 N_("Don't add new data to archive") },
138 { "add-to-archive", OPT_ADD_TO_ARCHIVE
, NULL
, 0,
139 N_("Add locales named by parameters to archive") },
140 { "replace", OPT_REPLACE
, NULL
, 0, N_("Replace existing archive content") },
141 { "delete-from-archive", OPT_DELETE_FROM_ARCHIVE
, NULL
, 0,
142 N_("Remove locales named by parameters from archive") },
143 { "list-archive", OPT_LIST_ARCHIVE
, NULL
, 0, N_("List content of archive") },
144 { "alias-file", 'A', "FILE", 0,
145 N_("locale.alias file to consult when making archive")},
146 { NULL
, 0, NULL
, 0, NULL
}
149 /* Short description of program. */
150 static const char doc
[] = N_("Compile locale specification");
152 /* Strings for arguments in help texts. */
153 static const char args_doc
[] = N_("\
155 [--add-to-archive|--delete-from-archive] FILE...\n\
156 --list-archive [FILE]");
158 /* Prototype for option handler. */
159 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
161 /* Function to print some extra text in the help message. */
162 static char *more_help (int key
, const char *text
, void *input
);
164 /* Data structure to communicate with argp functions. */
165 static struct argp argp
=
167 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
171 /* Prototypes for global functions. */
172 extern void *xmalloc (size_t __n
);
174 /* Prototypes for local functions. */
175 static void error_print (void);
176 static const char *construct_output_path (char *path
);
177 static const char *normalize_codeset (const char *codeset
, size_t name_len
);
181 main (int argc
, char *argv
[])
183 const char *output_path
;
184 int cannot_write_why
;
185 struct charmap_t
*charmap
;
186 struct localedef_t global
;
189 /* Set initial values for global variables. */
191 posix_conformance
= getenv ("POSIXLY_CORRECT") != NULL
;
192 error_print_progname
= error_print
;
194 /* Set locale. Do not set LC_ALL because the other categories must
195 not be affected (according to POSIX.2). */
196 setlocale (LC_MESSAGES
, "");
197 setlocale (LC_CTYPE
, "");
199 /* Initialize the message catalog. */
200 textdomain (_libc_intl_domainname
);
202 /* Parse and process arguments. */
203 argp_err_exit_status
= 4;
204 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
206 /* Handle a few special cases. */
208 show_archive_content (verbose
);
210 return add_locales_to_archive (argc
- remaining
, &argv
[remaining
],
212 if (delete_from_archive
)
213 return delete_locales_from_archive (argc
- remaining
, &argv
[remaining
]);
215 /* POSIX.2 requires to be verbose about missing characters in the
217 verbose
|= posix_conformance
;
219 if (argc
- remaining
!= 1)
221 /* We need exactly one non-option parameter. */
222 argp_help (&argp
, stdout
, ARGP_HELP_SEE
| ARGP_HELP_EXIT_ERR
,
223 program_invocation_short_name
);
227 /* The parameter describes the output path of the constructed files.
228 If the described files cannot be written return a NULL pointer. */
229 output_path
= construct_output_path (argv
[remaining
]);
230 if (output_path
== NULL
&& ! no_archive
)
231 error (4, errno
, _("cannot create directory for output files"));
232 cannot_write_why
= errno
;
234 /* Now that the parameters are processed we have to reset the local
235 ctype locale. (P1003.2 4.35.5.2) */
236 setlocale (LC_CTYPE
, "POSIX");
238 /* Look whether the system really allows locale definitions. POSIX
239 defines error code 3 for this situation so I think it must be
240 a fatal error (see P1003.2 4.35.8). */
241 if (sysconf (_SC_2_LOCALEDEF
) < 0)
242 WITH_CUR_LOCALE (error (3, 0, _("\
243 FATAL: system does not define `_POSIX2_LOCALEDEF'")));
245 /* Process charmap file. */
246 charmap
= charmap_read (charmap_file
, verbose
, 1, be_quiet
, 1);
248 /* Add the first entry in the locale list. */
249 memset (&global
, '\0', sizeof (struct localedef_t
));
250 global
.name
= input_file
?: "/dev/stdin";
251 global
.needed
= ALL_LOCALES
;
254 /* Now read the locale file. */
255 if (locfile_read (&global
, charmap
) != 0)
256 WITH_CUR_LOCALE (error (4, errno
, _("\
257 cannot open locale definition file `%s'"), input_file
));
259 /* Perhaps we saw some `copy' instructions. */
262 struct localedef_t
*runp
= locales
;
264 while (runp
!= NULL
&& (runp
->needed
& runp
->avail
) == runp
->needed
)
268 /* Everything read. */
271 if (locfile_read (runp
, charmap
) != 0)
272 WITH_CUR_LOCALE (error (4, errno
, _("\
273 cannot open locale definition file `%s'"), runp
->name
));
276 /* Check the categories we processed in source form. */
277 check_all_categories (locales
, charmap
);
279 /* We are now able to write the data files. If warning were given we
280 do it only if it is explicitly requested (--force). */
281 if (error_message_count
== 0 || force_output
!= 0)
283 if (cannot_write_why
!= 0)
284 WITH_CUR_LOCALE (error (4, cannot_write_why
, _("\
285 cannot write output files to `%s'"), output_path
));
287 write_all_categories (locales
, charmap
, argv
[remaining
], output_path
);
290 WITH_CUR_LOCALE (error (4, 0, _("\
291 no output file produced because warnings were issued")));
293 /* This exit status is prescribed by POSIX.2 4.35.7. */
294 exit (error_message_count
!= 0);
298 /* Handle program arguments. */
300 parse_opt (int key
, char *arg
, struct argp_state
*state
)
308 posix_conformance
= 1;
319 case OPT_ADD_TO_ARCHIVE
:
320 add_to_archive
= true;
323 replace_archive
= true;
325 case OPT_DELETE_FROM_ARCHIVE
:
326 delete_from_archive
= true;
328 case OPT_LIST_ARCHIVE
:
344 repertoire_global
= arg
;
350 return ARGP_ERR_UNKNOWN
;
357 more_help (int key
, const char *text
, void *input
)
363 case ARGP_KEY_HELP_EXTRA
:
364 /* We print some extra information. */
365 if (asprintf (&cp
, gettext ("\
366 System's directory for character maps : %s\n\
367 repertoire maps: %s\n\
370 CHARMAP_PATH
, REPERTOIREMAP_PATH
, LOCALE_PATH
, gettext ("\
371 For bug reporting instructions, please see:\n\
372 <http://www.gnu.org/software/libc/bugs.html>.\n")) < 0)
378 return (char *) text
;
381 /* Print the version information. */
383 print_version (FILE *stream
, struct argp_state
*state
)
385 fprintf (stream
, "localedef (GNU %s) %s\n", PACKAGE
, VERSION
);
386 fprintf (stream
, gettext ("\
387 Copyright (C) %s Free Software Foundation, Inc.\n\
388 This is free software; see the source for copying conditions. There is NO\n\
389 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
391 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
395 /* The address of this function will be assigned to the hook in the error
403 /* The parameter to localedef describes the output path. If it does
404 contain a '/' character it is a relative path. Otherwise it names the
405 locale this definition is for. */
407 construct_output_path (char *path
)
409 const char *normal
= NULL
;
413 if (strchr (path
, '/') == NULL
)
415 /* This is a system path. First examine whether the locale name
416 contains a reference to the codeset. This should be
422 /* We must be prepared for finding a CEN name or a location of
423 the introducing `.' where it is not possible anymore. */
424 while (*startp
!= '\0' && *startp
!= '@' && *startp
!= '.')
428 /* We found a codeset specification. Now find the end. */
430 while (*endp
!= '\0' && *endp
!= '@')
434 normal
= normalize_codeset (startp
, endp
- startp
);
437 /* This is to keep gcc quiet. */
440 /* We put an additional '\0' at the end of the string because at
441 the end of the function we need another byte for the trailing
444 n
= asprintf (&result
, "%s%s/%s%c",
445 output_prefix
?: "", LOCALEDIR
, path
, '\0');
447 n
= asprintf (&result
, "%s%s/%.*s%s%s%c",
448 output_prefix
?: "", LOCALEDIR
,
449 (int) (startp
- path
), path
, normal
, endp
, '\0');
454 endp
= result
+ n
- 1;
458 /* This is a user path. Please note the additional byte in the
459 memory allocation. */
460 size_t len
= strlen (path
) + 1;
461 result
= xmalloc (len
+ 1);
462 endp
= mempcpy (result
, path
, len
) - 1;
464 /* If the user specified an output path we cannot add the output
471 if (no_archive
&& euidaccess (result
, W_OK
) == -1)
472 /* Perhaps the directory does not exist now. Try to create it. */
476 if (mkdir (result
, 0777) < 0)
487 /* Normalize codeset name. There is no standard for the codeset
488 names. Normalization allows the user to use any of the common
491 normalize_codeset (codeset
, name_len
)
501 for (cnt
= 0; cnt
< name_len
; ++cnt
)
502 if (isalnum (codeset
[cnt
]))
506 if (isalpha (codeset
[cnt
]))
510 retval
= (char *) malloc ((only_digit
? 3 : 0) + len
+ 1);
515 wp
= stpcpy (retval
, "iso");
519 for (cnt
= 0; cnt
< name_len
; ++cnt
)
520 if (isalpha (codeset
[cnt
]))
521 *wp
++ = tolower (codeset
[cnt
]);
522 else if (isdigit (codeset
[cnt
]))
523 *wp
++ = codeset
[cnt
];
528 return (const char *) retval
;
533 add_to_readlist (int category
, const char *name
, const char *repertoire_name
,
534 int generate
, struct localedef_t
*copy_locale
)
536 struct localedef_t
*runp
= locales
;
538 while (runp
!= NULL
&& strcmp (name
, runp
->name
) != 0)
543 /* Add a new entry at the end. */
544 struct localedef_t
*newp
;
546 assert (generate
== 1);
548 newp
= xcalloc (1, sizeof (struct localedef_t
));
550 newp
->repertoire_name
= repertoire_name
;
553 runp
= locales
= newp
;
557 while (runp
->next
!= NULL
)
559 runp
= runp
->next
= newp
;
564 && (runp
->needed
& (1 << category
)) != 0
565 && (runp
->avail
& (1 << category
)) == 0)
566 WITH_CUR_LOCALE (error (5, 0, _("\
567 circular dependencies between locale definitions")));
569 if (copy_locale
!= NULL
)
571 if (runp
->categories
[category
].generic
!= NULL
)
572 WITH_CUR_LOCALE (error (5, 0, _("\
573 cannot add already read locale `%s' a second time"), name
));
575 runp
->categories
[category
].generic
=
576 copy_locale
->categories
[category
].generic
;
579 runp
->needed
|= 1 << category
;
586 find_locale (int category
, const char *name
, const char *repertoire_name
,
587 const struct charmap_t
*charmap
)
589 struct localedef_t
*result
;
591 /* Find the locale, but do not generate it since this would be a bug. */
592 result
= add_to_readlist (category
, name
, repertoire_name
, 0, NULL
);
594 assert (result
!= NULL
);
596 if ((result
->avail
& (1 << category
)) == 0
597 && locfile_read (result
, charmap
) != 0)
598 WITH_CUR_LOCALE (error (4, errno
, _("\
599 cannot open locale definition file `%s'"), result
->name
));
606 load_locale (int category
, const char *name
, const char *repertoire_name
,
607 const struct charmap_t
*charmap
, struct localedef_t
*copy_locale
)
609 struct localedef_t
*result
;
611 /* Generate the locale if it does not exist. */
612 result
= add_to_readlist (category
, name
, repertoire_name
, 1, copy_locale
);
614 assert (result
!= NULL
);
616 if ((result
->avail
& (1 << category
)) == 0
617 && locfile_read (result
, charmap
) != 0)
618 WITH_CUR_LOCALE (error (4, errno
, _("\
619 cannot open locale definition file `%s'"), result
->name
));
625 turn_on_mcheck (void)
627 /* Enable `malloc' debugging. */
629 /* Use the following line for a more thorough but much slower testing. */
630 /* mcheck_pedantic (NULL); */
633 void (*__malloc_initialize_hook
) (void) = turn_on_mcheck
;