[hackers] [sbase] Convert unexpand(1) to libutf || sin
commit cd35347203fbce8c8f1b4fd69102326e684e4f5e
Author: sin <sin_AT_2f30.org>
Date: Fri Nov 21 17:52:09 2014 +0000
Convert unexpand(1) to libutf
diff --git a/unexpand.c b/unexpand.c
index 48b0fb6..f282781 100644
--- a/unexpand.c
+++ b/unexpand.c
_AT_@ -3,14 +3,10 @@
#include <stdlib.h>
#include <wchar.h>
+#include "utf.h"
#include "util.h"
-typedef struct {
- FILE *fp;
- const char *name;
-} Fdescr;
-
-static void unexpand(Fdescr *dsc);
+static void unexpand(const char *, FILE *);
static int aflag = 0;
static int tabsize = 8;
_AT_@ -24,8 +20,8 @@ usage(void)
int
main(int argc, char *argv[])
{
- Fdescr dsc;
FILE *fp;
+ int ret = 0;
ARGBEGIN {
case 't':
_AT_@ -41,66 +37,50 @@ main(int argc, char *argv[])
} ARGEND;
if (argc == 0) {
- dsc.name = "<stdin>";
- dsc.fp = stdin;
- unexpand(&dsc);
+ unexpand("<stdin>", stdin);
} else {
for (; argc > 0; argc--, argv++) {
- if (!(fp = fopen(*argv, "r"))) {
- weprintf("fopen %s:", *argv);
+ if (!(fp = fopen(argv[0], "r"))) {
+ weprintf("fopen %s:", argv[0]);
+ ret = 1;
continue;
}
- dsc.name = *argv;
- dsc.fp = fp;
- unexpand(&dsc);
+ unexpand(argv[0], fp);
fclose(fp);
}
}
-
- return 0;
-}
-
-static wint_t
-in(Fdescr *f)
-{
- wint_t c = fgetwc(f->fp);
-
- if (c == WEOF && ferror(f->fp))
- eprintf("%s: read error:", f->name);
- return c;
-}
-
-static void
-out(wint_t c)
-{
- putwchar(c);
- if (ferror(stdout))
- eprintf("stdout: write error:");
+ return ret;
}
static void
unexpandspan(unsigned int n, unsigned int col)
{
unsigned int off = (col-n) % tabsize;
+ Rune r;
if (n + off >= tabsize && n > 1)
n += off;
+ r = '\t';
for (; n >= tabsize; n -= tabsize)
- out('\t');
+ writerune("<stdout>", stdout, &r);
+ r = ' ';
while (n--)
- out(' ');
+ writerune("<stdout>", stdout, &r);
}
static void
-unexpand(Fdescr *dsc)
+unexpand(const char *file, FILE *fp)
{
unsigned int n = 0, col = 0;
+ Rune r;
int bol = 1;
- wint_t c;
- while ((c = in(dsc)) != EOF) {
- switch (c) {
+ while (1) {
+ if (!readrune(file, fp, &r))
+ break;
+
+ switch (r) {
case ' ':
if (bol || aflag)
n++;
_AT_@ -131,8 +111,8 @@ unexpand(Fdescr *dsc)
col++;
bol = 0;
}
- if ((c != ' ' && c != '\t') || (!aflag && !bol))
- out(c);
+ if ((r != ' ' && r != '\t') || (!aflag && !bol))
+ writerune("<stdout>", stdout, &r);
}
if (n > 0 && (bol || aflag))
unexpandspan(n, col);
Received on Fri Nov 21 2014 - 18:52:26 CET
This archive was generated by hypermail 2.3.0
: Fri Nov 21 2014 - 19:00:11 CET