libimcv: Fixed processing of PTS Simple Component Evidence
[strongswan.git] / src / pki / command.c
index 8f53817..4ad4339 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2009 Martin Willi
- * Hochschule fuer Technik Rapperswil
+ * HSR Hochschule fuer Technik Rapperswil
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
 #include <stdio.h>
 
 #include <library.h>
-#include <debug.h>
+#include <utils/debug.h>
 #include <utils/optionsfrom.h>
 
 /**
  * Registered commands.
  */
-command_t cmds[MAX_COMMANDS];
+static command_t cmds[MAX_COMMANDS];
 
 /**
  * active command.
@@ -55,12 +55,12 @@ static options_t *options;
 /**
  * Global options used by all subcommands
  */
-static struct option command_opts[MAX_COMMANDS > MAX_OPTIONS ?: MAX_OPTIONS];
+static struct option command_opts[MAX_COMMANDS > MAX_OPTIONS ? MAX_COMMANDS : MAX_OPTIONS];
 
 /**
  * Global optstring used by all subcommands
  */
-static char command_optstring[(MAX_COMMANDS > MAX_OPTIONS ?: MAX_OPTIONS) * 3];
+static char command_optstring[(MAX_COMMANDS > MAX_OPTIONS ? MAX_COMMANDS : MAX_OPTIONS) * 3];
 
 /**
  * Build command_opts/command_optstr for the active command
@@ -73,7 +73,7 @@ static void build_opts()
        memset(command_optstring, 0, sizeof(command_optstring));
        if (active == help_idx)
        {
-               for (i = 0; cmds[i].cmd; i++)
+               for (i = 0; i < MAX_COMMANDS && cmds[i].cmd; i++)
                {
                        command_opts[i].name = cmds[i].cmd;
                        command_opts[i].val = cmds[i].op;
@@ -140,23 +140,46 @@ void command_register(command_t command)
 {
        int i;
 
+       if (registered == MAX_COMMANDS)
+       {
+               fprintf(stderr, "unable to register command, please increase "
+                               "MAX_COMMANDS\n");
+               return;
+       }
+
        cmds[registered] = command;
        /* append default options, but not to --help */
        if (!active)
        {
-               for (i = 0; i < countof(cmds[registered].options); i++)
+               for (i = 0; i < countof(cmds[registered].options) - 1; i++)
                {
-                       if (cmds[registered].options[i].name)
+                       if (!cmds[registered].options[i].name)
                        {
-                               continue;
+                               break;
                        }
+               }
+               if (i > countof(cmds[registered].options) - 3)
+               {
+                       fprintf(stderr, "command '%s' registered too many options, please "
+                                       "increase MAX_OPTIONS\n", command.cmd);
+               }
+               else
+               {
                        cmds[registered].options[i++] = (command_option_t) {
                                "debug",        'v', 1, "set debug level, default: 1"
                        };
                        cmds[registered].options[i++] = (command_option_t) {
                                "options",      '+', 1, "read command line options from file"
                        };
-                       break;
+               }
+               for (i = 0; cmds[registered].line[i]; i++)
+               {
+                       if (i == MAX_LINES - 1)
+                       {
+                               fprintf(stderr, "command '%s' specifies too many usage summary "
+                                               "lines, please increase MAX_LINES\n", command.cmd);
+                               break;
+                       }
                }
        }
        registered++;
@@ -168,7 +191,7 @@ void command_register(command_t command)
 int command_usage(char *error)
 {
        FILE *out = stdout;
-       int i;
+       int i, indent = 0;
 
        if (error)
        {
@@ -176,32 +199,39 @@ int command_usage(char *error)
                fprintf(out, "Error: %s\n", error);
        }
        fprintf(out, "strongSwan %s PKI tool\n", VERSION);
+
+       if (active == help_idx)
+       {
+               fprintf(out, "loaded plugins: %s\n",
+                               lib->plugins->loaded_plugins(lib->plugins));
+       }
+
        fprintf(out, "usage:\n");
        if (active == help_idx)
        {
-               for (i = 0; cmds[i].cmd; i++)
+               for (i = 0; i < MAX_COMMANDS && cmds[i].cmd; i++)
                {
-                       fprintf(out, "  pki --%-6s (-%c)  %s\n",
+                       fprintf(out, "  pki --%-7s (-%c)  %s\n",
                                        cmds[i].cmd, cmds[i].op, cmds[i].description);
                }
        }
        else
        {
-               for (i = 0; cmds[active].line[i]; i++)
+               for (i = 0; i < MAX_LINES && cmds[active].line[i]; i++)
                {
                        if (i == 0)
                        {
-                               fprintf(out, "  pki --%s %s\n",
-                                               cmds[active].cmd, cmds[active].line[i]);
+                               indent = fprintf(out, "  pki --%s ", cmds[active].cmd);
+                               fprintf(out, "%s\n", cmds[active].line[i]);
                        }
                        else
                        {
-                               fprintf(out, "              %s\n", cmds[active].line[i]);
+                               fprintf(out, "%*s%s\n", indent, "", cmds[active].line[i]);
                        }
                }
                for (i = 0; cmds[active].options[i].name; i++)
                {
-                       fprintf(out, "        --%-8s (-%c)  %s\n",
+                       fprintf(out, "        --%-15s (-%c)  %s\n",
                                        cmds[active].options[i].name, cmds[active].options[i].op,
                                        cmds[active].options[i].desc);
                }
@@ -242,7 +272,7 @@ int command_dispatch(int c, char *v[])
 
        build_opts();
        op = getopt_long(c, v, command_optstring, command_opts, NULL);
-       for (i = 0; cmds[i].cmd; i++)
+       for (i = 0; i < MAX_COMMANDS && cmds[i].cmd; i++)
        {
                if (cmds[i].op == op)
                {
@@ -251,6 +281,5 @@ int command_dispatch(int c, char *v[])
                        return cmds[i].call();
                }
        }
-       return command_usage("invalid operation");
+       return command_usage(c > 1 ? "invalid operation" : NULL);
 }
-