From: Keith Busch Date: Thu, 9 Jun 2016 15:22:40 +0000 (-0600) Subject: Make building plug-ins easier X-Git-Tag: v0.8~11 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=4476ec847ab23bb5f46d969b0a58e6ea0f924671;p=users%2Fhch%2Fnvme-cli.git Make building plug-ins easier This adds a level to the code generation to handle defining and registering plug-ins. Signed-off-by: Keith Busch --- diff --git a/cmd.h b/cmd.h index 368f6b9..cd1183a 100644 --- a/cmd.h +++ b/cmd.h @@ -1,6 +1,9 @@ #ifndef _CMD_H #define _CMD_H +#undef PLUGIN +#define PLUGIN(n, c) + #undef COMMAND_LIST #define COMMAND_LIST(args...) diff --git a/cmd_handler.h b/cmd_handler.h index d4210b5..e5979b0 100644 --- a/cmd_handler.h +++ b/cmd_handler.h @@ -1,7 +1,12 @@ /* * Stage 1 + * + * Define function prototypes. */ +#undef NAME +#define NAME(n, d) + #undef ENTRY #define ENTRY(n, h, f) \ static int f(int argc, char **argv, struct command *command, struct plugin *plugin); @@ -9,12 +14,20 @@ static int f(int argc, char **argv, struct command *command, struct plugin *plug #undef COMMAND_LIST #define COMMAND_LIST(args...) args +#undef PLUGIN +#define PLUGIN(name, cmds) cmds + #include CMD_INCLUDE(CMD_INC_FILE) /* * Stage 2 + * + * Define command structures. */ +#undef NAME +#define NAME(n, d) + #undef ENTRY #define ENTRY(n, h, f) \ static struct command f ## _cmd = { \ @@ -26,12 +39,20 @@ static struct command f ## _cmd = { \ #undef COMMAND_LIST #define COMMAND_LIST(args...) args +#undef PLUGIN +#define PLUGIN(name, cmds) cmds + #include CMD_INCLUDE(CMD_INC_FILE) /* * Stage 3 + * + * Generate list of commands for the plugin. */ +#undef NAME +#define NAME(n, d) + #undef ENTRY #define ENTRY(n, h, f) &f ## _cmd, @@ -42,4 +63,34 @@ static struct command *commands[] = { \ NULL, \ }; +#undef PLUGIN +#define PLUGIN(name, cmds) cmds + +#include CMD_INCLUDE(CMD_INC_FILE) + +/* + * Stage 4 + * + * Define and register plugin + */ + +#undef NAME +#define NAME(n, d) .name = n, .desc = d, + +#undef COMMAND_LIST +#define COMMAND_LIST(args...) + +#undef PLUGIN +#define PLUGIN(name, cmds) \ +static struct plugin plugin = { \ + name \ + .commands = commands \ +}; \ + \ +static void init() __attribute__((constructor)); \ +static void init() \ +{ \ + register_extension(&plugin); \ +} + #include CMD_INCLUDE(CMD_INC_FILE)