]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/opt.c
Xtypes: Documentation
[libucw.git] / ucw / opt.c
index 9ef721d672d64d72c68d88fdd94a4e7e65657bb9..d082d03b9e4e11f3c1f0d0f2285ce49cace614de 100644 (file)
--- a/ucw/opt.c
+++ b/ucw/opt.c
@@ -3,6 +3,7 @@
  *
  *     (c) 2013 Jan Moskyto Matejka <mq@ucw.cz>
  *     (c) 2014 Martin Mares <mj@ucw.cz>
+ *     (c) 2014 Pavel Charvat <pchar@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
@@ -39,7 +40,7 @@ void opt_failure(const char * mesg, ...) {
 
 static char *opt_name(struct opt_context *oc, struct opt_precomputed *opt)
 {
-  struct opt_item *item = opt->item;
+  const struct opt_item *item = opt->item;
   char *res;
   if (item->letter >= OPT_POSITIONAL_TAIL)
     res = stk_printf("positional argument #%d", oc->positional_count);
@@ -52,7 +53,7 @@ static char *opt_name(struct opt_context *oc, struct opt_precomputed *opt)
 
 #define THIS_OPT opt_name(oc, opt)
 
-void opt_precompute(struct opt_precomputed *opt, struct opt_item *item)
+void opt_precompute(struct opt_precomputed *opt, const struct opt_item *item)
 {
   opt->item = item;
   opt->count = 0;
@@ -71,10 +72,10 @@ void opt_precompute(struct opt_precomputed *opt, struct opt_item *item)
   opt->flags = flags;
 }
 
-static void opt_invoke_hooks(struct opt_context *oc, uint event, struct opt_item *item, char *value)
+static void opt_invoke_hooks(struct opt_context *oc, uint event, const struct opt_item *item, char *value)
 {
   for (int i = 0; i < oc->hook_count; i++) {
-    struct opt_item *hook = oc->hooks[i];
+    const 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);
@@ -126,7 +127,7 @@ static struct opt_precomputed * opt_find_item_longopt(struct opt_context * oc, c
 }
 
 static void opt_parse_value(struct opt_context * oc, struct opt_precomputed * opt, char * value) {
-  struct opt_item * item = opt->item;
+  const struct opt_item * item = opt->item;
 
   if (opt->count++ && (opt->flags & OPT_SINGLE))
     opt_failure("Option %s must be specified at most once.", THIS_OPT);
@@ -201,6 +202,13 @@ static void opt_parse_value(struct opt_context * oc, struct opt_precomputed * op
                  opt_failure("Cannot parse the value of %s: %s", THIS_OPT, e);
                break;
              }
+         case CT_XTYPE:
+             {
+               const char * e = item->u.xtype->parse(value, ptr, cf_get_pool());
+               if (e)
+                 opt_failure("Cannot parse the value of %s: %s", THIS_OPT, e);
+               break;
+             }
          default:
            ASSERT(0);
        }
@@ -225,6 +233,9 @@ static void opt_parse_value(struct opt_context * oc, struct opt_precomputed * op
        item->u.call(item, value, data);
        break;
       }
+    case OPT_CL_BREAK:
+      oc->stop_parsing = 2;
+      break;
     default:
       ASSERT(0);
   }
@@ -338,7 +349,7 @@ static void opt_count_items(struct opt_context *oc, const struct opt_section *se
 
 static void opt_prepare_items(struct opt_context *oc, const struct opt_section *sec)
 {
-  for (struct opt_item *item = sec->opt; item->cls != OPT_CL_END; item++) {
+  for (const 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)
@@ -357,7 +368,7 @@ static void opt_check_required(struct opt_context *oc)
   for (int i = 0; i < oc->opt_count; i++) {
     struct opt_precomputed *opt = &oc->opts[i];
     if (!opt->count && (opt->flags & OPT_REQUIRED)) {
-      struct opt_item *item = opt->item;
+      const struct opt_item *item = opt->item;
       if (item->letter > OPT_POSITIONAL_TAIL)
        opt_failure("Required positional argument #%d not found.", item->letter - OPT_POSITIONAL_TAIL);
       else if (item->letter == OPT_POSITIONAL_TAIL)
@@ -388,8 +399,9 @@ int opt_parse(const struct opt_section * options, char ** argv) {
   opt_prepare_items(oc, options);
 
   int force_positional = 0;
-  int i;
+  int i, start_i = 0;
   for (i=0; argv[i] && !oc->stop_parsing; i++) {
+    start_i = i;
     char *arg = argv[i];
     opt_invoke_hooks(oc, OPT_HOOK_BEFORE_ARG, NULL, NULL);
     if (arg[0] != '-' || force_positional)
@@ -409,5 +421,5 @@ int opt_parse(const struct opt_section * options, char ** argv) {
 
   opt_check_required(oc);
   opt_invoke_hooks(oc, OPT_HOOK_FINAL, NULL, NULL);
-  return i;
+  return (oc->stop_parsing < 2 ? i : start_i);
 }