]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
src: unclutter command_default function
authorJan Engelhardt <jengelh@medozas.de>
Mon, 7 Feb 2011 02:20:02 +0000 (03:20 +0100)
committerJan Engelhardt <jengelh@medozas.de>
Mon, 7 Feb 2011 02:31:50 +0000 (03:31 +0100)
(Essentially, 5 levels of indentation have been stripped compared to the
original layout, and this is surely a result that looks a lot better
than it did before.)

Things to note:

1. If the m->parse call succeeded, we can return from the function and
do not need to go through the other code. As such, "m" is guaranteed to
be useless at the end of the match loop, and so, conditions can be
removed.

2. Since the per-extension parse function only ever get their own option
codes (since v1.4.10-26-gd09b6d5), their return value no longer has a
meaning and can be ignored.

Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
ip6tables.c
iptables.c

index 3330420f5a365f9c2d1e7f8e1384e14a665d9208..c475bf260fbbb0643e9b9b69c40146c52d688da6 100644 (file)
@@ -1247,71 +1247,59 @@ static void command_default(struct iptables_command_state *cs)
 
        if (cs->target == NULL || cs->target->parse == NULL ||
            cs->c < cs->target->option_offset ||
-           cs->c >= cs->target->option_offset + XT_OPTION_OFFSET_SCALE ||
-           !cs->target->parse(cs->c - cs->target->option_offset,
-                              cs->argv, cs->invert,
-                              &cs->target->tflags,
-                              &cs->fw6, &cs->target->t)) {
-               for (matchp = cs->matches; matchp; matchp = matchp->next) {
-                       if (matchp->completed ||
-                           matchp->match->parse == NULL)
-                               continue;
-                       if (cs->c < matchp->match->option_offset ||
-                           cs->c >= matchp->match->option_offset + XT_OPTION_OFFSET_SCALE)
-                               continue;
-                       if (matchp->match->parse(cs->c - matchp->match->option_offset,
-                                    cs->argv, cs->invert,
-                                    &matchp->match->mflags,
-                                    &cs->fw6,
-                                    &matchp->match->m))
-                               break;
-               }
-               m = matchp ? matchp->match : NULL;
+           cs->c >= cs->target->option_offset + XT_OPTION_OFFSET_SCALE) {
+               cs->target->parse(cs->c - cs->target->option_offset, cs->argv,
+                                 cs->invert, &cs->target->tflags, &cs->fw6,
+                                 &cs->target->t);
+               return;
+       }
 
-               if (m == NULL && (m = load_proto(cs)) != NULL) {
-                       /* Try loading protocol */
-                       size_t size;
+       for (matchp = cs->matches; matchp; matchp = matchp->next) {
+               m = matchp->match;
 
-                       cs->proto_used = 1;
+               if (matchp->completed || m->parse == NULL)
+                       continue;
+               if (cs->c < matchp->match->option_offset ||
+                   cs->c >= matchp->match->option_offset + XT_OPTION_OFFSET_SCALE)
+                       continue;
+               m->parse(cs->c - m->option_offset, cs->argv, cs->invert,
+                        &m->mflags, &cs->fw6, &m->m);
+               return;
+       }
 
-                       size = IP6T_ALIGN(sizeof(struct ip6t_entry_match))
-                                        + m->size;
+       /* Try loading protocol */
+       m = load_proto(cs);
+       if (m != NULL) {
+               size_t size;
 
-                       m->m = xtables_calloc(1, size);
-                       m->m->u.match_size = size;
-                       strcpy(m->m->u.user.name, m->name);
-                       m->m->u.user.revision = m->revision;
-                       if (m->init != NULL)
-                               m->init(m->m);
+               cs->proto_used = 1;
 
-                       opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
-                           m->extra_opts, &m->option_offset);
+               size = IP6T_ALIGN(sizeof(struct ip6t_entry_match)) + m->size;
 
-                       optind--;
-                       return;
-               }
+               m->m = xtables_calloc(1, size);
+               m->m->u.match_size = size;
+               strcpy(m->m->u.user.name, m->name);
+               m->m->u.user.revision = m->revision;
+               if (m->init != NULL)
+                       m->init(m->m);
 
-               if (!m) {
-                       if (cs->c == '?') {
-                               if (optopt) {
-                                       xtables_error(
-                                          PARAMETER_PROBLEM,
-                                          "option `%s' "
-                                          "requires an "
-                                          "argument",
-                                          cs->argv[optind-1]);
-                               } else {
-                                       xtables_error(
-                                          PARAMETER_PROBLEM,
-                                          "unknown option "
-                                          "`%s'",
-                                          cs->argv[optind-1]);
-                               }
-                       }
+               opts = xtables_merge_options(ip6tables_globals.orig_opts, opts,
+                                            m->extra_opts, &m->option_offset);
+
+               optind--;
+               return;
+       }
+
+       if (cs->c == '?') {
+               if (optopt)
                        xtables_error(PARAMETER_PROBLEM,
-                                  "Unknown arg `%s'", optarg);
-               }
+                               "option \"%s\" requires an argument",
+                               cs->argv[optind-1]);
+               else
+                       xtables_error(PARAMETER_PROBLEM,
+                               "unknown option \"%s\"", cs->argv[optind-1]);
        }
