xref: /dragonfly/usr.bin/iconv/iconv.c (revision e98bdfd3)
1 /* $FreeBSD: head/usr.bin/iconv/iconv.c 252583 2013-07-03 18:27:45Z peter $ */
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 static struct option long_options[] = {
51 	{"from-code",		required_argument,	NULL, 'f'},
52 	{"list",		no_argument,		NULL, 'l'},
53 	{"silent",		no_argument,		NULL, 's'},
54         {"to-code",		required_argument,	NULL, 't'},
55         {NULL,                  no_argument,            NULL, 0}
56 };
57 
58 static void
59 usage(void)
60 {
61 	(void)fprintf(stderr,
62 	    "Usage:\t%1$s [-cs] -f <from_code> -t <to_code> [file ...]\n"
63 	    "\t%1$s -f <from_code> [-cs] [-t <to_code>] [file ...]\n"
64 	    "\t%1$s -t <to_code> [-cs] [-f <from_code>] [file ...]\n"
65 	    "\t%1$s -l\n", getprogname());
66 	exit(1);
67 }
68 
69 #define INBUFSIZE 1024
70 #define OUTBUFSIZE (INBUFSIZE * 2)
71 static void
72 do_conv(FILE *fp, const char *from, const char *to, bool silent,
73     bool hide_invalid)
74 {
75 	iconv_t cd;
76 	char inbuf[INBUFSIZE], outbuf[OUTBUFSIZE], *in, *out;
77 	size_t inbytes, outbytes, ret;
78 
79 	if ((cd = iconv_open(to, from)) == (iconv_t)-1)
80 		err(EXIT_FAILURE, "iconv_open(%s, %s)", to, from);
81 
82 	if (hide_invalid) {
83 		int arg = 1;
84 
85 		if (iconvctl(cd, ICONV_SET_DISCARD_ILSEQ, (void *)&arg) == -1)
86 			err(1, NULL);
87 	}
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 }
138 
139 static int
140 do_list(unsigned int n, const char * const *list, void *data __unused)
141 {
142 	unsigned int i;
143 
144 	for(i = 0; i < n; i++) {
145 		printf("%s", list[i]);
146 		if (i < n - 1)
147 			printf(" ");
148 	}
149 	printf("\n");
150 
151 	return (1);
152 }
153 
154 int
155 main(int argc, char **argv)
156 {
157 	FILE *fp;
158 	char *opt_f, *opt_t;
159 	int ch, i;
160 	bool opt_c = false, opt_s = false;
161 
162 	opt_f = opt_t = strdup("");
163 
164 	setlocale(LC_ALL, "");
165 	setprogname(argv[0]);
166 
167 	while ((ch = getopt_long(argc, argv, "csLlf:t:",
168 	    long_options, NULL)) != -1) {
169 		switch (ch) {
170 		case 'c':
171 			opt_c = true;
172 			break;
173 		case 's':
174 			opt_s = true;
175 			break;
176 		case 'l':
177 			/* list */
178 			if (opt_s || opt_c || strcmp(opt_f, "") != 0 ||
179 			    strcmp(opt_t, "") != 0) {
180 				warnx("-l is not allowed with other flags.");
181 				usage();
182 			}
183 			iconvlist(do_list, NULL);
184 			return (EXIT_SUCCESS);
185 		case 'f':
186 			/* from */
187 			if (optarg != NULL)
188 				opt_f = strdup(optarg);
189 			break;
190 		case 't':
191 			/* to */
192 			if (optarg != NULL)
193 				opt_t = strdup(optarg);
194 			break;
195 		default:
196 			usage();
197 		}
198 	}
199 	argc -= optind;
200 	argv += optind;
201 	if ((strcmp(opt_f, "") == 0) && (strcmp(opt_t, "") == 0))
202 		usage();
203 	if (argc == 0)
204 		do_conv(stdin, opt_f, opt_t, opt_s, opt_c);
205 	else {
206 		for (i = 0; i < argc; i++) {
207 			fp = (strcmp(argv[i], "-") != 0) ?
208 			    fopen(argv[i], "r") : stdin;
209 			if (fp == NULL)
210 				err(EXIT_FAILURE, "Cannot open `%s'",
211 				    argv[i]);
212 			do_conv(fp, opt_f, opt_t, opt_s,
213 			    opt_c);
214 			(void)fclose(fp);
215 		}
216 	}
217 	return (EXIT_SUCCESS);
218 }
219