Home | History | Annotate | Line # | Download | only in iconv
iconv.c revision 1.10.8.2
      1 /*	$NetBSD: iconv.c,v 1.10.8.2 2008/03/23 00:49:01 matt 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("iconv.c,v 1.10.8.1 2008/01/09 02:00:43 matt Exp");
     32 #endif /* LIBC_SCCS and not lint */
     33 
     34 #include <err.h>
     35 #include <errno.h>
     36 #include <iconv.h>
     37 #include <langinfo.h>
     38 #include <stdio.h>
     39 #include <stdlib.h>
     40 #include <string.h>
     41 #include <unistd.h>
     42 #include <util.h>
     43 
     44 static void usage(void) __unused;
     45 static int scmp(const void *, const void *);
     46 static void show_codesets(void);
     47 static void do_conv(const char *, FILE *, const char *, const char *, int, int);
     48 
     49 static void
     50 usage(void)
     51 {
     52 	(void)fprintf(stderr,
     53 	    "Usage:\t%1$s [-cs] -f <from_code> -t <to_code> [file ...]\n"
     54 	    "\t%1$s -f <from_code> [-cs] [-t <to_code>] [file ...]\n"
     55 	    "\t%1$s -t <to_code> [-cs] [-f <from_code>] [file ...]\n"
     56 	    "\t%1$s -l\n", getprogname());
     57 	exit(1);
     58 }
     59 
     60 /*
     61  * qsort() helper function
     62  */
     63 static int
     64 scmp(const void *v1, const void *v2)
     65 {
     66 	const char * const *s1 = v1;
     67 	const char * const *s2 = v2;
     68 
     69 	return strcasecmp(*s1, *s2);
     70 }
     71 
     72 static void
     73 show_codesets(void)
     74 {
     75 	char **list;
     76 	size_t sz, i;
     77 
     78 	if (__iconv_get_list(&list, &sz))
     79 		err(EXIT_FAILURE, "__iconv_get_list()");
     80 
     81 	qsort(list, sz, sizeof(char *), scmp);
     82 
     83 	for (i = 0; i < sz; i++)
     84 		(void)printf("%s\n", list[i]);
     85 
     86 	__iconv_free_list(list, sz);
     87 }
     88 
     89 #define INBUFSIZE 1024
     90 #define OUTBUFSIZE (INBUFSIZE * 2)
     91 static void
     92 do_conv(const char *fn, FILE *fp, const char *from, const char *to, int silent,
     93     int hide_invalid)
     94 {
     95 	char inbuf[INBUFSIZE], outbuf[OUTBUFSIZE], *out;
     96 	const char *in;
     97 	size_t inbytes, outbytes, ret, invalids;
     98 	iconv_t cd;
     99 	u_int32_t flags = 0;
    100 
    101 	if (hide_invalid)
    102 		flags |= __ICONV_F_HIDE_INVALID;
    103 	cd = iconv_open(to, from);
    104 	if (cd == (iconv_t)-1)
    105 		err(EXIT_FAILURE, "iconv_open(%s, %s)", to, from);
    106 
    107 	invalids = 0;
    108 	while ((inbytes = fread(inbuf, 1, INBUFSIZE, fp)) > 0) {
    109 		in = inbuf;
    110 		while (inbytes > 0) {
    111 			size_t inval;
    112 
    113 			out = outbuf;
    114 			outbytes = OUTBUFSIZE;
    115 			ret = __iconv(cd, &in, &inbytes, &out, &outbytes,
    116 			    flags, &inval);
    117 			invalids += inval;
    118 			if (ret == (size_t)-1 && errno != E2BIG) {
    119 				/*
    120 				 * XXX: iconv(3) is bad interface.
    121 				 *   invalid character count is lost here.
    122 				 *   instead, we just provide __iconv function.
    123 				 */
    124 				if (errno != EINVAL || in == inbuf)
    125 					err(EXIT_FAILURE, "iconv()");
    126 
    127 				/* incomplete input character */
    128 				(void)memmove(inbuf, in, inbytes);
    129 				ret = fread(inbuf + inbytes, 1,
    130 				    INBUFSIZE - inbytes, fp);
    131 				if (ret == 0) {
    132 					if (feof(fp))
    133 						errx(EXIT_FAILURE,
    134 						     "unexpected end of file; "
    135 						     "the last character is "
    136 						     "incomplete.");
    137 					else
    138 						err(EXIT_FAILURE, "fread()");
    139 				}
    140 				in = inbuf;
    141 				inbytes += ret;
    142 			}
    143 			if (outbytes < OUTBUFSIZE)
    144 				(void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes,
    145 				    stdout);
    146 		}
    147 	}
    148 	/* reset the shift state of the output buffer */
    149 	outbytes = OUTBUFSIZE;
    150 	out = outbuf;
    151 	ret = iconv(cd, NULL, NULL, &out, &outbytes);
    152 	if (ret == -1)
    153 		err(EXIT_FAILURE, "iconv()");
    154 	if (outbytes < OUTBUFSIZE)
    155 		(void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes, stdout);
    156 
    157 	if (invalids > 0 && !silent)
    158 		warnx("warning: invalid characters: %lu",
    159 		    (unsigned long)invalids);
    160 
    161 	iconv_close(cd);
    162 }
    163 
    164 int
    165 main(int argc, char **argv)
    166 {
    167 	int ch, i;
    168 	int opt_l = 0, opt_s = 0, opt_c = 0;
    169 	char *opt_f = NULL, *opt_t = NULL;
    170 	FILE *fp;
    171 
    172 	setprogname(argv[0]);
    173 
    174 	while ((ch = getopt(argc, argv, "cslf:t:")) != EOF) {
    175 		switch (ch) {
    176 		case 'c':
    177 			opt_c = 1;
    178 			break;
    179 		case 's':
    180 			opt_s = 1;
    181 			break;
    182 		case 'l':
    183 			/* list */
    184 			opt_l = 1;
    185 			break;
    186 		case 'f':
    187 			/* from */
    188 			opt_f = estrdup(optarg);
    189 			break;
    190 		case 't':
    191 			/* to */
    192 			opt_t = estrdup(optarg);
    193 			break;
    194 		default:
    195 			usage();
    196 		}
    197 	}
    198 	argc -= optind;
    199 	argv += optind;
    200 	if (opt_l) {
    201 		if (argc > 0 || opt_s || opt_f != NULL || opt_t != NULL) {
    202 			warnx("-l is not allowed with other flags.");
    203 			usage();
    204 		}
    205 		show_codesets();
    206 	} else {
    207 		if (opt_f == NULL) {
    208 			if (opt_t == NULL)
    209 				usage();
    210 			opt_f = nl_langinfo(CODESET);
    211 		} else if (opt_t == NULL)
    212 			opt_t = nl_langinfo(CODESET);
    213 
    214 		if (argc == 0)
    215 			do_conv("<stdin>", stdin, opt_f, opt_t, opt_s, opt_c);
    216 		else {
    217 			for (i = 0; i < argc; i++) {
    218 				fp = fopen(argv[i], "r");
    219 				if (fp == NULL)
    220 					err(EXIT_FAILURE, "Cannot open `%s'",
    221 					    argv[i]);
    222 				do_conv(argv[i], fp, opt_f, opt_t, opt_s,
    223 				    opt_c);
    224 				(void)fclose(fp);
    225 			}
    226 		}
    227 	}
    228 	return EXIT_SUCCESS;
    229 }
    230