xref: /freebsd/usr.bin/iconv/iconv.c (revision 79304f984ff37f7b096bbd45b900702a9cb19c3a)
1ad30f8e7SGabor Kovesdan /* $FreeBSD$ */
2ad30f8e7SGabor Kovesdan /* $NetBSD: iconv.c,v 1.16 2009/02/20 15:28:21 yamt Exp $ */
3ad30f8e7SGabor Kovesdan 
4ad30f8e7SGabor Kovesdan /*-
5ad30f8e7SGabor Kovesdan  * Copyright (c)2003 Citrus Project,
6ad30f8e7SGabor Kovesdan  * All rights reserved.
7ad30f8e7SGabor Kovesdan  *
8ad30f8e7SGabor Kovesdan  * Redistribution and use in source and binary forms, with or without
9ad30f8e7SGabor Kovesdan  * modification, are permitted provided that the following conditions
10ad30f8e7SGabor Kovesdan  * are met:
11ad30f8e7SGabor Kovesdan  * 1. Redistributions of source code must retain the above copyright
12ad30f8e7SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer.
13ad30f8e7SGabor Kovesdan  * 2. Redistributions in binary form must reproduce the above copyright
14ad30f8e7SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer in the
15ad30f8e7SGabor Kovesdan  *    documentation and/or other materials provided with the distribution.
16ad30f8e7SGabor Kovesdan  *
17ad30f8e7SGabor Kovesdan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18ad30f8e7SGabor Kovesdan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19ad30f8e7SGabor Kovesdan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20ad30f8e7SGabor Kovesdan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21ad30f8e7SGabor Kovesdan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22ad30f8e7SGabor Kovesdan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23ad30f8e7SGabor Kovesdan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24ad30f8e7SGabor Kovesdan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25ad30f8e7SGabor Kovesdan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26ad30f8e7SGabor Kovesdan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27ad30f8e7SGabor Kovesdan  * SUCH DAMAGE.
28ad30f8e7SGabor Kovesdan  */
29ad30f8e7SGabor Kovesdan 
30ad30f8e7SGabor Kovesdan #include <sys/cdefs.h>
31ad30f8e7SGabor Kovesdan 
32ad30f8e7SGabor Kovesdan #include <err.h>
33ad30f8e7SGabor Kovesdan #include <errno.h>
34ad30f8e7SGabor Kovesdan #include <getopt.h>
35ad30f8e7SGabor Kovesdan #include <iconv.h>
36ad30f8e7SGabor Kovesdan #include <limits.h>
37ad30f8e7SGabor Kovesdan #include <locale.h>
38ad30f8e7SGabor Kovesdan #include <stdbool.h>
39ad30f8e7SGabor Kovesdan #include <stdio.h>
40ad30f8e7SGabor Kovesdan #include <stdlib.h>
41ad30f8e7SGabor Kovesdan #include <string.h>
42ad30f8e7SGabor Kovesdan #include <unistd.h>
43ad30f8e7SGabor Kovesdan 
44*79304f98STijl Coosemans static int		do_conv(FILE *, const char *, const char *, bool, bool);
45ad30f8e7SGabor Kovesdan static int		do_list(unsigned int, const char * const *, void *);
46*79304f98STijl Coosemans static void		usage(void) __dead2;
47ad30f8e7SGabor Kovesdan 
48*79304f98STijl Coosemans static const struct option long_options[] = {
49ad30f8e7SGabor Kovesdan 	{"from-code",		required_argument,	NULL, 'f'},
50ad30f8e7SGabor Kovesdan 	{"list",		no_argument,		NULL, 'l'},
51ad30f8e7SGabor Kovesdan 	{"silent",		no_argument,		NULL, 's'},
52ad30f8e7SGabor Kovesdan         {"to-code",		required_argument,	NULL, 't'},
53ad30f8e7SGabor Kovesdan         {NULL,                  no_argument,            NULL, 0}
54ad30f8e7SGabor Kovesdan };
55ad30f8e7SGabor Kovesdan 
56ad30f8e7SGabor Kovesdan static void
57ad30f8e7SGabor Kovesdan usage(void)
58ad30f8e7SGabor Kovesdan {
59ad30f8e7SGabor Kovesdan 	(void)fprintf(stderr,
60ad30f8e7SGabor Kovesdan 	    "Usage:\t%1$s [-cs] -f <from_code> -t <to_code> [file ...]\n"
61ad30f8e7SGabor Kovesdan 	    "\t%1$s -f <from_code> [-cs] [-t <to_code>] [file ...]\n"
62ad30f8e7SGabor Kovesdan 	    "\t%1$s -t <to_code> [-cs] [-f <from_code>] [file ...]\n"
63ad30f8e7SGabor Kovesdan 	    "\t%1$s -l\n", getprogname());
64ad30f8e7SGabor Kovesdan 	exit(1);
65ad30f8e7SGabor Kovesdan }
66ad30f8e7SGabor Kovesdan 
67ad30f8e7SGabor Kovesdan #define INBUFSIZE 1024
68ad30f8e7SGabor Kovesdan #define OUTBUFSIZE (INBUFSIZE * 2)
69*79304f98STijl Coosemans static int
70ad30f8e7SGabor Kovesdan do_conv(FILE *fp, const char *from, const char *to, bool silent,
71ad30f8e7SGabor Kovesdan     bool hide_invalid)
72ad30f8e7SGabor Kovesdan {
73ad30f8e7SGabor Kovesdan 	iconv_t cd;
74ad30f8e7SGabor Kovesdan 	char inbuf[INBUFSIZE], outbuf[OUTBUFSIZE], *out;
75*79304f98STijl Coosemans 	unsigned long long invalids;
767900abffSPeter Wemm 	const char *in;
77ad30f8e7SGabor Kovesdan 	size_t inbytes, outbytes, ret;
78ad30f8e7SGabor Kovesdan 
79ad30f8e7SGabor Kovesdan 	if ((cd = iconv_open(to, from)) == (iconv_t)-1)
80ad30f8e7SGabor Kovesdan 		err(EXIT_FAILURE, "iconv_open(%s, %s)", to, from);
81ad30f8e7SGabor Kovesdan 
82ad30f8e7SGabor Kovesdan 	if (hide_invalid) {
83ad30f8e7SGabor Kovesdan 		int arg = 1;
84ad30f8e7SGabor Kovesdan 
85ad30f8e7SGabor Kovesdan 		if (iconvctl(cd, ICONV_SET_DISCARD_ILSEQ, (void *)&arg) == -1)
86*79304f98STijl Coosemans 			err(EXIT_FAILURE, NULL);
87ad30f8e7SGabor Kovesdan 	}
88*79304f98STijl Coosemans 	invalids = 0;
89ad30f8e7SGabor Kovesdan 	while ((inbytes = fread(inbuf, 1, INBUFSIZE, fp)) > 0) {
90ad30f8e7SGabor Kovesdan 		in = inbuf;
91ad30f8e7SGabor Kovesdan 		while (inbytes > 0) {
92ad30f8e7SGabor Kovesdan 			size_t inval;
93ad30f8e7SGabor Kovesdan 
94ad30f8e7SGabor Kovesdan 			out = outbuf;
95ad30f8e7SGabor Kovesdan 			outbytes = OUTBUFSIZE;
96ad30f8e7SGabor Kovesdan 			ret = __iconv(cd, &in, &inbytes, &out, &outbytes,
97ad30f8e7SGabor Kovesdan 			    0, &inval);
98ad30f8e7SGabor Kovesdan 			invalids += inval;
99ad30f8e7SGabor Kovesdan 			if (outbytes < OUTBUFSIZE)
100ad30f8e7SGabor Kovesdan 				(void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes,
101ad30f8e7SGabor Kovesdan 				    stdout);
102ad30f8e7SGabor Kovesdan 			if (ret == (size_t)-1 && errno != E2BIG) {
103ad30f8e7SGabor Kovesdan 				if (errno != EINVAL || in == inbuf)
104ad30f8e7SGabor Kovesdan 					err(EXIT_FAILURE, "iconv()");
105ad30f8e7SGabor Kovesdan 
106ad30f8e7SGabor Kovesdan 				/* incomplete input character */
107ad30f8e7SGabor Kovesdan 				(void)memmove(inbuf, in, inbytes);
108ad30f8e7SGabor Kovesdan 				ret = fread(inbuf + inbytes, 1,
109ad30f8e7SGabor Kovesdan 				    INBUFSIZE - inbytes, fp);
110ad30f8e7SGabor Kovesdan 				if (ret == 0) {
111ad30f8e7SGabor Kovesdan 					fflush(stdout);
112ad30f8e7SGabor Kovesdan 					if (feof(fp))
113ad30f8e7SGabor Kovesdan 						errx(EXIT_FAILURE,
114ad30f8e7SGabor Kovesdan 						    "unexpected end of file; "
115ad30f8e7SGabor Kovesdan 						    "the last character is "
116ad30f8e7SGabor Kovesdan 						    "incomplete.");
117ad30f8e7SGabor Kovesdan 					else
118ad30f8e7SGabor Kovesdan 						err(EXIT_FAILURE, "fread()");
119ad30f8e7SGabor Kovesdan 				}
120ad30f8e7SGabor Kovesdan 				in = inbuf;
121ad30f8e7SGabor Kovesdan 				inbytes += ret;
122ad30f8e7SGabor Kovesdan 			}
123ad30f8e7SGabor Kovesdan 		}
124ad30f8e7SGabor Kovesdan 	}
125ad30f8e7SGabor Kovesdan 	/* reset the shift state of the output buffer */
126ad30f8e7SGabor Kovesdan 	outbytes = OUTBUFSIZE;
127ad30f8e7SGabor Kovesdan 	out = outbuf;
128ad30f8e7SGabor Kovesdan 	ret = iconv(cd, NULL, NULL, &out, &outbytes);
129ad30f8e7SGabor Kovesdan 	if (ret == (size_t)-1)
130ad30f8e7SGabor Kovesdan 		err(EXIT_FAILURE, "iconv()");
131ad30f8e7SGabor Kovesdan 	if (outbytes < OUTBUFSIZE)
132ad30f8e7SGabor Kovesdan 		(void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes, stdout);
133ad30f8e7SGabor Kovesdan 
134ad30f8e7SGabor Kovesdan 	if (invalids > 0 && !silent)
135ad30f8e7SGabor Kovesdan 		warnx("warning: invalid characters: %llu", invalids);
136ad30f8e7SGabor Kovesdan 
137ad30f8e7SGabor Kovesdan 	iconv_close(cd);
138*79304f98STijl Coosemans 	return (invalids > 0);
139ad30f8e7SGabor Kovesdan }
140ad30f8e7SGabor Kovesdan 
141ad30f8e7SGabor Kovesdan static int
142ad30f8e7SGabor Kovesdan do_list(unsigned int n, const char * const *list, void *data __unused)
143ad30f8e7SGabor Kovesdan {
144ad30f8e7SGabor Kovesdan 	unsigned int i;
145ad30f8e7SGabor Kovesdan 
146ad30f8e7SGabor Kovesdan 	for(i = 0; i < n; i++) {
147ad30f8e7SGabor Kovesdan 		printf("%s", list[i]);
148ad30f8e7SGabor Kovesdan 		if (i < n - 1)
149ad30f8e7SGabor Kovesdan 			printf(" ");
150ad30f8e7SGabor Kovesdan 	}
151ad30f8e7SGabor Kovesdan 	printf("\n");
152ad30f8e7SGabor Kovesdan 
153ad30f8e7SGabor Kovesdan 	return (1);
154ad30f8e7SGabor Kovesdan }
155ad30f8e7SGabor Kovesdan 
156ad30f8e7SGabor Kovesdan int
157ad30f8e7SGabor Kovesdan main(int argc, char **argv)
158ad30f8e7SGabor Kovesdan {
159ad30f8e7SGabor Kovesdan 	FILE *fp;
160ad30f8e7SGabor Kovesdan 	char *opt_f, *opt_t;
161*79304f98STijl Coosemans 	int ch, i, res;
162ad30f8e7SGabor Kovesdan 	bool opt_c = false, opt_s = false;
163ad30f8e7SGabor Kovesdan 
164ad30f8e7SGabor Kovesdan 	opt_f = opt_t = strdup("");
165ad30f8e7SGabor Kovesdan 
166ad30f8e7SGabor Kovesdan 	setlocale(LC_ALL, "");
167ad30f8e7SGabor Kovesdan 	setprogname(argv[0]);
168ad30f8e7SGabor Kovesdan 
169ad30f8e7SGabor Kovesdan 	while ((ch = getopt_long(argc, argv, "csLlf:t:",
170ad30f8e7SGabor Kovesdan 	    long_options, NULL)) != -1) {
171ad30f8e7SGabor Kovesdan 		switch (ch) {
172ad30f8e7SGabor Kovesdan 		case 'c':
173ad30f8e7SGabor Kovesdan 			opt_c = true;
174ad30f8e7SGabor Kovesdan 			break;
175ad30f8e7SGabor Kovesdan 		case 's':
176ad30f8e7SGabor Kovesdan 			opt_s = true;
177ad30f8e7SGabor Kovesdan 			break;
178ad30f8e7SGabor Kovesdan 		case 'l':
179ad30f8e7SGabor Kovesdan 			/* list */
180ad30f8e7SGabor Kovesdan 			if (opt_s || opt_c || strcmp(opt_f, "") != 0 ||
181ad30f8e7SGabor Kovesdan 			    strcmp(opt_t, "") != 0) {
182ad30f8e7SGabor Kovesdan 				warnx("-l is not allowed with other flags.");
183ad30f8e7SGabor Kovesdan 				usage();
184ad30f8e7SGabor Kovesdan 			}
185ad30f8e7SGabor Kovesdan 			iconvlist(do_list, NULL);
186ad30f8e7SGabor Kovesdan 			return (EXIT_SUCCESS);
187ad30f8e7SGabor Kovesdan 		case 'f':
188ad30f8e7SGabor Kovesdan 			/* from */
189ad30f8e7SGabor Kovesdan 			if (optarg != NULL)
190ad30f8e7SGabor Kovesdan 				opt_f = strdup(optarg);
191ad30f8e7SGabor Kovesdan 			break;
192ad30f8e7SGabor Kovesdan 		case 't':
193ad30f8e7SGabor Kovesdan 			/* to */
194ad30f8e7SGabor Kovesdan 			if (optarg != NULL)
195ad30f8e7SGabor Kovesdan 				opt_t = strdup(optarg);
196ad30f8e7SGabor Kovesdan 			break;
197ad30f8e7SGabor Kovesdan 		default:
198ad30f8e7SGabor Kovesdan 			usage();
199ad30f8e7SGabor Kovesdan 		}
200ad30f8e7SGabor Kovesdan 	}
201ad30f8e7SGabor Kovesdan 	argc -= optind;
202ad30f8e7SGabor Kovesdan 	argv += optind;
203ad30f8e7SGabor Kovesdan 	if ((strcmp(opt_f, "") == 0) && (strcmp(opt_t, "") == 0))
204ad30f8e7SGabor Kovesdan 		usage();
205ad30f8e7SGabor Kovesdan 	if (argc == 0)
206*79304f98STijl Coosemans 		res = do_conv(stdin, opt_f, opt_t, opt_s, opt_c);
207ad30f8e7SGabor Kovesdan 	else {
208*79304f98STijl Coosemans 		res = 0;
209ad30f8e7SGabor Kovesdan 		for (i = 0; i < argc; i++) {
210ad30f8e7SGabor Kovesdan 			fp = (strcmp(argv[i], "-") != 0) ?
211ad30f8e7SGabor Kovesdan 			    fopen(argv[i], "r") : stdin;
212ad30f8e7SGabor Kovesdan 			if (fp == NULL)
213ad30f8e7SGabor Kovesdan 				err(EXIT_FAILURE, "Cannot open `%s'",
214ad30f8e7SGabor Kovesdan 				    argv[i]);
215*79304f98STijl Coosemans 			res |= do_conv(fp, opt_f, opt_t, opt_s, opt_c);
216ad30f8e7SGabor Kovesdan 			(void)fclose(fp);
217ad30f8e7SGabor Kovesdan 		}
218ad30f8e7SGabor Kovesdan 	}
219*79304f98STijl Coosemans 	return (res == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
220ad30f8e7SGabor Kovesdan }
221