sbase/unexpand.c

140 lines
2.1 KiB
C
Raw Normal View History

2014-06-09 15:52:20 +00:00
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
2014-06-09 15:52:20 +00:00
#include "util.h"
typedef struct {
FILE *fp;
const char *name;
} Fdescr;
static void unexpand(Fdescr *dsc);
static int aflag = 0;
2014-06-09 15:52:20 +00:00
static int tabsize = 8;
static void
usage(void)
{
eprintf("usage: %s [-a] [-t n] [file ...]\n", argv0);
}
int
main(int argc, char *argv[])
{
Fdescr dsc;
FILE *fp;
ARGBEGIN {
case 't':
tabsize = estrtol(EARGF(usage()), 0);
if (tabsize <= 0)
2014-06-09 15:52:20 +00:00
eprintf("unexpand: invalid tabsize\n", argv[0]);
/* Fallthrough: -t implies -a */
case 'a':
aflag = 1;
2014-06-09 15:52:20 +00:00
break;
default:
usage();
} ARGEND;
if (argc == 0) {
dsc.name = "<stdin>";
dsc.fp = stdin;
unexpand(&dsc);
} else {
for (; argc > 0; argc--, argv++) {
if (!(fp = fopen(*argv, "r"))) {
weprintf("fopen %s:", *argv);
continue;
}
dsc.name = *argv;
dsc.fp = fp;
unexpand(&dsc);
fclose(fp);
}
}
2014-10-02 22:46:04 +00:00
return 0;
2014-06-09 15:52:20 +00:00
}
static wint_t
in(Fdescr *f)
{
wint_t c = fgetwc(f->fp);
if (c == WEOF && ferror(f->fp))
eprintf("%s: read error:", f->name);
2014-06-09 15:52:20 +00:00
return c;
}
static void
out(wint_t c)
{
putwchar(c);
if (ferror(stdout))
eprintf("stdout: write error:");
2014-06-09 15:52:20 +00:00
}
static void
unexpandspan(unsigned int n, unsigned int col)
{
unsigned int off = (col-n) % tabsize;
if (n + off >= tabsize && n > 1)
2014-06-09 15:52:20 +00:00
n += off;
for (; n >= tabsize; n -= tabsize)
2014-06-09 15:52:20 +00:00
out('\t');
while (n--)
2014-06-09 15:52:20 +00:00
out(' ');
}
static void
unexpand(Fdescr *dsc)
{
unsigned int n = 0, col = 0;
int bol = 1;
2014-06-09 15:52:20 +00:00
wint_t c;
while ((c = in(dsc)) != EOF) {
switch (c) {
case ' ':
if (bol || aflag)
n++;
col++;
break;
case '\t':
if (bol || aflag)
n += tabsize - col % tabsize;
col += tabsize - col % tabsize;
break;
case '\b':
if (bol || aflag)
unexpandspan(n, col);
col -= (col > 0);
n = 0;
bol = 0;
2014-06-09 15:52:20 +00:00
break;
case '\n':
if (bol || aflag)
unexpandspan(n, col);
n = col = 0;
bol = 1;
2014-06-09 15:52:20 +00:00
break;
default:
if (bol || aflag)
unexpandspan(n, col);
n = 0;
col++;
bol = 0;
2014-06-09 15:52:20 +00:00
}
if ((c != ' ' && c != '\t') || (!aflag && !bol))
out(c);
}
if (n > 0 && (bol || aflag))
unexpandspan(n, col);
}