formatting

This commit is contained in:
Lukas Wurzinger 2022-09-25 12:08:56 +02:00
parent 4f5a9d794d
commit 0650e1ddb9
3 changed files with 387 additions and 388 deletions

View file

@ -5,14 +5,12 @@ AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: false AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: All AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None AlwaysBreakAfterReturnType: None
BasedOnStyle: LLVM BasedOnStyle: LLVM
BreakBeforeBinaryOperators: None BreakBeforeBinaryOperators: None
BreakBeforeBraces: Linux
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakStringLiterals: false BreakStringLiterals: false
BreakBeforeBraces: Allman
ColumnLimit: 0 ColumnLimit: 0
IncludeBlocks: Regroup IncludeBlocks: Regroup
IncludeCategories: IncludeCategories:
@ -21,7 +19,7 @@ IncludeCategories:
- Regex: '^".*"$' - Regex: '^".*"$'
Priority: 2 Priority: 2
IndentCaseLabels: false IndentCaseLabels: false
IndentWidth: 8 IndentWidth: 4
PointerAlignment: Right PointerAlignment: Right
ReflowComments: true ReflowComments: true
SortIncludes: true SortIncludes: true
@ -33,5 +31,3 @@ SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false SpacesInCStyleCastParentheses: false
SpacesInParentheses: false SpacesInParentheses: false
SpacesInSquareBrackets: false SpacesInSquareBrackets: false
TabWidth: 8
UseTab: ForIndentation

233
readopt.c
View file

