Use dynamic registration/usage invocation of command types
authorMartin Willi <martin@strongswan.org>
Thu, 10 Sep 2009 10:44:06 +0000 (12:44 +0200)
committerMartin Willi <martin@strongswan.org>
Thu, 10 Sep 2009 14:18:30 +0000 (16:18 +0200)
src/pki/Makefile.am
src/pki/command.c
src/pki/command.h
src/pki/commands/gen.c
src/pki/commands/issue.c
src/pki/commands/keyid.c
src/pki/commands/pub.c
src/pki/commands/self.c
src/pki/commands/verify.c

index 980ca56..3f2694d 100644 (file)
@@ -1,8 +1,9 @@
 ipsec_PROGRAMS = pki
 
 pki_SOURCES = pki.c pki.h command.c command.h \
-       commands/gen.c commands/pub.c commands/keyid.c \
-       commands/self.c commands/issue.c commands/verify.c
+       commands/verify.c commands/issue.c commands/self.c \
+       commands/keyid.c commands/pub.c commands/gen.c
+
 pki_LDADD = $(top_builddir)/src/libstrongswan/libstrongswan.la
 INCLUDES = -I$(top_srcdir)/src/libstrongswan
 AM_CFLAGS = \
index 9b02596..1055955 100644 (file)
 /**
  * Registered commands.
  */
-command_t cmds[CMD_MAX];
+command_t cmds[MAX_COMMANDS];
+
+/**
+ * active command.
+ */
+static int active = 0;
+
+/**
+ * number of registered commands
+ */
+static int registered = 0;
+
+/**
+ * help command index
+ */
+static int help_idx;
 
 /**
  * Global options used by all subcommands
  */
-struct option command_opts[CMD_MAX > MAX_OPTIONS ?: MAX_OPTIONS];
+struct option command_opts[MAX_COMMANDS > MAX_OPTIONS ?: MAX_OPTIONS];
 
 /**
  * Build long_opts for a specific command
  */
-static void build_opts(command_type_t cmd)
+static void build_opts()
 {
        int i;
 
        memset(command_opts, 0, sizeof(command_opts));
-       if (cmd == CMD_HELP)
+       if (active == help_idx)
        {
-               for (i = 0; i < CMD_MAX; i++)
+               for (i = 0; cmds[i].cmd; i++)
                {
                        command_opts[i].name = cmds[i].cmd;
                        command_opts[i].val = cmds[i].op;
@@ -48,11 +63,11 @@ static void build_opts(command_type_t cmd)
        }
        else
        {
-               for (i = 0; cmds[cmd].options[i].name; i++)
+               for (i = 0; cmds[active].options[i].name; i++)
                {
-                       command_opts[i].name = cmds[cmd].options[i].name;
-                       command_opts[i].has_arg = cmds[cmd].options[i].arg;
-                       command_opts[i].val = cmds[cmd].options[i].op;
+                       command_opts[i].name = cmds[active].options[i].name;
+                       command_opts[i].has_arg = cmds[active].options[i].arg;
+                       command_opts[i].val = cmds[active].options[i].op;
                }
        }
 }
@@ -60,15 +75,15 @@ static void build_opts(command_type_t cmd)
 /**
  * Register a command
  */
-void command_register(command_type_t type, command_t command)
+void command_register(command_t command)
 {
-       cmds[type] = command;
+       cmds[registered++] = command;
 }
 
 /**
  * Print usage text, with an optional error
  */
-int command_usage(command_type_t cmd, char *error)
+int command_usage(char *error)
 {
        FILE *out = stdout;
        int i;
@@ -80,43 +95,43 @@ int command_usage(command_type_t cmd, char *error)
        }
        fprintf(out, "strongSwan %s PKI tool\n", VERSION);
        fprintf(out, "usage:\n");
-       if (cmd == CMD_HELP)
+       if (active == help_idx)
        {
-               for (i = 0; i < CMD_MAX; i++)
+               for (i = 0; cmds[i].cmd; i++)
                {
                        fprintf(out, "  pki --%-6s %s\n", cmds[i].cmd, cmds[i].description);
                }
        }
        else
        {
-               for (i = 0; cmds[cmd].line[i]; i++)
+               for (i = 0; cmds[active].line[i]; i++)
                {
                        if (i == 0)
                        {
-                               fprintf(out, "  pki --%s %s\n", cmds[cmd].cmd, cmds[cmd].line[i]);
+                               fprintf(out, "  pki --%s %s\n",
+                                               cmds[active].cmd, cmds[active].line[i]);
                        }
                        else
                        {
-                               fprintf(out, "               %s\n", cmds[cmd].line[i]);
+                               fprintf(out, "              %s\n", cmds[active].line[i]);
                        }
                }
-               for (i = 0; cmds[cmd].options[i].name; i++)
+               for (i = 0; cmds[active].options[i].name; i++)
                {
                        fprintf(out, "        --%-8s %s\n",
-                                       cmds[cmd].options[i].name, cmds[cmd].options[i].desc);
+                                       cmds[active].options[i].name, cmds[active].options[i].desc);
                }
        }
        return error != NULL;
 }
 
 