+       xtables_error(PARAMETER_PROBLEM, "Unknown arg \"%s\"", optarg);
 }
 
 int do_command6(int argc, char *argv[], char **table, struct ip6tc_handle **handle)
index bae14afcc4fa75b36ace3d643af65c28c0f234d1..96732b40714ffb98dc5a10a6e537af6ad7677b5a 100644 (file)
@@ -1271,76 +1271,61 @@ static void command_default(struct iptables_command_state *cs)
 
        if (cs->target == NULL || cs->target->parse == NULL ||
            cs->c < cs->target->option_offset ||
-           cs->c >= cs->target->option_offset + XT_OPTION_OFFSET_SCALE ||
-           !cs->target->parse(cs->c - cs->target->option_offset,
-                              cs->argv, cs->invert,
-                              &cs->target->tflags,
-                              &cs->fw, &cs->target->t)) {
-               for (matchp = cs->matches; matchp; matchp = matchp->next) {
-                       if (matchp->completed ||
-                           matchp->match->parse == NULL)
-                               continue;
-                       if (cs->c < matchp->match->option_offset ||
-                           cs->c >= matchp->match->option_offset + XT_OPTION_OFFSET_SCALE)
-                               continue;
-                       if (matchp->match->parse(cs->c - matchp->match->option_offset,
-                                    cs->argv, cs->invert,
-                                    &matchp->match->mflags,
-                                    &cs->fw,
-                                    &matchp->match->m))
-                               break;
-               }
-               m = matchp ? matchp->match : NULL;
+           cs->c >= cs->target->option_offset + XT_OPTION_OFFSET_SCALE) {
+               cs->target->parse(cs->c - cs->target->option_offset, cs->argv,
+                                 cs->invert, &cs->target->tflags, &cs->fw,
+                                 &cs->target->t);
+               return;
+       }
 
-               if (m == NULL && (m = load_proto(cs)) != NULL) {
-                       /* Try loading protocol */
-                       size_t size;
+       for (matchp = cs->matches; matchp; matchp = matchp->next) {
+               m = matchp->match;
 
-                       cs->proto_used = 1;
+               if (matchp->completed || m->parse == NULL)
+                       continue;
+               if (cs->c < m->option_offset ||
+                   cs->c >= m->option_offset + XT_OPTION_OFFSET_SCALE)
+                       continue;
+               m->parse(cs->c - m->option_offset, cs->argv, cs->invert,
+                        &m->mflags, &cs->fw, &m->m);
+               return;
+       }
 
-                       size = IPT_ALIGN(sizeof(struct ipt_entry_match))
-                                        + m->size;
+       /* Try loading protocol */
+       m = load_proto(cs);
+       if (m != NULL) {
+               size_t size;
 
-                       m->m = xtables_calloc(1, size);
-                       m->m->u.match_size = size;
-                       strcpy(m->m->u.user.name, m->name);
-                       m->m->u.user.revision = m->revision;
-                       if (m->init != NULL)
-                               m->init(m->m);
+               cs->proto_used = 1;
 
-                       opts = xtables_merge_options(
-                                            iptables_globals.orig_opts,
-                                            opts,
-                                            m->extra_opts,
-                                            &m->option_offset);
-                       if (opts == NULL)
-                               xtables_error(OTHER_PROBLEM,
-                                       "can't alloc memory!");
+               size = IPT_ALIGN(sizeof(struct ipt_entry_match)) + m->size;
 
-                       optind--;
-                       return;
-               }
-               if (!m) {
-                       if (cs->c == '?') {
-                               if (optopt) {
-                                       xtables_error(
-                                          PARAMETER_PROBLEM,
-                                          "option `%s' "
-                                          "requires an "
-                                          "argument",
-                                          cs->argv[optind-1]);
-                               } else {
-                                       xtables_error(
-                                          PARAMETER_PROBLEM,
-                                          "unknown option "
-                                          "`%s'",
-                                          cs->argv[optind-1]);
-                               }
-                       }
+               m->m = xtables_calloc(1, size);
+               m->m->u.match_size = size;
+               strcpy(m->m->u.user.name, m->name);
+               m->m->u.user.revision = m->revision;
+               if (m->init != NULL)
+                       m->init(m->m);
+
+               opts = xtables_merge_options(iptables_globals.orig_opts, opts,
+                                            m->extra_opts, &m->option_offset);
+               if (opts == NULL)
+                       xtables_error(OTHER_PROBLEM, "can't alloc memory!");
+
+               optind--;
+               return;
+       }
+
+       if (cs->c == '?') {
+               if (optopt)
                        xtables_error(PARAMETER_PROBLEM,
-                                  "Unknown arg `%s'", optarg);
-               }
+                               "option \"%s\" requires an argument",
+                               cs->argv[optind-1]);
+               else
+                       xtables_error(PARAMETER_PROBLEM,
+                               "unknown option \"%s\"", cs->argv[optind-1]);
        }
+       xtables_error(PARAMETER_PROBLEM, "Unknown arg \"%s\"", optarg);
 }
 
 int do_command(int argc, char *argv[], char **table, struct iptc_handle **handle)