#include <alloca.h>
#include <math.h>
-#include <stdbool.h>
-/***
- * Value flags defaults
- * ~~~~~~~~~~~~~~~~~~~~
- *
- * OPT_NO_VALUE for OPT_BOOL, OPT_SWITCH and OPT_INC
- * OPT_MAYBE_VALUE for OPT_STRING, OPT_UNS, OPT_INT
- * Some of the value flags (OPT_NO_VALUE, OPT_MAYBE_VALUE, OPT_REQUIRED_VALUE)
- * must be specified for OPT_CALL and OPT_USER.
- ***/
static uns opt_default_value_flags[] = {
[OPT_CL_BOOL] = OPT_NO_VALUE,
[OPT_CL_STATIC] = OPT_MAYBE_VALUE,
[OPT_CL_HELP] = 0
};
-struct opt_context {
- struct opt_precomputed * opts;
- struct opt_precomputed ** shortopt;
- struct opt_item ** hooks;
- int opt_count;
- int hook_count;
- int positional_max;
- int positional_count;
- bool stop_parsing;
-};
-
void opt_failure(const char * mesg, ...) {
va_list args;
va_start(args, mesg);
{
for (int i = 0; i < oc->hook_count; i++) {
struct opt_item *hook = oc->hooks[i];
- if (hook->flags & event)
- hook->u.hook(item, event, value, hook->ptr);
+ if (hook->flags & event) {
+ void *data = (hook->flags & OPT_HOOK_INTERNAL) ? oc : hook->ptr;
+ hook->u.hook(item, event, value, data);
+ }
}
}
(*((int *)item->ptr))++;
break;
case OPT_CL_CALL:
- item->u.call(item, value, item->ptr);
- break;
+ {
+ void *data = (opt->flags & OPT_INTERNAL) ? oc : item->ptr;
+ item->u.call(item, value, data);
+ break;
+ }
case OPT_CL_USER:
{
char * e = NULL;
int opt_parse(const struct opt_section * options, char ** argv) {
struct opt_context * oc = alloca(sizeof(*oc));
memset(oc, 0, sizeof (*oc));
+ oc->options = options;
opt_count_items(oc, options);
oc->opts = alloca(sizeof(*oc->opts) * oc->opt_count);