const-qualify + cleanup

This commit is contained in:
Lukas Wurzinger 2021-03-09 19:54:03 +01:00
parent 77394a61dc
commit 11cd1cf7c3
3 changed files with 38 additions and 46 deletions

View file

@ -3,28 +3,28 @@
#include "readopt.h" #include "readopt.h"
static void parse_arg(struct readopt_parser *rp, char *arg); static void parse_arg(struct readopt_parser *rp, const char *arg);
static void parse_opt(struct readopt_parser *rp, enum readopt_form form, char **pos); static void 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, char **needle); 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, char **needle, char *cmp, struct readopt_opt *opt); static struct readopt_opt *match_finish(struct readopt_parser *rp, const char **needle, const char *adv, struct readopt_opt *opt);
static void update_opt(struct readopt_parser *rp, char *attach, struct readopt_opt *opt); static void 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 update_oper(struct readopt_parser *rp, struct readopt_view_strings val);
static void assign_opers(struct readopt_parser *rp); static void assign_opers(struct readopt_parser *rp);
static void add_val(struct readopt_parser *rp, struct readopt_oper *oper, char *str, int end); static void add_val(struct readopt_parser *rp, struct readopt_oper *oper, const char *string, int end);
static char *skip_incl(const char *inner, char *outer); static const char *skip_incl(const char *outer, const char *inner);
static void occ_opt(struct readopt_parser *rp, struct readopt_opt *opt); static void occ_opt(struct readopt_parser *rp, struct readopt_opt *opt);
/* permutes the argument list to store a value for an option or operand */ /* permutes the argument list to store a value for an option or operand */
static void permute_val(struct readopt_parser *rp, struct readopt_view_strings *target, char *val, int end); static void permute_val(struct readopt_parser *rp, struct readopt_view_strings *target, const char *val, int end);
static void incr_between(char **start, char **stop, struct readopt_view_strings *curr, struct readopt_view_strings *exclude); static void incr_between(const char **start, const char **stop, struct readopt_view_strings *curr, struct readopt_view_strings *exclude);
static void permute_rest(char **target, struct readopt_view_strings start); static void permute_rest(const char **target, struct readopt_view_strings start);
static void format_usage_opts(struct readopt_format_context *ctx, struct readopt_opt *opts); static void format_usage_opts(struct readopt_format_context *ctx, struct readopt_opt *opts);
static void format_usage_opers(struct readopt_format_context *ctx, struct readopt_oper *opers); static void format_usage_opers(struct readopt_format_context *ctx, struct readopt_oper *opers);
@ -68,7 +68,7 @@ readopt_parse(struct readopt_parser *rp)
} }
static void static void
parse_arg(struct readopt_parser *rp, char *arg) 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) {
@ -79,7 +79,7 @@ parse_arg(struct readopt_parser *rp, char *arg)
return; return;
} }
char *pos = arg; const char *pos = arg;
switch (*pos) { switch (*pos) {
case '-': case '-':
@ -110,20 +110,20 @@ parse_arg(struct readopt_parser *rp, char *arg)
return; return;
} }
case '\0': case '\0':
update_oper(rp, (struct readopt_view_strings){.len = 1, .strings = (char *[]){arg}}); update_oper(rp, (struct readopt_view_strings){.len = 1, .strings = (const char *[]){arg}});
return; return;
default: default:
parse_opt(rp, READOPT_FORM_SHORT, &pos); parse_opt(rp, READOPT_FORM_SHORT, &pos);
return; return;
} }
default: default:
update_oper(rp, (struct readopt_view_strings){.len = 1, .strings = (char *[]){arg}}); update_oper(rp, (struct readopt_view_strings){.len = 1, .strings = (const char *[]){arg}});
return; return;
} }
} }
static void static void
parse_opt(struct readopt_parser *rp, enum readopt_form form, char **pos) 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);
@ -131,7 +131,7 @@ parse_opt(struct readopt_parser *rp, enum readopt_form form, char **pos)
if (form == READOPT_FORM_SHORT) { if (form == READOPT_FORM_SHORT) {
match = match_opt(rp, form, pos); match = match_opt(rp, form, pos);
if (match) { if (match) {
char *strpos = *pos; const char *strpos = *pos;
if (!match->cont.req && *strpos) { if (!match->cont.req && *strpos) {
rp->state.grppos = strpos; rp->state.grppos = strpos;
@ -166,12 +166,12 @@ parse_opt(struct readopt_parser *rp, enum readopt_form form, char **pos)
} }
static struct readopt_opt * static struct readopt_opt *
match_opt(struct readopt_parser *rp, enum readopt_form form, char **needle) match_opt(struct readopt_parser *rp, enum readopt_form form, const char **needle)
{ {
/* structure representing the last, inexact match */ /* structure representing the last, inexact match */
struct { struct {
/* the current advanced string */ /* the current advanced string */
char *adv; const char *adv;
/* current option */ /* current option */
struct readopt_opt *opt; struct readopt_opt *opt;
} loose = {0}; } loose = {0};
@ -185,11 +185,11 @@ match_opt(struct readopt_parser *rp, enum readopt_form form, char **needle)
/* ignore the option as it does not have names in the required form */ /* ignore the option as it does not have names in the required form */
continue; continue;
char *cmp = loose.adv; const char *cmp = loose.adv;
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(name, *needle); cmp = skip_incl(*needle, name);
if (!cmp) if (!cmp)
continue; continue;
@ -207,7 +207,7 @@ match_opt(struct readopt_parser *rp, enum readopt_form form, char **needle)
} }
static struct readopt_opt * static struct readopt_opt *
match_finish(struct readopt_parser *rp, char **needle, char *adv, struct readopt_opt *opt) match_finish(struct readopt_parser *rp, const char **needle, const char *adv, struct readopt_opt *opt)
{ {
if (adv) if (adv)
*needle = adv; *needle = adv;
@ -219,7 +219,7 @@ match_finish(struct readopt_parser *rp, char **needle, char *adv, struct readopt
} }
static void static void
update_opt(struct readopt_parser *rp, char *attach, struct readopt_opt *opt) update_opt(struct readopt_parser *rp, const char *attach, struct readopt_opt *opt)
{ {
if (opt->cont.req) { if (opt->cont.req) {
if (attach) { if (attach) {
@ -304,7 +304,7 @@ assign_opers(struct readopt_parser *rp)
} }
static void static void
add_val(struct readopt_parser *rp, struct readopt_oper *oper, char *string, int end) add_val(struct readopt_parser *rp, struct readopt_oper *oper, const char *string, int end)
{ {
rp->state.pending = 0; rp->state.pending = 0;
@ -314,8 +314,8 @@ add_val(struct readopt_parser *rp, struct readopt_oper *oper, char *string, int
permute_val(rp, &oper->val, string, end); permute_val(rp, &oper->val, string, end);
} }
static char * static const char *
skip_incl(const char *inner, char *outer) skip_incl(const char *outer, const char *inner)
{ {
while (*inner == *outer) { while (*inner == *outer) {
if (!*inner) if (!*inner)
@ -334,13 +334,13 @@ occ_opt(struct readopt_parser *rp, struct readopt_opt *opt)
} }
static void static void
permute_val(struct readopt_parser *rp, struct readopt_view_strings *target, char *val, int end) 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 been set yet */ /* fallback position when no value has been set yet */
target->strings = rp->state.curr.eoval - (end ? 0 : rp->state.curr.ioper.len); target->strings = rp->state.curr.eoval - (end ? 0 : rp->state.curr.ioper.len);
char **pos = target->strings + (target->len - 1); const char **pos = target->strings + (target->len - 1);
assert(rp->state.curr.arg >= rp->state.curr.eoval); assert(rp->state.curr.arg >= rp->state.curr.eoval);
@ -349,7 +349,7 @@ permute_val(struct readopt_parser *rp, struct readopt_view_strings *target, char
*pos = val; *pos = val;
++rp->state.curr.eoval; ++rp->state.curr.eoval;
char **start = pos, **stop = rp->state.curr.eoval; const char **start = pos, **stop = rp->state.curr.eoval;
/* 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++)
@ -359,14 +359,14 @@ permute_val(struct readopt_parser *rp, struct readopt_view_strings *target, char
} }
static void static void
incr_between(char **start, char **stop, struct readopt_view_strings *curr, struct readopt_view_strings *exclude) 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 static void
permute_rest(char **target, struct readopt_view_strings start) 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);
} }
@ -385,14 +385,6 @@ readopt_parser_init(struct readopt_parser *rp, struct readopt_opt *opts, struct
}; };
} }
struct readopt_parser
readopt_parser_create(struct readopt_opt *opts, struct readopt_oper *opers, struct readopt_view_strings args)
{
struct readopt_parser rp = {0};
readopt_parser_init(&rp, opts, opers, args);
return rp;
}
int int
readopt_validate_opt(struct readopt_opt *opt) readopt_validate_opt(struct readopt_opt *opt)
{ {

View file

@ -24,7 +24,7 @@ enum readopt_format {
}; };
struct readopt_view_strings { struct readopt_view_strings {
char **strings; const char **strings;
size_t len; size_t len;
}; };
@ -58,13 +58,13 @@ struct readopt_parser {
struct readopt_view_strings args; struct readopt_view_strings args;
struct { struct {
int pending; int pending;
char *grppos; const char *grppos;
struct { struct {
struct readopt_opt *opt; struct readopt_opt *opt;
/* reference to the current argument being parsed */ /* reference to the current argument being parsed */
char **arg; const char **arg;
/* reference to the last element of the option/operand value view */ /* reference to the last element of the option/operand value view */
char **eoval; const char **eoval;
/* intermediate operands which have not yet been assigned */ /* intermediate operands which have not yet been assigned */
struct readopt_view_strings ioper; struct readopt_view_strings ioper;
} curr; } curr;
@ -105,8 +105,6 @@ struct readopt_format_context {
int readopt_parse(struct readopt_parser *rp); int readopt_parse(struct readopt_parser *rp);
/* args should always exclude the first element, like this: {.strings = argv + 1, .len = argc - 1} */ /* args should always exclude the first element, like this: {.strings = argv + 1, .len = argc - 1} */
void readopt_parser_init(struct readopt_parser *rp, struct readopt_opt *opts, struct readopt_oper *opers, struct readopt_view_strings args); void readopt_parser_init(struct readopt_parser *rp, struct readopt_opt *opts, struct readopt_oper *opers, struct readopt_view_strings args);
/* args should always exclude the first element, like this: {.strings = argv + 1, .len = argc - 1} */
struct readopt_parser readopt_parser_create(struct readopt_opt *opts, struct readopt_oper *opers, struct readopt_view_strings args);
/* check whether the argument is a valid option (can be used to check for the end of an array of options) */ /* check whether the argument is a valid option (can be used to check for the end of an array of options) */
int readopt_validate_opt(struct readopt_opt *opt); int readopt_validate_opt(struct readopt_opt *opt);
/* check whether the argument is a valid operand (can be used to check for the end of an array of operands) */ /* check whether the argument is a valid operand (can be used to check for the end of an array of operands) */

View file

@ -107,11 +107,13 @@ main(int argc, char **argv)
{0} {0}
}; };
struct readopt_parser rp = readopt_parser_create( struct readopt_parser rp;
readopt_parser_init(
&rp,
opts, opts,
opers, opers,
(struct readopt_view_strings){ (struct readopt_view_strings){
.strings = argv + 1, .strings = (const char **)argv + 1,
.len = argc - 1 .len = argc - 1
} }
); );