diff options
author | Stefan Tomanek <stefan.tomanek@wertarbyte.de> | 2011-03-07 18:30:27 +0100 |
---|---|---|
committer | Jan Engelhardt <jengelh@medozas.de> | 2011-03-07 20:07:55 +0100 |
commit | 9cc4f24e72f87ca191c2e723e7cd293f6477481c (patch) | |
tree | 04d0e4229715449f1c5786535d14e7142c809e24 /xshared.c | |
parent | f96cb8094ceffb9ffe8e94b4ee6800aa581dd021 (diff) |
ip(6)tables-multi: unify subcommand handling
I found the subcommand handling and naming done by iptables-multi and
ip6tables-multi very confusing and complicated; this patch
reorganizes the subcommands in a single table, allowing both variants
of them to be used (iptables/main) and also prints a list of the
allowed commands if an unknown command is entered by the user.
Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
Diffstat (limited to 'xshared.c')
-rw-r--r-- | xshared.c | 36 |
1 files changed, 36 insertions, 0 deletions
@@ -1,7 +1,10 @@ +#include <libgen.h> #include <netdb.h> #include <stdbool.h> #include <stdint.h> #include <stdio.h> +#include <stdlib.h> +#include <string.h> #include <xtables.h> #include "xshared.h" @@ -99,3 +102,36 @@ struct xtables_match *load_proto(struct iptables_command_state *cs) return find_proto(cs->protocol, XTF_TRY_LOAD, cs->options & OPT_NUMERIC, &cs->matches); } + +static mainfunc_t subcmd_get(const char *cmd, const struct subcommand *cb) +{ + for (; cb->name != NULL; ++cb) + if (strcmp(cb->name, cmd) == 0) + return cb->main; + return NULL; +} + +int subcmd_main(int argc, char **argv, const struct subcommand *cb) +{ + const char *cmd = basename(*argv); + mainfunc_t f = subcmd_get(cmd, cb); + + if (f == NULL && argc > 1) { + /* + * Unable to find a main method for our command name? + * Let's try again with the first argument! + */ + ++argv; + --argc; + f = subcmd_get(*argv, cb); + } + + /* now we should have a valid function pointer */ + if (f != NULL) + return f(argc, argv); + + fprintf(stderr, "ERROR: No valid subcommand given.\nValid subcommands:\n"); + for (; cb->name != NULL; ++cb) + fprintf(stderr, " * %s\n", cb->name); + exit(EXIT_FAILURE); +} |