]>
Commit | Line | Data |
---|---|---|
fb5663ca | 1 | /* Convert text in given files from the specified from-set to the to-set. |
8e597a18 | 2 | Copyright (C) 1998-2003, 2004 Free Software Foundation, Inc. |
fb5663ca UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
fb5663ca UD |
10 | |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
fb5663ca | 15 | |
41bdb6e2 AJ |
16 | You should have received a copy of the GNU Lesser General Public |
17 | License along with the GNU C Library; if not, write to the Free | |
18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
19 | 02111-1307 USA. */ | |
fb5663ca UD |
20 | |
21 | #include <argp.h> | |
e4060100 | 22 | #include <assert.h> |
8fe0fd03 | 23 | #include <ctype.h> |
fb5663ca UD |
24 | #include <errno.h> |
25 | #include <error.h> | |
26 | #include <fcntl.h> | |
27 | #include <iconv.h> | |
ac3d553b | 28 | #include <langinfo.h> |
fb5663ca | 29 | #include <locale.h> |
8fe0fd03 | 30 | #include <search.h> |
fa00744e | 31 | #include <stdbool.h> |
fb5663ca UD |
32 | #include <stdio.h> |
33 | #include <stdlib.h> | |
34 | #include <string.h> | |
35 | #include <unistd.h> | |
4360eafd | 36 | #include <libintl.h> |
7cabd57c UD |
37 | #ifdef _POSIX_MAPPED_FILES |
38 | # include <sys/mman.h> | |
39 | #endif | |
93693c4d | 40 | #include <charmap.h> |
e62c19f1 | 41 | #include <gconv_int.h> |
93693c4d | 42 | #include "iconv_prog.h" |
9a1f71a7 | 43 | #include "iconvconfig.h" |
fb5663ca UD |
44 | |
45 | /* Get libc version number. */ | |
46 | #include "../version.h" | |
47 | ||
48 | #define PACKAGE _libc_intl_domainname | |
49 | ||
50 | ||
51 | /* Name and version of program. */ | |
52 | static void print_version (FILE *stream, struct argp_state *state); | |
53 | void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version; | |
54 | ||
55 | #define OPT_VERBOSE 1000 | |
adcf0e4a | 56 | #define OPT_LIST 'l' |
fb5663ca UD |
57 | |
58 | /* Definitions of arguments for argp functions. */ | |
59 | static const struct argp_option options[] = | |
60 | { | |
61 | { NULL, 0, NULL, 0, N_("Input/Output format specification:") }, | |
62 | { "from-code", 'f', "NAME", 0, N_("encoding of original text") }, | |
63 | { "to-code", 't', "NAME", 0, N_("encoding for output") }, | |
8fe0fd03 | 64 | { NULL, 0, NULL, 0, N_("Information:") }, |
adcf0e4a | 65 | { "list", 'l', NULL, 0, N_("list all known coded character sets") }, |
fb5663ca | 66 | { NULL, 0, NULL, 0, N_("Output control:") }, |
adcf0e4a | 67 | { NULL, 'c', NULL, 0, N_("omit invalid characters from output") }, |
fb5663ca | 68 | { "output", 'o', "FILE", 0, N_("output file") }, |
6d77214d | 69 | { "silent", 's', NULL, 0, N_("suppress warnings") }, |
fb5663ca UD |
70 | { "verbose", OPT_VERBOSE, NULL, 0, N_("print progress information") }, |
71 | { NULL, 0, NULL, 0, NULL } | |
72 | }; | |
73 | ||
74 | /* Short description of program. */ | |
75 | static const char doc[] = N_("\ | |
76 | Convert encoding of given files from one encoding to another."); | |
77 | ||
78 | /* Strings for arguments in help texts. */ | |
79 | static const char args_doc[] = N_("[FILE...]"); | |
80 | ||
81 | /* Prototype for option handler. */ | |
adcf0e4a | 82 | static error_t parse_opt (int key, char *arg, struct argp_state *state); |
fb5663ca UD |
83 | |
84 | /* Function to print some extra text in the help message. */ | |
adcf0e4a | 85 | static char *more_help (int key, const char *text, void *input); |
fb5663ca UD |
86 | |
87 | /* Data structure to communicate with argp functions. */ | |
88 | static struct argp argp = | |
89 | { | |
90 | options, parse_opt, args_doc, doc, NULL, more_help | |
91 | }; | |
92 | ||
e0e86ccb UD |
93 | /* Code sets to convert from and to respectively. An empty string as the |
94 | default causes the 'iconv_open' function to look up the charset of the | |
95 | currently selected locale and use it. */ | |
96 | static const char *from_code = ""; | |
97 | static const char *to_code = ""; | |
fb5663ca UD |
98 | |
99 | /* File to write output to. If NULL write to stdout. */ | |
100 | static const char *output_file; | |
101 | ||
102 | /* Nonzero if verbose ouput is wanted. */ | |
93693c4d | 103 | int verbose; |
fb5663ca | 104 | |
8fe0fd03 UD |
105 | /* Nonzero if list of all coded character sets is wanted. */ |
106 | static int list; | |
107 | ||
85830c4c | 108 | /* If nonzero omit invalid character from output. */ |
93693c4d | 109 | int omit_invalid; |
85830c4c | 110 | |
fb5663ca | 111 | /* Prototypes for the functions doing the actual work. */ |
3b434940 | 112 | static int process_block (iconv_t cd, char *addr, size_t len, FILE *output); |
fb5663ca UD |
113 | static int process_fd (iconv_t cd, int fd, FILE *output); |
114 | static int process_file (iconv_t cd, FILE *input, FILE *output); | |
2bd60880 | 115 | static void print_known_names (void) internal_function; |
fb5663ca UD |
116 | |
117 | ||
118 | int | |
119 | main (int argc, char *argv[]) | |
120 | { | |
121 | int status = EXIT_SUCCESS; | |
122 | int remaining; | |
123 | FILE *output; | |
124 | iconv_t cd; | |
85830c4c | 125 | const char *orig_to_code; |
93693c4d UD |
126 | struct charmap_t *from_charmap = NULL; |
127 | struct charmap_t *to_charmap = NULL; | |
fb5663ca UD |
128 | |
129 | /* Set locale via LC_ALL. */ | |
130 | setlocale (LC_ALL, ""); | |
131 | ||
132 | /* Set the text message domain. */ | |
133 | textdomain (_libc_intl_domainname); | |
134 | ||
135 | /* Parse and process arguments. */ | |
136 | argp_parse (&argp, argc, argv, 0, &remaining, NULL); | |
137 | ||
8fe0fd03 UD |
138 | /* List all coded character sets if wanted. */ |
139 | if (list) | |
140 | { | |
141 | print_known_names (); | |
142 | exit (EXIT_SUCCESS); | |
143 | } | |
fb5663ca | 144 | |
85830c4c UD |
145 | /* If we have to ignore errors make sure we use the appropriate name for |
146 | the to-character-set. */ | |
147 | orig_to_code = to_code; | |
148 | if (omit_invalid) | |
149 | { | |
150 | const char *errhand = strchrnul (to_code, '/'); | |
151 | int nslash = 2; | |
152 | char *newp; | |
153 | char *cp; | |
154 | ||
155 | if (*errhand == '/') | |
156 | { | |
157 | --nslash; | |
158 | errhand = strchrnul (errhand, '/'); | |
159 | ||
160 | if (*errhand == '/') | |
161 | { | |
162 | --nslash; | |
b8750711 | 163 | errhand = strchr (errhand, '\0'); |
85830c4c UD |
164 | } |
165 | } | |
166 | ||
b8750711 | 167 | newp = (char *) alloca (errhand - to_code + nslash + 7 + 1); |
85830c4c | 168 | cp = mempcpy (newp, to_code, errhand - to_code); |
23054ade | 169 | while (nslash-- > 0) |
85830c4c | 170 | *cp++ = '/'; |
b8750711 UD |
171 | if (cp[-1] != '/') |
172 | *cp++ = ','; | |
c7fb46a9 | 173 | memcpy (cp, "IGNORE", sizeof ("IGNORE")); |
85830c4c UD |
174 | |
175 | to_code = newp; | |
176 | } | |
177 | ||
93693c4d UD |
178 | /* POSIX 1003.2b introduces a silly thing: the arguments to -t anf -f |
179 | can be file names of charmaps. In this case iconv will have to read | |
180 | those charmaps and use them to do the conversion. But there are | |
181 | holes in the specification. There is nothing said that if -f is a | |
182 | charmap filename that -t must be, too. And vice versa. There is | |
183 | also no word about the symbolic names used. What if they don't | |
184 | match? */ | |
185 | if (strchr (from_code, '/') != NULL) | |
186 | /* The from-name might be a charmap file name. Try reading the | |
187 | file. */ | |
188 | from_charmap = charmap_read (from_code, /*0, 1*/1, 0, 0); | |
189 | ||
190 | if (strchr (orig_to_code, '/') != NULL) | |
191 | /* The to-name might be a charmap file name. Try reading the | |
192 | file. */ | |
193 | to_charmap = charmap_read (orig_to_code, /*0, 1,*/1,0, 0); | |
194 | ||
fb5663ca UD |
195 | |
196 | /* Determine output file. */ | |
377c725f | 197 | if (output_file != NULL && strcmp (output_file, "-") != 0) |
fb5663ca UD |
198 | { |
199 | output = fopen (output_file, "w"); | |
200 | if (output == NULL) | |
201 | error (EXIT_FAILURE, errno, _("cannot open output file")); | |
202 | } | |
203 | else | |
204 | output = stdout; | |
205 | ||
93693c4d UD |
206 | /* At this point we have to handle two cases. The first one is |
207 | where a charmap is used for the from- or to-charset, or both. We | |
208 | handle this special since it is very different from the sane way of | |
209 | doing things. The other case allows converting using the iconv() | |
210 | function. */ | |
211 | if (from_charmap != NULL || to_charmap != NULL) | |
212 | /* Construct the conversion table and do the conversion. */ | |
213 | status = charmap_conversion (from_code, from_charmap, to_code, to_charmap, | |
214 | argc, remaining, argv, output); | |
fb5663ca | 215 | else |
93693c4d UD |
216 | { |
217 | /* Let's see whether we have these coded character sets. */ | |
218 | cd = iconv_open (to_code, from_code); | |
219 | if (cd == (iconv_t) -1) | |
220 | { | |
221 | if (errno == EINVAL) | |
fa00744e UD |
222 | { |
223 | /* Try to be nice with the user and tell her which of the | |
224 | two encoding names is wrong. This is possible because | |
225 | all supported encodings can be converted from/to Unicode, | |
226 | in other words, because the graph of encodings is | |
227 | connected. */ | |
228 | bool from_wrong = | |
229 | (iconv_open ("UTF-8", from_code) == (iconv_t) -1 | |
230 | && errno == EINVAL); | |
231 | bool to_wrong = | |
232 | (iconv_open (to_code, "UTF-8") == (iconv_t) -1 | |
233 | && errno == EINVAL); | |
234 | const char *from_pretty = | |
235 | (from_code[0] ? from_code : nl_langinfo (CODESET)); | |
236 | const char *to_pretty = | |
237 | (orig_to_code[0] ? orig_to_code : nl_langinfo (CODESET)); | |
238 | ||
239 | if (from_wrong) | |
240 | { | |
241 | if (to_wrong) | |
242 | error (EXIT_FAILURE, 0, | |
243 | _("\ | |
757f9fcb | 244 | conversion from `%s' and to `%s' are not supported"), |
fa00744e UD |
245 | from_pretty, to_pretty); |
246 | else | |
247 | error (EXIT_FAILURE, 0, | |
248 | _("conversion from `%s' is not supported"), | |
249 | from_pretty); | |
250 | } | |
251 | else | |
252 | { | |
253 | if (to_wrong) | |
254 | error (EXIT_FAILURE, 0, | |
255 | _("conversion to `%s' is not supported"), | |
256 | to_pretty); | |
257 | else | |
258 | error (EXIT_FAILURE, 0, | |
259 | _("conversion from `%s' to `%s' is not supported"), | |
260 | from_pretty, to_pretty); | |
261 | } | |
262 | } | |
93693c4d UD |
263 | else |
264 | error (EXIT_FAILURE, errno, | |
265 | _("failed to start conversion processing")); | |
266 | } | |
adcf0e4a | 267 | |
93693c4d UD |
268 | /* Now process the remaining files. Write them to stdout or the file |
269 | specified with the `-o' parameter. If we have no file given as | |
270 | the parameter process all from stdin. */ | |
271 | if (remaining == argc) | |
272 | { | |
273 | if (process_file (cd, stdin, output) != 0) | |
274 | status = EXIT_FAILURE; | |
275 | } | |
276 | else | |
277 | do | |
278 | { | |
d3f8be6d | 279 | #ifdef _POSIX_MAPPED_FILES |
93693c4d UD |
280 | struct stat st; |
281 | char *addr; | |
d3f8be6d | 282 | #endif |
f2fcb018 | 283 | int fd, ret; |
93693c4d UD |
284 | |
285 | if (verbose) | |
286 | printf ("%s:\n", argv[remaining]); | |
287 | if (strcmp (argv[remaining], "-") == 0) | |
288 | fd = 0; | |
289 | else | |
adcf0e4a | 290 | { |
93693c4d UD |
291 | fd = open (argv[remaining], O_RDONLY); |
292 | ||
293 | if (fd == -1) | |
294 | { | |
295 | error (0, errno, _("cannot open input file `%s'"), | |
296 | argv[remaining]); | |
297 | status = EXIT_FAILURE; | |
298 | continue; | |
299 | } | |
adcf0e4a | 300 | } |
fb5663ca | 301 | |
7cabd57c | 302 | #ifdef _POSIX_MAPPED_FILES |
93693c4d UD |
303 | /* We have possibilities for reading the input file. First try |
304 | to mmap() it since this will provide the fastest solution. */ | |
305 | if (fstat (fd, &st) == 0 | |
306 | && ((addr = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, | |
307 | fd, 0)) != MAP_FAILED)) | |
fb5663ca | 308 | { |
93693c4d UD |
309 | /* Yes, we can use mmap(). The descriptor is not needed |
310 | anymore. */ | |
311 | if (close (fd) != 0) | |
312 | error (EXIT_FAILURE, errno, | |
313 | _("error while closing input `%s'"), | |
314 | argv[remaining]); | |
315 | ||
f2fcb018 UD |
316 | ret = process_block (cd, addr, st.st_size, output); |
317 | ||
318 | /* We don't need the input data anymore. */ | |
319 | munmap ((void *) addr, st.st_size); | |
320 | ||
321 | if (ret != 0) | |
93693c4d | 322 | { |
93693c4d UD |
323 | status = EXIT_FAILURE; |
324 | ||
f2fcb018 UD |
325 | if (ret < 0) |
326 | /* We cannot go on with producing output since it might | |
327 | lead to problem because the last output might leave | |
328 | the output stream in an undefined state. */ | |
329 | break; | |
93693c4d | 330 | } |
fb5663ca | 331 | } |
93693c4d | 332 | else |
7cabd57c | 333 | #endif /* _POSIX_MAPPED_FILES */ |
fb5663ca | 334 | { |
93693c4d | 335 | /* Read the file in pieces. */ |
f2fcb018 UD |
336 | ret = process_fd (cd, fd, output); |
337 | ||
338 | /* Now close the file. */ | |
339 | close (fd); | |
340 | ||
341 | if (ret != 0) | |
93693c4d UD |
342 | { |
343 | /* Something went wrong. */ | |
344 | status = EXIT_FAILURE; | |
345 | ||
f2fcb018 UD |
346 | if (ret < 0) |
347 | /* We cannot go on with producing output since it might | |
348 | lead to problem because the last output might leave | |
349 | the output stream in an undefined state. */ | |
350 | break; | |
93693c4d | 351 | } |
fb5663ca | 352 | } |
fb5663ca | 353 | } |
93693c4d UD |
354 | while (++remaining < argc); |
355 | } | |
fb5663ca UD |
356 | |
357 | /* Close the output file now. */ | |
358 | if (fclose (output)) | |
359 | error (EXIT_FAILURE, errno, _("error while closing output file")); | |
360 | ||
361 | return status; | |
362 | } | |
363 | ||
364 | ||
365 | /* Handle program arguments. */ | |
366 | static error_t | |
367 | parse_opt (int key, char *arg, struct argp_state *state) | |
368 | { | |
369 | switch (key) | |
370 | { | |
371 | case 'f': | |
372 | from_code = arg; | |
373 | break; | |
374 | case 't': | |
375 | to_code = arg; | |
376 | break; | |
377 | case 'o': | |
378 | output_file = arg; | |
379 | break; | |
adcf0e4a UD |
380 | case 's': |
381 | /* Nothing, for now at least. We are not giving out any information | |
382 | about missing character or so. */ | |
383 | break; | |
384 | case 'c': | |
85830c4c UD |
385 | /* Omit invalid characters from output. */ |
386 | omit_invalid = 1; | |
adcf0e4a | 387 | break; |
fb5663ca UD |
388 | case OPT_VERBOSE: |
389 | verbose = 1; | |
390 | break; | |
8fe0fd03 UD |
391 | case OPT_LIST: |
392 | list = 1; | |
393 | break; | |
fb5663ca UD |
394 | default: |
395 | return ARGP_ERR_UNKNOWN; | |
396 | } | |
397 | return 0; | |
398 | } | |
399 | ||
400 | ||
401 | static char * | |
402 | more_help (int key, const char *text, void *input) | |
403 | { | |
404 | switch (key) | |
405 | { | |
406 | case ARGP_KEY_HELP_EXTRA: | |
407 | /* We print some extra information. */ | |
408 | return strdup (gettext ("\ | |
d40eb37a UD |
409 | For bug reporting instructions, please see:\n\ |
410 | <http://www.gnu.org/software/libc/bugs.html>.\n")); | |
fb5663ca UD |
411 | default: |
412 | break; | |
413 | } | |
414 | return (char *) text; | |
415 | } | |
416 | ||
417 | ||
418 | /* Print the version information. */ | |
419 | static void | |
420 | print_version (FILE *stream, struct argp_state *state) | |
421 | { | |
422 | fprintf (stream, "iconv (GNU %s) %s\n", PACKAGE, VERSION); | |
423 | fprintf (stream, gettext ("\ | |
424 | Copyright (C) %s Free Software Foundation, Inc.\n\ | |
425 | This is free software; see the source for copying conditions. There is NO\n\ | |
426 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\ | |
8e597a18 | 427 | "), "2004"); |
fb5663ca UD |
428 | fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper"); |
429 | } | |
430 | ||
431 | ||
432 | static int | |
3b434940 | 433 | process_block (iconv_t cd, char *addr, size_t len, FILE *output) |
fb5663ca UD |
434 | { |
435 | #define OUTBUF_SIZE 32768 | |
9b26f5c4 | 436 | const char *start = addr; |
fb5663ca | 437 | char outbuf[OUTBUF_SIZE]; |
8619129f UD |
438 | char *outptr; |
439 | size_t outlen; | |
440 | size_t n; | |
f2fcb018 | 441 | int ret = 0; |
fb5663ca UD |
442 | |
443 | while (len > 0) | |
444 | { | |
8619129f UD |
445 | outptr = outbuf; |
446 | outlen = OUTBUF_SIZE; | |
447 | n = iconv (cd, &addr, &len, &outptr, &outlen); | |
fb5663ca | 448 | |
f2fcb018 UD |
449 | if (n == (size_t) -1 && omit_invalid && errno == EILSEQ) |
450 | { | |
451 | ret = 1; | |
452 | if (len == 0) | |
453 | n = 0; | |
454 | else | |
455 | errno = E2BIG; | |
456 | } | |
457 | ||
fb5663ca UD |
458 | if (outptr != outbuf) |
459 | { | |
460 | /* We have something to write out. */ | |
4d3a563f UD |
461 | int errno_save = errno; |
462 | ||
6dd67bd5 UD |
463 | if (fwrite (outbuf, 1, outptr - outbuf, output) |
464 | < (size_t) (outptr - outbuf) | |
fb5663ca UD |
465 | || ferror (output)) |
466 | { | |
467 | /* Error occurred while printing the result. */ | |
468 | error (0, 0, _("\ | |
469 | conversion stopped due to problem in writing the output")); | |
470 | return -1; | |
471 | } | |
4d3a563f UD |
472 | |
473 | errno = errno_save; | |
fb5663ca UD |
474 | } |
475 | ||
476 | if (n != (size_t) -1) | |
c559a3ca UD |
477 | { |
478 | /* All the input test is processed. For state-dependent | |
479 | character sets we have to flush the state now. */ | |
480 | outptr = outbuf; | |
481 | outlen = OUTBUF_SIZE; | |
f2fcb018 | 482 | n = iconv (cd, NULL, NULL, &outptr, &outlen); |
c559a3ca UD |
483 | |
484 | if (outptr != outbuf) | |
485 | { | |
486 | /* We have something to write out. */ | |
487 | int errno_save = errno; | |
488 | ||
6dd67bd5 UD |
489 | if (fwrite (outbuf, 1, outptr - outbuf, output) |
490 | < (size_t) (outptr - outbuf) | |
c559a3ca UD |
491 | || ferror (output)) |
492 | { | |
493 | /* Error occurred while printing the result. */ | |
494 | error (0, 0, _("\ | |
495 | conversion stopped due to problem in writing the output")); | |
496 | return -1; | |
497 | } | |
498 | ||
499 | errno = errno_save; | |
500 | } | |
501 | ||
f2fcb018 UD |
502 | if (n != (size_t) -1) |
503 | break; | |
504 | ||
505 | if (omit_invalid && errno == EILSEQ) | |
506 | { | |
507 | ret = 1; | |
508 | break; | |
509 | } | |
c559a3ca | 510 | } |
fb5663ca UD |
511 | |
512 | if (errno != E2BIG) | |
513 | { | |
514 | /* iconv() ran into a problem. */ | |
515 | switch (errno) | |
516 | { | |
517 | case EILSEQ: | |
b8750711 UD |
518 | if (! omit_invalid) |
519 | error (0, 0, _("illegal input sequence at position %ld"), | |
520 | (long int) (addr - start)); | |
fb5663ca UD |
521 | break; |
522 | case EINVAL: | |
523 | error (0, 0, _("\ | |
524 | incomplete character or shift sequence at end of buffer")); | |
525 | break; | |
526 | case EBADF: | |
527 | error (0, 0, _("internal error (illegal descriptor)")); | |
528 | break; | |
529 | default: | |
530 | error (0, 0, _("unknown iconv() error %d"), errno); | |
531 | break; | |
532 | } | |
533 | ||
534 | return -1; | |
535 | } | |
536 | } | |
537 | ||
f2fcb018 | 538 | return ret; |
fb5663ca UD |
539 | } |
540 | ||
541 | ||
542 | static int | |
543 | process_fd (iconv_t cd, int fd, FILE *output) | |
544 | { | |
545 | /* we have a problem with reading from a desriptor since we must not | |
546 | provide the iconv() function an incomplete character or shift | |
547 | sequence at the end of the buffer. Since we have to deal with | |
548 | arbitrary encodings we must read the whole text in a buffer and | |
549 | process it in one step. */ | |
550 | static char *inbuf = NULL; | |
551 | static size_t maxlen = 0; | |
552 | char *inptr = NULL; | |
553 | size_t actlen = 0; | |
554 | ||
555 | while (actlen < maxlen) | |
556 | { | |
2e47aff5 | 557 | ssize_t n = read (fd, inptr, maxlen - actlen); |
fb5663ca UD |
558 | |
559 | if (n == 0) | |
560 | /* No more text to read. */ | |
561 | break; | |
562 | ||
563 | if (n == -1) | |
564 | { | |
565 | /* Error while reading. */ | |
566 | error (0, errno, _("error while reading the input")); | |
567 | return -1; | |
568 | } | |
569 | ||
570 | inptr += n; | |
571 | actlen += n; | |
572 | } | |
573 | ||
574 | if (actlen == maxlen) | |
575 | while (1) | |
576 | { | |
2e47aff5 | 577 | ssize_t n; |
d1dddedf | 578 | char *new_inbuf; |
fb5663ca UD |
579 | |
580 | /* Increase the buffer. */ | |
d1dddedf UD |
581 | new_inbuf = (char *) realloc (inbuf, maxlen + 32768); |
582 | if (new_inbuf == NULL) | |
583 | { | |
584 | error (0, errno, _("unable to allocate buffer for input")); | |
585 | return -1; | |
586 | } | |
587 | inbuf = new_inbuf; | |
fb5663ca | 588 | maxlen += 32768; |
fb5663ca UD |
589 | inptr = inbuf + actlen; |
590 | ||
591 | do | |
592 | { | |
593 | n = read (fd, inptr, maxlen - actlen); | |
594 | ||
595 | if (n == 0) | |
596 | /* No more text to read. */ | |
597 | break; | |
598 | ||
599 | if (n == -1) | |
600 | { | |
601 | /* Error while reading. */ | |
602 | error (0, errno, _("error while reading the input")); | |
603 | return -1; | |
604 | } | |
605 | ||
606 | inptr += n; | |
607 | actlen += n; | |
608 | } | |
609 | while (actlen < maxlen); | |
610 | ||
611 | if (n == 0) | |
612 | /* Break again so we leave both loops. */ | |
613 | break; | |
614 | } | |
615 | ||
616 | /* Now we have all the input in the buffer. Process it in one run. */ | |
617 | return process_block (cd, inbuf, actlen, output); | |
618 | } | |
619 | ||
620 | ||
621 | static int | |
622 | process_file (iconv_t cd, FILE *input, FILE *output) | |
623 | { | |
624 | /* This should be safe since we use this function only for `stdin' and | |
625 | we haven't read anything so far. */ | |
626 | return process_fd (cd, fileno (input), output); | |
627 | } | |
8fe0fd03 UD |
628 | |
629 | ||
630 | /* Print all known character sets/encodings. */ | |
631 | static void *printlist; | |
632 | static size_t column; | |
633 | static int not_first; | |
634 | ||
635 | static void | |
636 | insert_print_list (const void *nodep, VISIT value, int level) | |
637 | { | |
638 | if (value == leaf || value == postorder) | |
639 | { | |
640 | const struct gconv_alias *s = *(const struct gconv_alias **) nodep; | |
9b26f5c4 | 641 | tsearch (s->fromname, &printlist, (__compar_fn_t) strverscmp); |
8fe0fd03 UD |
642 | } |
643 | } | |
644 | ||
645 | static void | |
b17c0a8e | 646 | do_print_human (const void *nodep, VISIT value, int level) |
8fe0fd03 UD |
647 | { |
648 | if (value == leaf || value == postorder) | |
649 | { | |
650 | const char *s = *(const char **) nodep; | |
651 | size_t len = strlen (s); | |
652 | size_t cnt; | |
653 | ||
654 | while (len > 0 && s[len - 1] == '/') | |
655 | --len; | |
656 | ||
657 | for (cnt = 0; cnt < len; ++cnt) | |
658 | if (isalnum (s[cnt])) | |
659 | break; | |
660 | if (cnt == len) | |
661 | return; | |
662 | ||
663 | if (not_first) | |
664 | { | |
665 | putchar (','); | |
666 | ++column; | |
667 | ||
668 | if (column > 2 && column + len > 77) | |
669 | { | |
670 | fputs ("\n ", stdout); | |
671 | column = 2; | |
672 | } | |
673 | else | |
674 | { | |
675 | putchar (' '); | |
676 | ++column; | |
677 | } | |
678 | } | |
679 | else | |
9b26f5c4 | 680 | not_first = 1; |
8fe0fd03 UD |
681 | |
682 | fwrite (s, len, 1, stdout); | |
683 | column += len; | |
684 | } | |
685 | } | |
686 | ||
b17c0a8e UD |
687 | static void |
688 | do_print (const void *nodep, VISIT value, int level) | |
689 | { | |
690 | if (value == leaf || value == postorder) | |
691 | { | |
692 | const char *s = *(const char **) nodep; | |
693 | ||
694 | puts (s); | |
695 | } | |
696 | } | |
697 | ||
8fe0fd03 | 698 | static void |
2bd60880 UD |
699 | internal_function |
700 | add_known_names (struct gconv_module *node) | |
701 | { | |
702 | if (node->left != NULL) | |
703 | add_known_names (node->left); | |
704 | if (node->right != NULL) | |
705 | add_known_names (node->right); | |
2bd60880 UD |
706 | do |
707 | { | |
d2dfc5de UD |
708 | if (strcmp (node->from_string, "INTERNAL")) |
709 | tsearch (node->from_string, &printlist, | |
710 | (__compar_fn_t) strverscmp); | |
9a1f71a7 | 711 | if (strcmp (node->to_string, "INTERNAL") != 0) |
d2dfc5de | 712 | tsearch (node->to_string, &printlist, (__compar_fn_t) strverscmp); |
2bd60880 | 713 | |
d2dfc5de | 714 | node = node->same; |
2bd60880 UD |
715 | } |
716 | while (node != NULL); | |
717 | } | |
718 | ||
9a1f71a7 UD |
719 | |
720 | static void | |
721 | insert_cache (void) | |
722 | { | |
723 | const struct gconvcache_header *header; | |
724 | const char *strtab; | |
725 | const struct hash_entry *hashtab; | |
726 | size_t cnt; | |
727 | ||
230491f0 UD |
728 | header = (const struct gconvcache_header *) __gconv_get_cache (); |
729 | strtab = (char *) header + header->string_offset; | |
730 | hashtab = (struct hash_entry *) ((char *) header + header->hash_offset); | |
9a1f71a7 UD |
731 | |
732 | for (cnt = 0; cnt < header->hash_size; ++cnt) | |
733 | if (hashtab[cnt].string_offset != 0) | |
734 | { | |
735 | const char *str = strtab + hashtab[cnt].string_offset; | |
736 | ||
737 | if (strcmp (str, "INTERNAL") != 0) | |
738 | tsearch (str, &printlist, (__compar_fn_t) strverscmp); | |
739 | } | |
740 | } | |
741 | ||
742 | ||
2bd60880 UD |
743 | static void |
744 | internal_function | |
8fe0fd03 UD |
745 | print_known_names (void) |
746 | { | |
8fe0fd03 | 747 | iconv_t h; |
230491f0 | 748 | void *cache; |
8fe0fd03 UD |
749 | |
750 | /* We must initialize the internal databases first. */ | |
751 | h = iconv_open ("L1", "L1"); | |
752 | iconv_close (h); | |
753 | ||
9a1f71a7 | 754 | /* See whether we have a cache. */ |
230491f0 UD |
755 | cache = __gconv_get_cache (); |
756 | if (cache != NULL) | |
9a1f71a7 UD |
757 | /* Yep, use only this information. */ |
758 | insert_cache (); | |
759 | else | |
760 | { | |
230491f0 UD |
761 | struct gconv_module *modules; |
762 | ||
9a1f71a7 UD |
763 | /* No, then use the information read from the gconv-modules file. |
764 | First add the aliases. */ | |
230491f0 | 765 | twalk (__gconv_get_alias_db (), insert_print_list); |
8fe0fd03 | 766 | |
9a1f71a7 | 767 | /* Add the from- and to-names from the known modules. */ |
230491f0 UD |
768 | modules = __gconv_get_modules_db (); |
769 | if (modules != NULL) | |
770 | add_known_names (modules); | |
9a1f71a7 | 771 | } |
8fe0fd03 | 772 | |
1b6840e5 UD |
773 | bool human_readable = isatty (fileno (stdout)); |
774 | ||
775 | if (human_readable) | |
776 | fputs (_("\ | |
8fe0fd03 UD |
777 | The following list contain all the coded character sets known. This does\n\ |
778 | not necessarily mean that all combinations of these names can be used for\n\ | |
779 | the FROM and TO command line parameters. One coded character set can be\n\ | |
d2dfc5de | 780 | listed with several different names (aliases).\n\n "), stdout); |
8fe0fd03 UD |
781 | |
782 | /* Now print the collected names. */ | |
783 | column = 2; | |
1b6840e5 | 784 | twalk (printlist, human_readable ? do_print_human : do_print); |
8fe0fd03 | 785 | |
1b6840e5 UD |
786 | if (human_readable && column != 0) |
787 | puts (""); | |
8fe0fd03 | 788 | } |