Update.
[glibc.git] / iconv / iconv_prog.c
blobc91c76f34f6a8d0e210494ef21b9381ba48abad7
1 /* Convert text in given files from the specified from-set to the to-set.
2 Copyright (C) 1998, 1999 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 <libintl.h>
34 #ifdef _POSIX_MAPPED_FILES
35 # include <sys/mman.h>
36 #endif
37 #include <gconv_int.h>
39 /* Get libc version number. */
40 #include "../version.h"
42 #define PACKAGE _libc_intl_domainname
45 /* Name and version of program. */
46 static void print_version (FILE *stream, struct argp_state *state);
47 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
49 #define OPT_VERBOSE 1000
50 #define OPT_LIST 1001
52 /* Definitions of arguments for argp functions. */
53 static const struct argp_option options[] =
55 { NULL, 0, NULL, 0, N_("Input/Output format specification:") },
56 { "from-code", 'f', "NAME", 0, N_("encoding of original text") },
57 { "to-code", 't', "NAME", 0, N_("encoding for output") },
58 { NULL, 0, NULL, 0, N_("Information:") },
59 { "list", OPT_LIST, NULL, 0, N_("list all known coded character sets") },
60 { NULL, 0, NULL, 0, N_("Output control:") },
61 { "output", 'o', "FILE", 0, N_("output file") },
62 { "verbose", OPT_VERBOSE, NULL, 0, N_("print progress information") },
63 { NULL, 0, NULL, 0, NULL }
66 /* Short description of program. */
67 static const char doc[] = N_("\
68 Convert encoding of given files from one encoding to another.");
70 /* Strings for arguments in help texts. */
71 static const char args_doc[] = N_("[FILE...]");
73 /* Prototype for option handler. */
74 static error_t parse_opt __P ((int key, char *arg, struct argp_state *state));
76 /* Function to print some extra text in the help message. */
77 static char *more_help __P ((int key, const char *text, void *input));
79 /* Data structure to communicate with argp functions. */
80 static struct argp argp =
82 options, parse_opt, args_doc, doc, NULL, more_help
85 /* Code sets to convert from and to respectively. */
86 static const char *from_code;
87 static const char *to_code;
89 /* File to write output to. If NULL write to stdout. */
90 static const char *output_file;
92 /* Nonzero if verbose ouput is wanted. */
93 static int verbose;
95 /* Nonzero if list of all coded character sets is wanted. */
96 static int list;
98 /* Prototypes for the functions doing the actual work. */
99 static int process_block (iconv_t cd, const char *addr, size_t len,
100 FILE *output);
101 static int process_fd (iconv_t cd, int fd, FILE *output);
102 static int process_file (iconv_t cd, FILE *input, FILE *output);
103 static void print_known_names (void) internal_function;
107 main (int argc, char *argv[])
109 int status = EXIT_SUCCESS;
110 int remaining;
111 FILE *output;
112 iconv_t cd;
114 /* Set locale via LC_ALL. */
115 setlocale (LC_ALL, "");
117 /* Set the text message domain. */
118 textdomain (_libc_intl_domainname);
120 /* Parse and process arguments. */
121 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
123 /* List all coded character sets if wanted. */
124 if (list)
126 print_known_names ();
127 exit (EXIT_SUCCESS);
130 /* If either the from- or to-code is not specified this is an error
131 since we do not know what to do. */
132 if (from_code == NULL && to_code == NULL)
133 error (EXIT_FAILURE, 0,
134 _("neither original nor target encoding specified"));
135 if (from_code == NULL)
136 error (EXIT_FAILURE, 0, _("original encoding not specified using `-f'"));
137 if (to_code == NULL)
138 error (EXIT_FAILURE, 0, _("target encoding not specified using `-t'"));
140 /* Let's see whether we have these coded character sets. */
141 cd = iconv_open (to_code, from_code);
142 if (cd == (iconv_t) -1)
144 if (errno == EINVAL)
145 error (EXIT_FAILURE, 0, _("conversion from `%s' to `%s' not supported"),
146 from_code, to_code);
147 else
148 error (EXIT_FAILURE, errno, _("failed to start conversion processing"));
151 /* Determine output file. */
152 if (output_file != NULL)
154 output = fopen (output_file, "w");
155 if (output == NULL)
156 error (EXIT_FAILURE, errno, _("cannot open output file"));
158 else
159 output = stdout;
161 /* Now process the remaining files. Write them to stdout or the file
162 specified with the `-o' parameter. If we have no file given as
163 the parameter process all from stdin. */
164 if (remaining == argc)
165 process_file (cd, stdin, output);
166 else
169 struct stat st;
170 const char *addr;
171 int fd = open (argv[remaining], O_RDONLY);
173 if (verbose)
174 printf ("%s:\n", argv[remaining]);
176 if (fd == -1)
178 error (0, errno, _("cannot open input file `%s'"),
179 argv[remaining]);
180 status = EXIT_FAILURE;
181 continue;
184 #ifdef _POSIX_MAPPED_FILES
185 /* We have possibilities for reading the input file. First try
186 to mmap() it since this will provide the fastest solution. */
187 if (fstat (fd, &st) == 0
188 && ((addr = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0))
189 != MAP_FAILED))
191 /* Yes, we can use mmap(). The descriptor is not needed
192 anymore. */
193 if (close (fd) != 0)
194 error (EXIT_FAILURE, errno, _("error while closing input `%s'"),
195 argv[remaining]);
197 if (process_block (cd, addr, st.st_size, output) < 0)
199 /* Something went wrong. */
200 status = EXIT_FAILURE;
202 /* We don't need the input data anymore. */
203 munmap ((void *) addr, st.st_size);
205 /* We cannot go on with producing output since it might
206 lead to problem because the last output might leave
207 the output stream in an undefined state. */
208 break;
211 /* We don't need the input data anymore. */
212 munmap ((void *) addr, st.st_size);
214 else
215 #endif /* _POSIX_MAPPED_FILES */
217 /* Read the file in pieces. */
218 if (process_fd (cd, fd, output) != 0)
220 /* Something went wrong. */
221 status = EXIT_FAILURE;
223 /* We don't need the input file anymore. */
224 close (fd);
226 /* We cannot go on with producing output since it might
227 lead to problem because the last output might leave
228 the output stream in an undefined state. */
229 break;
232 /* Now close the file. */
233 close (fd);
236 while (++remaining < argc);
238 /* Close the output file now. */
239 if (fclose (output))
240 error (EXIT_FAILURE, errno, _("error while closing output file"));
242 return status;
246 /* Handle program arguments. */
247 static error_t
248 parse_opt (int key, char *arg, struct argp_state *state)
250 switch (key)
252 case 'f':
253 from_code = arg;
254 break;
255 case 't':
256 to_code = arg;
257 break;
258 case 'o':
259 output_file = arg;
260 break;
261 case OPT_VERBOSE:
262 verbose = 1;
263 break;
264 case OPT_LIST:
265 list = 1;
266 break;
267 default:
268 return ARGP_ERR_UNKNOWN;
270 return 0;
274 static char *
275 more_help (int key, const char *text, void *input)
277 switch (key)
279 case ARGP_KEY_HELP_EXTRA:
280 /* We print some extra information. */
281 return strdup (gettext ("\
282 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
283 default:
284 break;
286 return (char *) text;
290 /* Print the version information. */
291 static void
292 print_version (FILE *stream, struct argp_state *state)
294 fprintf (stream, "iconv (GNU %s) %s\n", PACKAGE, VERSION);
295 fprintf (stream, gettext ("\
296 Copyright (C) %s Free Software Foundation, Inc.\n\
297 This is free software; see the source for copying conditions. There is NO\n\
298 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
299 "), "1999");
300 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
304 static int
305 process_block (iconv_t cd, const char *addr, size_t len, FILE *output)
307 #define OUTBUF_SIZE 32768
308 const char *start = addr;
309 char outbuf[OUTBUF_SIZE];
310 char *outptr;
311 size_t outlen;
312 size_t n;
314 while (len > 0)
316 outptr = outbuf;
317 outlen = OUTBUF_SIZE;
318 n = iconv (cd, &addr, &len, &outptr, &outlen);
320 if (outptr != outbuf)
322 /* We have something to write out. */
323 if (fwrite (outbuf, 1, outptr - outbuf, output) < outptr - outbuf
324 || ferror (output))
326 /* Error occurred while printing the result. */
327 error (0, 0, _("\
328 conversion stopped due to problem in writing the output"));
329 return -1;
333 if (n != (size_t) -1)
334 /* Everything is processed. */
335 break;
337 if (errno != E2BIG)
339 /* iconv() ran into a problem. */
340 switch (errno)
342 case EILSEQ:
343 error (0, 0, _("illegal input sequence at position %ld"),
344 addr - start);
345 break;
346 case EINVAL:
347 error (0, 0, _("\
348 incomplete character or shift sequence at end of buffer"));
349 break;
350 case EBADF:
351 error (0, 0, _("internal error (illegal descriptor)"));
352 break;
353 default:
354 error (0, 0, _("unknown iconv() error %d"), errno);
355 break;
358 return -1;
362 return 0;
366 static int
367 process_fd (iconv_t cd, int fd, FILE *output)
369 /* we have a problem with reading from a desriptor since we must not
370 provide the iconv() function an incomplete character or shift
371 sequence at the end of the buffer. Since we have to deal with
372 arbitrary encodings we must read the whole text in a buffer and
373 process it in one step. */
374 static char *inbuf = NULL;
375 static size_t maxlen = 0;
376 char *inptr = NULL;
377 size_t actlen = 0;
379 while (actlen < maxlen)
381 size_t n = read (fd, inptr, maxlen - actlen);
383 if (n == 0)
384 /* No more text to read. */
385 break;
387 if (n == -1)
389 /* Error while reading. */
390 error (0, errno, _("error while reading the input"));
391 return -1;
394 inptr += n;
395 actlen += n;
398 if (actlen == maxlen)
399 while (1)
401 size_t n;
403 /* Increase the buffer. */
404 maxlen += 32768;
405 inbuf = realloc (inbuf, maxlen);
406 if (inbuf == NULL)
407 error (0, errno, _("unable to allocate buffer for input"));
408 inptr = inbuf + actlen;
412 n = read (fd, inptr, maxlen - actlen);
414 if (n == 0)
415 /* No more text to read. */
416 break;
418 if (n == -1)
420 /* Error while reading. */
421 error (0, errno, _("error while reading the input"));
422 return -1;
425 inptr += n;
426 actlen += n;
428 while (actlen < maxlen);
430 if (n == 0)
431 /* Break again so we leave both loops. */
432 break;
435 /* Now we have all the input in the buffer. Process it in one run. */
436 return process_block (cd, inbuf, actlen, output);
440 static int
441 process_file (iconv_t cd, FILE *input, FILE *output)
443 /* This should be safe since we use this function only for `stdin' and
444 we haven't read anything so far. */
445 return process_fd (cd, fileno (input), output);
449 /* Print all known character sets/encodings. */
450 static void *printlist;
451 static size_t column;
452 static int not_first;
454 static void
455 insert_print_list (const void *nodep, VISIT value, int level)
457 if (value == leaf || value == postorder)
459 const struct gconv_alias *s = *(const struct gconv_alias **) nodep;
460 tsearch (s->fromname, &printlist, (__compar_fn_t) strverscmp);
464 static void
465 do_print (const void *nodep, VISIT value, int level)
467 if (value == leaf || value == postorder)
469 const char *s = *(const char **) nodep;
470 size_t len = strlen (s);
471 size_t cnt;
473 while (len > 0 && s[len - 1] == '/')
474 --len;
476 for (cnt = 0; cnt < len; ++cnt)
477 if (isalnum (s[cnt]))
478 break;
479 if (cnt == len)
480 return;
482 if (not_first)
484 putchar (',');
485 ++column;
487 if (column > 2 && column + len > 77)
489 fputs ("\n ", stdout);
490 column = 2;
492 else
494 putchar (' ');
495 ++column;
498 else
499 not_first = 1;
501 fwrite (s, len, 1, stdout);
502 column += len;
506 static void
507 internal_function
508 add_known_names (struct gconv_module *node)
510 if (node->left != NULL)
511 add_known_names (node->left);
512 if (node->right != NULL)
513 add_known_names (node->right);
514 if (node->same != NULL)
515 add_known_names (node->same);
518 if (node->from_pattern == NULL)
520 if (strcmp (node->from_constpfx, "INTERNAL"))
521 tsearch (node->from_constpfx, &printlist,
522 (__compar_fn_t) strverscmp);
523 if (strcmp (node->to_string, "INTERNAL"))
524 tsearch (node->to_string, &printlist, (__compar_fn_t) strverscmp);
526 else
527 if (strcmp (node->from_pattern, "INTERNAL"))
528 tsearch (node->from_pattern, &printlist, (__compar_fn_t) strverscmp);
530 node = node->matching;
532 while (node != NULL);
535 static void
536 internal_function
537 print_known_names (void)
539 iconv_t h;
541 /* We must initialize the internal databases first. */
542 h = iconv_open ("L1", "L1");
543 iconv_close (h);
545 /* First add the aliases. */
546 twalk (__gconv_alias_db, insert_print_list);
548 /* Add the from- and to-names from the known modules. */
549 add_known_names (__gconv_modules_db);
551 fputs (_("\
552 The following list contain all the coded character sets known. This does\n\
553 not necessarily mean that all combinations of these names can be used for\n\
554 the FROM and TO command line parameters. One coded character set can be\n\
555 listed with several different names (aliases).\n\
556 Some of the names are no plain strings but instead regular expressions and\n\
557 they match a variety of names which can be given as parameters to the\n\
558 program.\n\n "), stdout);
560 /* Now print the collected names. */
561 column = 2;
562 twalk (printlist, do_print);
564 if (column != 0)
565 puts ("");