expand: Use strsep() for parsing the tablist

This commit is contained in:
sin 2015-01-25 18:13:32 +00:00
parent bc9c752df5
commit 34c9083598

View File

@ -7,44 +7,30 @@
#include "util.h" #include "util.h"
static int iflag = 0; static int iflag = 0;
static ssize_t *tablist = NULL; static size_t *tablist = NULL;
static size_t tablistlen = 0; static size_t tablistlen = 0;
static size_t static size_t
parselist(const char *s, size_t slen) parselist(const char *s)
{ {
size_t i, m, len; size_t i;
char *sep; char *p, *tmp;
if (s[0] == ',' || s[0] == ' ') tmp = estrdup(s);
eprintf("expand: tablist can't begin with a ',' or ' '.\n"); for (i = 0; (p = strsep(&tmp, " ,")); i++) {
if (s[slen - 1] == ',' || s[slen - 1] == ' ') if (*p == '\0')
eprintf("expand: tablist can't end with a ',' or ' '.\n"); eprintf("empty field in tablist\n");
tablist = erealloc(tablist, (i + 1) * sizeof(*tablist));
len = 1; tablist[i] = estrtol(p, 10);
for (i = 0; i < slen; i++) { if (!tablist[i] || tablist[i] < 0)
if (s[i] == ',' || s[i] == ' ') { eprintf("tab field must be positive\n");
if (i > 0 && (s[i - 1] == ',' || s[i - 1] == ' ')) if (i > 0 && tablist[i - 1] >= tablist[i])
eprintf("expand: empty field in tablist.\n"); eprintf("tablist must be ascending\n");
len++;
} }
} tablist = erealloc(tablist, (i + 1) * sizeof(*tablist));
tablist = emalloc((len + 1) * sizeof(ssize_t));
m = 0;
for (i = 0; i < slen; i += sep - (s + i) + 1) {
tablist[m++] = strtol(s + i, &sep, 10);
if (tablist[m - 1] <= 0)
eprintf("expand: tab size can't be negative or 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 */ /* tab length = 1 for the overflowing case later in the matcher */
tablist[len] = 1; tablist[i] = 1;
return len; return i;
} }
static int static int
@ -114,13 +100,13 @@ main(int argc, char *argv[])
case 't': case 't':
tl = EARGF(usage()); tl = EARGF(usage());
if (!*tl) if (!*tl)
eprintf("expand: tablist cannot be empty.\n"); eprintf("tablist cannot be empty\n");
break; break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
tablistlen = parselist(tl, strlen(tl)); tablistlen = parselist(tl);
if (argc == 0) if (argc == 0)
expand("<stdin>", stdin); expand("<stdin>", stdin);