1 /* Convert text in given files from the specified from-set to the to-set.
2 Copyright (C) 1998-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 This program is free software; you can redistribute it and/or modify
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.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <https://www.gnu.org/licenses/>. */
34 #ifdef _POSIX_MAPPED_FILES
35 # include <sys/mman.h>
38 #include <gconv_int.h>
39 #include "iconv_prog.h"
40 #include "iconvconfig.h"
41 #include "gconv_charset.h"
43 /* Get libc version number. */
44 #include "../version.h"
46 #define PACKAGE _libc_intl_domainname
49 /* Name and version of program. */
50 static void print_version (FILE *stream
, struct argp_state
*state
);
51 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
53 #define OPT_VERBOSE 1000
56 /* Definitions of arguments for argp functions. */
57 static const struct argp_option options
[] =
59 { NULL
, 0, NULL
, 0, N_("Input/Output format specification:") },
60 { "from-code", 'f', N_("NAME"), 0, N_("encoding of original text") },
61 { "to-code", 't', N_("NAME"), 0, N_("encoding for output") },
62 { NULL
, 0, NULL
, 0, N_("Information:") },
63 { "list", 'l', NULL
, 0, N_("list all known coded character sets") },
64 { NULL
, 0, NULL
, 0, N_("Output control:") },
65 { NULL
, 'c', NULL
, 0, N_("omit invalid characters from output") },
66 { "output", 'o', N_("FILE"), 0, N_("output file") },
67 { "silent", 's', NULL
, 0, N_("suppress warnings") },
68 { "verbose", OPT_VERBOSE
, NULL
, 0, N_("print progress information") },
69 { NULL
, 0, NULL
, 0, NULL
}
72 /* Short description of program. */
73 static const char doc
[] = N_("\
74 Convert encoding of given files from one encoding to another.");
76 /* Strings for arguments in help texts. */
77 static const char args_doc
[] = N_("[FILE...]");
79 /* Prototype for option handler. */
80 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
82 /* Function to print some extra text in the help message. */
83 static char *more_help (int key
, const char *text
, void *input
);
85 /* Data structure to communicate with argp functions. */
86 static struct argp argp
=
88 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
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. */
94 static const char *from_code
= "";
95 static const char *to_code
= "";
97 /* File to write output to. If NULL write to stdout. */
98 static const char *output_file
;
100 /* Nonzero if list of all coded character sets is wanted. */
103 /* If nonzero omit invalid character from output. */
106 /* Prototypes for the functions doing the actual work. */
107 static int process_block (iconv_t cd
, char *addr
, size_t len
, FILE **output
,
108 const char *output_file
);
109 static int process_fd (iconv_t cd
, int fd
, FILE **output
,
110 const char *output_file
);
111 static int process_file (iconv_t cd
, FILE *input
, FILE **output
,
112 const char *output_file
);
113 static void print_known_names (void);
117 main (int argc
, char *argv
[])
119 int status
= EXIT_SUCCESS
;
122 struct charmap_t
*from_charmap
= NULL
;
123 struct charmap_t
*to_charmap
= NULL
;
125 /* Set locale via LC_ALL. */
126 setlocale (LC_ALL
, "");
128 /* Set the text message domain. */
129 textdomain (_libc_intl_domainname
);
131 /* Parse and process arguments. */
132 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
134 /* List all coded character sets if wanted. */
137 print_known_names ();
141 /* POSIX 1003.2b introduces a silly thing: the arguments to -t anf -f
142 can be file names of charmaps. In this case iconv will have to read
143 those charmaps and use them to do the conversion. But there are
144 holes in the specification. There is nothing said that if -f is a
145 charmap filename that -t must be, too. And vice versa. There is
146 also no word about the symbolic names used. What if they don't
148 if (strchr (from_code
, '/') != NULL
)
149 /* The from-name might be a charmap file name. Try reading the
151 from_charmap
= charmap_read (from_code
, /*0, 1*/1, 0, 0, 0);
153 if (strchr (to_code
, '/') != NULL
)
154 /* The to-name might be a charmap file name. Try reading the
156 to_charmap
= charmap_read (to_code
, /*0, 1,*/1, 0, 0, 0);
159 /* At this point we have to handle two cases. The first one is
160 where a charmap is used for the from- or to-charset, or both. We
161 handle this special since it is very different from the sane way of
162 doing things. The other case allows converting using the iconv()
164 if (from_charmap
!= NULL
|| to_charmap
!= NULL
)
165 /* Construct the conversion table and do the conversion. */
166 status
= charmap_conversion (from_code
, from_charmap
, to_code
, to_charmap
,
167 argc
, remaining
, argv
, output_file
);
170 struct gconv_spec conv_spec
;
173 if (__gconv_create_spec (&conv_spec
, from_code
, to_code
) == NULL
)
175 error (EXIT_FAILURE
, errno
,
176 _("failed to start conversion processing"));
181 conv_spec
.ignore
= true;
183 /* Let's see whether we have these coded character sets. */
184 res
= __gconv_open (&conv_spec
, &cd
, 0);
186 __gconv_destroy_spec (&conv_spec
);
188 if (res
!= __GCONV_OK
)
190 if (res
== __GCONV_NOCONV
|| res
== __GCONV_NODB
)
192 /* Try to be nice with the user and tell her which of the
193 two encoding names is wrong. This is possible because
194 all supported encodings can be converted from/to Unicode,
195 in other words, because the graph of encodings is
198 (iconv_open ("UTF-8", from_code
) == (iconv_t
) -1
201 (iconv_open (to_code
, "UTF-8") == (iconv_t
) -1
203 const char *from_pretty
=
204 (from_code
[0] ? from_code
: nl_langinfo (CODESET
));
205 const char *to_pretty
=
206 (to_code
[0] ? to_code
: nl_langinfo (CODESET
));
213 conversions from `%s' and to `%s' are not supported"),
214 from_pretty
, to_pretty
);
217 _("conversion from `%s' is not supported"),
224 _("conversion to `%s' is not supported"),
228 _("conversion from `%s' to `%s' is not supported"),
229 from_pretty
, to_pretty
);
232 argp_help (&argp
, stderr
, ARGP_HELP_SEE
,
233 program_invocation_short_name
);
237 error (EXIT_FAILURE
, errno
,
238 _("failed to start conversion processing"));
241 /* The output file. Will be opened when we are ready to produce
245 /* Now process the remaining files. Write them to stdout or the file
246 specified with the `-o' parameter. If we have no file given as
247 the parameter process all from stdin. */
248 if (remaining
== argc
)
250 if (process_file (cd
, stdin
, &output
, output_file
) != 0)
251 status
= EXIT_FAILURE
;
256 #ifdef _POSIX_MAPPED_FILES
263 fprintf (stderr
, "%s:\n", argv
[remaining
]);
264 if (strcmp (argv
[remaining
], "-") == 0)
268 fd
= open (argv
[remaining
], O_RDONLY
);
272 error (0, errno
, _("cannot open input file `%s'"),
274 status
= EXIT_FAILURE
;
279 #ifdef _POSIX_MAPPED_FILES
280 /* We have possibilities for reading the input file. First try
281 to mmap() it since this will provide the fastest solution. */
282 if (fstat64 (fd
, &st
) == 0
283 && ((addr
= mmap (NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
,
284 fd
, 0)) != MAP_FAILED
))
286 /* Yes, we can use mmap(). The descriptor is not needed
289 error (EXIT_FAILURE
, errno
,
290 _("error while closing input `%s'"),
293 ret
= process_block (cd
, addr
, st
.st_size
, &output
,
296 /* We don't need the input data anymore. */
297 munmap ((void *) addr
, st
.st_size
);
301 status
= EXIT_FAILURE
;
304 /* We cannot go on with producing output since it might
305 lead to problem because the last output might leave
306 the output stream in an undefined state. */
311 #endif /* _POSIX_MAPPED_FILES */
313 /* Read the file in pieces. */
314 ret
= process_fd (cd
, fd
, &output
, output_file
);
316 /* Now close the file. */
321 /* Something went wrong. */
322 status
= EXIT_FAILURE
;
325 /* We cannot go on with producing output since it might
326 lead to problem because the last output might leave
327 the output stream in an undefined state. */
332 while (++remaining
< argc
);
334 /* Close the output file now. */
335 if (output
!= NULL
&& fclose (output
))
336 error (EXIT_FAILURE
, errno
, _("error while closing output file"));
343 /* Handle program arguments. */
345 parse_opt (int key
, char *arg
, struct argp_state
*state
)
359 /* Nothing, for now at least. We are not giving out any information
360 about missing character or so. */
363 /* Omit invalid characters from output. */
373 return ARGP_ERR_UNKNOWN
;
380 more_help (int key
, const char *text
, void *input
)
385 case ARGP_KEY_HELP_EXTRA
:
386 /* We print some extra information. */
387 if (asprintf (&tp
, gettext ("\
388 For bug reporting instructions, please see:\n\
389 %s.\n"), REPORT_BUGS_TO
) < 0)
395 return (char *) text
;
399 /* Print the version information. */
401 print_version (FILE *stream
, struct argp_state
*state
)
403 fprintf (stream
, "iconv %s%s\n", PKGVERSION
, VERSION
);
404 fprintf (stream
, gettext ("\
405 Copyright (C) %s Free Software Foundation, Inc.\n\
406 This is free software; see the source for copying conditions. There is NO\n\
407 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
409 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
414 write_output (const char *outbuf
, const char *outptr
, FILE **output
,
415 const char *output_file
)
417 /* We have something to write out. */
418 int errno_save
= errno
;
422 /* Determine output file. */
423 if (output_file
!= NULL
&& strcmp (output_file
, "-") != 0)
425 *output
= fopen (output_file
, "w");
427 error (EXIT_FAILURE
, errno
, _("cannot open output file"));
433 if (fwrite (outbuf
, 1, outptr
- outbuf
, *output
) < (size_t) (outptr
- outbuf
)
436 /* Error occurred while printing the result. */
438 conversion stopped due to problem in writing the output"));
449 process_block (iconv_t cd
, char *addr
, size_t len
, FILE **output
,
450 const char *output_file
)
452 #define OUTBUF_SIZE 32768
453 const char *start
= addr
;
454 char outbuf
[OUTBUF_SIZE
];
463 outlen
= OUTBUF_SIZE
;
464 n
= iconv (cd
, &addr
, &len
, &outptr
, &outlen
);
466 if (n
== (size_t) -1 && omit_invalid
&& errno
== EILSEQ
)
475 if (outptr
!= outbuf
)
477 ret
= write_output (outbuf
, outptr
, output
, output_file
);
482 if (n
!= (size_t) -1)
484 /* All the input test is processed. For state-dependent
485 character sets we have to flush the state now. */
487 outlen
= OUTBUF_SIZE
;
488 n
= iconv (cd
, NULL
, NULL
, &outptr
, &outlen
);
490 if (outptr
!= outbuf
)
492 ret
= write_output (outbuf
, outptr
, output
, output_file
);
497 if (n
!= (size_t) -1)
500 if (omit_invalid
&& errno
== EILSEQ
)
509 /* iconv() ran into a problem. */
514 error (0, 0, _("illegal input sequence at position %ld"),
515 (long int) (addr
- start
));
519 incomplete character or shift sequence at end of buffer"));
522 error (0, 0, _("internal error (illegal descriptor)"));
525 error (0, 0, _("unknown iconv() error %d"), errno
);
538 process_fd (iconv_t cd
, int fd
, FILE **output
, const char *output_file
)
540 /* we have a problem with reading from a descriptor since we must not
541 provide the iconv() function an incomplete character or shift
542 sequence at the end of the buffer. Since we have to deal with
543 arbitrary encodings we must read the whole text in a buffer and
544 process it in one step. */
545 static char *inbuf
= NULL
;
546 static size_t maxlen
= 0;
550 while (actlen
< maxlen
)
552 ssize_t n
= read (fd
, inptr
, maxlen
- actlen
);
555 /* No more text to read. */
560 /* Error while reading. */
561 error (0, errno
, _("error while reading the input"));
569 if (actlen
== maxlen
)
575 /* Increase the buffer. */
576 new_inbuf
= (char *) realloc (inbuf
, maxlen
+ 32768);
577 if (new_inbuf
== NULL
)
579 error (0, errno
, _("unable to allocate buffer for input"));
584 inptr
= inbuf
+ actlen
;
588 n
= read (fd
, inptr
, maxlen
- actlen
);
591 /* No more text to read. */
596 /* Error while reading. */
597 error (0, errno
, _("error while reading the input"));
604 while (actlen
< maxlen
);
607 /* Break again so we leave both loops. */
611 /* Now we have all the input in the buffer. Process it in one run. */
612 return process_block (cd
, inbuf
, actlen
, output
, output_file
);
617 process_file (iconv_t cd
, FILE *input
, FILE **output
, const char *output_file
)
619 /* This should be safe since we use this function only for `stdin' and
620 we haven't read anything so far. */
621 return process_fd (cd
, fileno (input
), output
, output_file
);
625 /* Print all known character sets/encodings. */
626 static void *printlist
;
627 static size_t column
;
628 static int not_first
;
631 insert_print_list (const void *nodep
, VISIT value
, int level
)
633 if (value
== leaf
|| value
== postorder
)
635 const struct gconv_alias
*s
= *(const struct gconv_alias
**) nodep
;
636 tsearch (s
->fromname
, &printlist
, (__compar_fn_t
) strverscmp
);
641 do_print_human (const void *nodep
, VISIT value
, int level
)
643 if (value
== leaf
|| value
== postorder
)
645 const char *s
= *(const char **) nodep
;
646 size_t len
= strlen (s
);
649 while (len
> 0 && s
[len
- 1] == '/')
652 for (cnt
= 0; cnt
< len
; ++cnt
)
653 if (isalnum (s
[cnt
]))
663 if (column
> 2 && column
+ len
> 77)
665 fputs ("\n ", stdout
);
677 fwrite (s
, len
, 1, stdout
);
683 do_print (const void *nodep
, VISIT value
, int level
)
685 if (value
== leaf
|| value
== postorder
)
687 const char *s
= *(const char **) nodep
;
694 add_known_names (struct gconv_module
*node
)
696 if (node
->left
!= NULL
)
697 add_known_names (node
->left
);
698 if (node
->right
!= NULL
)
699 add_known_names (node
->right
);
702 if (strcmp (node
->from_string
, "INTERNAL") != 0)
703 tsearch (node
->from_string
, &printlist
, (__compar_fn_t
) strverscmp
);
704 if (strcmp (node
->to_string
, "INTERNAL") != 0)
705 tsearch (node
->to_string
, &printlist
, (__compar_fn_t
) strverscmp
);
709 while (node
!= NULL
);
716 const struct gconvcache_header
*header
;
718 const struct hash_entry
*hashtab
;
721 header
= (const struct gconvcache_header
*) __gconv_get_cache ();
722 strtab
= (char *) header
+ header
->string_offset
;
723 hashtab
= (struct hash_entry
*) ((char *) header
+ header
->hash_offset
);
725 for (cnt
= 0; cnt
< header
->hash_size
; ++cnt
)
726 if (hashtab
[cnt
].string_offset
!= 0)
728 const char *str
= strtab
+ hashtab
[cnt
].string_offset
;
730 if (strcmp (str
, "INTERNAL") != 0)
731 tsearch (str
, &printlist
, (__compar_fn_t
) strverscmp
);
737 print_known_names (void)
742 /* We must initialize the internal databases first. */
743 h
= iconv_open ("L1", "L1");
746 /* See whether we have a cache. */
747 cache
= __gconv_get_cache ();
749 /* Yep, use only this information. */
753 struct gconv_module
*modules
;
755 /* No, then use the information read from the gconv-modules file.
756 First add the aliases. */
757 twalk (__gconv_get_alias_db (), insert_print_list
);
759 /* Add the from- and to-names from the known modules. */
760 modules
= __gconv_get_modules_db ();
762 add_known_names (modules
);
765 bool human_readable
= isatty (fileno (stdout
));
769 The following list contains all the coded character sets known. This does\n\
770 not necessarily mean that all combinations of these names can be used for\n\
771 the FROM and TO command line parameters. One coded character set can be\n\
772 listed with several different names (aliases).\n\n "), stdout
);
774 /* Now print the collected names. */
776 twalk (printlist
, human_readable
? do_print_human
: do_print
);
778 if (human_readable
&& column
!= 0)