1998-10-19 Roland McGrath <roland@baalperazim.frob.com>
[glibc.git] / iconv / iconv_prog.c
blob7dd0f3422c9c3bfe0a37ebe80a09b8c7d810b288
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 #ifdef _POSIX_MAPPED_FILES
34 # include <sys/mman.h>
35 #endif
36 #include <gconv_int.h>
38 /* Get libc version number. */
39 #include "../version.h"
41 #define PACKAGE _libc_intl_domainname
44 /* Name and version of program. */
45 static void print_version (FILE *stream, struct argp_state *state);
46 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
48 #define OPT_VERBOSE 1000
49 #define OPT_LIST 1001
51 /* Definitions of arguments for argp functions. */
52 static const struct argp_option options[] =
54 { NULL, 0, NULL, 0, N_("Input/Output format specification:") },
55 { "from-code", 'f', "NAME", 0, N_("encoding of original text") },
56 { "to-code", 't', "NAME", 0, N_("encoding for output") },
57 { NULL, 0, NULL, 0, N_("Information:") },
58 { "list", OPT_LIST, NULL, 0, N_("list all known coded character sets") },
59 { NULL, 0, NULL, 0, N_("Output control:") },
60 { "output", 'o', "FILE", 0, N_("output file") },
61 { "verbose", OPT_VERBOSE, NULL, 0, N_("print progress information") },
62 { NULL, 0, NULL, 0, NULL }
65 /* Short description of program. */
66 static const char doc[] = N_("\
67 Convert encoding of given files from one encoding to another.");
69 /* Strings for arguments in help texts. */
70 static const char args_doc[] = N_("[FILE...]");
72 /* Prototype for option handler. */
73 static error_t parse_opt __P ((int key, char *arg, struct argp_state *state));
75 /* Function to print some extra text in the help message. */
76 static char *more_help __P ((int key, const char *text, void *input));
78 /* Data structure to communicate with argp functions. */
79 static struct argp argp =
81 options, parse_opt, args_doc, doc, NULL, more_help
84 /* Code sets to convert from and to respectively. */
85 static const char *from_code;
86 static const char *to_code;
88 /* File to write output to. If NULL write to stdout. */
89 static const char *output_file;
91 /* Nonzero if verbose ouput is wanted. */
92 static int verbose;
94 /* Nonzero if list of all coded character sets is wanted. */
95 static int list;
97 /* Prototypes for the functions doing the actual work. */
98 static int process_block (iconv_t cd, const char *addr, size_t len,
99 FILE *output);
100 static int process_fd (iconv_t cd, int fd, FILE *output);
101 static int process_file (iconv_t cd, FILE *input, FILE *output);
102 static void print_known_names (void);
106 main (int argc, char *argv[])
108 int status = EXIT_SUCCESS;
109 int remaining;
110 FILE *output;
111 iconv_t cd;
113 /* Set locale via LC_ALL. */
114 setlocale (LC_ALL, "");
116 /* Set the text message domain. */
117 textdomain (_libc_intl_domainname);
119 /* Parse and process arguments. */
120 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
122 /* List all coded character sets if wanted. */
123 if (list)
125 print_known_names ();
126 exit (EXIT_SUCCESS);
129 /* If either the from- or to-code is not specified this is an error
130 since we do not know what to do. */
131 if (from_code == NULL && to_code == NULL)
132 error (EXIT_FAILURE, 0,
133 _("neither original nor target encoding specified"));
134 if (from_code == NULL)
135 error (EXIT_FAILURE, 0, _("original encoding not specified using `-f'"));
136 if (to_code == NULL)
137 error (EXIT_FAILURE, 0, _("target encoding not specified using `-t'"));
139 /* Let's see whether we have these coded character sets. */
140 cd = iconv_open (to_code, from_code);
141 if (cd == (iconv_t) -1)
143 if (errno == EINVAL)
144 error (EXIT_FAILURE, 0, _("conversion from `%s' to `%s' not supported"),
145 from_code, to_code);
146 else
147 error (EXIT_FAILURE, errno, _("failed to start conversion processing"));
150 /* Determine output file. */
151 if (output_file != NULL)
153 output = fopen (output_file, "w");
154 if (output == NULL)
155 error (EXIT_FAILURE, errno, _("cannot open output file"));
157 else
158 output = stdout;
160 /* Now process the remaining files. Write them to stdout or the file
161 specified with the `-o' parameter. If we have no file given as
162 the parameter process all from stdin. */
163 if (remaining == argc)
164 process_file (cd, stdin, output);
165 else
168 struct stat st;
169 const char *addr;
170 int fd = open (argv[remaining], O_RDONLY);
172 if (verbose)
173 printf ("%s:\n", argv[remaining]);
175 if (fd == -1)
177 error (0, errno, _("cannot open input file `%s'"),
178 argv[remaining]);
179 status = EXIT_FAILURE;
180 continue;
183 #ifdef _POSIX_MAPPED_FILES
184 /* We have possibilities for reading the input file. First try
185 to mmap() it since this will provide the fastest solution. */
186 if (fstat (fd, &st) == 0
187 && ((addr = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0))
188 != MAP_FAILED))
190 /* Yes, we can use mmap(). The descriptor is not needed
191 anymore. */
192 if (close (fd) != 0)
193 error (EXIT_FAILURE, errno, _("error while closing input `%s'"),
194 argv[remaining]);
196 if (process_block (cd, addr, st.st_size, output) < 0)
198 /* Something went wrong. */
199 status = EXIT_FAILURE;
201 /* We don't need the input data anymore. */
202 munmap ((void *) addr, st.st_size);
204 /* We cannot go on with producing output since it might
205 lead to problem because the last output might leave
206 the output stream in an undefined state. */
207 break;
210 /* We don't need the input data anymore. */
211 munmap ((void *) addr, st.st_size);
213 else
214 #endif /* _POSIX_MAPPED_FILES */
216 /* Read the file in pieces. */
217 if (process_fd (cd, fd, output) != 0)
219 /* Something went wrong. */
220 status = EXIT_FAILURE;
222 /* We don't need the input file anymore. */
223 close (fd);
225 /* We cannot go on with producing output since it might
226 lead to problem because the last output might leave
227 the output stream in an undefined state. */
228 break;
231 /* Now close the file. */
232 close (fd);
235 while (++remaining < argc);
237 /* Close the output file now. */
238 if (fclose (output))
239 error (EXIT_FAILURE, errno, _("error while closing output file"));
241 return status;
245 /* Handle program arguments. */
246 static error_t
247 parse_opt (int key, char *arg, struct argp_state *state)
249 switch (key)
251 case 'f':
252 from_code = arg;
253 break;
254 case 't':
255 to_code = arg;
256 break;
257 case 'o':
258 output_file = arg;
259 break;
260 case OPT_VERBOSE:
261 verbose = 1;
262 break;
263 case OPT_LIST:
264 list = 1;
265 break;
266 default:
267 return ARGP_ERR_UNKNOWN;
269 return 0;
273 static char *
274 more_help (int key, const char *text, void *input)
276 switch (key)
278 case ARGP_KEY_HELP_EXTRA:
279 /* We print some extra information. */
280 return strdup (gettext ("\
281 Report bugs using the `glibcbug' script to <bugs@gnu.org>.\n"));
282 default:
283 break;
285 return (char *) text;
289 /* Print the version information. */
290 static void
291 print_version (FILE *stream, struct argp_state *state)
293 fprintf (stream, "iconv (GNU %s) %s\n", PACKAGE, VERSION);
294 fprintf (stream, gettext ("\
295 Copyright (C) %s Free Software Foundation, Inc.\n\
296 This is free software; see the source for copying conditions. There is NO\n\
297 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
298 "), "1998");
299 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
303 static int
304 process_block (iconv_t cd, const char *addr, size_t len, FILE *output)
306 #define OUTBUF_SIZE 32768
307 const char *start = addr;
308 char outbuf[OUTBUF_SIZE];
309 char *outptr;
310 size_t outlen;
311 size_t n;
313 while (len > 0)
315 outptr = outbuf;
316 outlen = OUTBUF_SIZE;
317 n = iconv (cd, &addr, &len, &outptr, &outlen);
319 if (outptr != outbuf)
321 /* We have something to write out. */
322 if (fwrite (outbuf, 1, outptr - outbuf, output) < outptr - outbuf
323 || ferror (output))
325 /* Error occurred while printing the result. */
326 error (0, 0, _("\
327 conversion stopped due to problem in writing the output"));
328 return -1;
332 if (n != (size_t) -1)
333 /* Everything is processed. */
334 break;
336 if (errno != E2BIG)
338 /* iconv() ran into a problem. */
339 switch (errno)
341 case EILSEQ:
342 error (0, 0, _("illegal input sequence at position %ld"),
343 addr - start);
344 break;
345 case EINVAL:
346 error (0, 0, _("\
347 incomplete character or shift sequence at end of buffer"));
348 break;
349 case EBADF:
350 error (0, 0, _("internal error (illegal descriptor)"));
351 break;
352 default:
353 error (0, 0, _("unknown iconv() error %d"), errno);
354 break;
357 return -1;
361 return 0;
365 static int
366 process_fd (iconv_t cd, int fd, FILE *output)
368 /* we have a problem with reading from a desriptor since we must not
369 provide the iconv() function an incomplete character or shift
370 sequence at the end of the buffer. Since we have to deal with
371 arbitrary encodings we must read the whole text in a buffer and
372 process it in one step. */
373 static char *inbuf = NULL;
374 static size_t maxlen = 0;
375 char *inptr = NULL;
376 size_t actlen = 0;
378 while (actlen < maxlen)
380 size_t n = read (fd, inptr, maxlen - actlen);
382 if (n == 0)
383 /* No more text to read. */
384 break;
386 if (n == -1)
388 /* Error while reading. */
389 error (0, errno, _("error while reading the input"));
390 return -1;
393 inptr += n;
394 actlen += n;
397 if (actlen == maxlen)
398 while (1)
400 size_t n;
402 /* Increase the buffer. */
403 maxlen += 32768;
404 inbuf = realloc (inbuf, maxlen);
405 if (inbuf == NULL)
406 error (0, errno, _("unable to allocate buffer for input"));
407 inptr = inbuf + actlen;
411 n = read (fd, inptr, maxlen - actlen);
413 if (n == 0)
414 /* No more text to read. */
415 break;
417 if (n == -1)
419 /* Error while reading. */
420 error (0, errno, _("error while reading the input"));
421 return -1;
424 inptr += n;
425 actlen += n;
427 while (actlen < maxlen);
429 if (n == 0)
430 /* Break again so we leave both loops. */
431 break;
434 /* Now we have all the input in the buffer. Process it in one run. */
435 return process_block (cd, inbuf, actlen, output);
439 static int
440 process_file (iconv_t cd, FILE *input, FILE *output)
442 /* This should be safe since we use this function only for `stdin' and
443 we haven't read anything so far. */
444 return process_fd (cd, fileno (input), output);
448 /* Print all known character sets/encodings. */
449 static void *printlist;
450 static size_t column;
451 static int not_first;
453 static void
454 insert_print_list (const void *nodep, VISIT value, int level)
456 if (value == leaf || value == postorder)
458 const struct gconv_alias *s = *(const struct gconv_alias **) nodep;
459 tsearch (s->fromname, &printlist, (__compar_fn_t) strverscmp);
463 static void
464 do_print (const void *nodep, VISIT value, int level)
466 if (value == leaf || value == postorder)
468 const char *s = *(const char **) nodep;
469 size_t len = strlen (s);
470 size_t cnt;
472 while (len > 0 && s[len - 1] == '/')
473 --len;
475 for (cnt = 0; cnt < len; ++cnt)
476 if (isalnum (s[cnt]))
477 break;
478 if (cnt == len)
479 return;
481 if (not_first)
483 putchar (',');
484 ++column;
486 if (column > 2 && column + len > 77)
488 fputs ("\n ", stdout);
489 column = 2;
491 else
493 putchar (' ');
494 ++column;
497 else
498 not_first = 1;
500 fwrite (s, len, 1, stdout);
501 column += len;
505 static void
506 print_known_names (void)
508 size_t cnt;
509 iconv_t h;
511 /* We must initialize the internal databases first. */
512 h = iconv_open ("L1", "L1");
513 iconv_close (h);
515 /* First add the aliases. */
516 twalk (__gconv_alias_db, insert_print_list);
518 /* Add the from- and to-names from the known modules. */
519 for (cnt = 0; cnt < __gconv_nmodules; ++cnt)
521 if (__gconv_modules_db[cnt]->from_pattern == NULL)
523 if (strcmp (__gconv_modules_db[cnt]->from_constpfx, "INTERNAL"))
524 tsearch (__gconv_modules_db[cnt]->from_constpfx, &printlist,
525 (__compar_fn_t) strverscmp);
526 if (strcmp (__gconv_modules_db[cnt]->to_string, "INTERNAL"))
527 tsearch (__gconv_modules_db[cnt]->to_string, &printlist,
528 (__compar_fn_t) strverscmp);
530 else
531 if (strcmp (__gconv_modules_db[cnt]->from_pattern, "INTERNAL"))
532 tsearch (__gconv_modules_db[cnt]->from_pattern, &printlist,
533 (__compar_fn_t) strverscmp);
536 fputs (_("\
537 The following list contain all the coded character sets known. This does\n\
538 not necessarily mean that all combinations of these names can be used for\n\
539 the FROM and TO command line parameters. One coded character set can be\n\
540 listed with several different names (aliases).\n\
541 Some of the names are no plain strings but instead regular expressions and\n\
542 they match a variety of names which can be given as parameters to the\n\
543 program.\n\n "), stdout);
545 /* Now print the collected names. */
546 column = 2;
547 twalk (printlist, do_print);
549 if (column != 0)
550 puts ("");