#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_before_arg;
- struct opt_item ** hooks_before_value;
- struct opt_item ** hooks_after_value;
- short opt_count;
- short hooks_before_arg_count;
- short hooks_before_value_count;
- short hooks_after_value_count;
- int positional_max;
- int positional_count;
- bool stop_parsing;
-};
-
void opt_failure(const char * mesg, ...) {
va_list args;
va_start(args, mesg);
vfprintf(stderr, mesg, args);
- fprintf(stderr, "\n");
- opt_usage();
+ fprintf(stderr, "\nRun with --help for more information.\n");
exit(OPT_EXIT_BAD_ARGS);
}
opt->flags = flags;
}
+static void opt_invoke_hooks(struct opt_context *oc, uns event, struct opt_item *item, char *value)
+{
+ for (int i = 0; i < oc->hook_count; i++) {
+ struct opt_item *hook = oc->hooks[i];
+ if (hook->flags & event) {
+ void *data = (hook->flags & OPT_HOOK_INTERNAL) ? oc : hook->ptr;
+ hook->u.hook(item, event, value, data);
+ }
+ }
+}
+
static struct opt_precomputed * opt_find_item_longopt(struct opt_context * oc, char * str) {
uns len = strlen(str);
struct opt_precomputed * candidate = NULL;
if (opt->flags & OPT_LAST_ARG)
oc->stop_parsing = 1;
- for (int i = 0; i < oc->hooks_before_value_count; i++)
- oc->hooks_before_value[i]->u.call(item, value, oc->hooks_before_value[i]->ptr);
+ opt_invoke_hooks(oc, OPT_HOOK_BEFORE_VALUE, item, value);
switch (item->cls) {
case OPT_CL_BOOL:
(*((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;
ASSERT(0);
}
- for (int i = 0;i < oc->hooks_after_value_count; i++)
- oc->hooks_after_value[i]->u.call(item, value, oc->hooks_after_value[i]->ptr);
+ opt_invoke_hooks(oc, OPT_HOOK_AFTER_VALUE, item, value);
}
static int opt_longopt(struct opt_context * oc, char ** argv, int index) {
for (const struct opt_item *item = sec->opt; item->cls != OPT_CL_END; item++) {
if (item->cls == OPT_CL_SECTION)
opt_count_items(oc, item->u.section);
- else if (item->cls == OPT_CL_HOOK) {
- if (item->flags & OPT_HOOK_BEFORE_ARG)
- oc->hooks_before_arg_count++;
- else if (item->flags & OPT_HOOK_BEFORE_VALUE)
- oc->hooks_before_value_count++;
- else if (item->flags & OPT_HOOK_AFTER_VALUE)
- oc->hooks_after_value_count++;
- else
- ASSERT(0);
- } else if (item->letter || item->name) {
+ else if (item->cls == OPT_CL_HOOK)
+ oc->hook_count++;
+ else if (item->letter || item->name) {
oc->opt_count++;
if (item->letter > OPT_POSITIONAL_TAIL)
oc->positional_max++;
for (struct opt_item *item = sec->opt; item->cls != OPT_CL_END; item++) {
if (item->cls == OPT_CL_SECTION)
opt_prepare_items(oc, item->u.section);
- else if (item->cls == OPT_CL_HOOK) {
- if (item->flags & OPT_HOOK_BEFORE_ARG)
- oc->hooks_before_arg[oc->hooks_before_arg_count++] = item;
- else if (item->flags & OPT_HOOK_BEFORE_VALUE)
- oc->hooks_before_value[oc->hooks_before_value_count++] = item;
- else if (item->flags & OPT_HOOK_AFTER_VALUE)
- oc->hooks_after_value[oc->hooks_after_value_count++] = item;
- else
- ASSERT(0);
- } else if (item->letter || item->name) {
+ else if (item->cls == OPT_CL_HOOK)
+ oc->hooks[oc->hook_count++] = item;
+ else if (item->letter || item->name) {
struct opt_precomputed * opt = &oc->opts[oc->opt_count++];
opt_precompute(opt, item);
if (item->letter)
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);
oc->shortopt = alloca(sizeof(*oc->shortopt) * (oc->positional_max + 257));
memset(oc->shortopt, 0, sizeof(*oc->shortopt) * (oc->positional_max + 257));
- oc->hooks_before_arg = alloca(sizeof (*oc->hooks_before_arg) * oc->hooks_before_arg_count);
- oc->hooks_before_value = alloca(sizeof (*oc->hooks_before_value) * oc->hooks_before_value_count);
- oc->hooks_after_value = alloca(sizeof (*oc->hooks_after_value) * oc->hooks_after_value_count);
+ oc->hooks = alloca(sizeof (*oc->hooks) * oc->hook_count);
oc->opt_count = 0;
- oc->hooks_before_arg_count = 0;
- oc->hooks_before_value_count = 0;
- oc->hooks_after_value_count = 0;
+ oc->hook_count = 0;
opt_prepare_items(oc, options);
int force_positional = 0;
int i;
for (i=0; argv[i] && !oc->stop_parsing; i++) {
char *arg = argv[i];
- for (int j = 0; j < oc->hooks_before_arg_count; j++)
- oc->hooks_before_arg[j]->u.call(NULL, NULL, oc->hooks_before_arg[j]->ptr);
+ opt_invoke_hooks(oc, OPT_HOOK_BEFORE_ARG, NULL, NULL);
if (arg[0] != '-' || force_positional)
opt_positional(oc, arg);
else {
}
opt_check_required(oc);
+ opt_invoke_hooks(oc, OPT_HOOK_FINAL, NULL, NULL);
return i;
}