expand: Use strsep() for parsing the tablist
This commit is contained in:
parent
bc9c752df5
commit
34c9083598
52
expand.c
52
expand.c
@ -7,44 +7,30 @@
|
||||
#include "util.h"
|
||||
|
||||
static int iflag = 0;
|
||||
static ssize_t *tablist = NULL;
|
||||
static size_t *tablist = NULL;
|
||||
static size_t tablistlen = 0;
|
||||
|
||||
static size_t
|
||||
parselist(const char *s, size_t slen)
|
||||
parselist(const char *s)
|
||||
{
|
||||
size_t i, m, len;
|
||||
char *sep;
|
||||
size_t i;
|
||||
char *p, *tmp;
|
||||
|
||||
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");
|
||||
|
||||
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++;
|
||||
}
|
||||
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));
|
||||
tablist[i] = estrtol(p, 10);
|
||||
if (!tablist[i] || tablist[i] < 0)
|
||||
eprintf("tab field must be positive\n");
|
||||
if (i > 0 && tablist[i - 1] >= tablist[i])
|
||||
eprintf("tablist must be ascending\n");
|
||||
}
|
||||
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");
|
||||
}
|
||||
|
||||
tablist = erealloc(tablist, (i + 1) * sizeof(*tablist));
|
||||
/* tab length = 1 for the overflowing case later in the matcher */
|
||||
tablist[len] = 1;
|
||||
return len;
|
||||
tablist[i] = 1;
|
||||
return i;
|
||||
}
|
||||
|
||||
static int
|
||||
@ -114,13 +100,13 @@ main(int argc, char *argv[])
|
||||
case 't':
|
||||
tl = EARGF(usage());
|
||||
if (!*tl)
|
||||
eprintf("expand: tablist cannot be empty.\n");
|
||||
eprintf("tablist cannot be empty\n");
|
||||
break;
|
||||
default:
|
||||
usage();
|
||||
} ARGEND;
|
||||
|
||||
tablistlen = parselist(tl, strlen(tl));
|
||||
tablistlen = parselist(tl);
|
||||
|
||||
if (argc == 0)
|
||||
expand("<stdin>", stdin);
|
||||
|
Loading…
Reference in New Issue
Block a user