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