From: Keith Busch Date: Tue, 7 Jun 2016 22:38:40 +0000 (-0600) Subject: Make help and version generic commands X-Git-Tag: v0.8~20 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=dcfe5a128ebe39ae9f90c2ec38dd67c07192d91a;p=users%2Fhch%2Fnvme-cli.git Make help and version generic commands Plugins don't need to implement these. Signed-off-by: Keith Busch --- diff --git a/nvme-builtin.h b/nvme-builtin.h index 84c49fc..d8dc898 100644 --- a/nvme-builtin.h +++ b/nvme-builtin.h @@ -58,8 +58,6 @@ COMMAND_LIST( ENTRY("connect-all", "Discover and Connect to NVMeoF subsystems", connect_all_cmd) ENTRY("connect", "Connect to NVMeoF subsystem", connect_cmd) ENTRY("disconnect", "Disconnect from NVMeoF subsystem", disconnect_cmd) - ENTRY("version", "Shows the program version", version) - ENTRY("help", "Display this help", help) ); #endif diff --git a/nvme.c b/nvme.c index bc7e894..193096f 100644 --- a/nvme.c +++ b/nvme.c @@ -2888,29 +2888,6 @@ static int disconnect_cmd(int argc, char **argv, struct command *command, struct return disconnect(desc, argc, argv); } -static int version(int argc, char **argv, struct command *cmd, struct plugin *plugin) -{ - printf("nvme version %s\n", nvme_version_string); - return 0; -} - -static int help(int argc, char **argv, struct command *command, struct plugin *plugin) -{ - char man[0x100]; - - if (argc == 1) { - general_help(plugin); - return 0; - } - - sprintf(man, "nvme-%s", argv[1]); - if (execlp("man", "man", man, (char *)NULL)) { - perror(argv[1]); - exit(errno); - } - return 0; -} - void register_extention(struct plugin *plugin) { plugin->parent = &nvme; diff --git a/plugin.c b/plugin.c index 06950b1..9fbf840 100644 --- a/plugin.c +++ b/plugin.c @@ -1,14 +1,47 @@ +#include #include #include +#include #include "plugin.h" #include "argconfig.h" +static int version(struct plugin *plugin) +{ + struct program *prog = plugin->parent; + + if (plugin->name) + printf("%s %s version %s\n", prog->name, plugin->name, prog->version); + else + printf("%s version %s\n", prog->name, prog->version); + return 0; +} + +static int help(int argc, char **argv, struct plugin *plugin) +{ + char man[0x100]; + + if (argc == 1) { + general_help(plugin); + return 0; + } + + sprintf(man, "%s-%s", plugin->name, argv[1]); + if (execlp("man", "man", man, (char *)NULL)) { + perror(argv[1]); + exit(errno); + } + return 0; +} + void usage(struct plugin *plugin) { struct program *prog = plugin->parent; - printf("usage: %s %s\n", prog->name, prog->usage); + if (plugin->name) + printf("usage: %s %s %s\n", prog->name, plugin->name, prog->usage); + else + printf("usage: %s %s\n", prog->name, prog->usage); } void general_help(struct plugin *plugin) @@ -36,6 +69,9 @@ void general_help(struct plugin *plugin) for (; plugin->commands[i]; i++) printf(" %-*s %s\n", 15, plugin->commands[i]->name, plugin->commands[i]->help); + + printf(" %-*s %s\n", 15, "version", "Shows the program version"); + printf(" %-*s %s\n", 15, "help", "Display this help"); printf("\n"); if (plugin->name) @@ -68,6 +104,7 @@ int handle_plugin(int argc, char **argv, struct plugin *plugin) unsigned i = 0; char *str = argv[0]; char use[0x100]; + struct plugin *extension; struct program *prog = plugin->parent; @@ -89,6 +126,10 @@ int handle_plugin(int argc, char **argv, struct plugin *plugin) for (; plugin->commands[i]; i++) { struct command *cmd = plugin->commands[i]; + if (!strcmp(str, "help")) + return help(argc, argv, plugin); + if (!strcmp(str, "version")) + return version(plugin); if (strcmp(str, cmd->name)) continue;