xref: /freebsd/usr.bin/iconv/iconv.c (revision 9a14aa017b21c292740c00ee098195cd46642730)
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 unsigned long long invalids;
45 
46 static void		 do_conv(FILE *, const char *, const char *, bool, bool);
47 static int		 do_list(unsigned int, const char * const *, void *);
48 static void		 usage(void);
49 
50 struct option long_options[] =
51 {
52 	{"from-code",		required_argument,	NULL, 'f'},
53 	{"list",		no_argument,		NULL, 'l'},
54 	{"silent",		no_argument,		NULL, 's'},
55         {"to-code",		required_argument,	NULL, 't'},
56         {NULL,                  no_argument,            NULL, 0}
57 };
58 
59 static void
60 usage(void)
61 {
62 	(void)fprintf(stderr,
63 	    "Usage:\t%1$s [-cs] -f <from_code> -t <to_code> [file ...]\n"
64 	    "\t%1$s -f <from_code> [-cs] [-t <to_code>] [file ...]\n"
65 	    "\t%1$s -t <to_code> [-cs] [-f <from_code>] [file ...]\n"
66 	    "\t%1$s -l\n", getprogname());
67 	exit(1);
68 }
69 
70 #define INBUFSIZE 1024
71 #define OUTBUFSIZE (INBUFSIZE * 2)
72 static void
73 do_conv(FILE *fp, const char *from, const char *to, bool silent,
74     bool hide_invalid)
75 {
76 	iconv_t cd;
77 	char inbuf[INBUFSIZE], outbuf[OUTBUFSIZE], *out;
78 	char *in;
79 	size_t inbytes, outbytes, ret;
80 
81 	if ((cd = iconv_open(to, from)) == (iconv_t)-1)
82 		err(EXIT_FAILURE, "iconv_open(%s, %s)", to, from);
83 
84 	if (hide_invalid) {
85 		int arg = 1;
86 
87 		if (iconvctl(cd, ICONV_SET_DISCARD_ILSEQ, (void *)&arg) == -1)
88 			err(1, NULL);
89 	}
90 	while ((inbytes = fread(inbuf, 1, INBUFSIZE, fp)) > 0) {
91 		in = inbuf;
92 		while (inbytes > 0) {
93 			size_t inval;
94 
95 			out = outbuf;
96 			outbytes = OUTBUFSIZE;
97 			ret = __iconv(cd, &in, &inbytes, &out, &outbytes,
98 			    0, &inval);
99 			invalids += inval;
100 			if (outbytes < OUTBUFSIZE)
101 				(void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes,
102 				    stdout);
103 			if (ret == (size_t)-1 && errno != E2BIG) {
104 				if (errno != EINVAL || in == inbuf)
105 					err(EXIT_FAILURE, "iconv()");
106 
107 				/* incomplete input character */
108 				(void)memmove(inbuf, in, inbytes);
109 				ret = fread(inbuf + inbytes, 1,
110 				    INBUFSIZE - inbytes, fp);
111 				if (ret == 0) {
112 					fflush(stdout);
113 					if (feof(fp))
114 						errx(EXIT_FAILURE,
115 						    "unexpected end of file; "
116 						    "the last character is "
117 						    "incomplete.");
118 					else
119 						err(EXIT_FAILURE, "fread()");
120 				}
121 				in = inbuf;
122 				inbytes += ret;
123 			}
124 		}
125 	}
126 	/* reset the shift state of the output buffer */
127 	outbytes = OUTBUFSIZE;
128 	out = outbuf;
129 	ret = iconv(cd, NULL, NULL, &out, &outbytes);
130 	if (ret == (size_t)-1)
131 		err(EXIT_FAILURE, "iconv()");
132 	if (outbytes < OUTBUFSIZE)
133 		(void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes, stdout);
134 
135 	if (invalids > 0 && !silent)
136 		warnx("warning: invalid characters: %llu", invalids);
137 
138 	iconv_close(cd);
139 }
140 
141 static int
142 do_list(unsigned int n, const char * const *list, void *data __unused)
143 {
144 	unsigned int i;
145 
146 	for(i = 0; i < n; i++) {
147 		printf("%s", list[i]);
148 		if (i < n - 1)
149 			printf(" ");
150 	}
151 	printf("\n");
152 
153 	return (1);
154 }
155 
156 int
157 main(int argc, char **argv)
158 {
159 	FILE *fp;
160 	char *opt_f, *opt_t;
161 	int ch, i;
162 	bool opt_c = false, opt_s = false;
163 
164 	opt_f = opt_t = strdup("");
165 
166 	setlocale(LC_ALL, "");
167 	setprogname(argv[0]);
168 
169 	while ((ch = getopt_long(argc, argv, "csLlf:t:",
170 	    long_options, NULL)) != -1) {
171 		switch (ch) {
172 		case 'c':
173 			opt_c = true;
174 			break;
175 		case 's':
176 			opt_s = true;
177 			break;
178 		case 'l':
179 			/* list */
180 			if (opt_s || opt_c || strcmp(opt_f, "") != 0 ||
181 			    strcmp(opt_t, "") != 0) {
182 				warnx("-l is not allowed with other flags.");
183 				usage();
184 			}
185 			iconvlist(do_list, NULL);
186 			return (EXIT_SUCCESS);
187 		case 'f':
188 			/* from */
189 			if (optarg != NULL)
190 				opt_f = strdup(optarg);
191 			break;
192 		case 't':
193 			/* to */
194 			if (optarg != NULL)
195 				opt_t = strdup(optarg);
196 			break;
197 		default:
198 			usage();
199 		}
200 	}
201 	argc -= optind;
202 	argv += optind;
203 	if ((strcmp(opt_f, "") == 0) && (strcmp(opt_t, "") == 0))
204 		usage();
205 	if (argc == 0)
206 		do_conv(stdin, opt_f, opt_t, opt_s, opt_c);
207 	else {
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 			do_conv(fp, opt_f, opt_t, opt_s,
215 			    opt_c);
216 			(void)fclose(fp);
217 		}
218 	}
219 	return (EXIT_SUCCESS);
220 }
221