2013-06-17 11:00:36 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-01 00:24:03 +00:00
|
|
|
#include <stdint.h>
|
2013-06-17 11:00:36 +00:00
|
|
|
#include <stdlib.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2014-11-20 18:33:25 +00:00
|
|
|
#include "utf.h"
|
2013-06-17 11:00:36 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-02-01 00:24:03 +00:00
|
|
|
static int iflag = 0;
|
|
|
|
static size_t *tablist = NULL;
|
|
|
|
static size_t tablistlen = 0;
|
2013-06-17 11:00:36 +00:00
|
|
|
|
2015-01-25 13:31:02 +00:00
|
|
|
static size_t
|
2015-01-25 18:13:32 +00:00
|
|
|
parselist(const char *s)
|
2013-06-17 11:00:36 +00:00
|
|
|
{
|
2015-01-25 18:13:32 +00:00
|
|
|
size_t i;
|
|
|
|
char *p, *tmp;
|
2013-06-17 11:00:36 +00:00
|
|
|
|
2015-01-25 18:13:32 +00:00
|
|
|
tmp = estrdup(s);
|
|
|
|
for (i = 0; (p = strsep(&tmp, " ,")); i++) {
|
|
|
|
if (*p == '\0')
|
|
|
|
eprintf("empty field in tablist\n");
|
|
|
|
tablist = erealloc(tablist, (i + 1) * sizeof(*tablist));
|
2015-02-01 00:24:03 +00:00
|
|
|
tablist[i] = estrtonum(p, 1, MIN(LLONG_MAX, SIZE_MAX));
|
2015-01-25 18:13:32 +00:00
|
|
|
if (i > 0 && tablist[i - 1] >= tablist[i])
|
|
|
|
eprintf("tablist must be ascending\n");
|
2013-06-17 11:00:36 +00:00
|
|
|
}
|
2015-01-25 18:13:32 +00:00
|
|
|
tablist = erealloc(tablist, (i + 1) * sizeof(*tablist));
|
2015-01-25 13:31:02 +00:00
|
|
|
/* tab length = 1 for the overflowing case later in the matcher */
|
2015-01-25 18:13:32 +00:00
|
|
|
tablist[i] = 1;
|
|
|
|
return i;
|
2013-06-17 11:00:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-01-25 13:31:02 +00:00
|
|
|
expand(const char *file, FILE *fp)
|
2013-06-17 11:00:36 +00:00
|
|
|
{
|
2015-01-25 13:31:02 +00:00
|
|
|
size_t bol = 1, col = 0, i;
|
2014-11-20 18:33:25 +00:00
|
|
|
Rune r;
|
2013-06-17 11:00:36 +00:00
|
|
|
|
2015-02-11 19:13:43 +00:00
|
|
|
while (efgetrune(&r, fp, file)) {
|
2014-11-20 18:33:25 +00:00
|
|
|
switch (r) {
|
2013-06-17 11:00:36 +00:00
|
|
|
case '\t':
|
2015-01-25 13:31:02 +00:00
|
|
|
if (tablistlen == 1)
|
|
|
|
i = 0;
|
|
|
|
else for (i = 0; i < tablistlen; i++)
|
|
|
|
if (col < tablist[i])
|
|
|
|
break;
|
2014-06-09 15:54:45 +00:00
|
|
|
if (bol || !iflag) {
|
|
|
|
do {
|
|
|
|
col++;
|
2014-11-20 18:33:25 +00:00
|
|
|
putchar(' ');
|
2015-01-25 13:31:02 +00:00
|
|
|
} while (col % tablist[i]);
|
2014-06-09 15:54:45 +00:00
|
|
|
} else {
|
2014-11-20 18:33:25 +00:00
|
|
|
putchar('\t');
|
2015-01-25 13:31:02 +00:00
|
|
|
col = tablist[i];
|
2014-06-09 15:54:45 +00:00
|
|
|
}
|
2013-06-17 11:00:36 +00:00
|
|
|
break;
|
|
|
|
case '\b':
|
2015-01-25 13:31:02 +00:00
|
|
|
bol = 0;
|
2013-06-17 11:00:36 +00:00
|
|
|
if (col)
|
|
|
|
col--;
|
2015-01-25 13:31:02 +00:00
|
|
|
putchar('\b');
|
2013-06-17 11:00:36 +00:00
|
|
|
break;
|
|
|
|
case '\n':
|
2014-11-13 20:24:47 +00:00
|
|
|
bol = 1;
|
2015-01-25 13:31:02 +00:00
|
|
|
col = 0;
|
|
|
|
putchar('\n');
|
2013-06-17 11:00:36 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
col++;
|
2014-11-20 18:33:25 +00:00
|
|
|
if (r != ' ')
|
2014-11-13 20:24:47 +00:00
|
|
|
bol = 0;
|
2015-02-11 19:58:00 +00:00
|
|
|
efputrune(&r, stdout, "<stdout>");
|
2013-06-17 11:00:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-01-25 13:31:02 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s [-i] [-t tablist] [file ...]\n", argv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char *tl = "8";
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'i':
|
|
|
|
iflag = 1;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
tl = EARGF(usage());
|
|
|
|
if (!*tl)
|
2015-01-25 18:13:32 +00:00
|
|
|
eprintf("tablist cannot be empty\n");
|
2015-01-25 13:31:02 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2015-01-25 18:13:32 +00:00
|
|
|
tablistlen = parselist(tl);
|
2015-01-25 13:31:02 +00:00
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
expand("<stdin>", stdin);
|
|
|
|
else {
|
|
|
|
for (; argc > 0; argc--, argv++) {
|
|
|
|
if (!(fp = fopen(argv[0], "r"))) {
|
|
|
|
weprintf("fopen %s:", argv[0]);
|
|
|
|
ret = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
expand(argv[0], fp);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|