2013-06-17 11:00:36 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-01-25 13:31:02 +00:00
|
|
|
#include <string.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-01-25 13:31:02 +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
|
|
|
|
parselist(const char *s, size_t slen)
|
2013-06-17 11:00:36 +00:00
|
|
|
{
|
2015-01-25 13:31:02 +00:00
|
|
|
size_t i, m, len;
|
|
|
|
char *sep;
|
2013-06-17 11:00:36 +00:00
|
|
|
|
2015-01-25 13:31:02 +00:00
|
|
|
if (s[0] == ',' || s[0] == ' ')
|
|
|
|
eprintf("expand: tablist can't begin with a ',' or ' '.\n");
|
|
|
|
if (s[slen - 1] == ',' || s[slen - 1] == ' ')
|
|
|
|
eprintf("expand: tablist can't end with a ',' or ' '.\n");
|
2013-06-17 11:00:36 +00:00
|
|
|
|
2015-01-25 13:31:02 +00:00
|
|
|
len = 1;
|
|
|
|
for (i = 0; i < slen; i++) {
|
|
|
|
if (s[i] == ',' || s[i] == ' ') {
|
|
|
|
if (i > 0 && (s[i - 1] == ',' || s[i - 1] == ' '))
|
|
|
|
eprintf("expand: empty field in tablist.\n");
|
|
|
|
len++;
|
2013-06-17 11:00:36 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-25 13:31:02 +00:00
|
|
|
tablist = emalloc((len + 1) * sizeof(size_t));
|
|
|
|
|
|
|
|
m = 0;
|
|
|
|
for (i = 0; i < slen; i += sep - (s + i) + 1) {
|
|
|
|
tablist[m++] = strtol(s + i, &sep, 0);
|
|
|
|
if (tablist[m - 1] == 0)
|
|
|
|
eprintf("expand: tab size can't be zero.\n");
|
|
|
|
if (*sep && *sep != ',' && *sep != ' ')
|
|
|
|
eprintf("expand: invalid number in tablist.\n");
|
|
|
|
if (m > 1 && tablist[m - 1] < tablist[m - 2])
|
|
|
|
eprintf("expand: tablist must be ascending.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tab length = 1 for the overflowing case later in the matcher */
|
|
|
|
tablist[len] = 1;
|
|
|
|
return len;
|
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-01-25 13:31:02 +00:00
|
|
|
while (readrune(file, fp, &r)) {
|
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;
|
2014-11-21 16:34:44 +00:00
|
|
|
writerune("<stdout>", stdout, &r);
|
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)
|
|
|
|
eprintf("expand: tablist cannot be empty.\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
tablistlen = parselist(tl, strlen(tl));
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|