* configure.in: Remove gnu_ld definition and everything using it.
[glibc.git] / iconv / iconv_prog.c
blob5b3d35ecaaecf8ff0ba4e4febb1b3b2f2c7024f9
1 /* Convert text in given files from the specified from-set to the to-set.
2 Copyright (C) 1998-2003, 2004, 2005 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <argp.h>
22 #include <assert.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <error.h>
26 #include <fcntl.h>
27 #include <iconv.h>
28 #include <langinfo.h>
29 #include <locale.h>
30 #include <search.h>
31 #include <stdbool.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <libintl.h>
37 #ifdef _POSIX_MAPPED_FILES
38 # include <sys/mman.h>
39 #endif
40 #include <charmap.h>
41 #include <gconv_int.h>
42 #include "iconv_prog.h"
43 #include "iconvconfig.h"
45 /* Get libc version number. */
46 #include "../version.h"
48 #define PACKAGE _libc_intl_domainname
51 /* Name and version of program. */
52 static void print_version (FILE *stream, struct argp_state *state);
53 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
55 #define OPT_VERBOSE 1000
56 #define OPT_LIST 'l'
58 /* Definitions of arguments for argp functions. */
59 static const struct argp_option options[] =
61 { NULL, 0, NULL, 0, N_("Input/Output format specification:") },
62 { "from-code", 'f', "NAME", 0, N_("encoding of original text") },
63 { "to-code", 't', "NAME", 0, N_("encoding for output") },
64 { NULL, 0, NULL, 0, N_("Information:") },
65 { "list", 'l', NULL, 0, N_("list all known coded character sets") },
66 { NULL, 0, NULL, 0, N_("Output control:") },
67 { NULL, 'c', NULL, 0, N_("omit invalid characters from output") },
68 { "output", 'o', "FILE", 0, N_("output file") },
69 { "silent", 's', NULL, 0, N_("suppress warnings") },
70 { "verbose", OPT_VERBOSE, NULL, 0, N_("print progress information") },
71 { NULL, 0, NULL, 0, NULL }
74 /* Short description of program. */
75 static const char doc[] = N_("\
76 Convert encoding of given files from one encoding to another.");
78 /* Strings for arguments in help texts. */
79 static const char args_doc[] = N_("[FILE...]");
81 /* Prototype for option handler. */
82 static error_t parse_opt (int key, char *arg, struct argp_state *state);
84 /* Function to print some extra text in the help message. */
85 static char *more_help (int key, const char *text, void *input);
87 /* Data structure to communicate with argp functions. */
88 static struct argp argp =
90 options, parse_opt, args_doc, doc, NULL, more_help
93 /* Code sets to convert from and to respectively. An empty string as the
94 default causes the 'iconv_open' function to look up the charset of the
95 currently selected locale and use it. */
96 static const char *from_code = "";
97 static const char *to_code = "";
99 /* File to write output to. If NULL write to stdout. */
100 static const char *output_file;
102 /* Nonzero if verbose ouput is wanted. */
103 int verbose;
105 /* Nonzero if list of all coded character sets is wanted. */
106 static int list;
108 /* If nonzero omit invalid character from output. */
109 int omit_invalid;
111 /* Prototypes for the functions doing the actual work. */
112 static int process_block (iconv_t cd, char *addr, size_t len, FILE *output);
113 static int process_fd (iconv_t cd, int fd, FILE *output);
114 static int process_file (iconv_t cd, FILE *input, FILE *output);
115 static void print_known_names (void) internal_function;
119 main (int argc, char *argv[])
121 int status = EXIT_SUCCESS;
122 int remaining;
123 FILE *output;
124 iconv_t cd;
125 const char *orig_to_code;
126 struct charmap_t *from_charmap = NULL;
127 struct charmap_t *to_charmap = NULL;
129 /* Set locale via LC_ALL. */
130 setlocale (LC_ALL, "");
132 /* Set the text message domain. */
133 textdomain (_libc_intl_domainname);
135 /* Parse and process arguments. */
136 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
138 /* List all coded character sets if wanted. */
139 if (list)
141 print_known_names ();
142 exit (EXIT_SUCCESS);
145 /* If we have to ignore errors make sure we use the appropriate name for
146 the to-character-set. */
147 orig_to_code = to_code;
148 if (omit_invalid)
150 const char *errhand = strchrnul (to_code, '/');
151 int nslash = 2;
152 char *newp;
153 char *cp;
155 if (*errhand == '/')
157 --nslash;
158 errhand = strchrnul (errhand, '/');
160 if (*errhand == '/')
162 --nslash;
163 errhand = strchr (errhand, '\0');
167 newp = (char *) alloca (errhand - to_code + nslash + 7 + 1);
168 cp = mempcpy (newp, to_code, errhand - to_code);
169 while (nslash-- > 0)
170 *cp++ = '/';
171 if (cp[-1] != '/')
172 *cp++ = ',';
173 memcpy (cp, "IGNORE", sizeof ("IGNORE"));
175 to_code = newp;
178 /* POSIX 1003.2b introduces a silly thing: the arguments to -t anf -f
179 can be file names of charmaps. In this case iconv will have to read
180 those charmaps and use them to do the conversion. But there are
181 holes in the specification. There is nothing said that if -f is a
182 charmap filename that -t must be, too. And vice versa. There is
183 also no word about the symbolic names used. What if they don't
184 match? */
185 if (strchr (from_code, '/') != NULL)
186 /* The from-name might be a charmap file name. Try reading the
187 file. */
188 from_charmap = charmap_read (from_code, /*0, 1*/1, 0, 0);
190 if (strchr (orig_to_code, '/') != NULL)
191 /* The to-name might be a charmap file name. Try reading the
192 file. */
193 to_charmap = charmap_read (orig_to_code, /*0, 1,*/1,0, 0);
196 /* Determine output file. */
197 if (output_file != NULL && strcmp (output_file, "-") != 0)
199 output = fopen (output_file, "w");
200 if (output == NULL)
201 error (EXIT_FAILURE, errno, _("cannot open output file"));
203 else
204 output = stdout;
206 /* At this point we have to handle two cases. The first one is
207 where a charmap is used for the from- or to-charset, or both. We
208 handle this special since it is very different from the sane way of
209 doing things. The other case allows converting using the iconv()
210 function. */
211 if (from_charmap != NULL || to_charmap != NULL)
212 /* Construct the conversion table and do the conversion. */
213 status = charmap_conversion (from_code, from_charmap, to_code, to_charmap,
214 argc, remaining, argv, output);
215 else
217 /* Let's see whether we have these coded character sets. */
218 cd = iconv_open (to_code, from_code);
219 if (cd == (iconv_t) -1)
221 if (errno == EINVAL)
223 /* Try to be nice with the user and tell her which of the
224 two encoding names is wrong. This is possible because
225 all supported encodings can be converted from/to Unicode,
226 in other words, because the graph of encodings is
227 connected. */
228 bool from_wrong =
229 (iconv_open ("UTF-8", from_code) == (iconv_t) -1
230 && errno == EINVAL);
231 bool to_wrong =
232 (iconv_open (to_code, "UTF-8") == (iconv_t) -1
233 && errno == EINVAL);
234 const char *from_pretty =
235 (from_code[0] ? from_code : nl_langinfo (CODESET));
236 const char *to_pretty =
237 (orig_to_code[0] ? orig_to_code : nl_langinfo (CODESET));
239 if (from_wrong)
241 if (to_wrong)
242 error (0, 0,
243 _("\
244 conversion from `%s' and to `%s' are not supported"),
245 from_pretty, to_pretty);
246 else
247 error (0, 0,
248 _("conversion from `%s' is not supported"),
249 from_pretty);
251 else
253 if (to_wrong)
254 error (0, 0,
255 _("conversion to `%s' is not supported"),
256 to_pretty);
257 else
258 error (0, 0,
259 _("conversion from `%s' to `%s' is not supported"),
260 from_pretty, to_pretty);
263 argp_help (&argp, stderr, ARGP_HELP_SEE,
264 program_invocation_short_name);
265 exit (1);
267 else
268 error (EXIT_FAILURE, errno,
269 _("failed to start conversion processing"));
272 /* Now process the remaining files. Write them to stdout or the file
273 specified with the `-o' parameter. If we have no file given as
274 the parameter process all from stdin. */
275 if (remaining == argc)
277 if (process_file (cd, stdin, output) != 0)
278 status = EXIT_FAILURE;
280 else
283 #ifdef _POSIX_MAPPED_FILES
284 struct stat st;
285 char *addr;
286 #endif
287 int fd, ret;
289 if (verbose)
290 fprintf (stderr, "%s:\n", argv[remaining]);
291 if (strcmp (argv[remaining], "-") == 0)
292 fd = 0;
293 else
295 fd = open (argv[remaining], O_RDONLY);
297 if (fd == -1)
299 error (0, errno, _("cannot open input file `%s'"),
300 argv[remaining]);
301 status = EXIT_FAILURE;
302 continue;
306 #ifdef _POSIX_MAPPED_FILES
307 /* We have possibilities for reading the input file. First try
308 to mmap() it since this will provide the fastest solution. */
309 if (fstat (fd, &st) == 0
310 && ((addr = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE,
311 fd, 0)) != MAP_FAILED))
313 /* Yes, we can use mmap(). The descriptor is not needed
314 anymore. */
315 if (close (fd) != 0)
316 error (EXIT_FAILURE, errno,
317 _("error while closing input `%s'"),
318 argv[remaining]);
320 ret = process_block (cd, addr, st.st_size, output);
322 /* We don't need the input data anymore. */
323 munmap ((void *) addr, st.st_size);
325 if (ret != 0)
327 status = EXIT_FAILURE;
329 if (ret < 0)
330 /* We cannot go on with producing output since it might
331 lead to problem because the last output might leave
332 the output stream in an undefined state. */
333 break;
336 else
337 #endif /* _POSIX_MAPPED_FILES */
339 /* Read the file in pieces. */
340 ret = process_fd (cd, fd, output);
342 /* Now close the file. */
343 close (fd);
345 if (ret != 0)
347 /* Something went wrong. */
348 status = EXIT_FAILURE;
350 if (ret < 0)
351 /* We cannot go on with producing output since it might
352 lead to problem because the last output might leave
353 the output stream in an undefined state. */
354 break;
358 while (++remaining < argc);
361 /* Close the output file now. */
362 if (fclose (output))
363 error (EXIT_FAILURE, errno, _("error while closing output file"));
365 return status;
369 /* Handle program arguments. */
370 static error_t
371 parse_opt (int key, char *arg, struct argp_state *state)
373 switch (key)
375 case 'f':
376 from_code = arg;
377 break;
378 case 't':
379 to_code = arg;
380 break;
381 case 'o':
382 output_file = arg;
383 break;
384 case 's':
385 /* Nothing, for now at least. We are not giving out any information
386 about missing character or so. */
387 break;
388 case 'c':
389 /* Omit invalid characters from output. */
390 omit_invalid = 1;
391 break;
392 case OPT_VERBOSE:
393 verbose = 1;
394 break;
395 case OPT_LIST:
396 list = 1;
397 break;
398 default:
399 return ARGP_ERR_UNKNOWN;
401 return 0;
405 static char *
406 more_help (int key, const char *text, void *input)
408 switch (key)
410 case ARGP_KEY_HELP_EXTRA:
411 /* We print some extra information. */
412 return strdup (gettext ("\
413 For bug reporting instructions, please see:\n\
414 <http://www.gnu.org/software/libc/bugs.html>.\n"));
415 default:
416 break;
418 return (char *) text;
422 /* Print the version information. */
423 static void
424 print_version (FILE *stream, struct argp_state *state)
426 fprintf (stream, "iconv (GNU %s) %s\n", PACKAGE, VERSION);
427 fprintf (stream, gettext ("\
428 Copyright (C) %s Free Software Foundation, Inc.\n\
429 This is free software; see the source for copying conditions. There is NO\n\
430 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
431 "), "2005");
432 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
436 static int
437 process_block (iconv_t cd, char *addr, size_t len, FILE *output)
439 #define OUTBUF_SIZE 32768
440 const char *start = addr;
441 char outbuf[OUTBUF_SIZE];
442 char *outptr;
443 size_t outlen;
444 size_t n;
445 int ret = 0;
447 while (len > 0)
449 outptr = outbuf;
450 outlen = OUTBUF_SIZE;
451 n = iconv (cd, &addr, &len, &outptr, &outlen);
453 if (n == (size_t) -1 && omit_invalid && errno == EILSEQ)
455 ret = 1;
456 if (len == 0)
457 n = 0;
458 else
459 errno = E2BIG;
462 if (outptr != outbuf)
464 /* We have something to write out. */
465 int errno_save = errno;
467 if (fwrite (outbuf, 1, outptr - outbuf, output)
468 < (size_t) (outptr - outbuf)
469 || ferror (output))
471 /* Error occurred while printing the result. */
472 error (0, 0, _("\
473 conversion stopped due to problem in writing the output"));
474 return -1;
477 errno = errno_save;
480 if (n != (size_t) -1)
482 /* All the input test is processed. For state-dependent
483 character sets we have to flush the state now. */
484 outptr = outbuf;
485 outlen = OUTBUF_SIZE;
486 n = iconv (cd, NULL, NULL, &outptr, &outlen);
488 if (outptr != outbuf)
490 /* We have something to write out. */
491 int errno_save = errno;
493 if (fwrite (outbuf, 1, outptr - outbuf, output)
494 < (size_t) (outptr - outbuf)
495 || ferror (output))
497 /* Error occurred while printing the result. */
498 error (0, 0, _("\
499 conversion stopped due to problem in writing the output"));
500 return -1;
503 errno = errno_save;
506 if (n != (size_t) -1)
507 break;
509 if (omit_invalid && errno == EILSEQ)
511 ret = 1;
512 break;
516 if (errno != E2BIG)
518 /* iconv() ran into a problem. */
519 switch (errno)
521 case EILSEQ:
522 if (! omit_invalid)
523 error (0, 0, _("illegal input sequence at position %ld"),
524 (long int) (addr - start));
525 break;
526 case EINVAL:
527 error (0, 0, _("\
528 incomplete character or shift sequence at end of buffer"));
529 break;
530 case EBADF:
531 error (0, 0, _("internal error (illegal descriptor)"));
532 break;
533 default:
534 error (0, 0, _("unknown iconv() error %d"), errno);
535 break;
538 return -1;
542 return ret;
546 static int
547 process_fd (iconv_t cd, int fd, FILE *output)
549 /* we have a problem with reading from a desriptor since we must not
550 provide the iconv() function an incomplete character or shift
551 sequence at the end of the buffer. Since we have to deal with
552 arbitrary encodings we must read the whole text in a buffer and
553 process it in one step. */
554 static char *inbuf = NULL;
555 static size_t maxlen = 0;
556 char *inptr = NULL;
557 size_t actlen = 0;
559 while (actlen < maxlen)
561 ssize_t n = read (fd, inptr, maxlen - actlen);
563 if (n == 0)
564 /* No more text to read. */
565 break;
567 if (n == -1)
569 /* Error while reading. */
570 error (0, errno, _("error while reading the input"));
571 return -1;
574 inptr += n;
575 actlen += n;
578 if (actlen == maxlen)
579 while (1)
581 ssize_t n;
582 char *new_inbuf;
584 /* Increase the buffer. */
585 new_inbuf = (char *) realloc (inbuf, maxlen + 32768);
586 if (new_inbuf == NULL)
588 error (0, errno, _("unable to allocate buffer for input"));
589 return -1;
591 inbuf = new_inbuf;
592 maxlen += 32768;
593 inptr = inbuf + actlen;
597 n = read (fd, inptr, maxlen - actlen);
599 if (n == 0)
600 /* No more text to read. */
601 break;
603 if (n == -1)
605 /* Error while reading. */
606 error (0, errno, _("error while reading the input"));
607 return -1;
610 inptr += n;
611 actlen += n;
613 while (actlen < maxlen);
615 if (n == 0)
616 /* Break again so we leave both loops. */
617 break;
620 /* Now we have all the input in the buffer. Process it in one run. */
621 return process_block (cd, inbuf, actlen, output);
625 static int
626 process_file (iconv_t cd, FILE *input, FILE *output)
628 /* This should be safe since we use this function only for `stdin' and
629 we haven't read anything so far. */
630 return process_fd (cd, fileno (input), output);
634 /* Print all known character sets/encodings. */
635 static void *printlist;
636 static size_t column;
637 static int not_first;
639 static void
640 insert_print_list (const void *nodep, VISIT value, int level)
642 if (value == leaf || value == postorder)
644 const struct gconv_alias *s = *(const struct gconv_alias **) nodep;
645 tsearch (s->fromname, &printlist, (__compar_fn_t) strverscmp);
649 static void
650 do_print_human (const void *nodep, VISIT value, int level)
652 if (value == leaf || value == postorder)
654 const char *s = *(const char **) nodep;
655 size_t len = strlen (s);
656 size_t cnt;
658 while (len > 0 && s[len - 1] == '/')
659 --len;
661 for (cnt = 0; cnt < len; ++cnt)
662 if (isalnum (s[cnt]))
663 break;
664 if (cnt == len)
665 return;
667 if (not_first)
669 putchar (',');
670 ++column;
672 if (column > 2 && column + len > 77)
674 fputs ("\n ", stdout);
675 column = 2;
677 else
679 putchar (' ');
680 ++column;
683 else
684 not_first = 1;
686 fwrite (s, len, 1, stdout);
687 column += len;
691 static void
692 do_print (const void *nodep, VISIT value, int level)
694 if (value == leaf || value == postorder)
696 const char *s = *(const char **) nodep;
698 puts (s);
702 static void
703 internal_function
704 add_known_names (struct gconv_module *node)
706 if (node->left != NULL)
707 add_known_names (node->left);
708 if (node->right != NULL)
709 add_known_names (node->right);
712 if (strcmp (node->from_string, "INTERNAL"))
713 tsearch (node->from_string, &printlist,
714 (__compar_fn_t) strverscmp);
715 if (strcmp (node->to_string, "INTERNAL") != 0)
716 tsearch (node->to_string, &printlist, (__compar_fn_t) strverscmp);
718 node = node->same;
720 while (node != NULL);
724 static void
725 insert_cache (void)
727 const struct gconvcache_header *header;
728 const char *strtab;
729 const struct hash_entry *hashtab;
730 size_t cnt;
732 header = (const struct gconvcache_header *) __gconv_get_cache ();
733 strtab = (char *) header + header->string_offset;
734 hashtab = (struct hash_entry *) ((char *) header + header->hash_offset);
736 for (cnt = 0; cnt < header->hash_size; ++cnt)
737 if (hashtab[cnt].string_offset != 0)
739 const char *str = strtab + hashtab[cnt].string_offset;
741 if (strcmp (str, "INTERNAL") != 0)
742 tsearch (str, &printlist, (__compar_fn_t) strverscmp);
747 static void
748 internal_function
749 print_known_names (void)
751 iconv_t h;
752 void *cache;
754 /* We must initialize the internal databases first. */
755 h = iconv_open ("L1", "L1");
756 iconv_close (h);
758 /* See whether we have a cache. */
759 cache = __gconv_get_cache ();
760 if (cache != NULL)
761 /* Yep, use only this information. */
762 insert_cache ();
763 else
765 struct gconv_module *modules;
767 /* No, then use the information read from the gconv-modules file.
768 First add the aliases. */
769 twalk (__gconv_get_alias_db (), insert_print_list);
771 /* Add the from- and to-names from the known modules. */
772 modules = __gconv_get_modules_db ();
773 if (modules != NULL)
774 add_known_names (modules);
777 bool human_readable = isatty (fileno (stdout));
779 if (human_readable)
780 fputs (_("\
781 The following list contain all the coded character sets known. This does\n\
782 not necessarily mean that all combinations of these names can be used for\n\
783 the FROM and TO command line parameters. One coded character set can be\n\
784 listed with several different names (aliases).\n\n "), stdout);
786 /* Now print the collected names. */
787 column = 2;
788 twalk (printlist, human_readable ? do_print_human : do_print);
790 if (human_readable && column != 0)
791 puts ("");