prefix "private" functions
This commit is contained in:
parent
28cf939964
commit
a294c94716
106
readopt.h
106
readopt.h
|
@ -105,27 +105,27 @@ size_t readopt_select_lower(struct readopt_bounds bounds);
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static void parse_arg(struct readopt_parser *rp, const char *arg);
|
static void readopt_parse_arg(struct readopt_parser *rp, const char *arg);
|
||||||
|
|
||||||
static void parse_opt(struct readopt_parser *rp, enum readopt_form form, const char **pos);
|
static void readopt_parse_opt(struct readopt_parser *rp, enum readopt_form form, const char **pos);
|
||||||
|
|
||||||
static struct readopt_opt *match_opt(struct readopt_parser *rp, enum readopt_form form, const char **needle);
|
static struct readopt_opt *readopt_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);
|
static struct readopt_opt *readopt_match_finish(struct readopt_parser *rp, const char **needle, const char *adv, struct readopt_opt *opt);
|
||||||
|
|
||||||
static void update_opt(struct readopt_parser *rp, const char *attach, struct readopt_opt *opt);
|
static void readopt_update_opt(struct readopt_parser *rp, const char *attach, struct readopt_opt *opt);
|
||||||
static void update_oper(struct readopt_parser *rp, struct readopt_view_strings val);
|
static void readopt_update_oper(struct readopt_parser *rp, struct readopt_view_strings val);
|
||||||
|
|
||||||
static void assign_opers(struct readopt_parser *rp);
|
static void readopt_assign_opers(struct readopt_parser *rp);
|
||||||
|
|
||||||
static void add_val(struct readopt_parser *rp, struct readopt_oper *oper, const char *string, int end);
|
static void readopt_add_val(struct readopt_parser *rp, struct readopt_oper *oper, const char *string, int end);
|
||||||
|
|
||||||
static const char *skip_incl(const char *outer, const char *inner);
|
static const char *readopt_skip_incl(const char *outer, const char *inner);
|
||||||
|
|
||||||
static void occ_opt(struct readopt_parser *rp, struct readopt_opt *opt);
|
static void readopt_occ_opt(struct readopt_parser *rp, struct readopt_opt *opt);
|
||||||
|
|
||||||
static void permute_val(struct readopt_parser *rp, struct readopt_view_strings *target, const char *val, int end);
|
static void readopt_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 readopt_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);
|
static void readopt_permute_rest(const char **target, struct readopt_view_strings start);
|
||||||
|
|
||||||
int readopt_parse(struct readopt_parser *rp)
|
int readopt_parse(struct readopt_parser *rp)
|
||||||
{
|
{
|
||||||
|
@ -149,18 +149,18 @@ int readopt_parse(struct readopt_parser *rp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assign_opers(rp);
|
readopt_assign_opers(rp);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rp->state.pending)
|
if (rp->state.pending)
|
||||||
{
|
{
|
||||||
add_val(rp, &rp->state.curr.opt->cont.oper, *rp->state.curr.arg, 0);
|
readopt_add_val(rp, &rp->state.curr.opt->cont.oper, *rp->state.curr.arg, 0);
|
||||||
++rp->state.curr.arg;
|
++rp->state.curr.arg;
|
||||||
return !rp->error;
|
return !rp->error;
|
||||||
}
|
}
|
||||||
|
|
||||||
parse_arg(rp, *rp->state.curr.arg);
|
readopt_parse_arg(rp, *rp->state.curr.arg);
|
||||||
|
|
||||||
/* If grouped options are still being parsed, they should not be discarded. */
|
/* If grouped options are still being parsed, they should not be discarded. */
|
||||||
if (!rp->state.grppos)
|
if (!rp->state.grppos)
|
||||||
|
@ -169,12 +169,12 @@ int readopt_parse(struct readopt_parser *rp)
|
||||||
return !rp->error;
|
return !rp->error;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void parse_arg(struct readopt_parser *rp, const char *arg)
|
static void readopt_parse_arg(struct readopt_parser *rp, const char *arg)
|
||||||
{
|
{
|
||||||
/* Parse the next option in the grouped option string, which automatically advances it. */
|
/* Parse the next option in the grouped option string, which automatically advances it. */
|
||||||
if (rp->state.grppos)
|
if (rp->state.grppos)
|
||||||
{
|
{
|
||||||
parse_opt(rp, READOPT_FORM_SHORT, &rp->state.grppos);
|
readopt_parse_opt(rp, READOPT_FORM_SHORT, &rp->state.grppos);
|
||||||
if (!*rp->state.grppos)
|
if (!*rp->state.grppos)
|
||||||
{
|
{
|
||||||
rp->state.grppos = NULL;
|
rp->state.grppos = NULL;
|
||||||
|
@ -205,37 +205,37 @@ static void parse_arg(struct readopt_parser *rp, const char *arg)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
update_oper(rp, (struct readopt_view_strings){
|
readopt_update_oper(rp, (struct readopt_view_strings){
|
||||||
.len = off - 1,
|
.len = off - 1,
|
||||||
.strings = rp->state.curr.arg + 1});
|
.strings = rp->state.curr.arg + 1});
|
||||||
rp->state.curr.arg = rp->args.strings + rp->args.len - 1;
|
rp->state.curr.arg = rp->args.strings + rp->args.len - 1;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
default:
|
default:
|
||||||
parse_opt(rp, READOPT_FORM_LONG, &pos);
|
readopt_parse_opt(rp, READOPT_FORM_LONG, &pos);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
case '\0':
|
case '\0':
|
||||||
update_oper(rp, (struct readopt_view_strings){.len = 1, .strings = (const char *[]){arg}});
|
readopt_update_oper(rp, (struct readopt_view_strings){.len = 1, .strings = (const char *[]){arg}});
|
||||||
return;
|
return;
|
||||||
default:
|
default:
|
||||||
parse_opt(rp, READOPT_FORM_SHORT, &pos);
|
readopt_parse_opt(rp, READOPT_FORM_SHORT, &pos);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
update_oper(rp, (struct readopt_view_strings){.len = 1, .strings = (const char *[]){arg}});
|
readopt_update_oper(rp, (struct readopt_view_strings){.len = 1, .strings = (const char *[]){arg}});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void parse_opt(struct readopt_parser *rp, enum readopt_form form, const char **pos)
|
static void readopt_parse_opt(struct readopt_parser *rp, enum readopt_form form, const char **pos)
|
||||||
{
|
{
|
||||||
struct readopt_opt *match;
|
struct readopt_opt *match;
|
||||||
assert(form == READOPT_FORM_SHORT || form == READOPT_FORM_LONG);
|
assert(form == READOPT_FORM_SHORT || form == READOPT_FORM_LONG);
|
||||||
|
|
||||||
if (form == READOPT_FORM_SHORT)
|
if (form == READOPT_FORM_SHORT)
|
||||||
{
|
{
|
||||||
match = match_opt(rp, form, pos);
|
match = readopt_match_opt(rp, form, pos);
|
||||||
if (match)
|
if (match)
|
||||||
{
|
{
|
||||||
const char *strpos = *pos;
|
const char *strpos = *pos;
|
||||||
|
@ -243,11 +243,11 @@ static void parse_opt(struct readopt_parser *rp, enum readopt_form form, const c
|
||||||
if (!match->cont.req && *strpos)
|
if (!match->cont.req && *strpos)
|
||||||
{
|
{
|
||||||
rp->state.grppos = strpos;
|
rp->state.grppos = strpos;
|
||||||
update_opt(rp, NULL, match);
|
readopt_update_opt(rp, NULL, match);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
update_opt(rp, *strpos ? strpos : NULL, match);
|
readopt_update_opt(rp, *strpos ? strpos : NULL, match);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -258,18 +258,18 @@ static void parse_opt(struct readopt_parser *rp, enum readopt_form form, const c
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* Match and advance pos to the end of the match. */
|
/* Match and advance pos to the end of the match. */
|
||||||
match = match_opt(rp, form, pos);
|
match = readopt_match_opt(rp, form, pos);
|
||||||
|
|
||||||
if (match)
|
if (match)
|
||||||
{
|
{
|
||||||
switch (**pos)
|
switch (**pos)
|
||||||
{
|
{
|
||||||
case '\0':
|
case '\0':
|
||||||
update_opt(rp, NULL, match);
|
readopt_update_opt(rp, NULL, match);
|
||||||
break;
|
break;
|
||||||
case '=':
|
case '=':
|
||||||
++(*pos);
|
++(*pos);
|
||||||
update_opt(rp, *pos, match);
|
readopt_update_opt(rp, *pos, match);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
rp->error = READOPT_ERROR_NOTOPT;
|
rp->error = READOPT_ERROR_NOTOPT;
|
||||||
|
@ -283,7 +283,7 @@ static void parse_opt(struct readopt_parser *rp, enum readopt_form form, const c
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct readopt_opt *match_opt(struct readopt_parser *rp, enum readopt_form form, const char **needle)
|
static struct readopt_opt *readopt_match_opt(struct readopt_parser *rp, enum readopt_form form, const char **needle)
|
||||||
{
|
{
|
||||||
/* This represents the last inexact match. */
|
/* This represents the last inexact match. */
|
||||||
struct
|
struct
|
||||||
|
@ -309,24 +309,24 @@ static struct readopt_opt *match_opt(struct readopt_parser *rp, enum readopt_for
|
||||||
for (size_t j = 0; names[j]; j++)
|
for (size_t j = 0; names[j]; j++)
|
||||||
{
|
{
|
||||||
char *name = names[j];
|
char *name = names[j];
|
||||||
cmp = skip_incl(*needle, name);
|
cmp = readopt_skip_incl(*needle, name);
|
||||||
|
|
||||||
if (!cmp)
|
if (!cmp)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (!*cmp)
|
if (!*cmp)
|
||||||
/* A guaranteed match. */
|
/* A guaranteed match. */
|
||||||
return match_finish(rp, needle, cmp, haystack + i);
|
return readopt_match_finish(rp, needle, cmp, haystack + i);
|
||||||
else if ((cmp - *needle) > (loose.adv - *needle))
|
else if ((cmp - *needle) > (loose.adv - *needle))
|
||||||
/* Maybe a match, maybe not. */
|
/* Maybe a match, maybe not. */
|
||||||
loose.adv = cmp, loose.opt = haystack + i;
|
loose.adv = cmp, loose.opt = haystack + i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return match_finish(rp, needle, loose.adv, loose.opt);
|
return readopt_match_finish(rp, needle, loose.adv, loose.opt);
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct readopt_opt *match_finish(struct readopt_parser *rp, const char **needle, const char *adv, struct readopt_opt *opt)
|
static struct readopt_opt *readopt_match_finish(struct readopt_parser *rp, const char **needle, const char *adv, struct readopt_opt *opt)
|
||||||
{
|
{
|
||||||
if (adv)
|
if (adv)
|
||||||
*needle = adv;
|
*needle = adv;
|
||||||
|
@ -337,7 +337,7 @@ static struct readopt_opt *match_finish(struct readopt_parser *rp, const char **
|
||||||
return opt;
|
return opt;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void update_opt(struct readopt_parser *rp, const char *attach, struct readopt_opt *opt)
|
static void readopt_update_opt(struct readopt_parser *rp, const char *attach, struct readopt_opt *opt)
|
||||||
{
|
{
|
||||||
if (opt->cont.req)
|
if (opt->cont.req)
|
||||||
{
|
{
|
||||||
|
@ -345,41 +345,41 @@ static void update_opt(struct readopt_parser *rp, const char *attach, struct rea
|
||||||
{
|
{
|
||||||
/* --opt=value, --opt=, -ovalue */
|
/* --opt=value, --opt=, -ovalue */
|
||||||
struct readopt_oper *curr = &rp->state.curr.opt->cont.oper;
|
struct readopt_oper *curr = &rp->state.curr.opt->cont.oper;
|
||||||
occ_opt(rp, opt);
|
readopt_occ_opt(rp, opt);
|
||||||
add_val(rp, curr, attach, 0);
|
readopt_add_val(rp, curr, attach, 0);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* --opt value, -o value */
|
/* --opt value, -o value */
|
||||||
rp->state.pending = 1;
|
rp->state.pending = 1;
|
||||||
occ_opt(rp, opt);
|
readopt_occ_opt(rp, opt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
occ_opt(rp, opt);
|
readopt_occ_opt(rp, opt);
|
||||||
if (attach)
|
if (attach)
|
||||||
rp->error = READOPT_ERROR_NOTREQ;
|
rp->error = READOPT_ERROR_NOTREQ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void update_oper(struct readopt_parser *rp, struct readopt_view_strings val)
|
static void readopt_update_oper(struct readopt_parser *rp, struct readopt_view_strings val)
|
||||||
{
|
{
|
||||||
assert(val.len && val.strings);
|
assert(val.len && val.strings);
|
||||||
|
|
||||||
if (val.len == 1)
|
if (val.len == 1)
|
||||||
{
|
{
|
||||||
++rp->state.curr.ioper.len;
|
++rp->state.curr.ioper.len;
|
||||||
permute_val(rp, &rp->state.curr.ioper, val.strings[0], 1);
|
readopt_permute_val(rp, &rp->state.curr.ioper, val.strings[0], 1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
permute_rest(rp->state.curr.eoval, val);
|
readopt_permute_rest(rp->state.curr.eoval, val);
|
||||||
rp->state.curr.ioper.len += val.len;
|
rp->state.curr.ioper.len += val.len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void assign_opers(struct readopt_parser *rp)
|
static void readopt_assign_opers(struct readopt_parser *rp)
|
||||||
{
|
{
|
||||||
size_t count = rp->state.curr.ioper.len;
|
size_t count = rp->state.curr.ioper.len;
|
||||||
|
|
||||||
|
@ -433,17 +433,17 @@ static void assign_opers(struct readopt_parser *rp)
|
||||||
rp->error = READOPT_ERROR_RANGEOPER;
|
rp->error = READOPT_ERROR_RANGEOPER;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void add_val(struct readopt_parser *rp, struct readopt_oper *oper, const char *string, int end)
|
static void readopt_add_val(struct readopt_parser *rp, struct readopt_oper *oper, const char *string, int end)
|
||||||
{
|
{
|
||||||
rp->state.pending = 0;
|
rp->state.pending = 0;
|
||||||
|
|
||||||
if (!readopt_validate_within(oper))
|
if (!readopt_validate_within(oper))
|
||||||
rp->error = READOPT_ERROR_RANGEOPT;
|
rp->error = READOPT_ERROR_RANGEOPT;
|
||||||
else
|
else
|
||||||
permute_val(rp, &oper->val, string, end);
|
readopt_permute_val(rp, &oper->val, string, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *skip_incl(const char *outer, const char *inner)
|
static const char *readopt_skip_incl(const char *outer, const char *inner)
|
||||||
{
|
{
|
||||||
while (*inner == *outer)
|
while (*inner == *outer)
|
||||||
{
|
{
|
||||||
|
@ -454,14 +454,14 @@ static const char *skip_incl(const char *outer, const char *inner)
|
||||||
return !*inner ? outer : NULL;
|
return !*inner ? outer : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void occ_opt(struct readopt_parser *rp, struct readopt_opt *opt)
|
static void readopt_occ_opt(struct readopt_parser *rp, struct readopt_opt *opt)
|
||||||
{
|
{
|
||||||
assert(opt);
|
assert(opt);
|
||||||
rp->state.curr.opt = opt;
|
rp->state.curr.opt = opt;
|
||||||
++rp->state.curr.opt->cont.oper.val.len;
|
++rp->state.curr.opt->cont.oper.val.len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void permute_val(struct readopt_parser *rp, struct readopt_view_strings *target, const char *val, int end)
|
static void readopt_permute_val(struct readopt_parser *rp, struct readopt_view_strings *target, const char *val, int end)
|
||||||
{
|
{
|
||||||
if (!target->strings)
|
if (!target->strings)
|
||||||
/* Fallback position when no value has yet been set. */
|
/* Fallback position when no value has yet been set. */
|
||||||
|
@ -480,18 +480,18 @@ static void permute_val(struct readopt_parser *rp, struct readopt_view_strings *
|
||||||
|
|
||||||
/* Increment all value pointers in the options which are between start and stop (inclusive). */
|
/* Increment all value pointers in the options which are between start and stop (inclusive). */
|
||||||
for (size_t i = 0; readopt_validate_opt(rp->opts + i); i++)
|
for (size_t i = 0; readopt_validate_opt(rp->opts + i); i++)
|
||||||
incr_between(start, stop, &rp->opts[i].cont.oper.val, target);
|
readopt_incr_between(start, stop, &rp->opts[i].cont.oper.val, target);
|
||||||
|
|
||||||
incr_between(start, stop, &rp->state.curr.ioper, target);
|
readopt_incr_between(start, stop, &rp->state.curr.ioper, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void incr_between(const char **start, const char **stop, struct readopt_view_strings *curr, struct readopt_view_strings *exclude)
|
static void readopt_incr_between(const char **start, const char **stop, struct readopt_view_strings *curr, struct readopt_view_strings *exclude)
|
||||||
{
|
{
|
||||||
if (curr->strings >= start && curr->strings <= stop && curr != exclude)
|
if (curr->strings >= start && curr->strings <= stop && curr != exclude)
|
||||||
++curr->strings;
|
++curr->strings;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void permute_rest(const char **target, struct readopt_view_strings start)
|
static void readopt_permute_rest(const char **target, struct readopt_view_strings start)
|
||||||
{
|
{
|
||||||
memmove(target, start.strings, start.len * sizeof *start.strings);
|
memmove(target, start.strings, start.len * sizeof *start.strings);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue