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