]>
Commit | Line | Data |
---|---|---|
81fb02b0 | 1 | /* Copyright (C) 1995-2011, 2012 Free Software Foundation, Inc. |
e4cf5070 | 2 | This file is part of the GNU C Library. |
4b10dd6c | 3 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1995. |
19bc17a9 | 4 | |
43bc8ac6 | 5 | This program is free software; you can redistribute it and/or modify |
2e2efe65 RM |
6 | it under the terms of the GNU General Public License as published |
7 | by the Free Software Foundation; version 2 of the License, or | |
8 | (at your option) any later version. | |
19bc17a9 | 9 | |
43bc8ac6 | 10 | This program is distributed in the hope that it will be useful, |
e4cf5070 | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
43bc8ac6 UD |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | GNU General Public License for more details. | |
19bc17a9 | 14 | |
43bc8ac6 UD |
15 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software Foundation, | |
17 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
19bc17a9 RM |
18 | |
19 | #ifdef HAVE_CONFIG_H | |
20 | # include <config.h> | |
21 | #endif | |
22 | ||
5a97622d | 23 | #include <argp.h> |
19bc17a9 RM |
24 | #include <errno.h> |
25 | #include <fcntl.h> | |
19bc17a9 RM |
26 | #include <libintl.h> |
27 | #include <locale.h> | |
b9eb05d6 | 28 | #include <mcheck.h> |
a7b65cdc | 29 | #include <stdbool.h> |
19bc17a9 RM |
30 | #include <stdio.h> |
31 | #include <stdlib.h> | |
32 | #include <string.h> | |
33 | #include <unistd.h> | |
f2b98f97 | 34 | #include <error.h> |
4b10dd6c | 35 | #include <sys/mman.h> |
19bc17a9 RM |
36 | #include <sys/stat.h> |
37 | ||
f2b98f97 | 38 | #include "localedef.h" |
4b10dd6c | 39 | #include "charmap.h" |
19bc17a9 | 40 | #include "locfile.h" |
19bc17a9 | 41 | |
4b10dd6c UD |
42 | /* Undefine the following line in the production version. */ |
43 | /* #define NDEBUG 1 */ | |
44 | #include <assert.h> | |
19bc17a9 RM |
45 | |
46 | ||
47 | /* List of copied locales. */ | |
48 | struct copy_def_list_t *copy_list; | |
49 | ||
50 | /* If this is defined be POSIX conform. */ | |
51 | int posix_conformance; | |
52 | ||
19bc17a9 RM |
53 | /* If not zero give a lot more messages. */ |
54 | int verbose; | |
55 | ||
c84142e8 UD |
56 | /* If not zero suppress warnings and information messages. */ |
57 | int be_quiet; | |
19bc17a9 | 58 | |
a7b65cdc UD |
59 | /* If not zero, produce old-style hash table instead of 3-level access |
60 | tables. */ | |
ef446144 UD |
61 | int oldstyle_tables; |
62 | ||
5a97622d UD |
63 | /* If not zero force output even if warning were issued. */ |
64 | static int force_output; | |
19bc17a9 | 65 | |
8a82cc30 UD |
66 | /* Prefix for output files. */ |
67 | const char *output_prefix; | |
68 | ||
5a97622d UD |
69 | /* Name of the character map file. */ |
70 | static const char *charmap_file; | |
71 | ||
72 | /* Name of the locale definition file. */ | |
73 | static const char *input_file; | |
74 | ||
69f155d4 | 75 | /* Name of the repertoire map file. */ |
4b10dd6c UD |
76 | const char *repertoire_global; |
77 | ||
cb09a2cd RM |
78 | /* Name of the locale.alias file. */ |
79 | const char *alias_file; | |
80 | ||
4b10dd6c UD |
81 | /* List of all locales. */ |
82 | static struct localedef_t *locales; | |
5a97622d | 83 | |
a7b65cdc UD |
84 | /* If true don't add locale data to archive. */ |
85 | bool no_archive; | |
86 | ||
87 | /* If true add named locales to archive. */ | |
88 | static bool add_to_archive; | |
89 | ||
90 | /* If true delete named locales from archive. */ | |
91 | static bool delete_from_archive; | |
92 | ||
93 | /* If true replace archive content when adding. */ | |
94 | static bool replace_archive; | |
95 | ||
96 | /* If true list archive content. */ | |
97 | static bool list_archive; | |
98 | ||
99 | /* Maximum number of retries when opening the locale archive. */ | |
100 | int max_locarchive_open_retry = 10; | |
101 | ||
5a97622d UD |
102 | |
103 | /* Name and version of program. */ | |
104 | static void print_version (FILE *stream, struct argp_state *state); | |
105 | void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version; | |
106 | ||
a7b65cdc UD |
107 | #define OPT_POSIX 301 |
108 | #define OPT_QUIET 302 | |
109 | #define OPT_OLDSTYLE 303 | |
110 | #define OPT_PREFIX 304 | |
111 | #define OPT_NO_ARCHIVE 305 | |
112 | #define OPT_ADD_TO_ARCHIVE 306 | |
113 | #define OPT_REPLACE 307 | |
114 | #define OPT_DELETE_FROM_ARCHIVE 308 | |
115 | #define OPT_LIST_ARCHIVE 309 | |
5a97622d UD |
116 | |
117 | /* Definitions of arguments for argp functions. */ | |
118 | static const struct argp_option options[] = | |
119 | { | |
120 | { NULL, 0, NULL, 0, N_("Input Files:") }, | |
4b10dd6c | 121 | { "charmap", 'f', "FILE", 0, |
5a97622d | 122 | N_("Symbolic character names defined in FILE") }, |
4b10dd6c UD |
123 | { "inputfile", 'i', "FILE", 0, N_("Source definitions are found in FILE") }, |
124 | { "repertoire-map", 'u', "FILE", 0, | |
7c38dd46 | 125 | N_("FILE contains mapping from symbolic names to UCS4 values") }, |
5a97622d UD |
126 | |
127 | { NULL, 0, NULL, 0, N_("Output control:") }, | |
128 | { "force", 'c', NULL, 0, | |
129 | N_("Create output even if warning messages were issued") }, | |
ef446144 | 130 | { "old-style", OPT_OLDSTYLE, NULL, 0, N_("Create old-style tables") }, |
8a82cc30 | 131 | { "prefix", OPT_PREFIX, "PATH", 0, N_("Optional output file prefix") }, |
5a97622d UD |
132 | { "posix", OPT_POSIX, NULL, 0, N_("Be strictly POSIX conform") }, |
133 | { "quiet", OPT_QUIET, NULL, 0, | |
134 | N_("Suppress warnings and information messages") }, | |
a5a0310d | 135 | { "verbose", 'v', NULL, 0, N_("Print more messages") }, |
a7b65cdc UD |
136 | { NULL, 0, NULL, 0, N_("Archive control:") }, |
137 | { "no-archive", OPT_NO_ARCHIVE, NULL, 0, | |
138 | N_("Don't add new data to archive") }, | |
139 | { "add-to-archive", OPT_ADD_TO_ARCHIVE, NULL, 0, | |
140 | N_("Add locales named by parameters to archive") }, | |
141 | { "replace", OPT_REPLACE, NULL, 0, N_("Replace existing archive content") }, | |
142 | { "delete-from-archive", OPT_DELETE_FROM_ARCHIVE, NULL, 0, | |
143 | N_("Remove locales named by parameters from archive") }, | |
144 | { "list-archive", OPT_LIST_ARCHIVE, NULL, 0, N_("List content of archive") }, | |
cb09a2cd RM |
145 | { "alias-file", 'A', "FILE", 0, |
146 | N_("locale.alias file to consult when making archive")}, | |
5a97622d UD |
147 | { NULL, 0, NULL, 0, NULL } |
148 | }; | |
149 | ||
150 | /* Short description of program. */ | |
151 | static const char doc[] = N_("Compile locale specification"); | |
152 | ||
153 | /* Strings for arguments in help texts. */ | |
a7b65cdc UD |
154 | static const char args_doc[] = N_("\ |
155 | NAME\n\ | |
156 | [--add-to-archive|--delete-from-archive] FILE...\n\ | |
157 | --list-archive [FILE]"); | |
5a97622d UD |
158 | |
159 | /* Prototype for option handler. */ | |
160 | static error_t parse_opt (int key, char *arg, struct argp_state *state); | |
161 | ||
162 | /* Function to print some extra text in the help message. */ | |
163 | static char *more_help (int key, const char *text, void *input); | |
164 | ||
165 | /* Data structure to communicate with argp functions. */ | |
166 | static struct argp argp = | |
19bc17a9 | 167 | { |
5a97622d | 168 | options, parse_opt, args_doc, doc, NULL, more_help |
19bc17a9 RM |
169 | }; |
170 | ||
171 | ||
172 | /* Prototypes for global functions. */ | |
4b10dd6c | 173 | extern void *xmalloc (size_t __n); |
19bc17a9 RM |
174 | |
175 | /* Prototypes for local functions. */ | |
19bc17a9 | 176 | static void error_print (void); |
7a12c6bb | 177 | static const char *construct_output_path (char *path); |
900bec85 | 178 | static const char *normalize_codeset (const char *codeset, size_t name_len); |
19bc17a9 RM |
179 | |
180 | ||
181 | int | |
182 | main (int argc, char *argv[]) | |
183 | { | |
19bc17a9 RM |
184 | const char *output_path; |
185 | int cannot_write_why; | |
4b10dd6c UD |
186 | struct charmap_t *charmap; |
187 | struct localedef_t global; | |
2f6d1f1b | 188 | int remaining; |
19bc17a9 | 189 | |
6d52618b | 190 | /* Set initial values for global variables. */ |
4b10dd6c | 191 | copy_list = NULL; |
19bc17a9 | 192 | posix_conformance = getenv ("POSIXLY_CORRECT") != NULL; |
19bc17a9 | 193 | error_print_progname = error_print; |
19bc17a9 RM |
194 | |
195 | /* Set locale. Do not set LC_ALL because the other categories must | |
6d52618b | 196 | not be affected (according to POSIX.2). */ |
19bc17a9 RM |
197 | setlocale (LC_MESSAGES, ""); |
198 | setlocale (LC_CTYPE, ""); | |
199 | ||
200 | /* Initialize the message catalog. */ | |
19bc17a9 | 201 | textdomain (_libc_intl_domainname); |
19bc17a9 | 202 | |
5a97622d | 203 | /* Parse and process arguments. */ |
4cca6b86 | 204 | argp_err_exit_status = 4; |
2f6d1f1b | 205 | argp_parse (&argp, argc, argv, 0, &remaining, NULL); |
c84142e8 | 206 | |
a7b65cdc UD |
207 | /* Handle a few special cases. */ |
208 | if (list_archive) | |
b2bffca2 | 209 | show_archive_content (verbose); |
a7b65cdc UD |
210 | if (add_to_archive) |
211 | return add_locales_to_archive (argc - remaining, &argv[remaining], | |
212 | replace_archive); | |
213 | if (delete_from_archive) | |
214 | return delete_locales_from_archive (argc - remaining, &argv[remaining]); | |
215 | ||
19bc17a9 RM |
216 | /* POSIX.2 requires to be verbose about missing characters in the |
217 | character map. */ | |
218 | verbose |= posix_conformance; | |
219 | ||
2f6d1f1b | 220 | if (argc - remaining != 1) |
19bc17a9 | 221 | { |
5a97622d | 222 | /* We need exactly one non-option parameter. */ |
4cca6b86 | 223 | argp_help (&argp, stdout, ARGP_HELP_SEE | ARGP_HELP_EXIT_ERR, |
5a97622d | 224 | program_invocation_short_name); |
5a97622d | 225 | exit (4); |
19bc17a9 RM |
226 | } |
227 | ||
19bc17a9 RM |
228 | /* The parameter describes the output path of the constructed files. |
229 | If the described files cannot be written return a NULL pointer. */ | |
b459976e UD |
230 | output_path = construct_output_path (argv[remaining]); |
231 | if (output_path == NULL && ! no_archive) | |
232 | error (4, errno, _("cannot create directory for output files")); | |
233 | cannot_write_why = errno; | |
19bc17a9 RM |
234 | |
235 | /* Now that the parameters are processed we have to reset the local | |
236 | ctype locale. (P1003.2 4.35.5.2) */ | |
237 | setlocale (LC_CTYPE, "POSIX"); | |
238 | ||
239 | /* Look whether the system really allows locale definitions. POSIX | |
240 | defines error code 3 for this situation so I think it must be | |
241 | a fatal error (see P1003.2 4.35.8). */ | |
242 | if (sysconf (_SC_2_LOCALEDEF) < 0) | |
f2b98f97 UD |
243 | WITH_CUR_LOCALE (error (3, 0, _("\ |
244 | FATAL: system does not define `_POSIX2_LOCALEDEF'"))); | |
19bc17a9 RM |
245 | |
246 | /* Process charmap file. */ | |
8a6537b0 | 247 | charmap = charmap_read (charmap_file, verbose, 1, be_quiet, 1); |
4b10dd6c UD |
248 | |
249 | /* Add the first entry in the locale list. */ | |
250 | memset (&global, '\0', sizeof (struct localedef_t)); | |
dcf56f42 | 251 | global.name = input_file ?: "/dev/stdin"; |
4b10dd6c UD |
252 | global.needed = ALL_LOCALES; |
253 | locales = &global; | |
19bc17a9 RM |
254 | |
255 | /* Now read the locale file. */ | |
4b10dd6c | 256 | if (locfile_read (&global, charmap) != 0) |
f2b98f97 UD |
257 | WITH_CUR_LOCALE (error (4, errno, _("\ |
258 | cannot open locale definition file `%s'"), input_file)); | |
19bc17a9 | 259 | |
4b10dd6c UD |
260 | /* Perhaps we saw some `copy' instructions. */ |
261 | while (1) | |
19bc17a9 | 262 | { |
4b10dd6c | 263 | struct localedef_t *runp = locales; |
19bc17a9 | 264 | |
b9eb05d6 | 265 | while (runp != NULL && (runp->needed & runp->avail) == runp->needed) |
4b10dd6c | 266 | runp = runp->next; |
19bc17a9 | 267 | |
4b10dd6c UD |
268 | if (runp == NULL) |
269 | /* Everything read. */ | |
270 | break; | |
271 | ||
272 | if (locfile_read (runp, charmap) != 0) | |
f2b98f97 UD |
273 | WITH_CUR_LOCALE (error (4, errno, _("\ |
274 | cannot open locale definition file `%s'"), runp->name)); | |
19bc17a9 | 275 | } |
19bc17a9 RM |
276 | |
277 | /* Check the categories we processed in source form. */ | |
4b10dd6c | 278 | check_all_categories (locales, charmap); |
19bc17a9 RM |
279 | |
280 | /* We are now able to write the data files. If warning were given we | |
281 | do it only if it is explicitly requested (--force). */ | |
282 | if (error_message_count == 0 || force_output != 0) | |
283 | { | |
284 | if (cannot_write_why != 0) | |
f2b98f97 UD |
285 | WITH_CUR_LOCALE (error (4, cannot_write_why, _("\ |
286 | cannot write output files to `%s'"), output_path)); | |
19bc17a9 | 287 | else |
a7b65cdc | 288 | write_all_categories (locales, charmap, argv[remaining], output_path); |
19bc17a9 RM |
289 | } |
290 | else | |
f2b98f97 | 291 | WITH_CUR_LOCALE (error (4, 0, _("\ |
c69136ae | 292 | no output file produced because warnings were issued"))); |
19bc17a9 RM |
293 | |
294 | /* This exit status is prescribed by POSIX.2 4.35.7. */ | |
295 | exit (error_message_count != 0); | |
296 | } | |
297 | ||
298 | ||
5a97622d UD |
299 | /* Handle program arguments. */ |
300 | static error_t | |
301 | parse_opt (int key, char *arg, struct argp_state *state) | |
302 | { | |
303 | switch (key) | |
304 | { | |
305 | case OPT_QUIET: | |
306 | be_quiet = 1; | |
307 | break; | |
308 | case OPT_POSIX: | |
309 | posix_conformance = 1; | |
310 | break; | |
ef446144 UD |
311 | case OPT_OLDSTYLE: |
312 | oldstyle_tables = 1; | |
313 | break; | |
8a82cc30 UD |
314 | case OPT_PREFIX: |
315 | output_prefix = arg; | |
316 | break; | |
a7b65cdc UD |
317 | case OPT_NO_ARCHIVE: |
318 | no_archive = true; | |
319 | break; | |
320 | case OPT_ADD_TO_ARCHIVE: | |
321 | add_to_archive = true; | |
322 | break; | |
323 | case OPT_REPLACE: | |
324 | replace_archive = true; | |
325 | break; | |
326 | case OPT_DELETE_FROM_ARCHIVE: | |
327 | delete_from_archive = true; | |
328 | break; | |
329 | case OPT_LIST_ARCHIVE: | |
330 | list_archive = true; | |
331 | break; | |
5a97622d UD |
332 | case 'c': |
333 | force_output = 1; | |
334 | break; | |
335 | case 'f': | |
336 | charmap_file = arg; | |
337 | break; | |
cb09a2cd RM |
338 | case 'A': |
339 | alias_file = arg; | |
340 | break; | |
5a97622d UD |
341 | case 'i': |
342 | input_file = arg; | |
343 | break; | |
344 | case 'u': | |
4b10dd6c | 345 | repertoire_global = arg; |
5a97622d UD |
346 | break; |
347 | case 'v': | |
348 | verbose = 1; | |
349 | break; | |
350 | default: | |
351 | return ARGP_ERR_UNKNOWN; | |
352 | } | |
353 | return 0; | |
354 | } | |
355 | ||
356 | ||
357 | static char * | |
358 | more_help (int key, const char *text, void *input) | |
359 | { | |
360 | char *cp; | |
361 | ||
362 | switch (key) | |
363 | { | |
364 | case ARGP_KEY_HELP_EXTRA: | |
365 | /* We print some extra information. */ | |
322861e8 | 366 | if (asprintf (&cp, gettext ("\ |
69f155d4 | 367 | System's directory for character maps : %s\n\ |
561470e0 UD |
368 | repertoire maps: %s\n\ |
369 | locale path : %s\n\ | |
5a97622d | 370 | %s"), |
322861e8 | 371 | CHARMAP_PATH, REPERTOIREMAP_PATH, LOCALE_PATH, gettext ("\ |
d40eb37a | 372 | For bug reporting instructions, please see:\n\ |
322861e8 UD |
373 | <http://www.gnu.org/software/libc/bugs.html>.\n")) < 0) |
374 | return NULL; | |
5a97622d UD |
375 | return cp; |
376 | default: | |
377 | break; | |
378 | } | |
379 | return (char *) text; | |
380 | } | |
381 | ||
382 | /* Print the version information. */ | |
383 | static void | |
384 | print_version (FILE *stream, struct argp_state *state) | |
385 | { | |
386 | fprintf (stream, "localedef (GNU %s) %s\n", PACKAGE, VERSION); | |
387 | fprintf (stream, gettext ("\ | |
388 | Copyright (C) %s Free Software Foundation, Inc.\n\ | |
389 | This is free software; see the source for copying conditions. There is NO\n\ | |
390 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\ | |
81fb02b0 | 391 | "), "2012"); |
5a97622d UD |
392 | fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper"); |
393 | } | |
394 | ||
395 | ||
19bc17a9 RM |
396 | /* The address of this function will be assigned to the hook in the error |
397 | functions. */ | |
398 | static void | |
4b10dd6c | 399 | error_print (void) |
19bc17a9 | 400 | { |
19bc17a9 RM |
401 | } |
402 | ||
403 | ||
404 | /* The parameter to localedef describes the output path. If it does | |
6d52618b | 405 | contain a '/' character it is a relative path. Otherwise it names the |
19bc17a9 RM |
406 | locale this definition is for. */ |
407 | static const char * | |
7a12c6bb | 408 | construct_output_path (char *path) |
19bc17a9 | 409 | { |
c4029823 | 410 | const char *normal = NULL; |
19bc17a9 | 411 | char *result; |
4b10dd6c | 412 | char *endp; |
19bc17a9 RM |
413 | |
414 | if (strchr (path, '/') == NULL) | |
415 | { | |
7a12c6bb RM |
416 | /* This is a system path. First examine whether the locale name |
417 | contains a reference to the codeset. This should be | |
418 | normalized. */ | |
4b10dd6c | 419 | char *startp; |
7a12c6bb RM |
420 | |
421 | startp = path; | |
422 | /* We must be prepared for finding a CEN name or a location of | |
423 | the introducing `.' where it is not possible anymore. */ | |
c3d08707 | 424 | while (*startp != '\0' && *startp != '@' && *startp != '.') |
7a12c6bb RM |
425 | ++startp; |
426 | if (*startp == '.') | |
427 | { | |
428 | /* We found a codeset specification. Now find the end. */ | |
429 | endp = ++startp; | |
430 | while (*endp != '\0' && *endp != '@') | |
431 | ++endp; | |
432 | ||
433 | if (endp > startp) | |
900bec85 | 434 | normal = normalize_codeset (startp, endp - startp); |
7a12c6bb | 435 | } |
c4029823 UD |
436 | else |
437 | /* This is to keep gcc quiet. */ | |
438 | endp = NULL; | |
19bc17a9 | 439 | |
7a12c6bb RM |
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 | |
442 | '/'. */ | |
2952b117 | 443 | ssize_t n; |
7a12c6bb | 444 | if (normal == NULL) |
8a82cc30 UD |
445 | n = asprintf (&result, "%s%s/%s%c", |
446 | output_prefix ?: "", LOCALEDIR, path, '\0'); | |
7a12c6bb | 447 | else |
8a82cc30 UD |
448 | n = asprintf (&result, "%s%s/%.*s%s%s%c", |
449 | output_prefix ?: "", LOCALEDIR, | |
dbbbaf53 | 450 | (int) (startp - path), path, normal, endp, '\0'); |
4b10dd6c | 451 | |
383bd1c5 UD |
452 | if (n < 0) |
453 | return NULL; | |
454 | ||
16fe0a88 | 455 | endp = result + n - 1; |
19bc17a9 RM |
456 | } |
457 | else | |
458 | { | |
7a12c6bb RM |
459 | /* This is a user path. Please note the additional byte in the |
460 | memory allocation. */ | |
4b10dd6c UD |
461 | size_t len = strlen (path) + 1; |
462 | result = xmalloc (len + 1); | |
30e0f9c3 | 463 | endp = mempcpy (result, path, len) - 1; |
a7b65cdc UD |
464 | |
465 | /* If the user specified an output path we cannot add the output | |
466 | to the archive. */ | |
467 | no_archive = true; | |
19bc17a9 RM |
468 | } |
469 | ||
470 | errno = 0; | |
471 | ||
9813858f | 472 | if (no_archive && euidaccess (result, W_OK) == -1) |
19bc17a9 RM |
473 | /* Perhaps the directory does not exist now. Try to create it. */ |
474 | if (errno == ENOENT) | |
475 | { | |
476 | errno = 0; | |
383bd1c5 UD |
477 | if (mkdir (result, 0777) < 0) |
478 | return NULL; | |
19bc17a9 RM |
479 | } |
480 | ||
4b10dd6c UD |
481 | *endp++ = '/'; |
482 | *endp = '\0'; | |
19bc17a9 RM |
483 | |
484 | return result; | |
485 | } | |
900bec85 | 486 | |
4b10dd6c | 487 | |
900bec85 UD |
488 | /* Normalize codeset name. There is no standard for the codeset |
489 | names. Normalization allows the user to use any of the common | |
490 | names. */ | |
491 | static const char * | |
492 | normalize_codeset (codeset, name_len) | |
493 | const char *codeset; | |
494 | size_t name_len; | |
495 | { | |
496 | int len = 0; | |
497 | int only_digit = 1; | |
498 | char *retval; | |
499 | char *wp; | |
500 | size_t cnt; | |
501 | ||
502 | for (cnt = 0; cnt < name_len; ++cnt) | |
503 | if (isalnum (codeset[cnt])) | |
504 | { | |
505 | ++len; | |
506 | ||
507 | if (isalpha (codeset[cnt])) | |
508 | only_digit = 0; | |
509 | } | |
510 | ||
511 | retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1); | |
512 | ||
513 | if (retval != NULL) | |
514 | { | |
515 | if (only_digit) | |
516 | wp = stpcpy (retval, "iso"); | |
517 | else | |
518 | wp = retval; | |
519 | ||
520 | for (cnt = 0; cnt < name_len; ++cnt) | |
521 | if (isalpha (codeset[cnt])) | |
4b10dd6c | 522 | *wp++ = tolower (codeset[cnt]); |
900bec85 UD |
523 | else if (isdigit (codeset[cnt])) |
524 | *wp++ = codeset[cnt]; | |
525 | ||
526 | *wp = '\0'; | |
527 | } | |
528 | ||
529 | return (const char *) retval; | |
530 | } | |
4b10dd6c UD |
531 | |
532 | ||
533 | struct localedef_t * | |
c10d32c8 | 534 | add_to_readlist (int category, const char *name, const char *repertoire_name, |
07dab0c3 | 535 | int generate, struct localedef_t *copy_locale) |
4b10dd6c UD |
536 | { |
537 | struct localedef_t *runp = locales; | |
538 | ||
539 | while (runp != NULL && strcmp (name, runp->name) != 0) | |
540 | runp = runp->next; | |
541 | ||
542 | if (runp == NULL) | |
543 | { | |
544 | /* Add a new entry at the end. */ | |
b9eb05d6 UD |
545 | struct localedef_t *newp; |
546 | ||
547 | assert (generate == 1); | |
548 | ||
549 | newp = xcalloc (1, sizeof (struct localedef_t)); | |
4b10dd6c UD |
550 | newp->name = name; |
551 | newp->repertoire_name = repertoire_name; | |
552 | ||
553 | if (locales == NULL) | |
554 | runp = locales = newp; | |
555 | else | |
556 | { | |
557 | runp = locales; | |
558 | while (runp->next != NULL) | |
559 | runp = runp->next; | |
560 | runp = runp->next = newp; | |
561 | } | |
562 | } | |
563 | ||
69f6a804 | 564 | if (generate |
c10d32c8 UD |
565 | && (runp->needed & (1 << category)) != 0 |
566 | && (runp->avail & (1 << category)) == 0) | |
f2b98f97 UD |
567 | WITH_CUR_LOCALE (error (5, 0, _("\ |
568 | circular dependencies between locale definitions"))); | |
4b10dd6c | 569 | |
07dab0c3 UD |
570 | if (copy_locale != NULL) |
571 | { | |
c10d32c8 | 572 | if (runp->categories[category].generic != NULL) |
f2b98f97 UD |
573 | WITH_CUR_LOCALE (error (5, 0, _("\ |
574 | cannot add already read locale `%s' a second time"), name)); | |
07dab0c3 | 575 | else |
c10d32c8 UD |
576 | runp->categories[category].generic = |
577 | copy_locale->categories[category].generic; | |
07dab0c3 UD |
578 | } |
579 | ||
c10d32c8 | 580 | runp->needed |= 1 << category; |
4b10dd6c UD |
581 | |
582 | return runp; | |
583 | } | |
584 | ||
585 | ||
586 | struct localedef_t * | |
c10d32c8 | 587 | find_locale (int category, const char *name, const char *repertoire_name, |
47e8b443 | 588 | const struct charmap_t *charmap) |
4b10dd6c | 589 | { |
b9eb05d6 UD |
590 | struct localedef_t *result; |
591 | ||
592 | /* Find the locale, but do not generate it since this would be a bug. */ | |
c10d32c8 | 593 | result = add_to_readlist (category, name, repertoire_name, 0, NULL); |
b9eb05d6 UD |
594 | |
595 | assert (result != NULL); | |
4b10dd6c | 596 | |
c10d32c8 | 597 | if ((result->avail & (1 << category)) == 0 |
b9eb05d6 | 598 | && locfile_read (result, charmap) != 0) |
f2b98f97 UD |
599 | WITH_CUR_LOCALE (error (4, errno, _("\ |
600 | cannot open locale definition file `%s'"), result->name)); | |
4b10dd6c UD |
601 | |
602 | return result; | |
603 | } | |
70e51ab9 UD |
604 | |
605 | ||
606 | struct localedef_t * | |
c10d32c8 | 607 | load_locale (int category, const char *name, const char *repertoire_name, |
47e8b443 | 608 | const struct charmap_t *charmap, struct localedef_t *copy_locale) |
70e51ab9 UD |
609 | { |
610 | struct localedef_t *result; | |
611 | ||
612 | /* Generate the locale if it does not exist. */ | |
c10d32c8 | 613 | result = add_to_readlist (category, name, repertoire_name, 1, copy_locale); |
70e51ab9 UD |
614 | |
615 | assert (result != NULL); | |
616 | ||
c10d32c8 | 617 | if ((result->avail & (1 << category)) == 0 |
70e51ab9 | 618 | && locfile_read (result, charmap) != 0) |
f2b98f97 UD |
619 | WITH_CUR_LOCALE (error (4, errno, _("\ |
620 | cannot open locale definition file `%s'"), result->name)); | |
70e51ab9 UD |
621 | |
622 | return result; | |
623 | } | |
670066c3 UD |
624 | |
625 | static void | |
626 | turn_on_mcheck (void) | |
627 | { | |
628 | /* Enable `malloc' debugging. */ | |
629 | mcheck (NULL); | |
630 | /* Use the following line for a more thorough but much slower testing. */ | |
631 | /* mcheck_pedantic (NULL); */ | |
632 | } | |
633 | ||
634 | void (*__malloc_initialize_hook) (void) = turn_on_mcheck; |