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. */
33 #ifdef _POSIX_MAPPED_FILES
34 # include <sys/mman.h>
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
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. */
94 /* Nonzero if list of all coded character sets is wanted. */
97 /* Prototypes for the functions doing the actual work. */
98 static int process_block (iconv_t cd
, const char *addr
, size_t len
,
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
;
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. */
125 print_known_names ();
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'"));
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)
144 error (EXIT_FAILURE
, 0, _("conversion from `%s' to `%s' not supported"),
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");
155 error (EXIT_FAILURE
, errno
, _("cannot open output file"));
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
);
170 int fd
= open (argv
[remaining
], O_RDONLY
);
173 printf ("%s:\n", argv
[remaining
]);
177 error (0, errno
, _("cannot open input file `%s'"),
179 status
= EXIT_FAILURE
;
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))
190 /* Yes, we can use mmap(). The descriptor is not needed
193 error (EXIT_FAILURE
, errno
, _("error while closing input `%s'"),
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. */
210 /* We don't need the input data anymore. */
211 munmap ((void *) addr
, st
.st_size
);
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. */
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. */
231 /* Now close the file. */
235 while (++remaining
< argc
);
237 /* Close the output file now. */
239 error (EXIT_FAILURE
, errno
, _("error while closing output file"));
245 /* Handle program arguments. */
247 parse_opt (int key
, char *arg
, struct argp_state
*state
)
267 return ARGP_ERR_UNKNOWN
;
274 more_help (int key
, const char *text
, void *input
)
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"));
285 return (char *) text
;
289 /* Print the version information. */
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\
299 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
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
];
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
325 /* Error occurred while printing the result. */
327 conversion stopped due to problem in writing the output"));
332 if (n
!= (size_t) -1)
333 /* Everything is processed. */
338 /* iconv() ran into a problem. */
342 error (0, 0, _("illegal input sequence at position %ld"),
347 incomplete character or shift sequence at end of buffer"));
350 error (0, 0, _("internal error (illegal descriptor)"));
353 error (0, 0, _("unknown iconv() error %d"), errno
);
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;
378 while (actlen
< maxlen
)
380 size_t n
= read (fd
, inptr
, maxlen
- actlen
);
383 /* No more text to read. */
388 /* Error while reading. */
389 error (0, errno
, _("error while reading the input"));
397 if (actlen
== maxlen
)
402 /* Increase the buffer. */
404 inbuf
= realloc (inbuf
, maxlen
);
406 error (0, errno
, _("unable to allocate buffer for input"));
407 inptr
= inbuf
+ actlen
;
411 n
= read (fd
, inptr
, maxlen
- actlen
);
414 /* No more text to read. */
419 /* Error while reading. */
420 error (0, errno
, _("error while reading the input"));
427 while (actlen
< maxlen
);
430 /* Break again so we leave both loops. */
434 /* Now we have all the input in the buffer. Process it in one run. */
435 return process_block (cd
, inbuf
, actlen
, output
);
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
;
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
);
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
);
472 while (len
> 0 && s
[len
- 1] == '/')
475 for (cnt
= 0; cnt
< len
; ++cnt
)
476 if (isalnum (s
[cnt
]))
486 if (column
> 2 && column
+ len
> 77)
488 fputs ("\n ", stdout
);
500 fwrite (s
, len
, 1, stdout
);
506 print_known_names (void)
511 /* We must initialize the internal databases first. */
512 h
= iconv_open ("L1", "L1");
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
);
531 if (strcmp (__gconv_modules_db
[cnt
]->from_pattern
, "INTERNAL"))
532 tsearch (__gconv_modules_db
[cnt
]->from_pattern
, &printlist
,
533 (__compar_fn_t
) strverscmp
);
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. */
547 twalk (printlist
, do_print
);