@ -1,8 +1,8 @@
#include <string.h>
#include <assert.h>
#include "readopt.h" #include "readopt.h"
#include <assert.h>
#include <string.h>
static void parse_arg(struct readopt_parser *rp, const 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, const char **pos); static void parse_opt(struct readopt_parser *rp, enum readopt_form form, const char **pos);
@ -21,25 +21,27 @@ 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 */
static void permute_val(struct readopt_parser *rp, struct readopt_view_strings *target, const 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(const char **start, const 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(const char **target, struct readopt_view_strings start); static void permute_rest(const char **target, struct readopt_view_strings start);
int int readopt_parse(struct readopt_parser *rp)
readopt_parse(struct readopt_parser *rp)
{ {
/* check whether the current offset is at the end of argv */ /* Check whether the current offset is at the end of argv. */
size_t off = rp->state.curr.arg - rp->args.strings; size_t off = rp->state.curr.arg - rp->args.strings;
if (off >= rp->args.len) { if (off >= rp->args.len)
if (rp->state.pending) { {
/* the last specified option required an argument, but has been ignored */ if (rp->state.pending)
{
/* The last specified option required an argument, but no argument has been provided. */
rp->error = READOPT_ERROR_NOVAL; rp->error = READOPT_ERROR_NOVAL;
return 0; return 0;
} }
for (size_t i = 0; readopt_validate_opt(rp->opts + i); i++) { for (size_t i = 0; readopt_validate_opt(rp->opts + i); i++)
if (!readopt_validate_within(&rp->opts[i].cont.oper)) { {
if (!readopt_validate_within(&rp->opts[i].cont.oper))
{
rp->error = READOPT_ERROR_RANGEOPT; rp->error = READOPT_ERROR_RANGEOPT;
return 0; return 0;
} }
@ -49,7 +51,8 @@ readopt_parse(struct readopt_parser *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); 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;
@ -57,20 +60,21 @@ readopt_parse(struct readopt_parser *rp)
parse_arg(rp, *rp->state.curr.arg); 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)
++rp->state.curr.arg; ++rp->state.curr.arg;
return !rp->error; return !rp->error;
} }
static void static void parse_arg(struct readopt_parser *rp, const char *arg)
parse_arg(struct readopt_parser *rp, const char *arg) {
/* Parse the next option in the grouped option string, which automatically advances it. */
if (rp->state.grppos)
{ {
/* parse the next option in the grouped option string, which automatically advances it */
if (rp->state.grppos) {
parse_opt(rp, READOPT_FORM_SHORT, &rp->state.grppos); 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;
} }
return; return;
@ -78,27 +82,30 @@ parse_arg(struct readopt_parser *rp, const char *arg)
const char *pos = arg; const char *pos = arg;
switch (*pos) { switch (*pos)
{
case '-': case '-':
++pos; ++pos;
switch (*pos) { switch (*pos)
{
case '-': case '-':
++pos; ++pos;
switch (*pos) { switch (*pos)
{
size_t off; size_t off;
case '\0': case '\0':
/* "--" denotes the end of options */ /* "--" denotes the end of options. */
off = rp->args.len - (rp->state.curr.arg - rp->args.strings); off = rp->args.len - (rp->state.curr.arg - rp->args.strings);
assert(off); assert(off);
if (off == 1) { if (off == 1)
/* no operands after the "--" */ {
/* No operands after the "--". */
return; return;
} }
update_oper(rp, (struct readopt_view_strings){ 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;
@ -119,32 +126,42 @@ parse_arg(struct readopt_parser *rp, const char *arg)
} }
} }
static void static void parse_opt(struct readopt_parser *rp, enum readopt_form form, const 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);
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)
{
const 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;
update_opt(rp, NULL, match); update_opt(rp, NULL, match);
} else { }
else
{
update_opt(rp, *strpos ? strpos : NULL, match); update_opt(rp, *strpos ? strpos : NULL, match);
} }
} else { }
else
{
rp->error = READOPT_ERROR_NOTOPT; rp->error = READOPT_ERROR_NOTOPT;
} }
} else { }
/* match and advance pos to the end of the match */ else
{
/* Match and advance pos to the end of the match. */
match = match_opt(rp, form, pos); match = match_opt(rp, form, pos);
if (match) { if (match)
switch (**pos) { {
switch (**pos)
{
case '\0': case '\0':
update_opt(rp, NULL, match); update_opt(rp, NULL, match);
break; break;
@ -156,35 +173,39 @@ parse_opt(struct readopt_parser *rp, enum readopt_form form, const char **pos)
rp->error = READOPT_ERROR_NOTOPT; rp->error = READOPT_ERROR_NOTOPT;
break; break;
} }
} else { }
else
{
rp->error = READOPT_ERROR_NOTOPT; rp->error = READOPT_ERROR_NOTOPT;
} }
} }
} }
static struct readopt_opt * static struct readopt_opt *match_opt(struct readopt_parser *rp, enum readopt_form form, const char **needle)
match_opt(struct readopt_parser *rp, enum readopt_form form, const char **needle)
{ {
/* structure representing the last, inexact match */ /* This represents the last inexact match. */
struct { struct
/* the current advanced string */ {
/* The current advanced string. */
const char *adv; const char *adv;
/* current option */ /* The current option. */
struct readopt_opt *opt; struct readopt_opt *opt;
} loose = {0}; } loose = {0};
struct readopt_opt *haystack = rp->opts; struct readopt_opt *haystack = rp->opts;
for (size_t i = 0; readopt_validate_opt(haystack + i); i++) { 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 */ {
/* Iterate through all short or long names of the current option. */
char **names = haystack[i].names[form]; char **names = haystack[i].names[form];
if (!names) if (!names)
/* 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;
const 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(*needle, name); cmp = skip_incl(*needle, name);
@ -192,10 +213,10 @@ match_opt(struct readopt_parser *rp, enum readopt_form form, const char **needle
continue; continue;
if (!*cmp) if (!*cmp)
/* a guaranteed match */ /* A guaranteed match. */
return match_finish(rp, needle, cmp, haystack + i); return 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 a match, maybe not. */
loose.adv = cmp, loose.opt = haystack + i; loose.adv = cmp, loose.opt = haystack + i;
} }
} }
@ -203,8 +224,7 @@ match_opt(struct readopt_parser *rp, enum readopt_form form, const char **needle
return match_finish(rp, needle, loose.adv, loose.opt); return match_finish(rp, needle, loose.adv, loose.opt);
} }
static struct readopt_opt * static struct readopt_opt *match_finish(struct readopt_parser *rp, const char **needle, const 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;
@ -215,68 +235,78 @@ match_finish(struct readopt_parser *rp, const char **needle, const char *adv, st
return opt; return opt;
} }
static void static void update_opt(struct readopt_parser *rp, const char *attach, struct readopt_opt *opt)
update_opt(struct readopt_parser *rp, const char *attach, struct readopt_opt *opt) {
if (opt->cont.req)
{
if (attach)
{ {
if (opt->cont.req) {
if (attach) {
/* --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); occ_opt(rp, opt);
add_val(rp, curr, attach, 0); 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); occ_opt(rp, opt);
} }
} else { }
else
{
occ_opt(rp, opt); occ_opt(rp, opt);
if (attach) if (attach)
rp->error = READOPT_ERROR_NOTREQ; rp->error = READOPT_ERROR_NOTREQ;
} }
} }
static void static void update_oper(struct readopt_parser *rp, struct readopt_view_strings val)
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); permute_val(rp, &rp->state.curr.ioper, val.strings[0], 1);
} else { }
else
{
permute_rest(rp->state.curr.eoval, val); permute_rest(rp->state.curr.eoval, val);
rp->state.curr.ioper.len += val.len; rp->state.curr.ioper.len += val.len;
} }
} }
static void static void assign_opers(struct readopt_parser *rp)
assign_opers(struct readopt_parser *rp)
{ {
size_t count = rp->state.curr.ioper.len; size_t count = rp->state.curr.ioper.len;
size_t nlower = 0; size_t nlower = 0;
size_t nupper = 0; size_t nupper = 0;
for (size_t i = 0; readopt_validate_oper(rp->opers + i); i++) { for (size_t i = 0; readopt_validate_oper(rp->opers + i); i++)
{
nlower += readopt_select_lower(rp->opers[i].bounds); nlower += readopt_select_lower(rp->opers[i].bounds);
nupper += readopt_select_upper(rp->opers[i].bounds); nupper += readopt_select_upper(rp->opers[i].bounds);
} }
if (count < nlower) { if (count < nlower)
{
rp->error = READOPT_ERROR_RANGEOPER; rp->error = READOPT_ERROR_RANGEOPER;
return; return;
} }
struct { struct
{
size_t extra; size_t extra;
size_t req; size_t req;
} rest = { } rest = {
count - nlower, count - nlower,
nlower nlower};
};
for (size_t i = 0; readopt_validate_oper(rp->opers + i); i++) { for (size_t i = 0; readopt_validate_oper(rp->opers + i); i++)
if (count == 0 || !rp->opers[i].val.strings) { {
if (count == 0 || !rp->opers[i].val.strings)
{
size_t off = count - (rest.extra + rest.req); size_t off = count - (rest.extra + rest.req);
rp->opers[i].val.strings = rp->state.curr.ioper.strings + off; rp->opers[i].val.strings = rp->state.curr.ioper.strings + off;
} }
@ -287,12 +317,13 @@ assign_opers(struct readopt_parser *rp)
size_t add; size_t add;
/* add required elements */ /* Add required elements. */
add = rest.req > lower ? lower : rest.req; add = rest.req > lower ? lower : rest.req;
rp->opers[i].val.len += add, rest.req -= add; rp->opers[i].val.len += add, rest.req -= add;
/* add optional elements */ /* Add optional elements. */
add = inf ? rest.extra : rest.extra > upper ? upper : rest.extra; add = inf ? rest.extra : rest.extra > upper ? upper
: rest.extra;
rp->opers[i].val.len += add, rest.extra -= add; rp->opers[i].val.len += add, rest.extra -= add;
} }
@ -300,8 +331,7 @@ assign_opers(struct readopt_parser *rp)
rp->error = READOPT_ERROR_RANGEOPER; rp->error = READOPT_ERROR_RANGEOPER;
} }
static void static void add_val(struct readopt_parser *rp, struct readopt_oper *oper, const 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;
@ -311,10 +341,10 @@ add_val(struct readopt_parser *rp, struct readopt_oper *oper, const char *string
permute_val(rp, &oper->val, string, end); permute_val(rp, &oper->val, string, end);
} }
static const char * static const char *skip_incl(const char *outer, const char *inner)
skip_incl(const char *outer, const char *inner) {
while (*inner == *outer)
{ {
while (*inner == *outer) {
if (!*inner) if (!*inner)
return outer; return outer;
++inner, ++outer; ++inner, ++outer;
@ -322,19 +352,17 @@ skip_incl(const char *outer, const char *inner)
return !*inner ? outer : NULL; return !*inner ? outer : NULL;
} }
static void static void occ_opt(struct readopt_parser *rp, struct readopt_opt *opt)
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 static void permute_val(struct readopt_parser *rp, struct readopt_view_strings *target, const 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 yet been set. */
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);
const char **pos = target->strings + (target->len - 1); const char **pos = target->strings + (target->len - 1);
@ -348,28 +376,25 @@ permute_val(struct readopt_parser *rp, struct readopt_view_strings *target, cons
const 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++)
incr_between(start, stop, &rp->opts[i].cont.oper.val, target); incr_between(start, stop, &rp->opts[i].cont.oper.val, target);
incr_between(start, stop, &rp->state.curr.ioper, target); incr_between(start, stop, &rp->state.curr.ioper, target);
} }
static void static void incr_between(const char **start, const 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(const 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);
} }
void void readopt_parser_init(struct readopt_parser *rp, struct readopt_opt *opts, struct readopt_oper *opers, struct readopt_view_strings args)
readopt_parser_init(struct readopt_parser *rp, struct readopt_opt *opts, struct readopt_oper *opers, struct readopt_view_strings args)
{ {
*rp = (struct readopt_parser){ *rp = (struct readopt_parser){
.args = args, .args = args,
@ -377,27 +402,22 @@ readopt_parser_init(struct readopt_parser *rp, struct readopt_opt *opts, struct
.opers = opers, .opers = opers,
.state.curr = { .state.curr = {
.arg = args.strings, .arg = args.strings,
.eoval = args.strings .eoval = args.strings}};
}
};
} }
int int readopt_validate_opt(struct readopt_opt *opt)
readopt_validate_opt(struct readopt_opt *opt)
{ {
assert(opt); assert(opt);
return opt->names[0] || opt->names[1]; return opt->names[0] || opt->names[1];
} }
int int readopt_validate_oper(struct readopt_oper *oper)
readopt_validate_oper(struct readopt_oper *oper)
{ {
assert(oper); assert(oper);
return !!oper->name; return !!oper->name;
} }
int int readopt_validate_within(struct readopt_oper *oper)
readopt_validate_within(struct readopt_oper *oper)
{ {
size_t occ = oper->val.len; size_t occ = oper->val.len;
size_t upper = readopt_select_upper(oper->bounds); size_t upper = readopt_select_upper(oper->bounds);
@ -405,14 +425,15 @@ readopt_validate_within(struct readopt_oper *oper)
return occ >= lower && (occ <= upper || oper->bounds.inf); return occ >= lower && (occ <= upper || oper->bounds.inf);
} }
size_t size_t readopt_select_upper(struct readopt_bounds bounds)
readopt_select_upper(struct readopt_bounds bounds)
{ {
return bounds.val[0] > bounds.val[1] ? bounds.val[0] : bounds.val[1]; return bounds.val[0] > bounds.val[1] ? bounds.val[0] : bounds.val[1];
} }
size_t size_t readopt_select_lower(struct readopt_bounds bounds)
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]; return bounds.inf ? readopt_select_upper(bounds) : bounds.val[0] < bounds.val[1] ? bounds.val[0]
: bounds.val[1];
} }
// vim: set sw=4 ts=4 et :

View file

@ -4,7 +4,8 @@
#define READOPT_ALLOC_STRINGS(...) ((char *[]){__VA_ARGS__, NULL}) #define READOPT_ALLOC_STRINGS(...) ((char *[]){__VA_ARGS__, NULL})
enum readopt_error { enum readopt_error
{
READOPT_ERROR_SUCCESS, READOPT_ERROR_SUCCESS,
READOPT_ERROR_NOVAL, READOPT_ERROR_NOVAL,
READOPT_ERROR_NOTREQ, READOPT_ERROR_NOTREQ,
@ -13,105 +14,86 @@ enum readopt_error {
READOPT_ERROR_RANGEOPER READOPT_ERROR_RANGEOPER
}; };
enum readopt_form { enum readopt_form
{
READOPT_FORM_SHORT, READOPT_FORM_SHORT,
READOPT_FORM_LONG READOPT_FORM_LONG
}; };
enum readopt_format { enum readopt_format
{
READOPT_FORMAT_PLAIN, READOPT_FORMAT_PLAIN,
READOPT_FORMAT_MDOC, READOPT_FORMAT_MDOC,
}; };
struct readopt_view_strings { struct readopt_view_strings
{
const char **strings; const char **strings;
size_t len; size_t len;
}; };
struct readopt_bounds { struct readopt_bounds
/* upper val will be ignored if inf is non-zero */ {
/* The upper value will be ignored if inf is non-zero. */
size_t val[2]; size_t val[2];
int inf; int inf;
}; };
struct readopt_oper { struct readopt_oper
{
char *name; char *name;
struct readopt_bounds bounds; struct readopt_bounds bounds;
struct readopt_view_strings val; struct readopt_view_strings val;
}; };
struct readopt_opt { struct readopt_opt
/* two null-terminated arrays of either long or short option names */ {
/* Two null-terminated arrays of either long or short option names. */
char **names[2]; char **names[2];
struct { struct
{
int req; int req;
/* oper.name is the name of the value itself (not the option), such as "file" in "--config=file" */ /* oper.name is the name of the value itself (not the option). */
struct readopt_oper oper; struct readopt_oper oper;
} cont; } cont;
}; };
struct readopt_parser { struct readopt_parser
{
struct readopt_opt *opts; struct readopt_opt *opts;
struct readopt_oper *opers; struct readopt_oper *opers;
struct readopt_view_strings args; struct readopt_view_strings args;
struct { struct
{
int pending; int pending;
const 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. */
const 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. */
const 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;
} state; } state;
enum readopt_error error; enum readopt_error error;
}; };
struct readopt_write_context { /* Iteratively parse the arguments. */
size_t written;
void *additional;
struct {
FILE *stream;
size_t size;
} dest;
struct {
const char *payload;
size_t len;
} src;
/* returning 0 means that the current string will not be written */
int (*callback)(struct readopt_write_context *);
};
struct readopt_format_context {
enum readopt_format fmt;
struct {
const char *needles;
char pre;
} esc;
struct readopt_write_context *wr;
};
/* iteratively parse the arguments */
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. */
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);
/* 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). */
int readopt_validate_oper(struct readopt_oper *oper); int readopt_validate_oper(struct readopt_oper *oper);
/* check whether the operand's values are within the defined limits */ /* Check whether the operand's values are within the defined limits. */
int readopt_validate_within(struct readopt_oper *oper); int readopt_validate_within(struct readopt_oper *oper);
/* get the upper limit */ /* Get the upper limit. */
size_t readopt_select_upper(struct readopt_bounds bounds); size_t readopt_select_upper(struct readopt_bounds bounds);
/* get the lower limit (this does not always return the minimum, if e.g. .val is {0, 1} and inf != 0, then 1 will be considered the lower limit as well as the upper limit) */ /* Get the lower limit. This does not always return the minimum. */
size_t readopt_select_lower(struct readopt_bounds bounds); size_t readopt_select_lower(struct readopt_bounds bounds);