Example #1
0
void
get_list(char *list)
{
    size_t setautostart, start, stop;
    char *pos;
    char *p;

    /*
     * set a byte in the positions array to indicate if a field or
     * column is to be selected; use +1, it's 1-based, not 0-based.
     * This parser is less restrictive than the Draft 9 POSIX spec.
     * POSIX doesn't allow lists that aren't in increasing order or
     * overlapping lists.  We also handle "-3-5" although there's no
     * real reason too.
     */
    for (; (p = strsep(&list, ", \t")) != NULL;) {
        setautostart = start = stop = 0;
        if (*p == '-') {
            ++p;
            setautostart = 1;
        }
        if (isdigit((unsigned char)*p)) {
            start = stop = strtol(p, &p, 10);
            if (setautostart && start > autostart)
                autostart = start;
        }
        if (*p == '-') {
            if (isdigit((unsigned char)p[1]))
                stop = strtol(p + 1, &p, 10);
            if (*p == '-') {
                ++p;
                if (!autostop || autostop > stop)
                    autostop = stop;
            }
        }
        if (*p)
            errx(1, "[-cf] list: illegal list value");
        if (!stop || !start)
            errx(1, "[-cf] list: values may not include zero");
        if (maxval < stop) {
            maxval = stop;
            needpos(maxval + 1);
        }
        for (pos = positions + start; start++ <= stop; *pos++ = 1);
    }

    /* overlapping ranges */
    if (autostop && maxval > autostop) {
        maxval = autostop;
        needpos(maxval + 1);
    }

    /* set autostart */
    if (autostart)
        memset(positions + 1, '1', autostart);
}
Example #2
0
static void
get_list(char *list)
{
	size_t setautostart, start, stop;
	char *pos;
	char *p;

	/*
	 * set a byte in the positions array to indicate if a field or
	 * column is to be selected; use +1, it's 1-based, not 0-based.
	 * Numbers and number ranges may be overlapping, repeated, and in
	 * any order. We handle "-3-5" although there's no real reason to.
	 */
	for (; (p = strsep(&list, ", \t")) != NULL;) {
		setautostart = start = stop = 0;
		if (*p == '-') {
			++p;
			setautostart = 1;
		}
		if (isdigit((unsigned char)*p)) {
			start = stop = strtol(p, &p, 10);
			if (setautostart && start > autostart)
				autostart = start;
		}
		if (*p == '-') {
			if (isdigit((unsigned char)p[1]))
				stop = strtol(p + 1, &p, 10);
			if (*p == '-') {
				++p;
				if (!autostop || autostop > stop)
					autostop = stop;
			}
		}
		if (*p)
			errx(1, "[-bcf] list: illegal list value");
		if (!stop || !start)
			errx(1, "[-bcf] list: values may not include zero");
		if (maxval < stop) {
			maxval = stop;
			needpos(maxval + 1);
		}
		for (pos = positions + start; start++ <= stop; *pos++ = 1);
	}

	/* overlapping ranges */
	if (autostop && maxval > autostop) {
		maxval = autostop;
		needpos(maxval + 1);
	}

	/* set autostart */
	if (autostart)
		memset(positions + 1, '1', autostart);
}