pki: Add support to encode public keys in SSH key format
[strongswan.git] / src / pki / command.c
index 9b02596..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.
  */
-command_t cmds[CMD_MAX];
+command_t cmds[MAX_COMMANDS];
+
+/**
+ * active command.
+ */
+static int active = 0;
+
+/**
+ * number of registered commands
+ */
+static int registered = 0;
+
+/**
+ * help command index
+ */
+static int help_idx;
+
+static int argc;
+
+static char **argv;
+
+static options_t *options;
 
 /**
  * Global options used by all subcommands
  */
-struct option command_opts[CMD_MAX > MAX_OPTIONS ?: MAX_OPTIONS];
+static struct option command_opts[MAX_COMMANDS > MAX_OPTIONS ?: MAX_OPTIONS];
+
+/**
+ * Global optstring used by all subcommands
+ */
+static char command_optstring[(MAX_COMMANDS > MAX_OPTIONS ?: MAX_OPTIONS) * 3];
 
 /**
- * Build long_opts for a specific command
+ * Build command_opts/command_optstr for the active command
  */
-static void build_opts(command_type_t cmd)
+static void build_opts()
 {
-       int i;
+       int i, pos = 0;
 
        memset(command_opts, 0, sizeof(command_opts));
-       if (cmd == CMD_HELP)
+       memset(command_optstring, 0, sizeof(command_optstring));
+       if (active == help_idx)
        {
-               for (i = 0; i < CMD_MAX; i++)
+               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
        {
-               for (i = 0; cmds[cmd].options[i].name; i++)
+               for (i = 0; cmds[active].options[i].name; i++)
                {
-                       command_opts[i].name = cmds[cmd].options[i].name;
-                       command_opts[i].has_arg = cmds[cmd].options[i].arg;
-                       command_opts[i].val = cmds[cmd].options[i].op;
+                       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;
                }
        }
 }
@@ -60,15 +136,36 @@ static void build_opts(command_type_t cmd)
 /**
  * Register a command
  */
-void command_register(command_type_t type, command_t command)
+void command_register(command_t command)
 {
-       cmds[type] = 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++;
 }
 
 /**
  * Print usage text, with an optional error
  */
-int command_usage(command_type_t cmd, char *error)
+int command_usage(char *error)
 {
        FILE *out = stdout;
        int i;
@@ -79,65 +176,88 @@ int command_usage(command_type_t cmd, 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 (cmd == CMD_HELP)
+       if (active == help_idx)
        {
-               for (i = 0; i < CMD_MAX; i++)
+               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
        {
-               for (i = 0; cmds[cmd].line[i]; i++)
+               for (i = 0; cmds[active].line[i]; i++)
                {
                        if (i == 0)
                        {
-                               fprintf(out, "  pki --%s %s\n", cmds[cmd].cmd, cmds[cmd].line[i]);
+                               fprintf(out, "  pki --%s %s\n",
+                                               cmds[active].cmd, cmds[active].line[i]);
                        }
                        else
                        {
-                               fprintf(out, "               %s\n", cmds[cmd].line[i]);
+                               fprintf(out, "              %s\n", cmds[active].line[i]);
                        }
                }
-               for (i = 0; cmds[cmd].options[i].name; i++)
+               for (i = 0; cmds[active].options[i].name; i++)
                {
-                       fprintf(out, "        --%-8s %s\n",
-                                       cmds[cmd].options[i].name, cmds[cmd].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;
 }
 
 
-
 /**
  * Show usage information
  */
 static int help(int argc, char *argv[])
 {
-       return command_usage(CMD_HELP, NULL);
+       return command_usage(NULL);
+}
+
+/**
+ * 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;
 
-       command_register(CMD_HELP, (command_t) {
-                                        help, 'h', "help", "show usage information"});
-       build_opts(CMD_HELP);
-       op = getopt_long(argc, argv, "", command_opts, NULL);
-       for (i = 0; i < CMD_MAX; 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(c, v, command_optstring, command_opts, NULL);
+       for (i = 0; cmds[i].cmd; i++)
        {
                if (cmds[i].op == op)
                {
-                       build_opts(i);
-                       return cmds[i].call(argc, argv);
+                       active = i;
+                       build_opts();
+                       return cmds[i].call();
                }
        }
-       return command_usage(CMD_HELP, "invalid operation");
+       return command_usage(c > 1 ? "invalid operation" : NULL);
 }