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
37 #ifdef _POSIX_MAPPED_FILES
38 # include <sys/mman.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
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. */
105 /* Nonzero if list of all coded character sets is wanted. */
108 /* If nonzero omit invalid character from output. */
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
;
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. */
141 print_known_names ();
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
;
150 const char *errhand
= strchrnul (to_code
, '/');
158 errhand
= strchrnul (errhand
, '/');
167 newp
= (char *) alloca (errhand
- to_code
+ nslash
+ 6 + 1);
168 cp
= mempcpy (newp
, to_code
, errhand
- to_code
);
171 memcpy (cp
, "IGNORE", sizeof ("IGNORE"));
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
183 if (strchr (from_code
, '/') != NULL
)
184 /* The from-name might be a charmap file name. Try reading the
186 from_charmap
= charmap_read (from_code
, /*0, 1*/1, 0, 0);
188 if (strchr (orig_to_code
, '/') != NULL
)
189 /* The to-name might be a charmap file name. Try reading the
191 to_charmap
= charmap_read (orig_to_code
, /*0, 1,*/1,0, 0);
194 /* Determine output file. */
195 if (output_file
!= NULL
&& strcmp (output_file
, "-") != 0)
197 output
= fopen (output_file
, "w");
199 error (EXIT_FAILURE
, errno
, _("cannot open output file"));
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()
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
);
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)
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
227 (iconv_open ("UTF-8", from_code
) == (iconv_t
) -1
230 (iconv_open (to_code
, "UTF-8") == (iconv_t
) -1
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
));
240 error (EXIT_FAILURE
, 0,
242 conversions from `%s' and to `%s' are not supported"),
243 from_pretty
, to_pretty
);
245 error (EXIT_FAILURE
, 0,
246 _("conversion from `%s' is not supported"),
252 error (EXIT_FAILURE
, 0,
253 _("conversion to `%s' is not supported"),
256 error (EXIT_FAILURE
, 0,
257 _("conversion from `%s' to `%s' is not supported"),
258 from_pretty
, to_pretty
);
262 error (EXIT_FAILURE
, errno
,
263 _("failed to start conversion processing"));
266 /* Now process the remaining files. Write them to stdout or the file
267 specified with the `-o' parameter. If we have no file given as
268 the parameter process all from stdin. */
269 if (remaining
== argc
)
271 if (process_file (cd
, stdin
, output
) != 0)
272 status
= EXIT_FAILURE
;
277 #ifdef _POSIX_MAPPED_FILES
284 printf ("%s:\n", argv
[remaining
]);
285 if (strcmp (argv
[remaining
], "-") == 0)
289 fd
= open (argv
[remaining
], O_RDONLY
);
293 error (0, errno
, _("cannot open input file `%s'"),
295 status
= EXIT_FAILURE
;
300 #ifdef _POSIX_MAPPED_FILES
301 /* We have possibilities for reading the input file. First try
302 to mmap() it since this will provide the fastest solution. */
303 if (fstat (fd
, &st
) == 0
304 && ((addr
= mmap (NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
,
305 fd
, 0)) != MAP_FAILED
))
307 /* Yes, we can use mmap(). The descriptor is not needed
310 error (EXIT_FAILURE
, errno
,
311 _("error while closing input `%s'"),
314 if (process_block (cd
, addr
, st
.st_size
, output
) < 0)
316 /* Something went wrong. */
317 status
= EXIT_FAILURE
;
319 /* We don't need the input data anymore. */
320 munmap ((void *) addr
, st
.st_size
);
322 /* We cannot go on with producing output since it might
323 lead to problem because the last output might leave
324 the output stream in an undefined state. */
328 /* We don't need the input data anymore. */
329 munmap ((void *) addr
, st
.st_size
);
332 #endif /* _POSIX_MAPPED_FILES */
334 /* Read the file in pieces. */
335 if (process_fd (cd
, fd
, output
) != 0)
337 /* Something went wrong. */
338 status
= EXIT_FAILURE
;
340 /* We don't need the input file anymore. */
343 /* We cannot go on with producing output since it might
344 lead to problem because the last output might leave
345 the output stream in an undefined state. */
349 /* Now close the file. */
353 while (++remaining
< argc
);
356 /* Close the output file now. */
358 error (EXIT_FAILURE
, errno
, _("error while closing output file"));
364 /* Handle program arguments. */
366 parse_opt (int key
, char *arg
, struct argp_state
*state
)
380 /* Nothing, for now at least. We are not giving out any information
381 about missing character or so. */
384 /* Omit invalid characters from output. */
394 return ARGP_ERR_UNKNOWN
;
401 more_help (int key
, const char *text
, void *input
)
405 case ARGP_KEY_HELP_EXTRA
:
406 /* We print some extra information. */
407 return strdup (gettext ("\
408 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
412 return (char *) text
;
416 /* Print the version information. */
418 print_version (FILE *stream
, struct argp_state
*state
)
420 fprintf (stream
, "iconv (GNU %s) %s\n", PACKAGE
, VERSION
);
421 fprintf (stream
, gettext ("\
422 Copyright (C) %s Free Software Foundation, Inc.\n\
423 This is free software; see the source for copying conditions. There is NO\n\
424 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
426 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
431 process_block (iconv_t cd
, char *addr
, size_t len
, FILE *output
)
433 #define OUTBUF_SIZE 32768
434 const char *start
= addr
;
435 char outbuf
[OUTBUF_SIZE
];
443 outlen
= OUTBUF_SIZE
;
444 n
= iconv (cd
, &addr
, &len
, &outptr
, &outlen
);
446 if (outptr
!= outbuf
)
448 /* We have something to write out. */
449 int errno_save
= errno
;
451 if (fwrite (outbuf
, 1, outptr
- outbuf
, output
)
452 < (size_t) (outptr
- outbuf
)
455 /* Error occurred while printing the result. */
457 conversion stopped due to problem in writing the output"));
464 if (n
!= (size_t) -1)
466 /* All the input test is processed. For state-dependent
467 character sets we have to flush the state now. */
469 outlen
= OUTBUF_SIZE
;
470 (void) iconv (cd
, NULL
, NULL
, &outptr
, &outlen
);
472 if (outptr
!= outbuf
)
474 /* We have something to write out. */
475 int errno_save
= errno
;
477 if (fwrite (outbuf
, 1, outptr
- outbuf
, output
)
478 < (size_t) (outptr
- outbuf
)
481 /* Error occurred while printing the result. */
483 conversion stopped due to problem in writing the output"));
495 /* iconv() ran into a problem. */
499 error (0, 0, _("illegal input sequence at position %ld"),
500 (long) (addr
- start
));
504 incomplete character or shift sequence at end of buffer"));
507 error (0, 0, _("internal error (illegal descriptor)"));
510 error (0, 0, _("unknown iconv() error %d"), errno
);
523 process_fd (iconv_t cd
, int fd
, FILE *output
)
525 /* we have a problem with reading from a desriptor since we must not
526 provide the iconv() function an incomplete character or shift
527 sequence at the end of the buffer. Since we have to deal with
528 arbitrary encodings we must read the whole text in a buffer and
529 process it in one step. */
530 static char *inbuf
= NULL
;
531 static size_t maxlen
= 0;
535 while (actlen
< maxlen
)
537 ssize_t n
= read (fd
, inptr
, maxlen
- actlen
);
540 /* No more text to read. */
545 /* Error while reading. */
546 error (0, errno
, _("error while reading the input"));
554 if (actlen
== maxlen
)
560 /* Increase the buffer. */
561 new_inbuf
= (char *) realloc (inbuf
, maxlen
+ 32768);
562 if (new_inbuf
== NULL
)
564 error (0, errno
, _("unable to allocate buffer for input"));
569 inptr
= inbuf
+ actlen
;
573 n
= read (fd
, inptr
, maxlen
- actlen
);
576 /* No more text to read. */
581 /* Error while reading. */
582 error (0, errno
, _("error while reading the input"));
589 while (actlen
< maxlen
);
592 /* Break again so we leave both loops. */
596 /* Now we have all the input in the buffer. Process it in one run. */
597 return process_block (cd
, inbuf
, actlen
, output
);
602 process_file (iconv_t cd
, FILE *input
, FILE *output
)
604 /* This should be safe since we use this function only for `stdin' and
605 we haven't read anything so far. */
606 return process_fd (cd
, fileno (input
), output
);
610 /* Print all known character sets/encodings. */
611 static void *printlist
;
612 static size_t column
;
613 static int not_first
;
616 insert_print_list (const void *nodep
, VISIT value
, int level
)
618 if (value
== leaf
|| value
== postorder
)
620 const struct gconv_alias
*s
= *(const struct gconv_alias
**) nodep
;
621 tsearch (s
->fromname
, &printlist
, (__compar_fn_t
) strverscmp
);
626 do_print_human (const void *nodep
, VISIT value
, int level
)
628 if (value
== leaf
|| value
== postorder
)
630 const char *s
= *(const char **) nodep
;
631 size_t len
= strlen (s
);
634 while (len
> 0 && s
[len
- 1] == '/')
637 for (cnt
= 0; cnt
< len
; ++cnt
)
638 if (isalnum (s
[cnt
]))
648 if (column
> 2 && column
+ len
> 77)
650 fputs ("\n ", stdout
);
662 fwrite (s
, len
, 1, stdout
);
668 do_print (const void *nodep
, VISIT value
, int level
)
670 if (value
== leaf
|| value
== postorder
)
672 const char *s
= *(const char **) nodep
;
680 add_known_names (struct gconv_module
*node
)
682 if (node
->left
!= NULL
)
683 add_known_names (node
->left
);
684 if (node
->right
!= NULL
)
685 add_known_names (node
->right
);
688 if (strcmp (node
->from_string
, "INTERNAL"))
689 tsearch (node
->from_string
, &printlist
,
690 (__compar_fn_t
) strverscmp
);
691 if (strcmp (node
->to_string
, "INTERNAL") != 0)
692 tsearch (node
->to_string
, &printlist
, (__compar_fn_t
) strverscmp
);
696 while (node
!= NULL
);
703 const struct gconvcache_header
*header
;
705 const struct hash_entry
*hashtab
;
708 header
= (const struct gconvcache_header
*) __gconv_get_cache ();
709 strtab
= (char *) header
+ header
->string_offset
;
710 hashtab
= (struct hash_entry
*) ((char *) header
+ header
->hash_offset
);
712 for (cnt
= 0; cnt
< header
->hash_size
; ++cnt
)
713 if (hashtab
[cnt
].string_offset
!= 0)
715 const char *str
= strtab
+ hashtab
[cnt
].string_offset
;
717 if (strcmp (str
, "INTERNAL") != 0)
718 tsearch (str
, &printlist
, (__compar_fn_t
) strverscmp
);
725 print_known_names (void)
730 /* We must initialize the internal databases first. */
731 h
= iconv_open ("L1", "L1");
734 /* See whether we have a cache. */
735 cache
= __gconv_get_cache ();
737 /* Yep, use only this information. */
741 struct gconv_module
*modules
;
743 /* No, then use the information read from the gconv-modules file.
744 First add the aliases. */
745 twalk (__gconv_get_alias_db (), insert_print_list
);
747 /* Add the from- and to-names from the known modules. */
748 modules
= __gconv_get_modules_db ();
750 add_known_names (modules
);
754 The following list contain all the coded character sets known. This does\n\
755 not necessarily mean that all combinations of these names can be used for\n\
756 the FROM and TO command line parameters. One coded character set can be\n\
757 listed with several different names (aliases).\n\n "), stdout
);
759 /* Now print the collected names. */
761 if (isatty (fileno (stdout
)))
763 twalk (printlist
, do_print_human
);
769 twalk (printlist
, do_print
);