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 --- iptables-multi.c | 52 ++++++++++++++-------------------------------------- 1 file changed, 14 insertions(+), 38 deletions(-) (limited to 'iptables-multi.c') diff --git a/iptables-multi.c b/iptables-multi.c index 4dcc26de..754b5873 100644 --- a/iptables-multi.c +++ b/iptables-multi.c @@ -1,50 +1,26 @@ #include #include #include -#include +#include "xshared.h" int iptables_main(int argc, char **argv); int iptables_save_main(int argc, char **argv); int iptables_restore_main(int argc, char **argv); int iptables_xml_main(int argc, char **argv); +static const struct subcommand multi4_subcommands[] = { + {"iptables", iptables_main}, + {"main", iptables_main}, + {"iptables-save", iptables_save_main}, + {"save", iptables_save_main}, + {"iptables-restore", iptables_restore_main}, + {"restore", iptables_restore_main}, + {"iptables-xml", iptables_xml_main}, + {"xml", iptables_xml_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, "iptables") == 0) - return iptables_main(argc, argv); - if (strcmp(progname, "iptables-save") == 0) - return iptables_save_main(argc, argv); - if (strcmp(progname, "iptables-restore") == 0) - return iptables_restore_main(argc, argv); - if (strcmp(progname, "iptables-xml") == 0) - return iptables_xml_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 iptables_main(argc, argv); - if (strcmp(progname, "save") == 0) - return iptables_save_main(argc, argv); - if (strcmp(progname, "restore") == 0) - return iptables_restore_main(argc, argv); - if (strcmp(progname, "xml") == 0) - return iptables_xml_main(argc, argv); - - fprintf(stderr, "iptables multi-purpose version: " - "unknown subcommand \"%s\"\n", progname); - exit(EXIT_FAILURE); + return subcmd_main(argc, argv, multi4_subcommands); } -- cgit v1.2.3