aboutsummaryrefslogtreecommitdiffstats
path: root/src/pki/command.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pki/command.c')
-rw-r--r--src/pki/command.c88
1 files changed, 81 insertions, 7 deletions
diff --git a/src/pki/command.c b/src/pki/command.c
index 2cf136539..8f5bc14f8 100644
--- a/src/pki/command.c
+++ b/src/pki/command.c
@@ -14,12 +14,18 @@
*/
#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 <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_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); 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++;
}
/**
@@ -159,24 +221,36 @@ 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");