From 9cc4f24e72f87ca191c2e723e7cd293f6477481c Mon Sep 17 00:00:00 2001 From: Stefan Tomanek Date: Mon, 7 Mar 2011 18:30:27 +0100 Subject: 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 --- ip6tables-multi.c | 46 ++++++++++++---------------------------------- 1 file changed, 12 insertions(+), 34 deletions(-) (limited to 'ip6tables-multi.c') diff --git a/ip6tables-multi.c b/ip6tables-multi.c index 671558c5..7e6603f8 100644 --- a/ip6tables-multi.c +++ b/ip6tables-multi.c @@ -1,45 +1,23 @@ #include #include #include -#include +#include "xshared.h" int ip6tables_main(int argc, char **argv); int ip6tables_save_main(int argc, char **argv); int ip6tables_restore_main(int argc, char **argv); +static const struct subcommand multi6_subcommands[] = { + {"ip6tables", ip6tables_main}, + {"main", ip6tables_main}, + {"ip6tables-save", ip6tables_save_main}, + {"save", ip6tables_save_main}, + {"ip6tables-restore", ip6tables_restore_main}, + {"restore", ip6tables_restore_main}, + {NULL}, +}; + int main(int argc, char **argv) { - char *progname; - - if (argc < 1) { - fprintf(stderr, "ERROR: This should not happen.\n"); - exit(EXIT_FAILURE); - } - - progname = basename(argv[0]); - if (strcmp(progname, "ip6tables") == 0) - return ip6tables_main(argc, argv); - if (strcmp(progname, "ip6tables-save") == 0) - return ip6tables_save_main(argc, argv); - if (strcmp(progname, "ip6tables-restore") == 0) - return ip6tables_restore_main(argc, argv); - - ++argv; - --argc; - if (argc < 1) { - fprintf(stderr, "ERROR: No subcommand given.\n"); - exit(EXIT_FAILURE); - } - - progname = basename(argv[0]); - if (strcmp(progname, "main") == 0) - return ip6tables_main(argc, argv); - if (strcmp(progname, "save") == 0) - return ip6tables_save_main(argc, argv); - if (strcmp(progname, "restore") == 0) - return ip6tables_restore_main(argc, argv); - - fprintf(stderr, "ip6tables multi-purpose version: " - "unknown subcommand \"%s\"\n", progname); - exit(EXIT_FAILURE); + return subcmd_main(argc, argv, multi6_subcommands); } -- cgit v1.2.3