1 /* Convert text in given files from the specified from-set to the to-set.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002 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
35 #ifdef _POSIX_MAPPED_FILES
36 # include <sys/mman.h>
39 #include <gconv_int.h>
40 #include "iconv_prog.h"
41 #include "iconvconfig.h"
43 /* Get libc version number. */
44 #include "../version.h"
46 #define PACKAGE _libc_intl_domainname
49 /* Defined in gconv_cache.c. */
50 extern void *__gconv_cache;
52 /* Name and version of program. */
53 static void print_version (FILE *stream, struct argp_state *state);
54 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
56 #define OPT_VERBOSE 1000
59 /* Definitions of arguments for argp functions. */
60 static const struct argp_option options[] =
62 { NULL, 0, NULL, 0, N_("Input/Output format specification:") },
63 { "from-code", 'f', "NAME", 0, N_("encoding of original text") },
64 { "to-code", 't', "NAME", 0, N_("encoding for output") },
65 { NULL, 0, NULL, 0, N_("Information:") },
66 { "list", 'l', NULL, 0, N_("list all known coded character sets") },
67 { NULL, 0, NULL, 0, N_("Output control:") },
68 { NULL, 'c', NULL, 0, N_("omit invalid characters from output") },
69 { "output", 'o', "FILE", 0, N_("output file") },
70 { "silent", 's', NULL, 0, N_("suppress warnings") },
71 { "verbose", OPT_VERBOSE, NULL, 0, N_("print progress information") },
72 { NULL, 0, NULL, 0, NULL }
75 /* Short description of program. */
76 static const char doc[] = N_("\
77 Convert encoding of given files from one encoding to another.");
79 /* Strings for arguments in help texts. */
80 static const char args_doc[] = N_("[FILE...]");
82 /* Prototype for option handler. */
83 static error_t parse_opt (int key, char *arg, struct argp_state *state);
85 /* Function to print some extra text in the help message. */
86 static char *more_help (int key, const char *text, void *input);
88 /* Data structure to communicate with argp functions. */
89 static struct argp argp =
91 options, parse_opt, args_doc, doc, NULL, more_help
94 /* Code sets to convert from and to respectively. An empty string as the
95 default causes the 'iconv_open' function to look up the charset of the
96 currently selected locale and use it. */
97 static const char *from_code = "";
98 static const char *to_code = "";
100 /* File to write output to. If NULL write to stdout. */
101 static const char *output_file;
103 /* Nonzero if verbose ouput is wanted. */
106 /* Nonzero if list of all coded character sets is wanted. */
109 /* If nonzero omit invalid character from output. */
112 /* Prototypes for the functions doing the actual work. */
113 static int process_block (iconv_t cd, char *addr, size_t len, FILE *output);
114 static int process_fd (iconv_t cd, int fd, FILE *output);
115 static int process_file (iconv_t cd, FILE *input, FILE *output);
116 static void print_known_names (void) internal_function;
120 main (int argc, char *argv[])
122 int status = EXIT_SUCCESS;
126 const char *orig_to_code;
127 struct charmap_t *from_charmap = NULL;
128 struct charmap_t *to_charmap = NULL;
130 /* Set locale via LC_ALL. */
131 setlocale (LC_ALL, "");
133 /* Set the text message domain. */
134 textdomain (_libc_intl_domainname);
136 /* Parse and process arguments. */
137 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
139 /* List all coded character sets if wanted. */
142 print_known_names ();
146 /* If we have to ignore errors make sure we use the appropriate name for
147 the to-character-set. */
148 orig_to_code = to_code;
151 const char *errhand = strchrnul (to_code, '/');
159 errhand = strchrnul (errhand, '/');
168 newp = (char *) alloca (errhand - to_code + nslash + 6 + 1);
169 cp = mempcpy (newp, to_code, errhand - to_code);
172 memcpy (cp, "IGNORE", sizeof ("IGNORE"));
177 /* POSIX 1003.2b introduces a silly thing: the arguments to -t anf -f
178 can be file names of charmaps. In this case iconv will have to read
179 those charmaps and use them to do the conversion. But there are
180 holes in the specification. There is nothing said that if -f is a
181 charmap filename that -t must be, too. And vice versa. There is
182 also no word about the symbolic names used. What if they don't
184 if (strchr (from_code, '/') != NULL)
185 /* The from-name might be a charmap file name. Try reading the
187 from_charmap = charmap_read (from_code, /*0, 1*/1, 0, 0);
189 if (strchr (orig_to_code, '/') != NULL)
190 /* The to-name might be a charmap file name. Try reading the
192 to_charmap = charmap_read (orig_to_code, /*0, 1,*/1,0, 0);
195 /* Determine output file. */
196 if (output_file != NULL && strcmp (output_file, "-") != 0)
198 output = fopen (output_file, "w");
200 error (EXIT_FAILURE, errno, _("cannot open output file"));
205 /* At this point we have to handle two cases. The first one is
206 where a charmap is used for the from- or to-charset, or both. We
207 handle this special since it is very different from the sane way of
208 doing things. The other case allows converting using the iconv()
210 if (from_charmap != NULL || to_charmap != NULL)
211 /* Construct the conversion table and do the conversion. */
212 status = charmap_conversion (from_code, from_charmap, to_code, to_charmap,
213 argc, remaining, argv, output);
216 /* Let's see whether we have these coded character sets. */
217 cd = iconv_open (to_code, from_code);
218 if (cd == (iconv_t) -1)
221 error (EXIT_FAILURE, 0,
222 _("conversion from `%s' to `%s' not supported"),
223 from_code, orig_to_code);
225 error (EXIT_FAILURE, errno,
226 _("failed to start conversion processing"));
229 /* Now process the remaining files. Write them to stdout or the file
230 specified with the `-o' parameter. If we have no file given as
231 the parameter process all from stdin. */
232 if (remaining == argc)
234 if (process_file (cd, stdin, output) != 0)
235 status = EXIT_FAILURE;
240 #ifdef _POSIX_MAPPED_FILES
247 printf ("%s:\n", argv[remaining]);
248 if (strcmp (argv[remaining], "-") == 0)
252 fd = open (argv[remaining], O_RDONLY);
256 error (0, errno, _("cannot open input file `%s'"),
258 status = EXIT_FAILURE;
263 #ifdef _POSIX_MAPPED_FILES
264 /* We have possibilities for reading the input file. First try
265 to mmap() it since this will provide the fastest solution. */
266 if (fstat (fd, &st) == 0
267 && ((addr = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE,
268 fd, 0)) != MAP_FAILED))
270 /* Yes, we can use mmap(). The descriptor is not needed
273 error (EXIT_FAILURE, errno,
274 _("error while closing input `%s'"),
277 if (process_block (cd, addr, st.st_size, output) < 0)
279 /* Something went wrong. */
280 status = EXIT_FAILURE;
282 /* We don't need the input data anymore. */
283 munmap ((void *) addr, st.st_size);
285 /* We cannot go on with producing output since it might
286 lead to problem because the last output might leave
287 the output stream in an undefined state. */
291 /* We don't need the input data anymore. */
292 munmap ((void *) addr, st.st_size);
295 #endif /* _POSIX_MAPPED_FILES */
297 /* Read the file in pieces. */
298 if (process_fd (cd, fd, output) != 0)
300 /* Something went wrong. */
301 status = EXIT_FAILURE;
303 /* We don't need the input file anymore. */
306 /* We cannot go on with producing output since it might
307 lead to problem because the last output might leave
308 the output stream in an undefined state. */
312 /* Now close the file. */
316 while (++remaining < argc);
319 /* Close the output file now. */
321 error (EXIT_FAILURE, errno, _("error while closing output file"));
327 /* Handle program arguments. */
329 parse_opt (int key, char *arg, struct argp_state *state)
343 /* Nothing, for now at least. We are not giving out any information
344 about missing character or so. */
347 /* Omit invalid characters from output. */
357 return ARGP_ERR_UNKNOWN;
364 more_help (int key, const char *text, void *input)
368 case ARGP_KEY_HELP_EXTRA:
369 /* We print some extra information. */
370 return strdup (gettext ("\
371 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
375 return (char *) text;
379 /* Print the version information. */
381 print_version (FILE *stream, struct argp_state *state)
383 fprintf (stream, "iconv (GNU %s) %s\n", PACKAGE, VERSION);
384 fprintf (stream, gettext ("\
385 Copyright (C) %s Free Software Foundation, Inc.\n\
386 This is free software; see the source for copying conditions. There is NO\n\
387 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
389 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
394 process_block (iconv_t cd, char *addr, size_t len, FILE *output)
396 #define OUTBUF_SIZE 32768
397 const char *start = addr;
398 char outbuf[OUTBUF_SIZE];
406 outlen = OUTBUF_SIZE;
407 n = iconv (cd, &addr, &len, &outptr, &outlen);
409 if (outptr != outbuf)
411 /* We have something to write out. */
412 int errno_save = errno;
414 if (fwrite (outbuf, 1, outptr - outbuf, output) < outptr - outbuf
417 /* Error occurred while printing the result. */
419 conversion stopped due to problem in writing the output"));
426 if (n != (size_t) -1)
428 /* All the input test is processed. For state-dependent
429 character sets we have to flush the state now. */
431 outlen = OUTBUF_SIZE;
432 (void) iconv (cd, NULL, NULL, &outptr, &outlen);
434 if (outptr != outbuf)
436 /* We have something to write out. */
437 int errno_save = errno;
439 if (fwrite (outbuf, 1, outptr - outbuf, output) < outptr - outbuf
442 /* Error occurred while printing the result. */
444 conversion stopped due to problem in writing the output"));
456 /* iconv() ran into a problem. */
460 error (0, 0, _("illegal input sequence at position %ld"),
461 (long) (addr - start));
465 incomplete character or shift sequence at end of buffer"));
468 error (0, 0, _("internal error (illegal descriptor)"));
471 error (0, 0, _("unknown iconv() error %d"), errno);
484 process_fd (iconv_t cd, int fd, FILE *output)
486 /* we have a problem with reading from a desriptor since we must not
487 provide the iconv() function an incomplete character or shift
488 sequence at the end of the buffer. Since we have to deal with
489 arbitrary encodings we must read the whole text in a buffer and
490 process it in one step. */
491 static char *inbuf = NULL;
492 static size_t maxlen = 0;
496 while (actlen < maxlen)
498 ssize_t n = read (fd, inptr, maxlen - actlen);
501 /* No more text to read. */
506 /* Error while reading. */
507 error (0, errno, _("error while reading the input"));
515 if (actlen == maxlen)
521 /* Increase the buffer. */
522 new_inbuf = (char *) realloc (inbuf, maxlen + 32768);
523 if (new_inbuf == NULL)
525 error (0, errno, _("unable to allocate buffer for input"));
530 inptr = inbuf + actlen;
534 n = read (fd, inptr, maxlen - actlen);
537 /* No more text to read. */
542 /* Error while reading. */
543 error (0, errno, _("error while reading the input"));
550 while (actlen < maxlen);
553 /* Break again so we leave both loops. */
557 /* Now we have all the input in the buffer. Process it in one run. */
558 return process_block (cd, inbuf, actlen, output);
563 process_file (iconv_t cd, FILE *input, FILE *output)
565 /* This should be safe since we use this function only for `stdin' and
566 we haven't read anything so far. */
567 return process_fd (cd, fileno (input), output);
571 /* Print all known character sets/encodings. */
572 static void *printlist;
573 static size_t column;
574 static int not_first;
577 insert_print_list (const void *nodep, VISIT value, int level)
579 if (value == leaf || value == postorder)
581 const struct gconv_alias *s = *(const struct gconv_alias **) nodep;
582 tsearch (s->fromname, &printlist, (__compar_fn_t) strverscmp);
587 do_print_human (const void *nodep, VISIT value, int level)
589 if (value == leaf || value == postorder)
591 const char *s = *(const char **) nodep;
592 size_t len = strlen (s);
595 while (len > 0 && s[len - 1] == '/')
598 for (cnt = 0; cnt < len; ++cnt)
599 if (isalnum (s[cnt]))
609 if (column > 2 && column + len > 77)
611 fputs ("\n ", stdout);
623 fwrite (s, len, 1, stdout);
629 do_print (const void *nodep, VISIT value, int level)
631 if (value == leaf || value == postorder)
633 const char *s = *(const char **) nodep;
641 add_known_names (struct gconv_module *node)
643 if (node->left != NULL)
644 add_known_names (node->left);
645 if (node->right != NULL)
646 add_known_names (node->right);
649 if (strcmp (node->from_string, "INTERNAL"))
650 tsearch (node->from_string, &printlist,
651 (__compar_fn_t) strverscmp);
652 if (strcmp (node->to_string, "INTERNAL") != 0)
653 tsearch (node->to_string, &printlist, (__compar_fn_t) strverscmp);
657 while (node != NULL);
664 const struct gconvcache_header *header;
666 const struct hash_entry *hashtab;
669 header = (const struct gconvcache_header *) __gconv_cache;
670 strtab = (char *) __gconv_cache + header->string_offset;
671 hashtab = (struct hash_entry *) ((char *) __gconv_cache
672 + header->hash_offset);
674 for (cnt = 0; cnt < header->hash_size; ++cnt)
675 if (hashtab[cnt].string_offset != 0)
677 const char *str = strtab + hashtab[cnt].string_offset;
679 if (strcmp (str, "INTERNAL") != 0)
680 tsearch (str, &printlist, (__compar_fn_t) strverscmp);
687 print_known_names (void)
691 /* We must initialize the internal databases first. */
692 h = iconv_open ("L1", "L1");
695 /* See whether we have a cache. */
696 if (__gconv_cache != NULL)
697 /* Yep, use only this information. */
701 /* No, then use the information read from the gconv-modules file.
702 First add the aliases. */
703 twalk (__gconv_alias_db, insert_print_list);
705 /* Add the from- and to-names from the known modules. */
706 if (__gconv_modules_db != NULL)
707 add_known_names (__gconv_modules_db);
711 The following list contain all the coded character sets known. This does\n\
712 not necessarily mean that all combinations of these names can be used for\n\
713 the FROM and TO command line parameters. One coded character set can be\n\
714 listed with several different names (aliases).\n\n "), stdout);
716 /* Now print the collected names. */
718 if (isatty (fileno (stdout)))
720 twalk (printlist, do_print_human);
726 twalk (printlist, do_print);