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