-
 /**
  * Show usage information
  */
 static int help(int argc, char *argv[])
 {
-       return command_usage(CMD_HELP, NULL);
+       return command_usage(NULL);
 }
 
 /**
@@ -126,18 +141,20 @@ int command_dispatch(int argc, char *argv[])
 {
        int op, i;
 
-       command_register(CMD_HELP, (command_t) {
-                                        help, 'h', "help", "show usage information"});
-       build_opts(CMD_HELP);
+       active = help_idx = registered;
+       command_register((command_t){help, 'h', "help", "show usage information"});
+
+       build_opts();
        op = getopt_long(argc, argv, "", command_opts, NULL);
-       for (i = 0; i < CMD_MAX; i++)
+       for (i = 0; cmds[i].cmd; i++)
        {
                if (cmds[i].op == op)
                {
-                       build_opts(i);
+                       active = i;
+                       build_opts();
                        return cmds[i].call(argc, argv);
                }
        }
-       return command_usage(CMD_HELP, "invalid operation");
+       return command_usage("invalid operation");
 }
 
index 852a84d..b641814 100644 (file)
 #include <getopt.h>
 
 /**
+ * Maximum number of commands.
+ */
+#define MAX_COMMANDS 10
+
+/**
  * Maximum number of options in a command (+1)
  */
 #define MAX_OPTIONS 14
@@ -65,28 +70,14 @@ struct command_t {
 };
 
 /**
- * Type of available commands
- */
-enum command_type_t {
-       CMD_HELP = 0,
-       CMD_GEN,
-       CMD_PUB,
-       CMD_KEYID,
-       CMD_SELF,
-       CMD_ISSUE,
-       CMD_VERIFY,
-       CMD_MAX
-};
-
-/**
- * Options of the currently processing command.
+ * Options of the active command.
  */
 extern struct option command_opts[];
 
 /**
  * Register a command.
  */
-void command_register(command_type_t type, command_t command);
+void command_register(command_t command);
 
 /**
  * Dispatch commands.
@@ -96,6 +87,6 @@ int command_dispatch(int argc, char *argv[]);
 /**
  * Show usage information of active command.
  */
-int command_usage(command_type_t cmd, char *error);
+int command_usage(char *error);
 
 #endif /* COMMAND_H_ @}*/
index 4bc8f8c..791c135 100644 (file)
@@ -31,7 +31,7 @@ static int gen(int argc, char *argv[])
                switch (getopt_long(argc, argv, "", command_opts, NULL))
                {
                        case 'h':
-                               return command_usage(CMD_GEN, NULL);
+                               return command_usage(NULL);
                        case 't':
                                if (streq(optarg, "rsa"))
                                {
@@ -43,26 +43,26 @@ static int gen(int argc, char *argv[])
                                }
                                else
                                {
-                                       return command_usage(CMD_GEN, "invalid key type");
+                                       return command_usage("invalid key type");
                                }
                                continue;
                        case 'o':
                                if (!get_form(optarg, &form, FALSE))
                                {
-                                       return command_usage(CMD_GEN, "invalid key output format");
+                                       return command_usage("invalid key output format");
                                }
                                continue;
                        case 's':
                                size = atoi(optarg);
                                if (!size)
                                {
-                                       return command_usage(CMD_GEN, "invalid key size");
+                                       return command_usage("invalid key size");
                                }
                                continue;
                        case EOF:
                                break;
                        default:
-                               return command_usage(CMD_GEN, "invalid --gen option");
+                               return command_usage("invalid --gen option");
                }
                break;
        }
