Handle pki --debug and --options in a generic way for all command
This commit is contained in:
+81
-7
@@ -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
|
||||
@@ -92,12 +104,62 @@ 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++;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,25 +220,37 @@ static int help(int argc, char *argv[])
|
||||
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;
|
||||
|
||||
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");
|
||||
|
||||
Reference in New Issue
Block a user