2013-06-17 07:00:36 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-06-09 11:54:45 -04:00
|
|
|
#include <stdbool.h>
|
2013-06-17 07:00:36 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <wchar.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2013-06-17 07:00:36 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
FILE *fp;
|
|
|
|
const char *name;
|
|
|
|
} Fdescr;
|
|
|
|
|
|
|
|
static int expand(Fdescr *f, int tabstop);
|
|
|
|
|
2014-06-09 11:54:45 -04:00
|
|
|
static bool iflag = false;
|
|
|
|
|
2013-06-17 07:00:36 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2013-08-15 05:55:21 -04:00
|
|
|
eprintf("usage: %s [-i] [-t n] [file...]\n", argv0);
|
2013-06-17 07:00:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
Fdescr dsc;
|
|
|
|
FILE *fp;
|
|
|
|
int tabstop = 8;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
2013-08-15 05:55:21 -04:00
|
|
|
case 'i':
|
2014-06-09 11:54:45 -04:00
|
|
|
iflag = true;
|
|
|
|
break;
|
2013-06-17 07:00:36 -04:00
|
|
|
case 't':
|
|
|
|
tabstop = estrtol(EARGF(usage()), 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if (argc == 0) {
|
|
|
|
dsc.name = "<stdin>";
|
|
|
|
dsc.fp = stdin;
|
|
|
|
expand(&dsc, tabstop);
|
|
|
|
} else {
|
2014-06-09 11:53:55 -04:00
|
|
|
for (; argc > 0; argc--, argv++) {
|
2013-06-17 07:00:36 -04:00
|
|
|
if (!(fp = fopen(*argv, "r"))) {
|
2013-11-13 06:39:24 -05:00
|
|
|
weprintf("fopen %s:", *argv);
|
|
|
|
continue;
|
2013-06-17 07:00:36 -04:00
|
|
|
}
|
|
|
|
dsc.name = *argv;
|
|
|
|
dsc.fp = fp;
|
|
|
|
expand(&dsc, tabstop);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
2014-10-02 18:46:04 -04:00
|
|
|
return 0;
|
2013-06-17 07:00:36 -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 int
|
|
|
|
expand(Fdescr *dsc, int tabstop)
|
|
|
|
{
|
|
|
|
int col = 0;
|
|
|
|
wint_t c;
|
2014-06-09 11:54:45 -04:00
|
|
|
bool bol = true;
|
2013-06-17 07:00:36 -04:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
c = in(dsc);
|
|
|
|
if (c == WEOF)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case '\t':
|
2014-06-09 11:54:45 -04:00
|
|
|
if (bol || !iflag) {
|
|
|
|
do {
|
|
|
|
col++;
|
|
|
|
out(' ');
|
|
|
|
} while (col & (tabstop - 1));
|
|
|
|
} else {
|
|
|
|
out('\t');
|
|
|
|
col += tabstop - col % tabstop;
|
|
|
|
}
|
2013-06-17 07:00:36 -04:00
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
if (col)
|
|
|
|
col--;
|
2014-06-09 11:54:45 -04:00
|
|
|
bol = false;
|
2013-06-17 07:00:36 -04:00
|
|
|
out(c);
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
col = 0;
|
2014-06-09 11:54:45 -04:00
|
|
|
bol = true;
|
2013-06-17 07:00:36 -04:00
|
|
|
out(c);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
col++;
|
2014-06-09 11:54:45 -04:00
|
|
|
if (c != ' ')
|
|
|
|
bol = false;
|
2013-06-17 07:00:36 -04:00
|
|
|
out(c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|