swanctl: Fix --uri option
authorMartin Willi <martin@revosec.ch>
Tue, 5 May 2015 08:37:34 +0000 (10:37 +0200)
committerMartin Willi <martin@revosec.ch>
Tue, 5 May 2015 08:46:48 +0000 (10:46 +0200)
As we now pass the vici connection to the command dispatcher callback, we can't
parse the --uri option to create the connection from the same callback. Instead
pre-process the common command options in a separate loop, and ignore the same
options while processing the actual command.

src/swanctl/command.c

index 1c079ec..03cd8b9 100644 (file)
@@ -124,17 +124,8 @@ int command_getopt(char **arg)
                switch (op)
                {
                        case '+':
-                               if (!options->from(options, optarg, &argc, &argv, optind))
-                               {
-                                       /* a error value */
-                                       return 255;
-                               }
-                               continue;
                        case 'v':
-                               dbg_default_set_level(atoi(optarg));
-                               continue;
                        case 'u':
-                               uri = optarg;
                                continue;
                        default:
                                *arg = optarg;
@@ -257,6 +248,37 @@ static void cleanup()
 }
 
 /**
+ * Process options common for all commands
+ */
+static bool process_common_opts()
+{
+       while (TRUE)
+       {
+               switch (getopt_long(argc, argv, command_optstring, command_opts, NULL))
+               {
+                       case '+':
+                               if (!options->from(options, optarg, &argc, &argv, optind))
+                               {
+                                       return FALSE;
+                               }
+                               continue;
+                       case 'v':
+                               dbg_default_set_level(atoi(optarg));
+                               continue;
+                       case 'u':
+                               uri = optarg;
+                               continue;
+                       default:
+                               continue;
+                       case '?':
+                               return FALSE;
+                       case EOF:
+                               return TRUE;
+               }
+       }
+}
+
+/**
  * Open vici connection, call a command
  */
 static int call_command(command_t *cmd)
@@ -303,6 +325,11 @@ int command_dispatch(int c, char *v[])
                        {
                                return command_usage(NULL);
                        }
+                       if (!process_common_opts())
+                       {
+                               return command_usage("invalid options");
+                       }
+                       optind = 2;
                        return call_command(&cmds[i]);
                }
        }