pki: Add support to encode public keys in SSH key format
[strongswan.git] / src / pki / command.c
index 1055955..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,25 +46,38 @@ 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];
 
 /**
- * Build long_opts for a specific command
+ * Global optstring used by all subcommands
+ */
+static char command_optstring[(MAX_COMMANDS > MAX_OPTIONS ?: MAX_OPTIONS) * 3];
+
+/**
+ * Build command_opts/command_optstr for the active command
  */
 static void build_opts()
 {
-       int i;
+       int i, pos = 0;
 
        memset(command_opts, 0, sizeof(command_opts));
+       memset(command_optstring, 0, sizeof(command_optstring));
        if (active == help_idx)
        {
                for (i = 0; cmds[i].cmd; i++)
                {
                        command_opts[i].name = cmds[i].cmd;
                        command_opts[i].val = cmds[i].op;
+                       command_optstring[i] = cmds[i].op;
                }
        }
        else
@@ -68,6 +87,48 @@ static void build_opts()
                        command_opts[i].name = cmds[active].options[i].name;
                        command_opts[i].has_arg = cmds[active].options[i].arg;
                        command_opts[i].val = cmds[active].options[i].op;
+                       command_optstring[pos++] = cmds[active].options[i].op;
+                       switch (cmds[active].options[i].arg)
+                       {
+                               case optional_argument:
+                                       command_optstring[pos++] = ':';
+                                       /* FALL */
+                               case required_argument:
+                                       command_optstring[pos++] = ':';
+                                       /* FALL */
+                               case no_argument:
+                               default:
+                                       break;
+                       }
+               }
+       }
+}
+
+/**
+ * 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;
                }
        }
 }
@@ -77,7 +138,28 @@ static void build_opts()
  */
 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++;
 }
 
 /**
@@ -94,12 +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++)
                {
-                       fprintf(out, "  pki --%-6s %s\n", cmds[i].cmd, cmds[i].description);
+                       fprintf(out, "  pki --%-7s (-%c)  %s\n",
+                                       cmds[i].cmd, cmds[i].op, cmds[i].description);
                }
        }
        else
@@ -118,8 +208,9 @@ int command_usage(char *error)
                }
                for (i = 0; cmds[active].options[i].name; i++)
                {
-                       fprintf(out, "        --%-8s %s\n",
-                                       cmds[active].options[i].name, 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;
@@ -135,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_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);
 }