]> git.ipfire.org Git - thirdparty/glibc.git/blame - iconv/iconv_prog.c
[BZ #2510, BZ #2830, BZ #3137, BZ #3313, BZ #3426, BZ #3465, BZ #3480, BZ #3483,...
[thirdparty/glibc.git] / iconv / iconv_prog.c
CommitLineData
fb5663ca 1/* Convert text in given files from the specified from-set to the to-set.
11bf311e 2 Copyright (C) 1998-2004, 2005, 2006, 2007 Free Software Foundation, Inc.
fb5663ca
UD
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5
43bc8ac6
UD
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
fb5663ca 9
43bc8ac6 10 This program is distributed in the hope that it will be useful,
fb5663ca 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.
fb5663ca 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. */
fb5663ca
UD
18
19#include <argp.h>
e4060100 20#include <assert.h>
8fe0fd03 21#include <ctype.h>
fb5663ca
UD
22#include <errno.h>
23#include <error.h>
24#include <fcntl.h>
25#include <iconv.h>
ac3d553b 26#include <langinfo.h>
fb5663ca 27#include <locale.h>
8fe0fd03 28#include <search.h>
fa00744e 29#include <stdbool.h>
fb5663ca
UD
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
4360eafd 34#include <libintl.h>
7cabd57c
UD
35#ifdef _POSIX_MAPPED_FILES
36# include <sys/mman.h>
37#endif
93693c4d 38#include <charmap.h>
e62c19f1 39#include <gconv_int.h>
93693c4d 40#include "iconv_prog.h"
9a1f71a7 41#include "iconvconfig.h"
fb5663ca
UD
42
43/* Get libc version number. */
44#include "../version.h"
45
46#define PACKAGE _libc_intl_domainname
47
48
49/* Name and version of program. */
50static void print_version (FILE *stream, struct argp_state *state);
51void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
52
53#define OPT_VERBOSE 1000
adcf0e4a 54#define OPT_LIST 'l'
fb5663ca
UD
55
56/* Definitions of arguments for argp functions. */
57static const struct argp_option options[] =
58{
59 { NULL, 0, NULL, 0, N_("Input/Output format specification:") },
60 { "from-code", 'f', "NAME", 0, N_("encoding of original text") },
61 { "to-code", 't', "NAME", 0, N_("encoding for output") },
8fe0fd03 62 { NULL, 0, NULL, 0, N_("Information:") },
adcf0e4a 63 { "list", 'l', NULL, 0, N_("list all known coded character sets") },
fb5663ca 64 { NULL, 0, NULL, 0, N_("Output control:") },
adcf0e4a 65 { NULL, 'c', NULL, 0, N_("omit invalid characters from output") },
fb5663ca 66 { "output", 'o', "FILE", 0, N_("output file") },
6d77214d 67 { "silent", 's', NULL, 0, N_("suppress warnings") },
fb5663ca
UD
68 { "verbose", OPT_VERBOSE, NULL, 0, N_("print progress information") },
69 { NULL, 0, NULL, 0, NULL }
70};
71
72/* Short description of program. */
73static const char doc[] = N_("\
74Convert encoding of given files from one encoding to another.");
75
76/* Strings for arguments in help texts. */
77static const char args_doc[] = N_("[FILE...]");
78
79/* Prototype for option handler. */
adcf0e4a 80static error_t parse_opt (int key, char *arg, struct argp_state *state);
fb5663ca
UD
81
82/* Function to print some extra text in the help message. */
adcf0e4a 83static char *more_help (int key, const char *text, void *input);
fb5663ca
UD
84
85/* Data structure to communicate with argp functions. */
86static struct argp argp =
87{
88 options, parse_opt, args_doc, doc, NULL, more_help
89};
90
e0e86ccb
UD
91/* Code sets to convert from and to respectively. An empty string as the
92 default causes the 'iconv_open' function to look up the charset of the
93 currently selected locale and use it. */
94static const char *from_code = "";
95static const char *to_code = "";
fb5663ca
UD
96
97/* File to write output to. If NULL write to stdout. */
98static const char *output_file;
99
100/* Nonzero if verbose ouput is wanted. */
93693c4d 101int verbose;
fb5663ca 102
8fe0fd03
UD
103/* Nonzero if list of all coded character sets is wanted. */
104static int list;
105
85830c4c 106/* If nonzero omit invalid character from output. */
93693c4d 107int omit_invalid;
85830c4c 108
fb5663ca 109/* Prototypes for the functions doing the actual work. */
3b434940 110static int process_block (iconv_t cd, char *addr, size_t len, FILE *output);
fb5663ca
UD
111static int process_fd (iconv_t cd, int fd, FILE *output);
112static int process_file (iconv_t cd, FILE *input, FILE *output);
2bd60880 113static void print_known_names (void) internal_function;
fb5663ca
UD
114
115
116int
117main (int argc, char *argv[])
118{
119 int status = EXIT_SUCCESS;
120 int remaining;
121 FILE *output;
122 iconv_t cd;
85830c4c 123 const char *orig_to_code;
93693c4d
UD
124 struct charmap_t *from_charmap = NULL;
125 struct charmap_t *to_charmap = NULL;
fb5663ca
UD
126
127 /* Set locale via LC_ALL. */
128 setlocale (LC_ALL, "");
129
130 /* Set the text message domain. */
131 textdomain (_libc_intl_domainname);
132
133 /* Parse and process arguments. */
134 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
135
8fe0fd03
UD
136 /* List all coded character sets if wanted. */
137 if (list)
138 {
139 print_known_names ();
140 exit (EXIT_SUCCESS);
141 }
fb5663ca 142
85830c4c
UD
143 /* If we have to ignore errors make sure we use the appropriate name for
144 the to-character-set. */
145 orig_to_code = to_code;
146 if (omit_invalid)
147 {
148 const char *errhand = strchrnul (to_code, '/');
149 int nslash = 2;
150 char *newp;
151 char *cp;
152
153 if (*errhand == '/')
154 {
155 --nslash;
156 errhand = strchrnul (errhand, '/');
157
158 if (*errhand == '/')
159 {
160 --nslash;
b8750711 161 errhand = strchr (errhand, '\0');
85830c4c
UD
162 }
163 }
164
b8750711 165 newp = (char *) alloca (errhand - to_code + nslash + 7 + 1);
85830c4c 166 cp = mempcpy (newp, to_code, errhand - to_code);
23054ade 167 while (nslash-- > 0)
85830c4c 168 *cp++ = '/';
b8750711
UD
169 if (cp[-1] != '/')
170 *cp++ = ',';
c7fb46a9 171 memcpy (cp, "IGNORE", sizeof ("IGNORE"));
85830c4c
UD
172
173 to_code = newp;
174 }
175
93693c4d
UD
176 /* POSIX 1003.2b introduces a silly thing: the arguments to -t anf -f
177 can be file names of charmaps. In this case iconv will have to read
178 those charmaps and use them to do the conversion. But there are
179 holes in the specification. There is nothing said that if -f is a
180 charmap filename that -t must be, too. And vice versa. There is
181 also no word about the symbolic names used. What if they don't
182 match? */
183 if (strchr (from_code, '/') != NULL)
184 /* The from-name might be a charmap file name. Try reading the
185 file. */
8a6537b0 186 from_charmap = charmap_read (from_code, /*0, 1*/1, 0, 0, 0);
93693c4d
UD
187
188 if (strchr (orig_to_code, '/') != NULL)
189 /* The to-name might be a charmap file name. Try reading the
190 file. */
8a6537b0 191 to_charmap = charmap_read (orig_to_code, /*0, 1,*/1, 0, 0, 0);
93693c4d 192
fb5663ca
UD
193
194 /* Determine output file. */
377c725f 195 if (output_file != NULL && strcmp (output_file, "-") != 0)
fb5663ca
UD
196 {
197 output = fopen (output_file, "w");
198 if (output == NULL)
199 error (EXIT_FAILURE, errno, _("cannot open output file"));
200 }
201 else
202 output = stdout;
203
93693c4d
UD
204 /* At this point we have to handle two cases. The first one is
205 where a charmap is used for the from- or to-charset, or both. We
206 handle this special since it is very different from the sane way of
207 doing things. The other case allows converting using the iconv()
208 function. */
209 if (from_charmap != NULL || to_charmap != NULL)
210 /* Construct the conversion table and do the conversion. */
211 status = charmap_conversion (from_code, from_charmap, to_code, to_charmap,
212 argc, remaining, argv, output);
fb5663ca 213 else
93693c4d
UD
214 {
215 /* Let's see whether we have these coded character sets. */
216 cd = iconv_open (to_code, from_code);
217 if (cd == (iconv_t) -1)
218 {
219 if (errno == EINVAL)
fa00744e
UD
220 {
221 /* Try to be nice with the user and tell her which of the
222 two encoding names is wrong. This is possible because
223 all supported encodings can be converted from/to Unicode,
224 in other words, because the graph of encodings is
225 connected. */
226 bool from_wrong =
227 (iconv_open ("UTF-8", from_code) == (iconv_t) -1
228 && errno == EINVAL);
229 bool to_wrong =
230 (iconv_open (to_code, "UTF-8") == (iconv_t) -1
231 && errno == EINVAL);
232 const char *from_pretty =
233 (from_code[0] ? from_code : nl_langinfo (CODESET));
234 const char *to_pretty =
235 (orig_to_code[0] ? orig_to_code : nl_langinfo (CODESET));
236
237 if (from_wrong)
238 {
239 if (to_wrong)
dd1e8878 240 error (0, 0,
fa00744e 241 _("\
c69136ae 242conversions from `%s' and to `%s' are not supported"),
fa00744e
UD
243 from_pretty, to_pretty);
244 else
dd1e8878 245 error (0, 0,
fa00744e
UD
246 _("conversion from `%s' is not supported"),
247 from_pretty);
248 }
249 else
250 {
251 if (to_wrong)
dd1e8878 252 error (0, 0,
fa00744e
UD
253 _("conversion to `%s' is not supported"),
254 to_pretty);
255 else
dd1e8878 256 error (0, 0,
fa00744e
UD
257 _("conversion from `%s' to `%s' is not supported"),
258 from_pretty, to_pretty);
259 }
dd1e8878
UD
260
261 argp_help (&argp, stderr, ARGP_HELP_SEE,
262 program_invocation_short_name);
263 exit (1);
fa00744e 264 }
93693c4d
UD
265 else
266 error (EXIT_FAILURE, errno,
267 _("failed to start conversion processing"));
268 }
adcf0e4a 269
93693c4d
UD
270 /* Now process the remaining files. Write them to stdout or the file
271 specified with the `-o' parameter. If we have no file given as
272 the parameter process all from stdin. */
273 if (remaining == argc)
274 {
275 if (process_file (cd, stdin, output) != 0)
276 status = EXIT_FAILURE;
277 }
278 else
279 do
280 {
d3f8be6d 281#ifdef _POSIX_MAPPED_FILES
93693c4d
UD
282 struct stat st;
283 char *addr;
d3f8be6d 284#endif
f2fcb018 285 int fd, ret;
93693c4d
UD
286
287 if (verbose)
c4f4ef87 288 fprintf (stderr, "%s:\n", argv[remaining]);
93693c4d
UD
289 if (strcmp (argv[remaining], "-") == 0)
290 fd = 0;
291 else
adcf0e4a 292 {
93693c4d
UD
293 fd = open (argv[remaining], O_RDONLY);
294
295 if (fd == -1)
296 {
297 error (0, errno, _("cannot open input file `%s'"),
298 argv[remaining]);
299 status = EXIT_FAILURE;
300 continue;
301 }
adcf0e4a 302 }
fb5663ca 303
7cabd57c 304#ifdef _POSIX_MAPPED_FILES
93693c4d
UD
305 /* We have possibilities for reading the input file. First try
306 to mmap() it since this will provide the fastest solution. */
307 if (fstat (fd, &st) == 0
308 && ((addr = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE,
309 fd, 0)) != MAP_FAILED))
fb5663ca 310 {
93693c4d
UD
311 /* Yes, we can use mmap(). The descriptor is not needed
312 anymore. */
313 if (close (fd) != 0)
314 error (EXIT_FAILURE, errno,
315 _("error while closing input `%s'"),
316 argv[remaining]);
317
f2fcb018
UD
318 ret = process_block (cd, addr, st.st_size, output);
319
320 /* We don't need the input data anymore. */
321 munmap ((void *) addr, st.st_size);
322
323 if (ret != 0)
93693c4d 324 {
93693c4d
UD
325 status = EXIT_FAILURE;
326
f2fcb018
UD
327 if (ret < 0)
328 /* We cannot go on with producing output since it might
329 lead to problem because the last output might leave
330 the output stream in an undefined state. */
331 break;
93693c4d 332 }
fb5663ca 333 }
93693c4d 334 else
7cabd57c 335#endif /* _POSIX_MAPPED_FILES */
fb5663ca 336 {
93693c4d 337 /* Read the file in pieces. */
f2fcb018
UD
338 ret = process_fd (cd, fd, output);
339
340 /* Now close the file. */
341 close (fd);
342
343 if (ret != 0)
93693c4d
UD
344 {
345 /* Something went wrong. */
346 status = EXIT_FAILURE;
347
f2fcb018
UD
348 if (ret < 0)
349 /* We cannot go on with producing output since it might
350 lead to problem because the last output might leave
351 the output stream in an undefined state. */
352 break;
93693c4d 353 }
fb5663ca 354 }
fb5663ca 355 }
93693c4d
UD
356 while (++remaining < argc);
357 }
fb5663ca
UD
358
359 /* Close the output file now. */
360 if (fclose (output))
361 error (EXIT_FAILURE, errno, _("error while closing output file"));
362
363 return status;
364}
365
366
367/* Handle program arguments. */
368static error_t
369parse_opt (int key, char *arg, struct argp_state *state)
370{
371 switch (key)
372 {
373 case 'f':
374 from_code = arg;
375 break;
376 case 't':
377 to_code = arg;
378 break;
379 case 'o':
380 output_file = arg;
381 break;
adcf0e4a
UD
382 case 's':
383 /* Nothing, for now at least. We are not giving out any information
384 about missing character or so. */
385 break;
386 case 'c':
85830c4c
UD
387 /* Omit invalid characters from output. */
388 omit_invalid = 1;
adcf0e4a 389 break;
fb5663ca
UD
390 case OPT_VERBOSE:
391 verbose = 1;
392 break;
8fe0fd03
UD
393 case OPT_LIST:
394 list = 1;
395 break;
fb5663ca
UD
396 default:
397 return ARGP_ERR_UNKNOWN;
398 }
399 return 0;
400}
401
402
403static char *
404more_help (int key, const char *text, void *input)
405{
406 switch (key)
407 {
408 case ARGP_KEY_HELP_EXTRA:
409 /* We print some extra information. */
410 return strdup (gettext ("\
d40eb37a
UD
411For bug reporting instructions, please see:\n\
412<http://www.gnu.org/software/libc/bugs.html>.\n"));
fb5663ca
UD
413 default:
414 break;
415 }
416 return (char *) text;
417}
418
419
420/* Print the version information. */
421static void
422print_version (FILE *stream, struct argp_state *state)
423{
424 fprintf (stream, "iconv (GNU %s) %s\n", PACKAGE, VERSION);
425 fprintf (stream, gettext ("\
426Copyright (C) %s Free Software Foundation, Inc.\n\
427This is free software; see the source for copying conditions. There is NO\n\
428warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
11bf311e 429"), "2007");
fb5663ca
UD
430 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
431}
432
433
434static int
3b434940 435process_block (iconv_t cd, char *addr, size_t len, FILE *output)
fb5663ca
UD
436{
437#define OUTBUF_SIZE 32768
9b26f5c4 438 const char *start = addr;
fb5663ca 439 char outbuf[OUTBUF_SIZE];
8619129f
UD
440 char *outptr;
441 size_t outlen;
442 size_t n;
f2fcb018 443 int ret = 0;
fb5663ca
UD
444
445 while (len > 0)
446 {
8619129f
UD
447 outptr = outbuf;
448 outlen = OUTBUF_SIZE;
449 n = iconv (cd, &addr, &len, &outptr, &outlen);
fb5663ca 450
f2fcb018
UD
451 if (n == (size_t) -1 && omit_invalid && errno == EILSEQ)
452 {
453 ret = 1;
454 if (len == 0)
455 n = 0;
456 else
457 errno = E2BIG;
458 }
459
fb5663ca
UD
460 if (outptr != outbuf)
461 {
462 /* We have something to write out. */
4d3a563f
UD
463 int errno_save = errno;
464
6dd67bd5
UD
465 if (fwrite (outbuf, 1, outptr - outbuf, output)
466 < (size_t) (outptr - outbuf)
fb5663ca
UD
467 || ferror (output))
468 {
469 /* Error occurred while printing the result. */
470 error (0, 0, _("\
471conversion stopped due to problem in writing the output"));
472 return -1;
473 }
4d3a563f
UD
474
475 errno = errno_save;
fb5663ca
UD
476 }
477
478 if (n != (size_t) -1)
c559a3ca
UD
479 {
480 /* All the input test is processed. For state-dependent
481 character sets we have to flush the state now. */
482 outptr = outbuf;
483 outlen = OUTBUF_SIZE;
f2fcb018 484 n = iconv (cd, NULL, NULL, &outptr, &outlen);
c559a3ca
UD
485
486 if (outptr != outbuf)
487 {
488 /* We have something to write out. */
489 int errno_save = errno;
490
6dd67bd5
UD
491 if (fwrite (outbuf, 1, outptr - outbuf, output)
492 < (size_t) (outptr - outbuf)
c559a3ca
UD
493 || ferror (output))
494 {
495 /* Error occurred while printing the result. */
496 error (0, 0, _("\
497conversion stopped due to problem in writing the output"));
498 return -1;
499 }
500
501 errno = errno_save;
502 }
503
f2fcb018
UD
504 if (n != (size_t) -1)
505 break;
506
507 if (omit_invalid && errno == EILSEQ)
508 {
509 ret = 1;
510 break;
511 }
c559a3ca 512 }
fb5663ca
UD
513
514 if (errno != E2BIG)
515 {
516 /* iconv() ran into a problem. */
517 switch (errno)
518 {
519 case EILSEQ:
b8750711
UD
520 if (! omit_invalid)
521 error (0, 0, _("illegal input sequence at position %ld"),
522 (long int) (addr - start));
fb5663ca
UD
523 break;
524 case EINVAL:
525 error (0, 0, _("\
526incomplete character or shift sequence at end of buffer"));
527 break;
528 case EBADF:
529 error (0, 0, _("internal error (illegal descriptor)"));
530 break;
531 default:
532 error (0, 0, _("unknown iconv() error %d"), errno);
533 break;
534 }
535
536 return -1;
537 }
538 }
539
f2fcb018 540 return ret;
fb5663ca
UD
541}
542
543
544static int
545process_fd (iconv_t cd, int fd, FILE *output)
546{
547 /* we have a problem with reading from a desriptor since we must not
548 provide the iconv() function an incomplete character or shift
549 sequence at the end of the buffer. Since we have to deal with
550 arbitrary encodings we must read the whole text in a buffer and
551 process it in one step. */
552 static char *inbuf = NULL;
553 static size_t maxlen = 0;
554 char *inptr = NULL;
555 size_t actlen = 0;
556
557 while (actlen < maxlen)
558 {
2e47aff5 559 ssize_t n = read (fd, inptr, maxlen - actlen);
fb5663ca
UD
560
561 if (n == 0)
562 /* No more text to read. */
563 break;
564
565 if (n == -1)
566 {
567 /* Error while reading. */
568 error (0, errno, _("error while reading the input"));
569 return -1;
570 }
571
572 inptr += n;
573 actlen += n;
574 }
575
576 if (actlen == maxlen)
577 while (1)
578 {
2e47aff5 579 ssize_t n;
d1dddedf 580 char *new_inbuf;
fb5663ca
UD
581
582 /* Increase the buffer. */
d1dddedf
UD
583 new_inbuf = (char *) realloc (inbuf, maxlen + 32768);
584 if (new_inbuf == NULL)
585 {
586 error (0, errno, _("unable to allocate buffer for input"));
587 return -1;
588 }
589 inbuf = new_inbuf;
fb5663ca 590 maxlen += 32768;
fb5663ca
UD
591 inptr = inbuf + actlen;
592
593 do
594 {
595 n = read (fd, inptr, maxlen - actlen);
596
597 if (n == 0)
598 /* No more text to read. */
599 break;
600
601 if (n == -1)
602 {
603 /* Error while reading. */
604 error (0, errno, _("error while reading the input"));
605 return -1;
606 }
607
608 inptr += n;
609 actlen += n;
610 }
611 while (actlen < maxlen);
612
613 if (n == 0)
614 /* Break again so we leave both loops. */
615 break;
616 }
617
618 /* Now we have all the input in the buffer. Process it in one run. */
619 return process_block (cd, inbuf, actlen, output);
620}
621
622
623static int
624process_file (iconv_t cd, FILE *input, FILE *output)
625{
626 /* This should be safe since we use this function only for `stdin' and
627 we haven't read anything so far. */
628 return process_fd (cd, fileno (input), output);
629}
8fe0fd03
UD
630
631
632/* Print all known character sets/encodings. */
633static void *printlist;
634static size_t column;
635static int not_first;
636
637static void
638insert_print_list (const void *nodep, VISIT value, int level)
639{
640 if (value == leaf || value == postorder)
641 {
642 const struct gconv_alias *s = *(const struct gconv_alias **) nodep;
9b26f5c4 643 tsearch (s->fromname, &printlist, (__compar_fn_t) strverscmp);
8fe0fd03
UD
644 }
645}
646
647static void
b17c0a8e 648do_print_human (const void *nodep, VISIT value, int level)
8fe0fd03
UD
649{
650 if (value == leaf || value == postorder)
651 {
652 const char *s = *(const char **) nodep;
653 size_t len = strlen (s);
654 size_t cnt;
655
656 while (len > 0 && s[len - 1] == '/')
657 --len;
658
659 for (cnt = 0; cnt < len; ++cnt)
660 if (isalnum (s[cnt]))
661 break;
662 if (cnt == len)
663 return;
664
665 if (not_first)
666 {
667 putchar (',');
668 ++column;
669
670 if (column > 2 && column + len > 77)
671 {
672 fputs ("\n ", stdout);
673 column = 2;
674 }
675 else
676 {
677 putchar (' ');
678 ++column;
679 }
680 }
681 else
9b26f5c4 682 not_first = 1;
8fe0fd03
UD
683
684 fwrite (s, len, 1, stdout);
685 column += len;
686 }
687}
688
b17c0a8e
UD
689static void
690do_print (const void *nodep, VISIT value, int level)
691{
692 if (value == leaf || value == postorder)
693 {
694 const char *s = *(const char **) nodep;
695
696 puts (s);
697 }
698}
699
8fe0fd03 700static void
2bd60880
UD
701internal_function
702add_known_names (struct gconv_module *node)
703{
704 if (node->left != NULL)
705 add_known_names (node->left);
706 if (node->right != NULL)
707 add_known_names (node->right);
2bd60880
UD
708 do
709 {
d2dfc5de
UD
710 if (strcmp (node->from_string, "INTERNAL"))
711 tsearch (node->from_string, &printlist,
712 (__compar_fn_t) strverscmp);
9a1f71a7 713 if (strcmp (node->to_string, "INTERNAL") != 0)
d2dfc5de 714 tsearch (node->to_string, &printlist, (__compar_fn_t) strverscmp);
2bd60880 715
d2dfc5de 716 node = node->same;
2bd60880
UD
717 }
718 while (node != NULL);
719}
720
9a1f71a7
UD
721
722static void
723insert_cache (void)
724{
725 const struct gconvcache_header *header;
726 const char *strtab;
727 const struct hash_entry *hashtab;
728 size_t cnt;
729
230491f0
UD
730 header = (const struct gconvcache_header *) __gconv_get_cache ();
731 strtab = (char *) header + header->string_offset;
732 hashtab = (struct hash_entry *) ((char *) header + header->hash_offset);
9a1f71a7
UD
733
734 for (cnt = 0; cnt < header->hash_size; ++cnt)
735 if (hashtab[cnt].string_offset != 0)
736 {
737 const char *str = strtab + hashtab[cnt].string_offset;
738
739 if (strcmp (str, "INTERNAL") != 0)
740 tsearch (str, &printlist, (__compar_fn_t) strverscmp);
741 }
742}
743
744
2bd60880
UD
745static void
746internal_function
8fe0fd03
UD
747print_known_names (void)
748{
8fe0fd03 749 iconv_t h;
230491f0 750 void *cache;
8fe0fd03
UD
751
752 /* We must initialize the internal databases first. */
753 h = iconv_open ("L1", "L1");
754 iconv_close (h);
755
9a1f71a7 756 /* See whether we have a cache. */
230491f0
UD
757 cache = __gconv_get_cache ();
758 if (cache != NULL)
9a1f71a7
UD
759 /* Yep, use only this information. */
760 insert_cache ();
761 else
762 {
230491f0
UD
763 struct gconv_module *modules;
764
9a1f71a7
UD
765 /* No, then use the information read from the gconv-modules file.
766 First add the aliases. */
230491f0 767 twalk (__gconv_get_alias_db (), insert_print_list);
8fe0fd03 768
9a1f71a7 769 /* Add the from- and to-names from the known modules. */
230491f0
UD
770 modules = __gconv_get_modules_db ();
771 if (modules != NULL)
772 add_known_names (modules);
9a1f71a7 773 }
8fe0fd03 774
1b6840e5
UD
775 bool human_readable = isatty (fileno (stdout));
776
777 if (human_readable)
778 fputs (_("\
8fe0fd03
UD
779The following list contain all the coded character sets known. This does\n\
780not necessarily mean that all combinations of these names can be used for\n\
781the FROM and TO command line parameters. One coded character set can be\n\
d2dfc5de 782listed with several different names (aliases).\n\n "), stdout);
8fe0fd03
UD
783
784 /* Now print the collected names. */
785 column = 2;
1b6840e5 786 twalk (printlist, human_readable ? do_print_human : do_print);
8fe0fd03 787
1b6840e5
UD
788 if (human_readable && column != 0)
789 puts ("");
8fe0fd03 790}