pki: Add support to encode public keys in SSH key format
[strongswan.git] / src / pki / command.c
index 2cf1365..984da59 100644 (file)
  */
 
 #include "command.h"
+#include "pki.h"
 
-
+#define _GNU_SOURCE
+#include <getopt.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 
+#include <library.h>
+#include <utils/debug.h>
+#include <utils/optionsfrom.h>
+
 /**
  * Registered commands.
  */
@@ -40,15 +46,21 @@ static int registered = 0;
  */
 static int help_idx;
 
+static int argc;
+
+static char **argv;
+
+static options_t *options;
+
 /**
  * Global options used by all subcommands
  */
-struct option command_opts[MAX_COMMANDS > MAX_OPTIONS ?: MAX_OPTIONS];
+static struct option command_opts[MAX_COMMANDS > MAX_OPTIONS ?: MAX_OPTIONS];
 
 /**
  * Global optstring used by all subcommands
  */
-char command_optstring[(MAX_COMMANDS > MAX_OPTIONS ?: MAX_OPTIONS) * 3];
+static char command_optstring[(MAX_COMMANDS > MAX_OPTIONS ?: MAX_OPTIONS) * 3];
 
 /**
  * Build command_opts/command_optstr for the active command
@@ -93,11 +105,61 @@ static void build_opts()
 }
 
 /**
+ * getopt_long wrapper
+ */
+int command_getopt(char **arg)
+{
+       int op;
+
+       while (TRUE)
+       {
+               op = getopt_long(argc, argv, command_optstring, command_opts, NULL);
+               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;
+                       default:
+                               *arg = optarg;
+                               return op;
+               }
+       }
+}
+
+/**
  * Register a command
  */
 void command_register(command_t command)
 {
-       cmds[registered++] = command;
+       int i;
+
+       cmds[registered] = command;
+       /* append default options, but not to --help */
+       if (!active)
+       {
+               for (i = 0; i < countof(cmds[registered].options) - 1; i++)
+               {
+                       if (cmds[registered].options[i].name)
+                       {
+                               continue;
+                       }
+                       cmds[registered].options[i++] = (command_option_t) {
+                               "debug",        'v', 1, "set debug level, default: 1"
+                       };
+                       cmds[registered].options[i++] = (command_option_t) {
+                               "options",      '+', 1, "read command line options from file"
+                       };
+                       break;
+               }
+       }
+       registered++;
 }
 
 /**
@@ -106,7 +168,6 @@ void command_register(command_t command)
 int command_usage(char *error)
 {
        FILE *out = stdout;
-       char buf[64];
        int i;
 
        if (error)
@@ -115,13 +176,20 @@ int command_usage(char *error)
                fprintf(out, "Error: %s\n", error);
        }
        fprintf(out, "strongSwan %s PKI tool\n", VERSION);
+
+       if (active == help_idx)
+       {
+               fprintf(out, "loaded plugins: %s\n",
+                               lib->plugins->loaded_plugins(lib->plugins));
+       }
+
        fprintf(out, "usage:\n");
        if (active == help_idx)
        {
                for (i = 0; cmds[i].cmd; i++)
                {
-                       snprintf(buf, sizeof(buf), "--%s (-%c)", cmds[i].cmd, cmds[i].op);
-                       fprintf(out, "  pki %-14s %s\n", buf, cmds[i].description);
+                       fprintf(out, "  pki --%-7s (-%c)  %s\n",
+                                       cmds[i].cmd, cmds[i].op, cmds[i].description);
                }
        }
        else
@@ -140,10 +208,9 @@ int command_usage(char *error)
                }
                for (i = 0; cmds[active].options[i].name; i++)
                {
-                       snprintf(buf, sizeof(buf), "--%s (-%c)",
-                                        cmds[active].options[i].name, cmds[active].options[i].op);
-                       fprintf(out, "        %-15s %s\n",
-                                       buf, cmds[active].options[i].desc);
+                       fprintf(out, "        --%-15s (-%c)  %s\n",
+                                       cmds[active].options[i].name, cmds[active].options[i].op,
+                                       cmds[active].options[i].desc);
                }
        }
        return error != NULL;
@@ -159,26 +226,38 @@ static int help(int argc, char *argv[])
 }
 
 /**
+ * Dispatch cleanup hook
+ */
+static void cleanup()
+{
+       options->destroy(options);
+}
+
+/**
  * Dispatch commands.
  */
-int command_dispatch(int argc, char *argv[])
+int command_dispatch(int c, char *v[])
 {
        int op, i;
 
+       options = options_create();
+       atexit(cleanup);
        active = help_idx = registered;
+       argc = c;
+       argv = v;
        command_register((command_t){help, 'h', "help", "show usage information"});
 
        build_opts();
-       op = getopt_long(argc, argv, command_optstring, command_opts, NULL);
+       op = getopt_long(c, v, command_optstring, command_opts, NULL);
        for (i = 0; cmds[i].cmd; i++)
        {
                if (cmds[i].op == op)
                {
                        active = i;
                        build_opts();
-                       return cmds[i].call(argc, argv);
+                       return cmds[i].call();
                }
        }
-       return command_usage("invalid operation");
+       return command_usage(c > 1 ? "invalid operation" : NULL);
 }