pki: Add global --debug and --options arguments

This allows setting the log level before initializing the library and
loading the plugins, as well as having the top-level command itself in
an options file.
This commit is contained in:
Tobias Brunner
2026-02-06 11:21:34 +01:00
parent dd7b0283ef
commit be68bd50b7
3 changed files with 150 additions and 58 deletions
+130 -51
View File
@@ -1,4 +1,5 @@
/* /*
* Copyright (C) 2012-2026 Tobias Brunner
* Copyright (C) 2009 Martin Willi * Copyright (C) 2009 Martin Willi
* *
* Copyright (C) secunet Security Networks AG * Copyright (C) secunet Security Networks AG
@@ -63,12 +64,48 @@ static struct option command_opts[MAX_COMMANDS > MAX_OPTIONS ? MAX_COMMANDS : MA
*/ */
static char command_optstring[(MAX_COMMANDS > MAX_OPTIONS ? MAX_COMMANDS : MAX_OPTIONS) * 3]; static char command_optstring[(MAX_COMMANDS > MAX_OPTIONS ? MAX_COMMANDS : MAX_OPTIONS) * 3];
/**
* Common options
*/
static command_option_t shared_options[] = {
{
"debug", 'v', 1, "set debug level, default: 1"
},
{
"options", '+', 1, "read command line options from file"
},
};
/**
* Add a single option to command_opts/command_optstr at the current locations.
*/
static void build_option(command_option_t *option, int i, int *pos)
{
command_opts[i].name = option->name;
command_opts[i].has_arg = option->arg;
command_opts[i].val = option->op;
command_optstring[(*pos)++] = option->op;
switch (option->arg)
{
case optional_argument:
command_optstring[(*pos)++] = ':';
/* FALL */
case required_argument:
command_optstring[(*pos)++] = ':';
/* FALL */
case no_argument:
default:
break;
}
}
/** /**
* Build command_opts/command_optstr for the active command * Build command_opts/command_optstr for the active command
*/ */
static void build_opts() static void build_opts()
{ {
int i, pos = 0; int i, j, pos = 0;
memset(command_opts, 0, sizeof(command_opts)); memset(command_opts, 0, sizeof(command_opts));
memset(command_optstring, 0, sizeof(command_optstring)); memset(command_optstring, 0, sizeof(command_optstring));
@@ -80,33 +117,32 @@ static void build_opts()
command_opts[i].val = cmds[i].op; command_opts[i].val = cmds[i].op;
command_optstring[i] = cmds[i].op; command_optstring[i] = cmds[i].op;
} }
/* add shared options not as actual option but as commands with
* argument (which commands usually don't take) */
if (i > MAX_COMMANDS - countof(shared_options))
{
fprintf(stderr, "unable to add global shared options, please "
"increase MAX_COMMANDS\n");
}
else
{
for (j = 0, pos = i; j < countof(shared_options); j++, i++)
{
build_option(&shared_options[j], i, &pos);
}
}
} }
else else
{ {
for (i = 0; cmds[active].options[i].name; i++) for (i = 0; i < MAX_OPTIONS && cmds[active].options[i].name; i++)
{ {
command_opts[i].name = cmds[active].options[i].name; build_option(&cmds[active].options[i], i, &pos);
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 * Wrapper around getopt_long() that skippes common options
*/ */
int command_getopt(char **arg) int command_getopt(char **arg)
{ {
@@ -121,17 +157,25 @@ int command_getopt(char **arg)
case 'v': case 'v':
continue; continue;
default: default:
*arg = optarg; if (arg)
{
*arg = optarg;
}
return op; return op;
} }
} }
} }
/** /**
* Process options common for all commands * Pre-process options common for all commands
*/ */
static bool process_common_opts() static bool process_common_opts(bool init)
{ {
int prevoptind = optind;
bool success = TRUE;
/* don't report any errors during this pre-processing */
opterr = 0;
while (TRUE) while (TRUE)
{ {
switch (getopt_long(argc, argv, command_optstring, command_opts, NULL)) switch (getopt_long(argc, argv, command_optstring, command_opts, NULL))
@@ -139,20 +183,30 @@ static bool process_common_opts()
case '+': case '+':
if (!options->from(options, optarg, &argc, &argv, optind)) if (!options->from(options, optarg, &argc, &argv, optind))
{ {
return FALSE; success = FALSE;
break;
} }
continue; continue;
case 'v': case 'v':
dbg_default_set_level(atoi(optarg)); dbg_default_set_level(atoi(optarg));
continue; continue;
default: default:
if (init)
{ /* stop if we found a known command during initialization,
* otherwise we'd process e.g. --options twice */
break;
}
continue; continue;
case '?': case '?':
return FALSE;
case EOF: case EOF:
return TRUE; break;
} }
break;
} }
/* restore option parser state after pre-processing */
optind = prevoptind;
opterr = 1;
return success;
} }
/** /**
@@ -160,7 +214,7 @@ static bool process_common_opts()
*/ */
void command_register(command_t command) void command_register(command_t command)
{ {
int i; int i, j;
if (registered == MAX_COMMANDS) if (registered == MAX_COMMANDS)
{ {
@@ -173,26 +227,24 @@ void command_register(command_t command)
/* append default options, but not to --help */ /* append default options, but not to --help */
if (!active) if (!active)
{ {
for (i = 0; i < countof(cmds[registered].options) - 1; i++) for (i = 0; i < MAX_OPTIONS; i++)
{ {
if (!cmds[registered].options[i].name) if (!cmds[registered].options[i].name)
{ {
break; break;
} }
} }
if (i > countof(cmds[registered].options) - 3) if (i > MAX_OPTIONS - countof(shared_options))
{ {
fprintf(stderr, "command '%s' registered too many options, please " fprintf(stderr, "command '%s' registered too many options, please "
"increase MAX_OPTIONS\n", command.cmd); "increase MAX_OPTIONS\n", command.cmd);
} }
else else
{ {
cmds[registered].options[i++] = (command_option_t) { for (j = 0; j < countof(shared_options); j++)
"debug", 'v', 1, "set debug level, default: 1" {
}; cmds[registered].options[i++] = shared_options[j];
cmds[registered].options[i++] = (command_option_t) { }
"options", '+', 1, "read command line options from file"
};
} }
for (i = 0; cmds[registered].line[i]; i++) for (i = 0; cmds[registered].line[i]; i++)
{ {
@@ -223,14 +275,25 @@ int command_usage(char *error)
if (active == help_idx) if (active == help_idx)
{ {
fprintf(out, "\nloaded plugins: %s\nusage:\n" fprintf(out, "\n");
" pki command [options]\ncommands:\n", if (lib)
lib->plugins->loaded_plugins(lib->plugins)); {
fprintf(out, "loaded plugins: %s\nusage:\n"
" pki command [options]\ncommands:\n",
lib->plugins->loaded_plugins(lib->plugins));
}
for (i = 0; i < MAX_COMMANDS && cmds[i].cmd; i++) for (i = 0; i < MAX_COMMANDS && cmds[i].cmd; i++)
{ {
fprintf(out, " --%-7s (-%c) %s\n", fprintf(out, " --%-7s (-%c) %s\n",
cmds[i].cmd, cmds[i].op, cmds[i].description); cmds[i].cmd, cmds[i].op, cmds[i].description);
} }
fprintf(out, "options:\n");
for (i = 0; i < countof(shared_options); i++)
{
fprintf(out, " --%-7s (-%c) %s\n",
shared_options[i].name, shared_options[i].op,
shared_options[i].desc);
}
} }
else else
{ {
@@ -249,7 +312,7 @@ int command_usage(char *error)
} }
} }
fprintf(out, "options:\n"); fprintf(out, "options:\n");
for (i = 0; cmds[active].options[i].name; i++) for (i = 0; i < MAX_OPTIONS && cmds[active].options[i].name; i++)
{ {
fprintf(out, " --%-15s (-%c) %s\n", fprintf(out, " --%-15s (-%c) %s\n",
cmds[active].options[i].name, cmds[active].options[i].op, cmds[active].options[i].name, cmds[active].options[i].op,
@@ -280,35 +343,51 @@ static void cleanup()
options->destroy(options); options->destroy(options);
} }
/** /*
* Dispatch commands. * Described in header
*/ */
int command_dispatch(int c, char *v[]) int command_init(int c, char *v[])
{ {
int op, i; int op, i;
options = options_create();
atexit(cleanup);
active = help_idx = registered;
argc = c; argc = c;
argv = v; argv = v;
command_register((command_t){help, 'h', "help", "show usage information"});
options = options_create();
atexit(cleanup);
active = help_idx = registered;
command_register((command_t){help, 'h', "help", "show usage, version and "
"plugin information"});
build_opts(); build_opts();
op = getopt_long(c, v, command_optstring, command_opts, NULL); /* handle common options until we find a command */
if (!process_common_opts(TRUE))
{
return command_usage("invalid --options");
}
op = command_getopt(NULL);
for (i = 0; i < MAX_COMMANDS && cmds[i].cmd; i++) for (i = 0; i < MAX_COMMANDS && cmds[i].cmd; i++)
{ {
if (cmds[i].op == op) if (cmds[i].op == op)
{ {
active = i; active = i;
build_opts(); build_opts();
if (!process_common_opts()) /* handle common options again, now with specific options loaded */
if (!process_common_opts(FALSE))
{ {
return command_usage("invalid options"); return command_usage("invalid --options");
} }
optind = 2; return 0;
return cmds[i].call();
} }
} }
return command_usage(c > 1 ? "invalid operation" : NULL); return command_usage(op != EOF ? "invalid command" : NULL);
}
/*
* Described in header
*/
int command_dispatch()
{
return cmds[active].call();
} }
+11 -5
View File
@@ -1,4 +1,5 @@
/* /*
* Copyright (C) 2014-2026 Tobias Brunner
* Copyright (C) 2009 Martin Willi * Copyright (C) 2009 Martin Willi
* *
* Copyright (C) secunet Security Networks AG * Copyright (C) secunet Security Networks AG
@@ -23,14 +24,14 @@
#define COMMAND_H_ #define COMMAND_H_
/** /**
* Maximum number of commands (+1). * Maximum number of commands (+2).
*/ */
#define MAX_COMMANDS 19 #define MAX_COMMANDS 21
/** /**
* Maximum number of options in a command (+3) * Maximum number of options in a command (+2)
*/ */
#define MAX_OPTIONS 36 #define MAX_OPTIONS 35
/** /**
* Maximum number of usage summary lines (+1) * Maximum number of usage summary lines (+1)
@@ -83,10 +84,15 @@ int command_getopt(char **arg);
*/ */
void command_register(command_t command); void command_register(command_t command);
/**
* Initialize command parsing/dispatching.
*/
int command_init(int argc, char *argv[]);
/** /**
* Dispatch commands. * Dispatch commands.
*/ */
int command_dispatch(int argc, char *argv[]); int command_dispatch();
/** /**
* Show usage information of active command. * Show usage information of active command.
+9 -2
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2012-2023 Tobias Brunner * Copyright (C) 2012-2026 Tobias Brunner
* Copyright (C) 2009 Martin Willi * Copyright (C) 2009 Martin Willi
* *
* Copyright (C) secunet Security Networks AG * Copyright (C) secunet Security Networks AG
@@ -485,6 +485,13 @@ static void remove_callback()
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char *plugins; char *plugins;
int status;
status = command_init(argc, argv);
if (status)
{
return status;
}
atexit(library_deinit); atexit(library_deinit);
if (!library_init(NULL, "pki")) if (!library_init(NULL, "pki"))
@@ -509,5 +516,5 @@ int main(int argc, char *argv[])
add_callback(); add_callback();
atexit(remove_callback); atexit(remove_callback);
return command_dispatch(argc, argv); return command_dispatch();
} }