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