@@ -110,7 +110,7 @@ static int gen(int argc, char *argv[])
  */
 static void __attribute__ ((constructor))reg()
 {
-       command_register(CMD_GEN, (command_t) {
+       command_register((command_t) {
                gen, 'g', "gen", "generate a new private key",
                {"[--type rsa|ecdsa] [--size bits] [--outform der|pem|pgp]"},
                {
index 097d7d2..c141827 100644 (file)
@@ -247,7 +247,7 @@ end:
 usage:
        san->destroy_offset(san, offsetof(identification_t, destroy));
        options->destroy(options);
-       return command_usage(CMD_ISSUE, error);
+       return command_usage(error);
 }
 
 /**
@@ -255,7 +255,7 @@ usage:
  */
 static void __attribute__ ((constructor))reg()
 {
-       command_register(CMD_ISSUE, (command_t) {
+       command_register((command_t) {
                issue, 'i', "issue",
                "issue a certificate using a CA certificate and key",
                {"[--in file] [--type pub|pkcs10]",
index e270c94..128645e 100644 (file)
@@ -37,7 +37,7 @@ static int keyid(int argc, char *argv[])
                switch (getopt_long(argc, argv, "", command_opts, NULL))
                {
                        case 'h':
-                               return command_usage(CMD_KEYID, NULL);
+                               return command_usage(NULL);
                        case 't':
                                if (streq(optarg, "rsa-priv"))
                                {
@@ -61,7 +61,7 @@ static int keyid(int argc, char *argv[])
                                }
                                else
                                {
-                                       return command_usage(CMD_KEYID, "invalid input type");
+                                       return command_usage( "invalid input type");
                                }
                                continue;
                        case 'i':
@@ -70,7 +70,7 @@ static int keyid(int argc, char *argv[])
                        case EOF:
                                break;
                        default:
-                               return command_usage(CMD_KEYID, "invalid --keyid option");
+                               return command_usage("invalid --keyid option");
                }
                break;
        }
@@ -144,7 +144,7 @@ static int keyid(int argc, char *argv[])
  */
 static void __attribute__ ((constructor))reg()
 {
-       command_register(CMD_KEYID, (command_t)
+       command_register((command_t)
                { keyid, 'k', "keyid",
                "calculate key identifiers of a key/certificate",
                {"[--in file] [--type rsa-priv|ecdsa-priv|pub|x509]"},
index 5e7839f..16bd0b0 100644 (file)
@@ -38,7 +38,7 @@ static int pub(int argc, char *argv[])
                switch (getopt_long(argc, argv, "", command_opts, NULL))
                {
                        case 'h':
-                               return command_usage(CMD_PUB, NULL);
+                               return command_usage(NULL);
                        case 't':
                                if (streq(optarg, "rsa"))
                                {
@@ -57,13 +57,13 @@ static int pub(int argc, char *argv[])
                                }
                                else
                                {
-                                       return command_usage(CMD_PUB, "invalid input type");
+                                       return command_usage("invalid input type");
                                }
                                continue;
                        case 'f':
                                if (!get_form(optarg, &form, TRUE))
                                {
-                                       return command_usage(CMD_PUB, "invalid output format");
+                                       return command_usage("invalid output format");
                                }
                                continue;
                        case 'i':
@@ -72,7 +72,7 @@ static int pub(int argc, char *argv[])
                        case EOF:
                                break;
                        default:
-                               return command_usage(CMD_PUB, "invalid --pub option");
+                               return command_usage("invalid --pub option");
                }
                break;
        }
@@ -136,7 +136,7 @@ static int pub(int argc, char *argv[])
  */
 static void __attribute__ ((constructor))reg()
 {
-       command_register(CMD_PUB, (command_t) {
+       command_register((command_t) {
                pub, 'p', "pub",
                "extract the public key from a private key/certificate",
                {"[--in file] [--type rsa|ecdsa|x509] [--outform der|pem|pgp]"},
index 1f1138e..de1761c 100644 (file)
@@ -206,7 +206,7 @@ end:
 usage:
        san->destroy_offset(san, offsetof(identification_t, destroy));
        options->destroy(options);
-       return command_usage(CMD_SELF, error);
+       return command_usage(error);
 }
 
 /**
@@ -214,7 +214,7 @@ usage:
  */
 static void __attribute__ ((constructor))reg()
 {
-       command_register(CMD_SELF, (command_t) {
+       command_register((command_t) {
                self, 's', "self",
                "create a self signed certificate",
                {"[--in file] [--type rsa|ecdsa]",
index 6905112..d089d33 100644 (file)
@@ -32,7 +32,7 @@ static int verify(int argc, char *argv[])
                switch (getopt_long(argc, argv, "", command_opts, NULL))
                {
                        case 'h':
-                               return command_usage(CMD_VERIFY, NULL);
+                               return command_usage(NULL);
                        case 'i':
                                file = optarg;
                                continue;
@@ -42,7 +42,7 @@ static int verify(int argc, char *argv[])
                        case EOF:
                                break;
                        default:
-                               return command_usage(CMD_VERIFY, "invalid --verify option");
+                               return command_usage("invalid --verify option");
                }
                break;
        }
@@ -121,7 +121,7 @@ static int verify(int argc, char *argv[])
  */
 static void __attribute__ ((constructor))reg()
 {
-       command_register(CMD_VERIFY, (command_t) {
+       command_register((command_t) {
                verify, 'v', "verify",
                "verify a certificate using the CA certificate",
                {"[--in file] [--ca file]"},