1 /* Convert text in given files from the specified from-set to the to-set.
2 Copyright (C) 1998, 1999, 2000, 2001 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
36 #ifdef _POSIX_MAPPED_FILES
37 # include <sys/mman.h>
40 #include <gconv_int.h>
41 #include "iconv_prog.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', "NAME", 0, N_("encoding of original text") },
61 { "to-code", 't', "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', "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. */
92 static const char *from_code
;
93 static const char *to_code
;
95 /* File to write output to. If NULL write to stdout. */
96 static const char *output_file
;
98 /* Nonzero if verbose ouput is wanted. */
101 /* Nonzero if list of all coded character sets is wanted. */
104 /* If nonzero omit invalid character from output. */
107 /* Prototypes for the functions doing the actual work. */
108 static int process_block (iconv_t cd
, char *addr
, size_t len
, FILE *output
);
109 static int process_fd (iconv_t cd
, int fd
, FILE *output
);
110 static int process_file (iconv_t cd
, FILE *input
, FILE *output
);
111 static void print_known_names (void) internal_function
;
115 main (int argc
, char *argv
[])
117 int status
= EXIT_SUCCESS
;
121 const char *orig_to_code
;
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 ();
140 if (from_code
== NULL
)
142 /* The Unix standard says that in this case the charset of the current
144 from_code
= nl_langinfo (CODESET
);
145 assert (from_code
!= NULL
);
149 /* The Unix standard says that in this case the charset of the current
151 to_code
= nl_langinfo (CODESET
);
152 assert (to_code
!= NULL
);
155 /* If we have to ignore errors make sure we use the appropriate name for
156 the to-character-set. */
157 orig_to_code
= to_code
;
160 const char *errhand
= strchrnul (to_code
, '/');
168 errhand
= strchrnul (errhand
, '/');
177 newp
= (char *) alloca (errhand
- to_code
+ nslash
+ 6 + 1);
178 cp
= mempcpy (newp
, to_code
, errhand
- to_code
);
181 memcpy (cp
, "IGNORE", sizeof ("IGNORE"));
186 /* POSIX 1003.2b introduces a silly thing: the arguments to -t anf -f
187 can be file names of charmaps. In this case iconv will have to read
188 those charmaps and use them to do the conversion. But there are
189 holes in the specification. There is nothing said that if -f is a
190 charmap filename that -t must be, too. And vice versa. There is
191 also no word about the symbolic names used. What if they don't
193 if (strchr (from_code
, '/') != NULL
)
194 /* The from-name might be a charmap file name. Try reading the
196 from_charmap
= charmap_read (from_code
, /*0, 1*/1, 0, 0);
198 if (strchr (orig_to_code
, '/') != NULL
)
199 /* The to-name might be a charmap file name. Try reading the
201 to_charmap
= charmap_read (orig_to_code
, /*0, 1,*/1,0, 0);
204 /* Determine output file. */
205 if (output_file
!= NULL
&& strcmp (output_file
, "-") != 0)
207 output
= fopen (output_file
, "w");
209 error (EXIT_FAILURE
, errno
, _("cannot open output file"));
214 /* At this point we have to handle two cases. The first one is
215 where a charmap is used for the from- or to-charset, or both. We
216 handle this special since it is very different from the sane way of
217 doing things. The other case allows converting using the iconv()
219 if (from_charmap
!= NULL
|| to_charmap
!= NULL
)
220 /* Construct the conversion table and do the conversion. */
221 status
= charmap_conversion (from_code
, from_charmap
, to_code
, to_charmap
,
222 argc
, remaining
, argv
, output
);
225 /* Let's see whether we have these coded character sets. */
226 cd
= iconv_open (to_code
, from_code
);
227 if (cd
== (iconv_t
) -1)
230 error (EXIT_FAILURE
, 0,
231 _("conversion from `%s' to `%s' not supported"),
232 from_code
, orig_to_code
);
234 error (EXIT_FAILURE
, errno
,
235 _("failed to start conversion processing"));
238 /* Now process the remaining files. Write them to stdout or the file
239 specified with the `-o' parameter. If we have no file given as
240 the parameter process all from stdin. */
241 if (remaining
== argc
)
243 if (process_file (cd
, stdin
, output
) != 0)
244 status
= EXIT_FAILURE
;
254 printf ("%s:\n", argv
[remaining
]);
255 if (strcmp (argv
[remaining
], "-") == 0)
259 fd
= open (argv
[remaining
], O_RDONLY
);
263 error (0, errno
, _("cannot open input file `%s'"),
265 status
= EXIT_FAILURE
;
270 #ifdef _POSIX_MAPPED_FILES
271 /* We have possibilities for reading the input file. First try
272 to mmap() it since this will provide the fastest solution. */
273 if (fstat (fd
, &st
) == 0
274 && ((addr
= mmap (NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
,
275 fd
, 0)) != MAP_FAILED
))
277 /* Yes, we can use mmap(). The descriptor is not needed
280 error (EXIT_FAILURE
, errno
,
281 _("error while closing input `%s'"),
284 if (process_block (cd
, addr
, st
.st_size
, output
) < 0)
286 /* Something went wrong. */
287 status
= EXIT_FAILURE
;
289 /* We don't need the input data anymore. */
290 munmap ((void *) addr
, st
.st_size
);
292 /* We cannot go on with producing output since it might
293 lead to problem because the last output might leave
294 the output stream in an undefined state. */
298 /* We don't need the input data anymore. */
299 munmap ((void *) addr
, st
.st_size
);
302 #endif /* _POSIX_MAPPED_FILES */
304 /* Read the file in pieces. */
305 if (process_fd (cd
, fd
, output
) != 0)
307 /* Something went wrong. */
308 status
= EXIT_FAILURE
;
310 /* We don't need the input file anymore. */
313 /* We cannot go on with producing output since it might
314 lead to problem because the last output might leave
315 the output stream in an undefined state. */
319 /* Now close the file. */
323 while (++remaining
< argc
);
326 /* Close the output file now. */
328 error (EXIT_FAILURE
, errno
, _("error while closing output file"));
334 /* Handle program arguments. */
336 parse_opt (int key
, char *arg
, struct argp_state
*state
)
350 /* Nothing, for now at least. We are not giving out any information
351 about missing character or so. */
354 /* Omit invalid characters from output. */
364 return ARGP_ERR_UNKNOWN
;
371 more_help (int key
, const char *text
, void *input
)
375 case ARGP_KEY_HELP_EXTRA
:
376 /* We print some extra information. */
377 return strdup (gettext ("\
378 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
382 return (char *) text
;
386 /* Print the version information. */
388 print_version (FILE *stream
, struct argp_state
*state
)
390 fprintf (stream
, "iconv (GNU %s) %s\n", PACKAGE
, VERSION
);
391 fprintf (stream
, gettext ("\
392 Copyright (C) %s Free Software Foundation, Inc.\n\
393 This is free software; see the source for copying conditions. There is NO\n\
394 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
396 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
401 process_block (iconv_t cd
, char *addr
, size_t len
, FILE *output
)
403 #define OUTBUF_SIZE 32768
404 const char *start
= addr
;
405 char outbuf
[OUTBUF_SIZE
];
413 outlen
= OUTBUF_SIZE
;
414 n
= iconv (cd
, &addr
, &len
, &outptr
, &outlen
);
416 if (outptr
!= outbuf
)
418 /* We have something to write out. */
419 int errno_save
= errno
;
421 if (fwrite (outbuf
, 1, outptr
- outbuf
, output
) < outptr
- outbuf
424 /* Error occurred while printing the result. */
426 conversion stopped due to problem in writing the output"));
433 if (n
!= (size_t) -1)
435 /* All the input test is processed. For state-dependent
436 character sets we have to flush the state now. */
438 outlen
= OUTBUF_SIZE
;
439 (void) iconv (cd
, NULL
, NULL
, &outptr
, &outlen
);
441 if (outptr
!= outbuf
)
443 /* We have something to write out. */
444 int errno_save
= errno
;
446 if (fwrite (outbuf
, 1, outptr
- outbuf
, output
) < outptr
- outbuf
449 /* Error occurred while printing the result. */
451 conversion stopped due to problem in writing the output"));
463 /* iconv() ran into a problem. */
467 error (0, 0, _("illegal input sequence at position %ld"),
468 (long) (addr
- start
));
472 incomplete character or shift sequence at end of buffer"));
475 error (0, 0, _("internal error (illegal descriptor)"));
478 error (0, 0, _("unknown iconv() error %d"), errno
);
491 process_fd (iconv_t cd
, int fd
, FILE *output
)
493 /* we have a problem with reading from a desriptor since we must not
494 provide the iconv() function an incomplete character or shift
495 sequence at the end of the buffer. Since we have to deal with
496 arbitrary encodings we must read the whole text in a buffer and
497 process it in one step. */
498 static char *inbuf
= NULL
;
499 static size_t maxlen
= 0;
503 while (actlen
< maxlen
)
505 ssize_t n
= read (fd
, inptr
, maxlen
- actlen
);
508 /* No more text to read. */
513 /* Error while reading. */
514 error (0, errno
, _("error while reading the input"));
522 if (actlen
== maxlen
)
527 /* Increase the buffer. */
529 inbuf
= realloc (inbuf
, maxlen
);
531 error (0, errno
, _("unable to allocate buffer for input"));
532 inptr
= inbuf
+ actlen
;
536 n
= read (fd
, inptr
, maxlen
- actlen
);
539 /* No more text to read. */
544 /* Error while reading. */
545 error (0, errno
, _("error while reading the input"));
552 while (actlen
< maxlen
);
555 /* Break again so we leave both loops. */
559 /* Now we have all the input in the buffer. Process it in one run. */
560 return process_block (cd
, inbuf
, actlen
, output
);
565 process_file (iconv_t cd
, FILE *input
, FILE *output
)
567 /* This should be safe since we use this function only for `stdin' and
568 we haven't read anything so far. */
569 return process_fd (cd
, fileno (input
), output
);
573 /* Print all known character sets/encodings. */
574 static void *printlist
;
575 static size_t column
;
576 static int not_first
;
579 insert_print_list (const void *nodep
, VISIT value
, int level
)
581 if (value
== leaf
|| value
== postorder
)
583 const struct gconv_alias
*s
= *(const struct gconv_alias
**) nodep
;
584 tsearch (s
->fromname
, &printlist
, (__compar_fn_t
) strverscmp
);
589 do_print (const void *nodep
, VISIT value
, int level
)
591 if (value
== leaf
|| value
== postorder
)
593 const char *s
= *(const char **) nodep
;
594 size_t len
= strlen (s
);
597 while (len
> 0 && s
[len
- 1] == '/')
600 for (cnt
= 0; cnt
< len
; ++cnt
)
601 if (isalnum (s
[cnt
]))
611 if (column
> 2 && column
+ len
> 77)
613 fputs ("\n ", stdout
);
625 fwrite (s
, len
, 1, stdout
);
632 add_known_names (struct gconv_module
*node
)
634 if (node
->left
!= NULL
)
635 add_known_names (node
->left
);
636 if (node
->right
!= NULL
)
637 add_known_names (node
->right
);
640 if (strcmp (node
->from_string
, "INTERNAL"))
641 tsearch (node
->from_string
, &printlist
,
642 (__compar_fn_t
) strverscmp
);
643 if (strcmp (node
->to_string
, "INTERNAL"))
644 tsearch (node
->to_string
, &printlist
, (__compar_fn_t
) strverscmp
);
648 while (node
!= NULL
);
653 print_known_names (void)
657 /* We must initialize the internal databases first. */
658 h
= iconv_open ("L1", "L1");
661 /* First add the aliases. */
662 twalk (__gconv_alias_db
, insert_print_list
);
664 /* Add the from- and to-names from the known modules. */
665 add_known_names (__gconv_modules_db
);
668 The following list contain all the coded character sets known. This does\n\
669 not necessarily mean that all combinations of these names can be used for\n\
670 the FROM and TO command line parameters. One coded character set can be\n\
671 listed with several different names (aliases).\n\n "), stdout
);
673 /* Now print the collected names. */
675 twalk (printlist
, do_print
);