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 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
36 #ifdef _POSIX_MAPPED_FILES
37 # include <sys/mman.h>
40 #include <gconv_int.h>
41 #include "iconv_prog.h"
42 #include "iconvconfig.h"
44 /* Get libc version number. */
45 #include "../version.h"
47 #define PACKAGE _libc_intl_domainname
50 /* Defined in gconv_cache.c. */
51 extern void *__gconv_cache
;
53 /* Name and version of program. */
54 static void print_version (FILE *stream
, struct argp_state
*state
);
55 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
57 #define OPT_VERBOSE 1000
60 /* Definitions of arguments for argp functions. */
61 static const struct argp_option options
[] =
63 { NULL
, 0, NULL
, 0, N_("Input/Output format specification:") },
64 { "from-code", 'f', "NAME", 0, N_("encoding of original text") },
65 { "to-code", 't', "NAME", 0, N_("encoding for output") },
66 { NULL
, 0, NULL
, 0, N_("Information:") },
67 { "list", 'l', NULL
, 0, N_("list all known coded character sets") },
68 { NULL
, 0, NULL
, 0, N_("Output control:") },
69 { NULL
, 'c', NULL
, 0, N_("omit invalid characters from output") },
70 { "output", 'o', "FILE", 0, N_("output file") },
71 { "silent", 's', NULL
, 0, N_("suppress warnings") },
72 { "verbose", OPT_VERBOSE
, NULL
, 0, N_("print progress information") },
73 { NULL
, 0, NULL
, 0, NULL
}
76 /* Short description of program. */
77 static const char doc
[] = N_("\
78 Convert encoding of given files from one encoding to another.");
80 /* Strings for arguments in help texts. */
81 static const char args_doc
[] = N_("[FILE...]");
83 /* Prototype for option handler. */
84 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
86 /* Function to print some extra text in the help message. */
87 static char *more_help (int key
, const char *text
, void *input
);
89 /* Data structure to communicate with argp functions. */
90 static struct argp argp
=
92 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
95 /* Code sets to convert from and to respectively. */
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 ();
144 if (from_code
== NULL
)
146 /* The Unix standard says that in this case the charset of the current
148 from_code
= nl_langinfo (CODESET
);
149 assert (from_code
!= NULL
);
153 /* The Unix standard says that in this case the charset of the current
155 to_code
= nl_langinfo (CODESET
);
156 assert (to_code
!= NULL
);
159 /* If we have to ignore errors make sure we use the appropriate name for
160 the to-character-set. */
161 orig_to_code
= to_code
;
164 const char *errhand
= strchrnul (to_code
, '/');
172 errhand
= strchrnul (errhand
, '/');
181 newp
= (char *) alloca (errhand
- to_code
+ nslash
+ 6 + 1);
182 cp
= mempcpy (newp
, to_code
, errhand
- to_code
);
185 memcpy (cp
, "IGNORE", sizeof ("IGNORE"));
190 /* POSIX 1003.2b introduces a silly thing: the arguments to -t anf -f
191 can be file names of charmaps. In this case iconv will have to read
192 those charmaps and use them to do the conversion. But there are
193 holes in the specification. There is nothing said that if -f is a
194 charmap filename that -t must be, too. And vice versa. There is
195 also no word about the symbolic names used. What if they don't
197 if (strchr (from_code
, '/') != NULL
)
198 /* The from-name might be a charmap file name. Try reading the
200 from_charmap
= charmap_read (from_code
, /*0, 1*/1, 0, 0);
202 if (strchr (orig_to_code
, '/') != NULL
)
203 /* The to-name might be a charmap file name. Try reading the
205 to_charmap
= charmap_read (orig_to_code
, /*0, 1,*/1,0, 0);
208 /* Determine output file. */
209 if (output_file
!= NULL
&& strcmp (output_file
, "-") != 0)
211 output
= fopen (output_file
, "w");
213 error (EXIT_FAILURE
, errno
, _("cannot open output file"));
218 /* At this point we have to handle two cases. The first one is
219 where a charmap is used for the from- or to-charset, or both. We
220 handle this special since it is very different from the sane way of
221 doing things. The other case allows converting using the iconv()
223 if (from_charmap
!= NULL
|| to_charmap
!= NULL
)
224 /* Construct the conversion table and do the conversion. */
225 status
= charmap_conversion (from_code
, from_charmap
, to_code
, to_charmap
,
226 argc
, remaining
, argv
, output
);
229 /* Let's see whether we have these coded character sets. */
230 cd
= iconv_open (to_code
, from_code
);
231 if (cd
== (iconv_t
) -1)
234 error (EXIT_FAILURE
, 0,
235 _("conversion from `%s' to `%s' not supported"),
236 from_code
, orig_to_code
);
238 error (EXIT_FAILURE
, errno
,
239 _("failed to start conversion processing"));
242 /* Now process the remaining files. Write them to stdout or the file
243 specified with the `-o' parameter. If we have no file given as
244 the parameter process all from stdin. */
245 if (remaining
== argc
)
247 if (process_file (cd
, stdin
, output
) != 0)
248 status
= EXIT_FAILURE
;
253 #ifdef _POSIX_MAPPED_FILES
260 printf ("%s:\n", argv
[remaining
]);
261 if (strcmp (argv
[remaining
], "-") == 0)
265 fd
= open (argv
[remaining
], O_RDONLY
);
269 error (0, errno
, _("cannot open input file `%s'"),
271 status
= EXIT_FAILURE
;
276 #ifdef _POSIX_MAPPED_FILES
277 /* We have possibilities for reading the input file. First try
278 to mmap() it since this will provide the fastest solution. */
279 if (fstat (fd
, &st
) == 0
280 && ((addr
= mmap (NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
,
281 fd
, 0)) != MAP_FAILED
))
283 /* Yes, we can use mmap(). The descriptor is not needed
286 error (EXIT_FAILURE
, errno
,
287 _("error while closing input `%s'"),
290 if (process_block (cd
, addr
, st
.st_size
, output
) < 0)
292 /* Something went wrong. */
293 status
= EXIT_FAILURE
;
295 /* We don't need the input data anymore. */
296 munmap ((void *) addr
, st
.st_size
);
298 /* We cannot go on with producing output since it might
299 lead to problem because the last output might leave
300 the output stream in an undefined state. */
304 /* We don't need the input data anymore. */
305 munmap ((void *) addr
, st
.st_size
);
308 #endif /* _POSIX_MAPPED_FILES */
310 /* Read the file in pieces. */
311 if (process_fd (cd
, fd
, output
) != 0)
313 /* Something went wrong. */
314 status
= EXIT_FAILURE
;
316 /* We don't need the input file anymore. */
319 /* We cannot go on with producing output since it might
320 lead to problem because the last output might leave
321 the output stream in an undefined state. */
325 /* Now close the file. */
329 while (++remaining
< argc
);
332 /* Close the output file now. */
334 error (EXIT_FAILURE
, errno
, _("error while closing output file"));
340 /* Handle program arguments. */
342 parse_opt (int key
, char *arg
, struct argp_state
*state
)
356 /* Nothing, for now at least. We are not giving out any information
357 about missing character or so. */
360 /* Omit invalid characters from output. */
370 return ARGP_ERR_UNKNOWN
;
377 more_help (int key
, const char *text
, void *input
)
381 case ARGP_KEY_HELP_EXTRA
:
382 /* We print some extra information. */
383 return strdup (gettext ("\
384 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
388 return (char *) text
;
392 /* Print the version information. */
394 print_version (FILE *stream
, struct argp_state
*state
)
396 fprintf (stream
, "iconv (GNU %s) %s\n", PACKAGE
, VERSION
);
397 fprintf (stream
, gettext ("\
398 Copyright (C) %s Free Software Foundation, Inc.\n\
399 This is free software; see the source for copying conditions. There is NO\n\
400 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
402 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
407 process_block (iconv_t cd
, char *addr
, size_t len
, FILE *output
)
409 #define OUTBUF_SIZE 32768
410 const char *start
= addr
;
411 char outbuf
[OUTBUF_SIZE
];
419 outlen
= OUTBUF_SIZE
;
420 n
= iconv (cd
, &addr
, &len
, &outptr
, &outlen
);
422 if (outptr
!= outbuf
)
424 /* We have something to write out. */
425 int errno_save
= errno
;
427 if (fwrite (outbuf
, 1, outptr
- outbuf
, output
) < outptr
- outbuf
430 /* Error occurred while printing the result. */
432 conversion stopped due to problem in writing the output"));
439 if (n
!= (size_t) -1)
441 /* All the input test is processed. For state-dependent
442 character sets we have to flush the state now. */
444 outlen
= OUTBUF_SIZE
;
445 (void) iconv (cd
, NULL
, NULL
, &outptr
, &outlen
);
447 if (outptr
!= outbuf
)
449 /* We have something to write out. */
450 int errno_save
= errno
;
452 if (fwrite (outbuf
, 1, outptr
- outbuf
, output
) < outptr
- outbuf
455 /* Error occurred while printing the result. */
457 conversion stopped due to problem in writing the output"));
469 /* iconv() ran into a problem. */
473 error (0, 0, _("illegal input sequence at position %ld"),
474 (long) (addr
- start
));
478 incomplete character or shift sequence at end of buffer"));
481 error (0, 0, _("internal error (illegal descriptor)"));
484 error (0, 0, _("unknown iconv() error %d"), errno
);
497 process_fd (iconv_t cd
, int fd
, FILE *output
)
499 /* we have a problem with reading from a desriptor since we must not
500 provide the iconv() function an incomplete character or shift
501 sequence at the end of the buffer. Since we have to deal with
502 arbitrary encodings we must read the whole text in a buffer and
503 process it in one step. */
504 static char *inbuf
= NULL
;
505 static size_t maxlen
= 0;
509 while (actlen
< maxlen
)
511 ssize_t n
= read (fd
, inptr
, maxlen
- actlen
);
514 /* No more text to read. */
519 /* Error while reading. */
520 error (0, errno
, _("error while reading the input"));
528 if (actlen
== maxlen
)
533 /* Increase the buffer. */
535 inbuf
= realloc (inbuf
, maxlen
);
537 error (0, errno
, _("unable to allocate buffer for input"));
538 inptr
= inbuf
+ actlen
;
542 n
= read (fd
, inptr
, maxlen
- actlen
);
545 /* No more text to read. */
550 /* Error while reading. */
551 error (0, errno
, _("error while reading the input"));
558 while (actlen
< maxlen
);
561 /* Break again so we leave both loops. */
565 /* Now we have all the input in the buffer. Process it in one run. */
566 return process_block (cd
, inbuf
, actlen
, output
);
571 process_file (iconv_t cd
, FILE *input
, FILE *output
)
573 /* This should be safe since we use this function only for `stdin' and
574 we haven't read anything so far. */
575 return process_fd (cd
, fileno (input
), output
);
579 /* Print all known character sets/encodings. */
580 static void *printlist
;
581 static size_t column
;
582 static int not_first
;
585 insert_print_list (const void *nodep
, VISIT value
, int level
)
587 if (value
== leaf
|| value
== postorder
)
589 const struct gconv_alias
*s
= *(const struct gconv_alias
**) nodep
;
590 tsearch (s
->fromname
, &printlist
, (__compar_fn_t
) strverscmp
);
595 do_print_human (const void *nodep
, VISIT value
, int level
)
597 if (value
== leaf
|| value
== postorder
)
599 const char *s
= *(const char **) nodep
;
600 size_t len
= strlen (s
);
603 while (len
> 0 && s
[len
- 1] == '/')
606 for (cnt
= 0; cnt
< len
; ++cnt
)
607 if (isalnum (s
[cnt
]))
617 if (column
> 2 && column
+ len
> 77)
619 fputs ("\n ", stdout
);
631 fwrite (s
, len
, 1, stdout
);
637 do_print (const void *nodep
, VISIT value
, int level
)
639 if (value
== leaf
|| value
== postorder
)
641 const char *s
= *(const char **) nodep
;
649 add_known_names (struct gconv_module
*node
)
651 if (node
->left
!= NULL
)
652 add_known_names (node
->left
);
653 if (node
->right
!= NULL
)
654 add_known_names (node
->right
);
657 if (strcmp (node
->from_string
, "INTERNAL"))
658 tsearch (node
->from_string
, &printlist
,
659 (__compar_fn_t
) strverscmp
);
660 if (strcmp (node
->to_string
, "INTERNAL") != 0)
661 tsearch (node
->to_string
, &printlist
, (__compar_fn_t
) strverscmp
);
665 while (node
!= NULL
);
672 const struct gconvcache_header
*header
;
674 const struct hash_entry
*hashtab
;
677 header
= (const struct gconvcache_header
*) __gconv_cache
;
678 strtab
= (char *) __gconv_cache
+ header
->string_offset
;
679 hashtab
= (struct hash_entry
*) ((char *) __gconv_cache
680 + header
->hash_offset
);
682 for (cnt
= 0; cnt
< header
->hash_size
; ++cnt
)
683 if (hashtab
[cnt
].string_offset
!= 0)
685 const char *str
= strtab
+ hashtab
[cnt
].string_offset
;
687 if (strcmp (str
, "INTERNAL") != 0)
688 tsearch (str
, &printlist
, (__compar_fn_t
) strverscmp
);
695 print_known_names (void)
699 /* We must initialize the internal databases first. */
700 h
= iconv_open ("L1", "L1");
703 /* See whether we have a cache. */
704 if (__gconv_cache
!= NULL
)
705 /* Yep, use only this information. */
709 /* No, then use the information read from the gconv-modules file.
710 First add the aliases. */
711 twalk (__gconv_alias_db
, insert_print_list
);
713 /* Add the from- and to-names from the known modules. */
714 if (__gconv_modules_db
!= NULL
)
715 add_known_names (__gconv_modules_db
);
719 The following list contain all the coded character sets known. This does\n\
720 not necessarily mean that all combinations of these names can be used for\n\
721 the FROM and TO command line parameters. One coded character set can be\n\
722 listed with several different names (aliases).\n\n "), stdout
);
724 /* Now print the collected names. */
726 if (isatty (fileno (stdout
)))
728 twalk (printlist
, do_print_human
);
734 twalk (printlist
, do_print
);