2014-06-09 11:52:20 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <wchar.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2014-06-09 11:52:20 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
FILE *fp;
|
|
|
|
const char *name;
|
|
|
|
} Fdescr;
|
|
|
|
|
|
|
|
static void unexpand(Fdescr *dsc);
|
|
|
|
|
2014-11-13 15:24:47 -05:00
|
|
|
static int aflag = 0;
|
2014-06-09 11:52:20 -04: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);
|
2014-11-13 12:29:30 -05:00
|
|
|
if (tabsize <= 0)
|
2014-06-09 11:52:20 -04:00
|
|
|
eprintf("unexpand: invalid tabsize\n", argv[0]);
|
|
|
|
/* Fallthrough: -t implies -a */
|
|
|
|
case 'a':
|
2014-11-13 15:24:47 -05:00
|
|
|
aflag = 1;
|
2014-06-09 11:52:20 -04: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 18:46:04 -04:00
|
|
|
return 0;
|
2014-06-09 11:52:20 -04: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);
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
out(wint_t c)
|
|
|
|
{
|
|
|
|
putwchar(c);
|
|
|
|
if (ferror(stdout))
|
|
|
|
eprintf("write error:");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
unexpandspan(unsigned int n, unsigned int col)
|
|
|
|
{
|
|
|
|
unsigned int off = (col-n) % tabsize;
|
|
|
|
|
2014-11-13 12:29:30 -05:00
|
|
|
if (n + off >= tabsize && n > 1)
|
2014-06-09 11:52:20 -04:00
|
|
|
n += off;
|
|
|
|
|
2014-11-13 12:29:30 -05:00
|
|
|
for (; n >= tabsize; n -= tabsize)
|
2014-06-09 11:52:20 -04:00
|
|
|
out('\t');
|
2014-11-13 12:29:30 -05:00
|
|
|
while (n--)
|
2014-06-09 11:52:20 -04:00
|
|
|
out(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
unexpand(Fdescr *dsc)
|
|
|
|
{
|
|
|
|
unsigned int n = 0, col = 0;
|
2014-11-13 15:24:47 -05:00
|
|
|
int bol = 1;
|
2014-06-09 11:52:20 -04: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;
|
2014-11-13 15:24:47 -05:00
|
|
|
bol = 0;
|
2014-06-09 11:52:20 -04:00
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
if (bol || aflag)
|
|
|
|
unexpandspan(n, col);
|
|
|
|
n = col = 0;
|
2014-11-13 15:24:47 -05:00
|
|
|
bol = 1;
|
2014-06-09 11:52:20 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (bol || aflag)
|
|
|
|
unexpandspan(n, col);
|
|
|
|
n = 0;
|
|
|
|
col++;
|
2014-11-13 15:24:47 -05:00
|
|
|
bol = 0;
|
2014-06-09 11:52:20 -04:00
|
|
|
}
|
|
|
|
if ((c != ' ' && c != '\t') || (!aflag && !bol))
|
|
|
|
out(c);
|
|
|
|
}
|
|
|
|
if (n > 0 && (bol || aflag))
|
|
|
|
unexpandspan(n, col);
|
|
|
|
}
|