]> www.infradead.org Git - users/hch/nvme-cli.git/commitdiff
Make building plug-ins easier
authorKeith Busch <keith.busch@intel.com>
Thu, 9 Jun 2016 15:22:40 +0000 (09:22 -0600)
committerKeith Busch <keith.busch@intel.com>
Thu, 9 Jun 2016 15:54:40 +0000 (09:54 -0600)
This adds a level to the code generation to handle defining and
registering plug-ins.

Signed-off-by: Keith Busch <keith.busch@intel.com>
cmd.h
cmd_handler.h

diff --git a/cmd.h b/cmd.h
index 368f6b930a78b2e4d2da6e7367b046941ab8a8ad..cd1183a11dd8225dabfd78bc4d759e4d8672159e 100644 (file)
--- 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...)
 
index d4210b50abdbe9e91b971e6640fa723725473b12..e5979b068f2d64f1dd01647a2bc3e6b6d022cf20 100644 (file)
@@ -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)