> Good point, here's a fixed version:

New version looks good to me.

Ethan

>
> /* Parses a subfield from the beginning of '*sp' into 'sf'.  If successful,
>  * returns NULL and advances '*sp' to the first byte following the parsed
>  * string.  On failure, returns a malloc()'d error message, does not modify
>  * '*sp', and does not properly initialize 'sf'.
>  *
>  * The syntax parsed from '*sp' takes the form "header[start..end]" where
>  * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
>  * bit indexes.  "..end" may be omitted to indicate a single bit.  
> "start..end"
>  * may both be omitted (the [] are still required) to indicate an entire
>  * field. */
> char *
> mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
> {
>    const struct mf_field *field;
>    const char *name;
>    int start, end;
>    const char *s;
>    int name_len;
>    bool wild;
>
>    s = *sp;
>    name = s;
>    name_len = strcspn(s, "[");
>    if (s[name_len] != '[') {
>        return xasprintf("%s: missing [ looking for field name", *sp);
>    }
>
>    field = mf_parse_subfield_name(name, name_len, &wild);
>    if (!field) {
>        return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
>    }
>
>    s += name_len;
>    if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
>        /* Nothing to do. */
>    } else if (sscanf(s, "[%d]", &start) == 1) {
>        end = start;
>    } else if (!strncmp(s, "[]", 2)) {
>        start = 0;
>        end = field->n_bits - 1;
>    } else {
>        return xasprintf("%s: syntax error expecting [] or [<bit>] or "
>                         "[<start>..<end>]", *sp);
>    }
>    s = strchr(s, ']') + 1;
>
>    if (start > end) {
>        return xasprintf("%s: starting bit %d is after ending bit %d",
>                         *sp, start, end);
>    } else if (start >= field->n_bits) {
>        return xasprintf("%s: starting bit %d is not valid because field is "
>                         "only %d bits wide", *sp, start, field->n_bits);
>    } else if (end >= field->n_bits){
>        return xasprintf("%s: ending bit %d is not valid because field is "
>                         "only %d bits wide", *sp, end, field->n_bits);
>    }
>
>    sf->field = field;
>    sf->ofs = start;
>    sf->n_bits = end - start + 1;
>
>    *sp = s;
>    return NULL;
> }
_______________________________________________
dev mailing list
dev@openvswitch.org
http://openvswitch.org/mailman/listinfo/dev

Reply via email to