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