Convert expand.c to libutf

We should librarify these chunks for now into a readrune
function.
This commit is contained in:
sin 2014-11-20 19:33:25 +01:00
parent 9a967e13f9
commit 3de6a7510d

View File

@ -1,16 +1,11 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <wchar.h>
#include "utf.h"
#include "util.h" #include "util.h"
typedef struct { static int expand(const char *, FILE *, int);
FILE *fp;
const char *name;
} Fdescr;
static int expand(Fdescr *f, int tabstop);
static int iflag = 0; static int iflag = 0;
@ -23,7 +18,6 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
Fdescr dsc;
FILE *fp; FILE *fp;
int tabstop = 8; int tabstop = 8;
@ -39,64 +33,81 @@ main(int argc, char *argv[])
} ARGEND; } ARGEND;
if (argc == 0) { if (argc == 0) {
dsc.name = "<stdin>"; expand("<stdin>", stdin, tabstop);
dsc.fp = stdin;
expand(&dsc, tabstop);
} else { } else {
for (; argc > 0; argc--, argv++) { for (; argc > 0; argc--, argv++) {
if (!(fp = fopen(*argv, "r"))) { if (!(fp = fopen(argv[0], "r"))) {
weprintf("fopen %s:", *argv); weprintf("fopen %s:", argv[0]);
continue; continue;
} }
dsc.name = *argv; expand(argv[0], fp, tabstop);
dsc.fp = fp;
expand(&dsc, tabstop);
fclose(fp); fclose(fp);
} }
} }
return 0; return 0;
} }
static wint_t int
in(Fdescr *f) in(const char *file, FILE *fp, Rune *r)
{ {
wint_t c = fgetwc(f->fp); char buf[UTFmax];
int c, i;
if (c == WEOF && ferror(f->fp)) c = fgetc(fp);
eprintf("'%s' read error:", f->name); if (ferror(fp))
eprintf("%s: read error:", file);
return c; if (feof(fp))
return 0;
if (c < Runeself) {
*r = (Rune)c;
return 1;
}
buf[0] = c;
for (i = 1; ;) {
c = fgetc(fp);
if (ferror(fp))
eprintf("%s: read error:", file);
if (feof(fp))
return 0;
buf[i++] = c;
if (fullrune(buf, i))
return chartorune(r, buf);
}
} }
static void static void
out(wint_t c) out(Rune *r)
{ {
putwchar(c); char buf[UTFmax];
if (ferror(stdout)) int len;
eprintf("write error:");
if ((len = runetochar(buf, r))) {
fwrite(buf, len, 1, stdout);
if (ferror(stdout))
eprintf("stdout: write error:");
}
} }
static int static int
expand(Fdescr *dsc, int tabstop) expand(const char *file, FILE *fp, int tabstop)
{ {
int col = 0; int col = 0;
wint_t c; Rune r;
int bol = 1; int bol = 1;
for (;;) { for (;;) {
c = in(dsc); if (!in(file, fp, &r))
if (c == WEOF)
break; break;
switch (c) { switch (r) {
case '\t': case '\t':
if (bol || !iflag) { if (bol || !iflag) {
do { do {
col++; col++;
out(' '); putchar(' ');
} while (col & (tabstop - 1)); } while (col & (tabstop - 1));
} else { } else {
out('\t'); putchar('\t');
col += tabstop - col % tabstop; col += tabstop - col % tabstop;
} }
break; break;
@ -104,18 +115,18 @@ expand(Fdescr *dsc, int tabstop)
if (col) if (col)
col--; col--;
bol = 0; bol = 0;
out(c); out(&r);
break; break;
case '\n': case '\n':
col = 0; col = 0;
bol = 1; bol = 1;
out(c); out(&r);
break; break;
default: default:
col++; col++;
if (c != ' ') if (r != ' ')
bol = 0; bol = 0;
out(c); out(&r);
break; break;
} }
} }