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. */
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
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
, 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. */
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 const char *start
= addr
;
302 char outbuf
[OUTBUF_SIZE
];
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
319 /* Error occurred while printing the result. */
321 conversion stopped due to problem in writing the output"));
326 if (n
!= (size_t) -1)
327 /* Everything is processed. */
332 /* iconv() ran into a problem. */
336 error (0, 0, _("illegal input sequence at position %ld"),
341 incomplete character or shift sequence at end of buffer"));
344 error (0, 0, _("internal error (illegal descriptor)"));
347 error (0, 0, _("unknown iconv() error %d"), errno
);
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;
372 while (actlen
< maxlen
)
374 size_t n
= read (fd
, inptr
, maxlen
- actlen
);
377 /* No more text to read. */
382 /* Error while reading. */
383 error (0, errno
, _("error while reading the input"));
391 if (actlen
== maxlen
)
396 /* Increase the buffer. */
398 inbuf
= realloc (inbuf
, maxlen
);
400 error (0, errno
, _("unable to allocate buffer for input"));
401 inptr
= inbuf
+ actlen
;
405 n
= read (fd
, inptr
, maxlen
- actlen
);
408 /* No more text to read. */
413 /* Error while reading. */
414 error (0, errno
, _("error while reading the input"));
421 while (actlen
< maxlen
);
424 /* Break again so we leave both loops. */
428 /* Now we have all the input in the buffer. Process it in one run. */
429 return process_block (cd
, inbuf
, actlen
, output
);
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
;
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
);
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
);
466 while (len
> 0 && s
[len
- 1] == '/')
469 for (cnt
= 0; cnt
< len
; ++cnt
)
470 if (isalnum (s
[cnt
]))
480 if (column
> 2 && column
+ len
> 77)
482 fputs ("\n ", stdout
);
494 fwrite (s
, len
, 1, stdout
);
500 print_known_names (void)
505 /* We must initialize the internal databases first. */
506 h
= iconv_open ("L1", "L1");
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
);
525 if (strcmp (__gconv_modules_db
[cnt
]->from_pattern
, "INTERNAL"))
526 tsearch (__gconv_modules_db
[cnt
]->from_pattern
, &printlist
,
527 (__compar_fn_t
) strverscmp
);
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. */
541 twalk (printlist
, do_print
);