]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/opt.c
tableprinter: update of xtypes for tableprinter
[libucw.git] / ucw / opt.c
index 5598a13b340bdbff9402495d8c38781feebfefa6..9ef721d672d64d72c68d88fdd94a4e7e65657bb9 100644 (file)
--- a/ucw/opt.c
+++ b/ucw/opt.c
@@ -18,7 +18,7 @@
 #include <alloca.h>
 #include <math.h>
 
-static uns opt_default_value_flags[] = {
+static uint opt_default_value_flags[] = {
     [OPT_CL_BOOL] = OPT_NO_VALUE,
     [OPT_CL_STATIC] = OPT_MAYBE_VALUE,
     [OPT_CL_MULTIPLE] = OPT_REQUIRED_VALUE,
@@ -57,7 +57,7 @@ void opt_precompute(struct opt_precomputed *opt, struct opt_item *item)
   opt->item = item;
   opt->count = 0;
   opt->name = item->name;
-  uns flags = item->flags;
+  uint flags = item->flags;
 
   if (item->letter >= OPT_POSITIONAL_TAIL) {
     flags &= ~OPT_VALUE_FLAGS;
@@ -71,7 +71,7 @@ void opt_precompute(struct opt_precomputed *opt, struct opt_item *item)
   opt->flags = flags;
 }
 
-static void opt_invoke_hooks(struct opt_context *oc, uns event, struct opt_item *item, char *value)
+static void opt_invoke_hooks(struct opt_context *oc, uint event, struct opt_item *item, char *value)
 {
   for (int i = 0; i < oc->hook_count; i++) {
     struct opt_item *hook = oc->hooks[i];
@@ -83,7 +83,7 @@ static void opt_invoke_hooks(struct opt_context *oc, uns event, struct opt_item
 }
 
 static struct opt_precomputed * opt_find_item_longopt(struct opt_context * oc, char * str) {
-  uns len = strlen(str);
+  uint len = strlen(str);
   struct opt_precomputed * candidate = NULL;
 
   for (int i = 0; i < oc->opt_count; i++) {
@@ -94,9 +94,22 @@ static struct opt_precomputed * opt_find_item_longopt(struct opt_context * oc, c
     if (!strncmp(opt->name, str, len)) {
       if (strlen(opt->name) == len)
        return opt;
-    } else if (opt->item->cls == OPT_CL_BOOL && !strncmp("no-", str, 3) && !strncmp(opt->name, str+3, len-3)) {
-      if (strlen(opt->name) == len-3)
-       return opt;
+    } else if (opt->item->cls == OPT_CL_BOOL) {
+      if (opt->flags & OPT_NEGATIVE) {
+       // If the option is called no-X, match X as well
+       if (!strncmp("no-", opt->name, 3) && !strncmp(opt->name+3, str, len)) {
+         if (strlen(opt->name) == len+3)
+           return opt;
+       } else
+         continue;
+      } else {
+       // Match no-X as well
+       if (!strncmp("no-", str, 3) && !strncmp(opt->name, str+3, len-3)) {
+         if (strlen(opt->name) == len-3)
+           return opt;
+       } else
+         continue;
+      }
     } else
       continue;
 
@@ -222,13 +235,16 @@ static void opt_parse_value(struct opt_context * oc, struct opt_precomputed * op
 static int opt_longopt(struct opt_context * oc, char ** argv, int index) {
   int eaten = 0;
   char * name_in = argv[index] + 2; // skipping the -- on the beginning
-  uns pos = strchrnul(name_in, '=') - name_in;
+  uint pos = strchrnul(name_in, '=') - name_in;
   struct opt_precomputed * opt = opt_find_item_longopt(oc, strndupa(name_in, pos));
   char * value = NULL;
 
   opt->flags |= OPT_SEEN_AS_LONG;
 
-  if (opt->item->cls == OPT_CL_BOOL && !strncmp(name_in, "no-", 3) && !strncmp(name_in+3, opt->item->name, pos-3)) {
+  if (opt->item->cls == OPT_CL_BOOL &&
+      ((opt->flags & OPT_NEGATIVE)
+         ? (!strncmp(opt->item->name, "no-", 3) && !strncmp(name_in, opt->item->name + 3, pos-3))
+         : (!strncmp(name_in, "no-", 3) && !strncmp(name_in+3, opt->item->name, pos-3)))) {
     if (name_in[pos])
       opt_failure("Option --%s must not have any value.", name_in);
     value = "n";
@@ -295,12 +311,12 @@ static int opt_shortopt(struct opt_context * oc, char ** argv, int index) {
 
 static void opt_positional(struct opt_context * oc, char * value) {
   oc->positional_count++;
-  uns id = oc->positional_count > oc->positional_max ? OPT_POSITIONAL_TAIL : OPT_POSITIONAL(oc->positional_count);
+  uint id = oc->positional_count > oc->positional_max ? OPT_POSITIONAL_TAIL : OPT_POSITIONAL(oc->positional_count);
   struct opt_precomputed * opt = oc->shortopt[id];
   if (!opt)
     opt_failure("Too many positional arguments.");
   else {
-    opt->flags &= OPT_SEEN_AS_LONG;
+    opt->flags &= ~OPT_SEEN_AS_LONG;
     opt_parse_value(oc, opt, value);
   }
 }
@@ -363,8 +379,8 @@ int opt_parse(const struct opt_section * options, char ** argv) {
 
   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->shortopt = alloca(sizeof(*oc->shortopt) * (oc->positional_max + OPT_POSITIONAL_TAIL + 1));
+  memset(oc->shortopt, 0, sizeof(*oc->shortopt) * (oc->positional_max + OPT_POSITIONAL_TAIL + 1));
   oc->hooks = alloca(sizeof (*oc->hooks) * oc->hook_count);
 
   oc->opt_count = 0;