1 /* Convert text in given files from the specified from-set to the to-set.
2 Copyright (C) 1998 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 /* Get libc version number. */
37 #include "../version.h"
39 #define PACKAGE _libc_intl_domainname
42 /* Name and version of program. */
43 static void print_version (FILE *stream, struct argp_state *state);
44 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
46 #define OPT_VERBOSE 1000
49 /* Definitions of arguments for argp functions. */
50 static const struct argp_option options[] =
52 { NULL, 0, NULL, 0, N_("Input/Output format specification:") },
53 { "from-code", 'f', "NAME", 0, N_("encoding of original text") },
54 { "to-code", 't', "NAME", 0, N_("encoding for output") },
55 { NULL, 0, NULL, 0, N_("Information:") },
56 { "list", OPT_LIST, NULL, 0, N_("list all known coded character sets") },
57 { NULL, 0, NULL, 0, N_("Output control:") },
58 { "output", 'o', "FILE", 0, N_("output file") },
59 { "verbose", OPT_VERBOSE, NULL, 0, N_("print progress information") },
60 { NULL, 0, NULL, 0, NULL }
63 /* Short description of program. */
64 static const char doc[] = N_("\
65 Convert encoding of given files from one encoding to another.");
67 /* Strings for arguments in help texts. */
68 static const char args_doc[] = N_("[FILE...]");
70 /* Prototype for option handler. */
71 static error_t parse_opt __P ((int key, char *arg, struct argp_state *state));
73 /* Function to print some extra text in the help message. */
74 static char *more_help __P ((int key, const char *text, void *input));
76 /* Data structure to communicate with argp functions. */
77 static struct argp argp =
79 options, parse_opt, args_doc, doc, NULL, more_help
82 /* Code sets to convert from and to respectively. */
83 static const char *from_code;
84 static const char *to_code;
86 /* File to write output to. If NULL write to stdout. */
87 static const char *output_file;
89 /* Nonzero if verbose ouput is wanted. */
92 /* Nonzero if list of all coded character sets is wanted. */
95 /* Prototypes for the functions doing the actual work. */
96 static int process_block (iconv_t cd, const char *addr, size_t len,
98 static int process_fd (iconv_t cd, int fd, FILE *output);
99 static int process_file (iconv_t cd, FILE *input, FILE *output);
100 static void print_known_names (void);
104 main (int argc, char *argv[])
106 int status = EXIT_SUCCESS;
111 /* Set locale via LC_ALL. */
112 setlocale (LC_ALL, "");
114 /* Set the text message domain. */
115 textdomain (_libc_intl_domainname);
117 /* Parse and process arguments. */
118 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
120 /* List all coded character sets if wanted. */
123 print_known_names ();
127 /* If either the from- or to-code is not specified this is an error
128 since we do not know what to do. */
129 if (from_code == NULL && to_code == NULL)
130 error (EXIT_FAILURE, 0,
131 _("neither original nor target encoding specified"));
132 if (from_code == NULL)
133 error (EXIT_FAILURE, 0, _("original encoding not specified using `-f'"));
135 error (EXIT_FAILURE, 0, _("target encoding not specified using `-t'"));
137 /* Let's see whether we have these coded character sets. */
138 cd = iconv_open (to_code, from_code);
139 if (cd == (iconv_t) -1)
141 error (EXIT_FAILURE, 0, _("conversion from `%s' to `%s' not supported"),
144 error (EXIT_FAILURE, errno, _("failed to start conversion processing"));
146 /* Determine output file. */
147 if (output_file != NULL)
149 output = fopen (output_file, "w");
151 error (EXIT_FAILURE, errno, _("cannot open output file"));
156 /* Now process the remaining files. Write them to stdout or the file
157 specified with the `-o' parameter. If we have no file given as
158 the parameter process all from stdin. */
159 if (remaining == argc)
160 process_file (cd, stdin, output);
166 int fd = open (argv[remaining], O_RDONLY);
169 printf ("%s:\n", argv[remaining]);
173 error (0, errno, _("cannot open input file `%s'"),
175 status = EXIT_FAILURE;
179 /* We have possibilities for reading the input file. First try
180 to mmap() it since this will provide the fastest solution. */
181 if (fstat (fd, &st) == 0
182 && ((addr = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0))
185 /* Yes, we can use mmap(). The descriptor is not needed
188 error (EXIT_FAILURE, errno, _("error while closing input `%s'"),
191 if (process_block (cd, addr, st.st_size, stdout) < 0)
193 /* Something went wrong. */
194 status = EXIT_FAILURE;
196 /* We don't need the input data anymore. */
197 munmap ((void *) addr, st.st_size);
199 /* We cannot go on with producing output since it might
200 lead to problem because the last output might leave
201 the output stream in an undefined state. */
205 /* We don't need the input data anymore. */
206 munmap ((void *) addr, st.st_size);
210 /* Read the file in pieces. */
211 if (process_fd (cd, fd, output) != 0)
213 /* Something went wrong. */
214 status = EXIT_FAILURE;
216 /* We don't need the input file anymore. */
219 /* We cannot go on with producing output since it might
220 lead to problem because the last output might leave
221 the output stream in an undefined state. */
225 /* Now close the file. */
229 while (++remaining < argc);
231 /* Close the output file now. */
233 error (EXIT_FAILURE, errno, _("error while closing output file"));
239 /* Handle program arguments. */
241 parse_opt (int key, char *arg, struct argp_state *state)
261 return ARGP_ERR_UNKNOWN;
268 more_help (int key, const char *text, void *input)
272 case ARGP_KEY_HELP_EXTRA:
273 /* We print some extra information. */
274 return strdup (gettext ("\
275 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
279 return (char *) text;
283 /* Print the version information. */
285 print_version (FILE *stream, struct argp_state *state)
287 fprintf (stream, "iconv (GNU %s) %s\n", PACKAGE, VERSION);
288 fprintf (stream, gettext ("\
289 Copyright (C) %s Free Software Foundation, Inc.\n\
290 This is free software; see the source for copying conditions. There is NO\n\
291 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
293 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
298 process_block (iconv_t cd, const char *addr, size_t len, FILE *output)
300 #define OUTBUF_SIZE 32768
301 char outbuf[OUTBUF_SIZE];
302 char *outptr = outbuf;
303 size_t outlen = OUTBUF_SIZE;
307 size_t n = iconv (cd, &addr, &len, &outptr, &outlen);
309 if (outptr != outbuf)
311 /* We have something to write out. */
312 if (fwrite (outbuf, 1, outptr - outbuf, output) < outptr - outbuf
315 /* Error occurred while printing the result. */
317 conversion stopped due to problem in writing the output"));
322 if (n != (size_t) -1)
323 /* Everything is processed. */
328 /* iconv() ran into a problem. */
332 error (0, 0, _("illegal input sequence"));
336 incomplete character or shift sequence at end of buffer"));
339 error (0, 0, _("internal error (illegal descriptor)"));
342 error (0, 0, _("unknown iconv() error %d"), errno);
355 process_fd (iconv_t cd, int fd, FILE *output)
357 /* we have a problem with reading from a desriptor since we must not
358 provide the iconv() function an incomplete character or shift
359 sequence at the end of the buffer. Since we have to deal with
360 arbitrary encodings we must read the whole text in a buffer and
361 process it in one step. */
362 static char *inbuf = NULL;
363 static size_t maxlen = 0;
367 while (actlen < maxlen)
369 size_t n = read (fd, inptr, maxlen - actlen);
372 /* No more text to read. */
377 /* Error while reading. */
378 error (0, errno, _("error while reading the input"));
386 if (actlen == maxlen)
391 /* Increase the buffer. */
393 inbuf = realloc (inbuf, maxlen);
395 error (0, errno, _("unable to allocate buffer for input"));
396 inptr = inbuf + actlen;
400 n = read (fd, inptr, maxlen - actlen);
403 /* No more text to read. */
408 /* Error while reading. */
409 error (0, errno, _("error while reading the input"));
416 while (actlen < maxlen);
419 /* Break again so we leave both loops. */
423 /* Now we have all the input in the buffer. Process it in one run. */
424 return process_block (cd, inbuf, actlen, output);
429 process_file (iconv_t cd, FILE *input, FILE *output)
431 /* This should be safe since we use this function only for `stdin' and
432 we haven't read anything so far. */
433 return process_fd (cd, fileno (input), output);
437 /* Print all known character sets/encodings. */
438 static void *printlist;
439 static size_t column;
440 static int not_first;
443 insert_print_list (const void *nodep, VISIT value, int level)
445 if (value == leaf || value == postorder)
447 const struct gconv_alias *s = *(const struct gconv_alias **) nodep;
448 tsearch (s->fromname, &printlist, (__compar_fn_t) strcoll);
453 do_print (const void *nodep, VISIT value, int level)
455 if (value == leaf || value == postorder)
457 const char *s = *(const char **) nodep;
458 size_t len = strlen (s);
461 while (len > 0 && s[len - 1] == '/')
464 for (cnt = 0; cnt < len; ++cnt)
465 if (isalnum (s[cnt]))
475 if (column > 2 && column + len > 77)
477 fputs ("\n ", stdout);
489 fwrite (s, len, 1, stdout);
495 print_known_names (void)
500 /* We must initialize the internal databases first. */
501 h = iconv_open ("L1", "L1");
504 /* First add the aliases. */
505 twalk (__gconv_alias_db, insert_print_list);
507 /* Add the from- and to-names from the known modules. */
508 for (cnt = 0; cnt < __gconv_nmodules; ++cnt)
510 if (__gconv_modules_db[cnt]->from_pattern == NULL)
512 tsearch (__gconv_modules_db[cnt]->from_constpfx, &printlist,
513 (__compar_fn_t) strcoll);
514 tsearch (__gconv_modules_db[cnt]->to_string, &printlist,
515 (__compar_fn_t) strcoll);
518 tsearch (__gconv_modules_db[cnt]->from_pattern, &printlist,
519 (__compar_fn_t) strcoll);
523 The following list contain all the coded character sets known. This does\n\
524 not necessarily mean that all combinations of these names can be used for\n\
525 the FROM and TO command line parameters. One coded character set can be\n\
526 listed with several different names (aliases).\n\
527 Some of the names are no plain strings but instead regular expressions and\n\
528 they match a variety of names which can be given as parameters to the\n\
529 program.\n\n "), stdout);
531 /* Now print the collected names. */
533 twalk (printlist, do_print);