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