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