Home | History | Annotate | Line # | Download | only in iconv
iconv.c revision 1.5
      1  1.5  jdolecek /*	$NetBSD: iconv.c,v 1.5 2004/02/21 09:16:25 jdolecek 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.5  jdolecek __RCSID("$NetBSD: iconv.c,v 1.5 2004/02/21 09:16:25 jdolecek 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.5  jdolecek /*
     54  1.5  jdolecek  * qsort() helper function
     55  1.5  jdolecek  */
     56  1.5  jdolecek static int
     57  1.5  jdolecek scmp(const void *s1, const void *s2)
     58  1.5  jdolecek {
     59  1.5  jdolecek 	return strcasecmp(*(const char **)s1, *(const char **)s2);
     60  1.5  jdolecek }
     61  1.5  jdolecek 
     62  1.1  tshiozak static void
     63  1.1  tshiozak show_codesets(void)
     64  1.1  tshiozak {
     65  1.1  tshiozak 	char **list;
     66  1.1  tshiozak 	size_t sz, i;
     67  1.1  tshiozak 
     68  1.1  tshiozak 	if (__iconv_get_list(&list, &sz))
     69  1.1  tshiozak 		err(EXIT_FAILURE, "__iconv_get_list()");
     70  1.1  tshiozak 
     71  1.5  jdolecek 	qsort(list, sz, sizeof(char *), scmp);
     72  1.5  jdolecek 
     73  1.1  tshiozak 	for (i=0; i<sz; i++) {
     74  1.1  tshiozak 		printf("%s\n", list[i]);
     75  1.1  tshiozak 	}
     76  1.1  tshiozak 
     77  1.1  tshiozak 	__iconv_free_list(list, sz);
     78  1.1  tshiozak }
     79  1.1  tshiozak 
     80  1.1  tshiozak #define INBUFSIZE 1024
     81  1.1  tshiozak #define OUTBUFSIZE (INBUFSIZE*2)
     82  1.1  tshiozak static void
     83  1.1  tshiozak do_conv(const char *fn, FILE *fp, const char *from, const char *to, int silent,
     84  1.1  tshiozak 	int hide_invalid)
     85  1.1  tshiozak {
     86  1.1  tshiozak 	char inbuf[INBUFSIZE], outbuf[OUTBUFSIZE], *out;
     87  1.1  tshiozak 	const char *in;
     88  1.1  tshiozak 	size_t inbytes, outbytes, ret, invalids;
     89  1.1  tshiozak 	iconv_t cd;
     90  1.1  tshiozak 	u_int32_t flags = 0;
     91  1.1  tshiozak 
     92  1.1  tshiozak 	if (hide_invalid)
     93  1.1  tshiozak 		flags |= __ICONV_F_HIDE_INVALID;
     94  1.1  tshiozak 	cd = iconv_open(to, from);
     95  1.1  tshiozak 	if (cd == (iconv_t)-1)
     96  1.3       wiz 		err(EXIT_FAILURE, "iconv_open(%s, %s)", to, from);
     97  1.1  tshiozak 
     98  1.1  tshiozak 	invalids = 0;
     99  1.1  tshiozak 	while ((inbytes = fread(inbuf, 1, INBUFSIZE, fp)) > 0) {
    100  1.1  tshiozak 		in = inbuf;
    101  1.1  tshiozak 		while (inbytes>0) {
    102  1.4      yamt 			size_t inval;
    103  1.4      yamt 
    104  1.1  tshiozak 			out = outbuf;
    105  1.1  tshiozak 			outbytes = OUTBUFSIZE;
    106  1.1  tshiozak 			ret = __iconv(cd, &in, &inbytes, &out, &outbytes,
    107  1.4      yamt 				      flags, &inval);
    108  1.4      yamt 			invalids += inval;
    109  1.1  tshiozak 			if (ret==(size_t)-1 && errno != E2BIG) {
    110  1.1  tshiozak 				/*
    111  1.1  tshiozak 				 * XXX: iconv(3) is bad interface.
    112  1.1  tshiozak 				 *   invalid character count is lost here.
    113  1.1  tshiozak 				 *   instead, we just provide __iconv function.
    114  1.1  tshiozak 				 */
    115  1.1  tshiozak 				if (errno != EINVAL || in == inbuf)
    116  1.1  tshiozak 					err(EXIT_FAILURE, "iconv()");
    117  1.1  tshiozak 
    118  1.1  tshiozak 				/* incomplete input character */
    119  1.1  tshiozak 				memmove(inbuf, in, inbytes);
    120  1.1  tshiozak 				ret = fread(inbuf+inbytes, 1,
    121  1.1  tshiozak 					    INBUFSIZE-inbytes, fp);
    122  1.1  tshiozak 				if (ret == 0) {
    123  1.1  tshiozak 					if (feof(fp))
    124  1.1  tshiozak 						errx(EXIT_FAILURE,
    125  1.1  tshiozak 						     "iconv(): %s",
    126  1.1  tshiozak 						     strerror(EINVAL));
    127  1.1  tshiozak 					else
    128  1.1  tshiozak 						err(EXIT_FAILURE, "fread()");
    129  1.1  tshiozak 				}
    130  1.1  tshiozak 				in = inbuf;
    131  1.1  tshiozak 				inbytes += ret;
    132  1.1  tshiozak 			}
    133  1.1  tshiozak 			if (outbytes < OUTBUFSIZE)
    134  1.1  tshiozak 				fwrite(outbuf, 1, OUTBUFSIZE-outbytes, stdout);
    135  1.1  tshiozak 		}
    136  1.1  tshiozak 	}
    137  1.1  tshiozak 	/* reset the shift state of the output buffer */
    138  1.1  tshiozak 	outbytes = OUTBUFSIZE;
    139  1.1  tshiozak 	out = outbuf;
    140  1.1  tshiozak 	ret = iconv(cd, NULL, NULL, &out, &outbytes);
    141  1.1  tshiozak 	if (ret == -1)
    142  1.1  tshiozak 		err(EXIT_FAILURE, "iconv()");
    143  1.1  tshiozak 	if (outbytes < OUTBUFSIZE)
    144  1.1  tshiozak 		fwrite(outbuf, 1, OUTBUFSIZE-outbytes, stdout);
    145  1.1  tshiozak 
    146  1.1  tshiozak 	if (invalids > 0 && !silent)
    147  1.2  tshiozak 		warnx("warning: invalid characters: %lu",
    148  1.2  tshiozak 		      (unsigned long)invalids);
    149  1.1  tshiozak 
    150  1.1  tshiozak 	iconv_close(cd);
    151  1.1  tshiozak }
    152  1.1  tshiozak 
    153  1.1  tshiozak int
    154  1.1  tshiozak main(int argc, char **argv)
    155  1.1  tshiozak {
    156  1.1  tshiozak 	int ch, i;
    157  1.1  tshiozak 	extern char *optarg;
    158  1.1  tshiozak 	extern int optind;
    159  1.1  tshiozak 	int opt_l = 0, opt_s = 0, opt_c = 0;
    160  1.1  tshiozak 	char *opt_f = NULL, *opt_t = NULL;
    161  1.1  tshiozak 	FILE *fp;
    162  1.1  tshiozak 
    163  1.1  tshiozak 	while ((ch=getopt(argc, argv, "cslf:t:")) != EOF) {
    164  1.1  tshiozak 		switch (ch) {
    165  1.1  tshiozak 		case 'c':
    166  1.1  tshiozak 			opt_c = 1;
    167  1.1  tshiozak 			break;
    168  1.1  tshiozak 		case 's':
    169  1.1  tshiozak 			opt_s = 1;
    170  1.1  tshiozak 			break;
    171  1.1  tshiozak 		case 'l':
    172  1.1  tshiozak 			/* list */
    173  1.1  tshiozak 			opt_l = 1;
    174  1.1  tshiozak 			break;
    175  1.1  tshiozak 		case 'f':
    176  1.1  tshiozak 			/* from */
    177  1.1  tshiozak 			opt_f = strdup(optarg);
    178  1.1  tshiozak 			break;
    179  1.1  tshiozak 		case 't':
    180  1.1  tshiozak 			/* to */
    181  1.1  tshiozak 			opt_t = strdup(optarg);
    182  1.1  tshiozak 			break;
    183  1.1  tshiozak 		default:
    184  1.1  tshiozak 			usage();
    185  1.1  tshiozak 		}
    186  1.1  tshiozak 	}
    187  1.1  tshiozak 	argc-=optind;
    188  1.1  tshiozak 	argv+=optind;
    189  1.1  tshiozak 	if (opt_l) {
    190  1.1  tshiozak 		if (argc>0 || opt_s || opt_f != NULL || opt_t != NULL) {
    191  1.1  tshiozak 			warnx("%s: -l should be specified solely.",
    192  1.1  tshiozak 			      getprogname());
    193  1.1  tshiozak 			usage();
    194  1.1  tshiozak 		}
    195  1.1  tshiozak 		show_codesets();
    196  1.1  tshiozak 	} else {
    197  1.1  tshiozak 		if (opt_f == NULL || opt_t == NULL)
    198  1.1  tshiozak 			usage();
    199  1.1  tshiozak 
    200  1.1  tshiozak 		if (argc == 0)
    201  1.1  tshiozak 			do_conv("<stdin>", stdin, opt_f, opt_t, opt_s, opt_c);
    202  1.1  tshiozak 		else {
    203  1.1  tshiozak 			for (i=0; i<argc; i++) {
    204  1.1  tshiozak 				fp = fopen(argv[i], "r");
    205  1.1  tshiozak 				if (fp == NULL)
    206  1.1  tshiozak 					errx(EXIT_FAILURE, "%s: %s:%s",
    207  1.1  tshiozak 					     getprogname(), argv[i],
    208  1.1  tshiozak 					     strerror(errno));
    209  1.1  tshiozak 				do_conv(argv[i], fp, opt_f, opt_t, opt_s,
    210  1.1  tshiozak 					opt_c);
    211  1.1  tshiozak 				fclose(fp);
    212  1.1  tshiozak 			}
    213  1.1  tshiozak 		}
    214  1.1  tshiozak 	}
    215  1.1  tshiozak 
    216  1.1  tshiozak 	return EXIT_SUCCESS;
    217  1.1  tshiozak }
    218