Update.
[glibc.git] / iconv / iconv_prog.c
blob82ff5cf80254630afa7de9cff18ece957c3c59b6
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. */
21 #include <argp.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <error.h>
25 #include <fcntl.h>
26 #include <iconv.h>
27 #include <locale.h>
28 #include <search.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/mman.h>
34 #include <gconv_int.h>
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
47 #define OPT_LIST 1001
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. */
90 static int verbose;
92 /* Nonzero if list of all coded character sets is wanted. */
93 static int list;
95 /* Prototypes for the functions doing the actual work. */
96 static int process_block (iconv_t cd, const char *addr, size_t len,
97 FILE *output);
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;
107 int remaining;
108 FILE *output;
109 iconv_t cd;
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. */
121 if (list)
123 print_known_names ();
124 exit (EXIT_SUCCESS);
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'"));
134 if (to_code == NULL)
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)
140 if (errno == EINVAL)
141 error (EXIT_FAILURE, 0, _("conversion from `%s' to `%s' not supported"),
142 from_code, to_code);
143 else
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");
150 if (output == NULL)
151 error (EXIT_FAILURE, errno, _("cannot open output file"));
153 else
154 output = stdout;
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);
161 else
164 struct stat st;
165 const char *addr;
166 int fd = open (argv[remaining], O_RDONLY);
168 if (verbose)
169 printf ("%s:\n", argv[remaining]);
171 if (fd == -1)
173 error (0, errno, _("cannot open input file `%s'"),
174 argv[remaining]);
175 status = EXIT_FAILURE;
176 continue;
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))
183 != MAP_FAILED))
185 /* Yes, we can use mmap(). The descriptor is not needed
186 anymore. */
187 if (close (fd) != 0)
188 error (EXIT_FAILURE, errno, _("error while closing input `%s'"),
189 argv[remaining]);
191 if (process_block (cd, addr, st.st_size, output) < 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. */
202 break;
205 /* We don't need the input data anymore. */
206 munmap ((void *) addr, st.st_size);
208 else
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. */
217 close (fd);
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. */
222 break;
225 /* Now close the file. */
226 close (fd);
229 while (++remaining < argc);
231 /* Close the output file now. */
232 if (fclose (output))
233 error (EXIT_FAILURE, errno, _("error while closing output file"));
235 return status;
239 /* Handle program arguments. */
240 static error_t
241 parse_opt (int key, char *arg, struct argp_state *state)
243 switch (key)
245 case 'f':
246 from_code = arg;
247 break;
248 case 't':
249 to_code = arg;
250 break;
251 case 'o':
252 output_file = arg;
253 break;
254 case OPT_VERBOSE:
255 verbose = 1;
256 break;
257 case OPT_LIST:
258 list = 1;
259 break;
260 default:
261 return ARGP_ERR_UNKNOWN;
263 return 0;
267 static char *
268 more_help (int key, const char *text, void *input)
270 switch (key)
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"));
276 default:
277 break;
279 return (char *) text;
283 /* Print the version information. */
284 static void
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\
292 "), "1998");
293 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
297 static int
298 process_block (iconv_t cd, const char *addr, size_t len, FILE *output)
300 #define OUTBUF_SIZE 32768
301 const char *start = addr;
302 char outbuf[OUTBUF_SIZE];
303 char *outptr;
304 size_t outlen;
305 size_t n;
307 while (len > 0)
309 outptr = outbuf;
310 outlen = OUTBUF_SIZE;
311 n = iconv (cd, &addr, &len, &outptr, &outlen);
313 if (outptr != outbuf)
315 /* We have something to write out. */
316 if (fwrite (outbuf, 1, outptr - outbuf, output) < outptr - outbuf
317 || ferror (output))
319 /* Error occurred while printing the result. */
320 error (0, 0, _("\
321 conversion stopped due to problem in writing the output"));
322 return -1;
326 if (n != (size_t) -1)
327 /* Everything is processed. */
328 break;
330 if (errno != E2BIG)
332 /* iconv() ran into a problem. */
333 switch (errno)
335 case EILSEQ:
336 error (0, 0, _("illegal input sequence at position %ld"),
337 addr - start);
338 break;
339 case EINVAL:
340 error (0, 0, _("\
341 incomplete character or shift sequence at end of buffer"));
342 break;
343 case EBADF:
344 error (0, 0, _("internal error (illegal descriptor)"));
345 break;
346 default:
347 error (0, 0, _("unknown iconv() error %d"), errno);
348 break;
351 return -1;
355 return 0;
359 static int
360 process_fd (iconv_t cd, int fd, FILE *output)
362 /* we have a problem with reading from a desriptor since we must not
363 provide the iconv() function an incomplete character or shift
364 sequence at the end of the buffer. Since we have to deal with
365 arbitrary encodings we must read the whole text in a buffer and
366 process it in one step. */
367 static char *inbuf = NULL;
368 static size_t maxlen = 0;
369 char *inptr = NULL;
370 size_t actlen = 0;
372 while (actlen < maxlen)
374 size_t n = read (fd, inptr, maxlen - actlen);
376 if (n == 0)
377 /* No more text to read. */
378 break;
380 if (n == -1)
382 /* Error while reading. */
383 error (0, errno, _("error while reading the input"));
384 return -1;
387 inptr += n;
388 actlen += n;
391 if (actlen == maxlen)
392 while (1)
394 size_t n;
396 /* Increase the buffer. */
397 maxlen += 32768;
398 inbuf = realloc (inbuf, maxlen);
399 if (inbuf == NULL)
400 error (0, errno, _("unable to allocate buffer for input"));
401 inptr = inbuf + actlen;
405 n = read (fd, inptr, maxlen - actlen);
407 if (n == 0)
408 /* No more text to read. */
409 break;
411 if (n == -1)
413 /* Error while reading. */
414 error (0, errno, _("error while reading the input"));
415 return -1;
418 inptr += n;
419 actlen += n;
421 while (actlen < maxlen);
423 if (n == 0)
424 /* Break again so we leave both loops. */
425 break;
428 /* Now we have all the input in the buffer. Process it in one run. */
429 return process_block (cd, inbuf, actlen, output);
433 static int
434 process_file (iconv_t cd, FILE *input, FILE *output)
436 /* This should be safe since we use this function only for `stdin' and
437 we haven't read anything so far. */
438 return process_fd (cd, fileno (input), output);
442 /* Print all known character sets/encodings. */
443 static void *printlist;
444 static size_t column;
445 static int not_first;
447 static void
448 insert_print_list (const void *nodep, VISIT value, int level)
450 if (value == leaf || value == postorder)
452 const struct gconv_alias *s = *(const struct gconv_alias **) nodep;
453 tsearch (s->fromname, &printlist, (__compar_fn_t) strverscmp);
457 static void
458 do_print (const void *nodep, VISIT value, int level)
460 if (value == leaf || value == postorder)
462 const char *s = *(const char **) nodep;
463 size_t len = strlen (s);
464 size_t cnt;
466 while (len > 0 && s[len - 1] == '/')
467 --len;
469 for (cnt = 0; cnt < len; ++cnt)
470 if (isalnum (s[cnt]))
471 break;
472 if (cnt == len)
473 return;
475 if (not_first)
477 putchar (',');
478 ++column;
480 if (column > 2 && column + len > 77)
482 fputs ("\n ", stdout);
483 column = 2;
485 else
487 putchar (' ');
488 ++column;
491 else
492 not_first = 1;
494 fwrite (s, len, 1, stdout);
495 column += len;
499 static void
500 print_known_names (void)
502 size_t cnt;
503 iconv_t h;
505 /* We must initialize the internal databases first. */
506 h = iconv_open ("L1", "L1");
507 iconv_close (h);
509 /* First add the aliases. */
510 twalk (__gconv_alias_db, insert_print_list);
512 /* Add the from- and to-names from the known modules. */
513 for (cnt = 0; cnt < __gconv_nmodules; ++cnt)
515 if (__gconv_modules_db[cnt]->from_pattern == NULL)
517 if (strcmp (__gconv_modules_db[cnt]->from_constpfx, "INTERNAL"))
518 tsearch (__gconv_modules_db[cnt]->from_constpfx, &printlist,
519 (__compar_fn_t) strverscmp);
520 if (strcmp (__gconv_modules_db[cnt]->to_string, "INTERNAL"))
521 tsearch (__gconv_modules_db[cnt]->to_string, &printlist,
522 (__compar_fn_t) strverscmp);
524 else
525 if (strcmp (__gconv_modules_db[cnt]->from_pattern, "INTERNAL"))
526 tsearch (__gconv_modules_db[cnt]->from_pattern, &printlist,
527 (__compar_fn_t) strverscmp);
530 fputs (_("\
531 The following list contain all the coded character sets known. This does\n\
532 not necessarily mean that all combinations of these names can be used for\n\
533 the FROM and TO command line parameters. One coded character set can be\n\
534 listed with several different names (aliases).\n\
535 Some of the names are no plain strings but instead regular expressions and\n\
536 they match a variety of names which can be given as parameters to the\n\
537 program.\n\n "), stdout);
539 /* Now print the collected names. */
540 column = 2;
541 twalk (printlist, do_print);
543 if (column != 0)
544 puts ("");