SHA256
381 lines
9.3 KiB
C
381 lines
9.3 KiB
C
/*
|
|
* iconv.c
|
|
* SemiOS iconv command line utility.
|
|
*/
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <getopt.h>
|
|
#include <iconv.h>
|
|
#include <langinfo.h>
|
|
#include <locale.h>
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#define INBUF_SIZE 65536
|
|
#define OUTBUF_SIZE 65536
|
|
|
|
static const char *progname = "iconv";
|
|
static int silent_flag = 0;
|
|
static int had_error = 0;
|
|
|
|
static void diag(const char *fmt, ...) {
|
|
va_list ap;
|
|
|
|
if (silent_flag) {
|
|
return;
|
|
}
|
|
|
|
fprintf(stderr, "%s: ", progname);
|
|
va_start(ap, fmt);
|
|
vfprintf(stderr, fmt, ap);
|
|
va_end(ap);
|
|
fputc('\n', stderr);
|
|
}
|
|
|
|
static void usage(FILE *fp) {
|
|
fprintf(fp,
|
|
"Usage: %s [OPTION...] [FILE...]\n"
|
|
"Convert text from one character encoding to another.\n"
|
|
"\n"
|
|
" -f, --from-code=NAME source character set\n"
|
|
" -t, --to-code=NAME destination character set\n"
|
|
" -c, --omit-invalid omit invalid input characters\n"
|
|
" -s, --silent suppress error messages\n"
|
|
" -l, --list list supported character sets\n"
|
|
" -o, --output=FILE write output to FILE\n"
|
|
" --help display this help and exit\n"
|
|
" --version output version information and exit\n"
|
|
"\n"
|
|
"If no FILE is given, or if FILE is -, read standard input.\n"
|
|
"If -f or -t is omitted, the current locale's codeset is used.\n",
|
|
progname);
|
|
}
|
|
|
|
static int write_output(FILE *out, const char *buf, size_t len) {
|
|
if (len == 0) {
|
|
return 0;
|
|
}
|
|
if (fwrite(buf, 1, len, out) != len) {
|
|
diag("write error: %s", strerror(errno));
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int list_codesets(void) {
|
|
puts("UTF-8");
|
|
|
|
if (fflush(stdout) != 0) {
|
|
diag("write error: %s", strerror(errno));
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
struct input_source {
|
|
char **names;
|
|
int count;
|
|
int index;
|
|
int fd;
|
|
int opened;
|
|
int used_stdin;
|
|
};
|
|
|
|
/*
|
|
* Return the next block of input. Files are consumed in order and are
|
|
* treated as a single concatenated stream. Returns 0 at end of input.
|
|
*/
|
|
static ssize_t source_read(struct input_source *src, void *buf, size_t len) {
|
|
for (;;) {
|
|
if (!src->opened) {
|
|
if (src->count == 0) {
|
|
if (src->used_stdin) {
|
|
return 0;
|
|
}
|
|
src->fd = STDIN_FILENO;
|
|
src->used_stdin = 1;
|
|
src->opened = 1;
|
|
} else if (src->index < src->count) {
|
|
const char *name = src->names[src->index++];
|
|
|
|
if (strcmp(name, "-") == 0) {
|
|
src->fd = STDIN_FILENO;
|
|
} else {
|
|
src->fd = open(name, O_RDONLY);
|
|
if (src->fd < 0) {
|
|
diag("%s: %s", name, strerror(errno));
|
|
had_error = 1;
|
|
continue;
|
|
}
|
|
}
|
|
src->opened = 1;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
ssize_t n = read(src->fd, buf, len);
|
|
if (n < 0) {
|
|
if (errno == EINTR) {
|
|
continue;
|
|
}
|
|
diag("read error: %s", strerror(errno));
|
|
had_error = 1;
|
|
if (src->fd != STDIN_FILENO) {
|
|
close(src->fd);
|
|
}
|
|
src->opened = 0;
|
|
continue;
|
|
}
|
|
|
|
if (n == 0) {
|
|
if (src->fd != STDIN_FILENO) {
|
|
close(src->fd);
|
|
}
|
|
src->opened = 0;
|
|
continue;
|
|
}
|
|
|
|
return n;
|
|
}
|
|
}
|
|
|
|
static int convert_all(iconv_t cd, struct input_source *src,
|
|
int omit_invalid, FILE *out) {
|
|
char inbuf[INBUF_SIZE];
|
|
char outbuf[OUTBUF_SIZE];
|
|
size_t inlen = 0;
|
|
int eof = 0;
|
|
|
|
for (;;) {
|
|
if (!eof && inlen < sizeof(inbuf)) {
|
|
ssize_t n = source_read(src, inbuf + inlen, sizeof(inbuf) - inlen);
|
|
|
|
if (n == 0) {
|
|
eof = 1;
|
|
} else if (n > 0) {
|
|
inlen += (size_t)n;
|
|
}
|
|
}
|
|
|
|
size_t before = inlen;
|
|
char *inp = inbuf;
|
|
size_t inleft = inlen;
|
|
|
|
for (;;) {
|
|
char *outp = outbuf;
|
|
size_t outleft = sizeof(outbuf);
|
|
|
|
errno = 0;
|
|
size_t r = iconv(cd, &inp, &inleft, &outp, &outleft);
|
|
|
|
if (write_output(out, outbuf, (size_t)(outp - outbuf)) != 0) {
|
|
return 1;
|
|
}
|
|
|
|
if (r != (size_t)-1) {
|
|
break;
|
|
}
|
|
|
|
if (errno == E2BIG) {
|
|
continue;
|
|
}
|
|
|
|
if (errno == EILSEQ) {
|
|
if (omit_invalid) {
|
|
/* Skip the byte that cannot be converted. */
|
|
inp++;
|
|
inleft--;
|
|
continue;
|
|
}
|
|
diag("invalid input sequence");
|
|
return 1;
|
|
}
|
|
|
|
if (errno == EINVAL) {
|
|
/* Incomplete sequence; wait for more input. */
|
|
break;
|
|
}
|
|
|
|
diag("%s", strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
if (inleft > 0 && inp != inbuf) {
|
|
memmove(inbuf, inp, inleft);
|
|
}
|
|
inlen = inleft;
|
|
|
|
if (eof) {
|
|
if (inlen > 0) {
|
|
if (!omit_invalid) {
|
|
diag("incomplete character or shift sequence at end of input");
|
|
return 1;
|
|
}
|
|
inlen = 0;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
if (inlen == before && inlen == sizeof(inbuf)) {
|
|
diag("input buffer too small for a character");
|
|
return 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
static int flush_iconv(iconv_t cd, FILE *out) {
|
|
char outbuf[OUTBUF_SIZE];
|
|
|
|
for (;;) {
|
|
char *outp = outbuf;
|
|
size_t outleft = sizeof(outbuf);
|
|
|
|
errno = 0;
|
|
size_t r = iconv(cd, NULL, NULL, &outp, &outleft);
|
|
|
|
if (write_output(out, outbuf, (size_t)(outp - outbuf)) != 0) {
|
|
return 1;
|
|
}
|
|
|
|
if (r != (size_t)-1) {
|
|
return 0;
|
|
}
|
|
|
|
if (errno == E2BIG) {
|
|
continue;
|
|
}
|
|
|
|
diag("%s", strerror(errno));
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
static const struct option long_options[] = {
|
|
{ "from-code", required_argument, NULL, 'f' },
|
|
{ "to-code", required_argument, NULL, 't' },
|
|
{ "omit-invalid", no_argument, NULL, 'c' },
|
|
{ "silent", no_argument, NULL, 's' },
|
|
{ "list", no_argument, NULL, 'l' },
|
|
{ "output", required_argument, NULL, 'o' },
|
|
{ "help", no_argument, NULL, 'h' },
|
|
{ "version", no_argument, NULL, 'V' },
|
|
{ NULL, 0, NULL, 0 }
|
|
};
|
|
|
|
const char *from_code = NULL;
|
|
const char *to_code = NULL;
|
|
const char *output_name = NULL;
|
|
int omit_invalid = 0;
|
|
int do_list = 0;
|
|
int c;
|
|
|
|
if (argv[0] != NULL) {
|
|
const char *slash = strrchr(argv[0], '/');
|
|
progname = slash != NULL ? slash + 1 : argv[0];
|
|
}
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
while ((c = getopt_long(argc, argv, "f:t:lcso:hV",
|
|
long_options, NULL)) != -1) {
|
|
switch (c) {
|
|
case 'f':
|
|
from_code = optarg;
|
|
break;
|
|
case 't':
|
|
to_code = optarg;
|
|
break;
|
|
case 'c':
|
|
omit_invalid = 1;
|
|
break;
|
|
case 's':
|
|
silent_flag = 1;
|
|
break;
|
|
case 'l':
|
|
do_list = 1;
|
|
break;
|
|
case 'o':
|
|
output_name = optarg;
|
|
break;
|
|
case 'h':
|
|
usage(stdout);
|
|
return 0;
|
|
case 'V':
|
|
printf("%s (SemiOS) 1.0\n", progname);
|
|
return 0;
|
|
default:
|
|
usage(stderr);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
if (do_list) {
|
|
return list_codesets();
|
|
}
|
|
|
|
if (from_code == NULL) {
|
|
from_code = nl_langinfo(CODESET);
|
|
}
|
|
if (to_code == NULL) {
|
|
to_code = nl_langinfo(CODESET);
|
|
}
|
|
|
|
iconv_t cd = iconv_open(to_code, from_code);
|
|
if (cd == (iconv_t)-1) {
|
|
diag("conversion from '%s' to '%s' is not supported",
|
|
from_code, to_code);
|
|
return 1;
|
|
}
|
|
|
|
FILE *out = stdout;
|
|
if (output_name != NULL) {
|
|
out = fopen(output_name, "wb");
|
|
if (out == NULL) {
|
|
diag("%s: %s", output_name, strerror(errno));
|
|
iconv_close(cd);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
struct input_source src;
|
|
src.names = argv + optind;
|
|
src.count = argc - optind;
|
|
src.index = 0;
|
|
src.fd = -1;
|
|
src.opened = 0;
|
|
src.used_stdin = 0;
|
|
|
|
int status = convert_all(cd, &src, omit_invalid, out);
|
|
if (status == 0) {
|
|
status = flush_iconv(cd, out);
|
|
}
|
|
|
|
iconv_close(cd);
|
|
|
|
if (out == stdout) {
|
|
if (fflush(stdout) != 0) {
|
|
diag("write error: %s", strerror(errno));
|
|
status = 1;
|
|
}
|
|
} else {
|
|
if (fclose(out) != 0) {
|
|
diag("%s: %s", output_name, strerror(errno));
|
|
status = 1;
|
|
}
|
|
}
|
|
|
|
if (had_error) {
|
|
status = 1;
|
|
}
|
|
|
|
return status;
|
|
}
|