readarg/readopt.c

419 lines
10 KiB
C
Raw Normal View History

2021-02-08 16:07:25 +00:00
#include <string.h>
#include <assert.h>
#include "readopt.h"
2021-03-09 18:54:03 +00:00
static void parse_arg(struct readopt_parser *rp, const char *arg);
2021-02-08 16:07:25 +00:00
2021-03-09 18:54:03 +00:00
static void parse_opt(struct readopt_parser *rp, enum readopt_form form, const char **pos);
2021-02-08 16:07:25 +00:00
2021-03-09 18:54:03 +00:00
static struct readopt_opt *match_opt(struct readopt_parser *rp, enum readopt_form form, const char **needle);
static struct readopt_opt *match_finish(struct readopt_parser *rp, const char **needle, const char *adv, struct readopt_opt *opt);
2021-02-08 16:07:25 +00:00
2021-03-09 18:54:03 +00:00
static void update_opt(struct readopt_parser *rp, const char *attach, struct readopt_opt *opt);
2021-03-02 17:25:50 +00:00
static void update_oper(struct readopt_parser *rp, struct readopt_view_strings val);
2021-02-08 16:07:25 +00:00
2021-03-02 17:25:50 +00:00
static void assign_opers(struct readopt_parser *rp);
2021-02-08 16:07:25 +00:00
2021-03-09 18:54:03 +00:00
static void add_val(struct readopt_parser *rp, struct readopt_oper *oper, const char *string, int end);
2021-02-08 16:07:25 +00:00
2021-03-09 18:54:03 +00:00
static const char *skip_incl(const char *outer, const char *inner);
2021-02-08 16:07:25 +00:00
2021-03-02 17:25:50 +00:00
static void occ_opt(struct readopt_parser *rp, struct readopt_opt *opt);
2021-02-08 16:07:25 +00:00
/* permutes the argument list to store a value for an option or operand */
2021-03-09 18:54:03 +00:00
static void permute_val(struct readopt_parser *rp, struct readopt_view_strings *target, const char *val, int end);
static void incr_between(const char **start, const char **stop, struct readopt_view_strings *curr, struct readopt_view_strings *exclude);
static void permute_rest(const char **target, struct readopt_view_strings start);
2021-02-08 16:07:25 +00:00
2021-03-02 17:25:50 +00:00
int
2021-02-08 16:07:25 +00:00
readopt_parse(struct readopt_parser *rp)
{
/* check whether the current offset is at the end of argv */
size_t off = rp->state.curr.arg - rp->args.strings;
if (off >= rp->args.len) {
2021-03-02 17:25:50 +00:00
if (rp->state.pending) {
2021-02-08 16:07:25 +00:00
/* the last specified option required an argument, but has been ignored */
2021-03-02 17:25:50 +00:00
rp->error = READOPT_ERROR_NOVAL;
return 0;
}
2021-02-08 16:07:25 +00:00
2021-03-02 17:25:50 +00:00
for (size_t i = 0; readopt_validate_opt(rp->opts + i); i++) {
if (!readopt_validate_within(&rp->opts[i].cont.oper)) {
rp->error = READOPT_ERROR_RANGEOPT;
return 0;
}
}
2021-02-08 16:07:25 +00:00
2021-03-02 17:25:50 +00:00
assign_opers(rp);
return 0;
2021-02-08 16:07:25 +00:00
}
if (rp->state.pending) {
2021-03-02 17:25:50 +00:00
add_val(rp, &rp->state.curr.opt->cont.oper, *rp->state.curr.arg, 0);
2021-02-08 16:07:25 +00:00
++rp->state.curr.arg;
2021-03-02 17:25:50 +00:00
return !rp->error;
2021-02-08 16:07:25 +00:00
}
2021-03-02 17:25:50 +00:00
parse_arg(rp, *rp->state.curr.arg);
2021-02-08 16:07:25 +00:00
/* if grouped options are still being parsed, they should not be discarded */
if (!rp->state.grppos)
++rp->state.curr.arg;
2021-03-02 17:25:50 +00:00
return !rp->error;
2021-02-08 16:07:25 +00:00
}
2021-03-02 17:25:50 +00:00
static void
2021-03-09 18:54:03 +00:00
parse_arg(struct readopt_parser *rp, const char *arg)
2021-02-08 16:07:25 +00:00
{
/* parse the next option in the grouped option string, which automatically advances it */
if (rp->state.grppos) {
2021-03-02 17:25:50 +00:00
parse_opt(rp, READOPT_FORM_SHORT, &rp->state.grppos);
2021-02-08 16:07:25 +00:00
if (!*rp->state.grppos) {
rp->state.grppos = NULL;
}
2021-03-02 17:25:50 +00:00
return;
2021-02-08 16:07:25 +00:00
}
2021-03-09 18:54:03 +00:00
const char *pos = arg;
2021-02-08 16:07:25 +00:00
switch (*pos) {
case '-':
++pos;
switch (*pos) {
case '-':
++pos;
switch (*pos) {
size_t off;
case '\0':
/* "--" denotes the end of options */
off = rp->args.len - (rp->state.curr.arg - rp->args.strings);
assert(off);
if (off == 1) {
/* no operands after the "--" */
2021-03-02 17:25:50 +00:00
return;
2021-02-08 16:07:25 +00:00
}
2021-03-02 17:25:50 +00:00
update_oper(rp, (struct readopt_view_strings){
.len = off - 1,
.strings = rp->state.curr.arg + 1
});
rp->state.curr.arg = rp->args.strings + rp->args.len - 1;
return;
2021-02-08 16:07:25 +00:00
default:
2021-03-02 17:25:50 +00:00
parse_opt(rp, READOPT_FORM_LONG, &pos);
return;
2021-02-08 16:07:25 +00:00
}
case '\0':
2021-03-09 18:54:03 +00:00
update_oper(rp, (struct readopt_view_strings){.len = 1, .strings = (const char *[]){arg}});
2021-03-02 17:25:50 +00:00
return;
2021-02-08 16:07:25 +00:00
default:
2021-03-02 17:25:50 +00:00
parse_opt(rp, READOPT_FORM_SHORT, &pos);
return;
2021-02-08 16:07:25 +00:00
}
default:
2021-03-09 18:54:03 +00:00
update_oper(rp, (struct readopt_view_strings){.len = 1, .strings = (const char *[]){arg}});
2021-03-02 17:25:50 +00:00
return;
2021-02-08 16:07:25 +00:00
}
}
2021-03-02 17:25:50 +00:00
static void
2021-03-09 18:54:03 +00:00
parse_opt(struct readopt_parser *rp, enum readopt_form form, const char **pos)
2021-02-08 16:07:25 +00:00
{
struct readopt_opt *match;
assert(form == READOPT_FORM_SHORT || form == READOPT_FORM_LONG);
if (form == READOPT_FORM_SHORT) {
match = match_opt(rp, form, pos);
if (match) {
2021-03-09 18:54:03 +00:00
const char *strpos = *pos;
2021-02-08 16:07:25 +00:00
if (!match->cont.req && *strpos) {
rp->state.grppos = strpos;
2021-03-02 17:25:50 +00:00
update_opt(rp, NULL, match);
} else {
update_opt(rp, *strpos ? strpos : NULL, match);
2021-02-08 16:07:25 +00:00
}
} else {
2021-03-02 17:25:50 +00:00
rp->error = READOPT_ERROR_NOTOPT;
2021-02-08 16:07:25 +00:00
}
} else {
/* match and advance pos to the end of the match */
match = match_opt(rp, form, pos);
if (match) {
switch (**pos) {
case '\0':
2021-03-02 17:25:50 +00:00
update_opt(rp, NULL, match);
break;
2021-02-08 16:07:25 +00:00
case '=':
++(*pos);
2021-03-02 17:25:50 +00:00
update_opt(rp, *pos, match);
break;
2021-02-08 16:07:25 +00:00
default:
2021-03-02 17:25:50 +00:00
rp->error = READOPT_ERROR_NOTOPT;
break;
2021-02-08 16:07:25 +00:00
}
} else {
2021-03-02 17:25:50 +00:00
rp->error = READOPT_ERROR_NOTOPT;
2021-02-08 16:07:25 +00:00
}
}
}
static struct readopt_opt *
2021-03-09 18:54:03 +00:00
match_opt(struct readopt_parser *rp, enum readopt_form form, const char **needle)
2021-02-08 16:07:25 +00:00
{
/* structure representing the last, inexact match */
struct {
/* the current advanced string */
2021-03-09 18:54:03 +00:00
const char *adv;
2021-02-08 16:07:25 +00:00
/* current option */
struct readopt_opt *opt;
} loose = {0};
struct readopt_opt *haystack = rp->opts;
for (size_t i = 0; readopt_validate_opt(haystack + i); i++) {
/* iterate through all names (null-terminated) of the current option with the correct form */
char **names = haystack[i].names[form];
if (!names)
/* ignore the option as it does not have names in the required form */
continue;
2021-03-09 18:54:03 +00:00
const char *cmp = loose.adv;
2021-02-08 16:07:25 +00:00
for (size_t j = 0; names[j]; j++) {
char *name = names[j];
2021-03-09 18:54:03 +00:00
cmp = skip_incl(*needle, name);
2021-02-08 16:07:25 +00:00
if (!cmp)
continue;
if (!*cmp)
/* a guaranteed match */
return match_finish(rp, needle, cmp, haystack + i);
else if ((cmp - *needle) > (loose.adv - *needle))
/* maybe a match */
loose.adv = cmp, loose.opt = haystack + i;
}
}
return match_finish(rp, needle, loose.adv, loose.opt);
}
static struct readopt_opt *
2021-03-09 18:54:03 +00:00
match_finish(struct readopt_parser *rp, const char **needle, const char *adv, struct readopt_opt *opt)
2021-02-08 16:07:25 +00:00
{
if (adv)
*needle = adv;
if (opt)
rp->state.curr.opt = opt;
return opt;
}
2021-03-02 17:25:50 +00:00
static void
2021-03-09 18:54:03 +00:00
update_opt(struct readopt_parser *rp, const char *attach, struct readopt_opt *opt)
2021-02-08 16:07:25 +00:00
{
if (opt->cont.req) {
if (attach) {
/* --opt=value, --opt=, -ovalue */
struct readopt_oper *curr = &rp->state.curr.opt->cont.oper;
2021-03-02 17:25:50 +00:00
occ_opt(rp, opt);
add_val(rp, curr, attach, 0);
2021-02-08 16:07:25 +00:00
} else {
/* --opt value, -o value */
rp->state.pending = 1;
2021-03-02 17:25:50 +00:00
occ_opt(rp, opt);
2021-02-08 16:07:25 +00:00
}
} else {
2021-03-02 17:25:50 +00:00
occ_opt(rp, opt);
if (attach)
rp->error = READOPT_ERROR_NOTREQ;
2021-02-08 16:07:25 +00:00
}
}
2021-03-02 17:25:50 +00:00
static void
2021-02-08 16:07:25 +00:00
update_oper(struct readopt_parser *rp, struct readopt_view_strings val)
{
assert(val.len && val.strings);
if (val.len == 1) {
++rp->state.curr.ioper.len;
permute_val(rp, &rp->state.curr.ioper, val.strings[0], 1);
} else {
permute_rest(rp->state.curr.eoval, val);
rp->state.curr.ioper.len += val.len;
}
}
2021-03-02 17:25:50 +00:00
static void
2021-02-08 16:07:25 +00:00
assign_opers(struct readopt_parser *rp)
{
size_t count = rp->state.curr.ioper.len;
size_t nlower = 0;
size_t nupper = 0;
for (size_t i = 0; readopt_validate_oper(rp->opers + i); i++) {
nlower += readopt_select_lower(rp->opers[i].bounds);
nupper += readopt_select_upper(rp->opers[i].bounds);
}
2021-03-02 17:25:50 +00:00
if (count < nlower) {
rp->error = READOPT_ERROR_RANGEOPER;
return;
}
2021-02-08 16:07:25 +00:00
struct {
size_t extra;
size_t req;
} rest = {
count - nlower,
nlower
};
for (size_t i = 0; readopt_validate_oper(rp->opers + i); i++) {
if (count == 0 || !rp->opers[i].val.strings) {
size_t off = count - (rest.extra + rest.req);
rp->opers[i].val.strings = rp->state.curr.ioper.strings + off;
}
size_t lower = readopt_select_lower(rp->opers[i].bounds);
size_t upper = readopt_select_upper(rp->opers[i].bounds);
int inf = rp->opers[i].bounds.inf;
size_t add;
/* add required elements */
add = rest.req > lower ? lower : rest.req;
rp->opers[i].val.len += add, rest.req -= add;
/* add optional elements */
add = inf ? rest.extra : rest.extra > upper ? upper : rest.extra;
rp->opers[i].val.len += add, rest.extra -= add;
}
2021-03-02 17:25:50 +00:00
if (rest.extra || rest.req)
rp->error = READOPT_ERROR_RANGEOPER;
2021-02-08 16:07:25 +00:00
}
2021-03-02 17:25:50 +00:00
static void
2021-03-09 18:54:03 +00:00
add_val(struct readopt_parser *rp, struct readopt_oper *oper, const char *string, int end)
2021-02-08 16:07:25 +00:00
{
rp->state.pending = 0;
if (!readopt_validate_within(oper))
2021-03-02 17:25:50 +00:00
rp->error = READOPT_ERROR_RANGEOPT;
else
permute_val(rp, &oper->val, string, end);
2021-02-08 16:07:25 +00:00
}
2021-03-09 18:54:03 +00:00
static const char *
skip_incl(const char *outer, const char *inner)
2021-02-08 16:07:25 +00:00
{
while (*inner == *outer) {
if (!*inner)
return outer;
++inner, ++outer;
}
return !*inner ? outer : NULL;
}
2021-03-02 17:25:50 +00:00
static void
2021-02-08 16:07:25 +00:00
occ_opt(struct readopt_parser *rp, struct readopt_opt *opt)
{
assert(opt);
rp->state.curr.opt = opt;
++rp->state.curr.opt->cont.oper.val.len;
}
static void
2021-03-09 18:54:03 +00:00
permute_val(struct readopt_parser *rp, struct readopt_view_strings *target, const char *val, int end)
2021-02-08 16:07:25 +00:00
{
if (!target->strings)
/* fallback position when no value has been set yet */
target->strings = rp->state.curr.eoval - (end ? 0 : rp->state.curr.ioper.len);
2021-03-09 18:54:03 +00:00
const char **pos = target->strings + (target->len - 1);
2021-02-08 16:07:25 +00:00
assert(rp->state.curr.arg >= rp->state.curr.eoval);
memmove(pos + 1, pos, (rp->state.curr.eoval - pos) * sizeof *pos);
*pos = val;
++rp->state.curr.eoval;
2021-03-09 18:54:03 +00:00
const char **start = pos, **stop = rp->state.curr.eoval;
2021-02-08 16:07:25 +00:00
2021-03-02 17:25:50 +00:00
/* increment all value pointers in the options which are between start and stop, inclusive */
2021-02-08 16:07:25 +00:00
for (size_t i = 0; readopt_validate_opt(rp->opts + i); i++)
incr_between(start, stop, &rp->opts[i].cont.oper.val, target);
incr_between(start, stop, &rp->state.curr.ioper, target);
}
static void
2021-03-09 18:54:03 +00:00
incr_between(const char **start, const char **stop, struct readopt_view_strings *curr, struct readopt_view_strings *exclude)
2021-02-08 16:07:25 +00:00
{
if (curr->strings >= start && curr->strings <= stop && curr != exclude)
++curr->strings;
}
static void
2021-03-09 18:54:03 +00:00
permute_rest(const char **target, struct readopt_view_strings start)
2021-02-08 16:07:25 +00:00
{
memmove(target, start.strings, start.len * sizeof *start.strings);
}
void
readopt_parser_init(struct readopt_parser *rp, struct readopt_opt *opts, struct readopt_oper *opers, struct readopt_view_strings args)
{
*rp = (struct readopt_parser){
.args = args,
.opts = opts,
.opers = opers,
.state.curr = {
.arg = args.strings,
.eoval = args.strings
}
};
}
int
readopt_validate_opt(struct readopt_opt *opt)
{
assert(opt);
return opt->names[0] || opt->names[1];
}
int
readopt_validate_oper(struct readopt_oper *oper)
{
assert(oper);
return !!oper->name;
}
int
readopt_validate_within(struct readopt_oper *oper)
{
size_t occ = oper->val.len;
size_t upper = readopt_select_upper(oper->bounds);
size_t lower = readopt_select_lower(oper->bounds);
return occ >= lower && (occ <= upper || oper->bounds.inf);
}
size_t
readopt_select_upper(struct readopt_bounds bounds)
{
return bounds.val[0] > bounds.val[1] ? bounds.val[0] : bounds.val[1];
}
size_t
readopt_select_lower(struct readopt_bounds bounds)
{
return bounds.inf ? readopt_select_upper(bounds) : bounds.val[0] < bounds.val[1] ? bounds.val[0] : bounds.val[1];
}