]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
ip[6]tables-restore: cleanup to reduce one level of indentation
authorPablo Neira Ayuso <pablo@netfilter.org>
Fri, 3 Aug 2012 09:12:14 +0000 (11:12 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Fri, 3 Aug 2012 09:13:59 +0000 (11:13 +0200)
This patch moves the parameter parsing to one function to reduce
one level of indentation. Jan Engelhardt likes this.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
iptables/ip6tables-restore.c
iptables/iptables-restore.c

index 294b242e685f15f969e4c1ea82dc62e5b654adab..0e8252f4133c82e2b263ca6c693c3ce3c66a895a 100644 (file)
@@ -114,6 +114,70 @@ static void free_argv(void) {
                free(newargv[i]);
 }
 
+static void add_param_to_argv(char *parsestart)
+{
+       int quote_open = 0, escaped = 0, param_len = 0;
+       char param_buffer[1024], *curchar;
+
+       /* After fighting with strtok enough, here's now
+        * a 'real' parser. According to Rusty I'm now no
+        * longer a real hacker, but I can live with that */
+
+       for (curchar = parsestart; *curchar; curchar++) {
+               if (quote_open) {
+                       if (escaped) {
+                               param_buffer[param_len++] = *curchar;
+                               escaped = 0;
+                               continue;
+                       } else if (*curchar == '\\') {
+                               escaped = 1;
+                               continue;
+                       } else if (*curchar == '"') {
+                               quote_open = 0;
+                               *curchar = ' ';
+                       } else {
+                               param_buffer[param_len++] = *curchar;
+                               continue;
+                       }
+               } else {
+                       if (*curchar == '"') {
+                               quote_open = 1;
+                               continue;
+                       }
+               }
+
+               if (*curchar == ' '
+                   || *curchar == '\t'
+                   || * curchar == '\n') {
+                       if (!param_len) {
+                               /* two spaces? */
+                               continue;
+                       }
+
+                       param_buffer[param_len] = '\0';
+
+                       /* check if table name specified */
+                       if (!strncmp(param_buffer, "-t", 2)
+                            || !strncmp(param_buffer, "--table", 8)) {
+                               xtables_error(PARAMETER_PROBLEM,
+                               "The -t option (seen in line %u) cannot be "
+                               "used in ip6tables-restore.\n", line);
+                               exit(1);
+                       }
+
+                       add_argv(param_buffer);
+                       param_len = 0;
+               } else {
+                       /* regular character, copy to buffer */
+                       param_buffer[param_len++] = *curchar;
+
+                       if (param_len >= sizeof(param_buffer))
+                               xtables_error(PARAMETER_PROBLEM,
+                                  "Parameter too long!");
+               }
+       }
+}
+
 int ip6tables_restore_main(int argc, char *argv[])
 {
        struct xtc_handle *handle = NULL;
@@ -325,12 +389,6 @@ int ip6tables_restore_main(int argc, char *argv[])
                        char *bcnt = NULL;
                        char *parsestart;
 
-                       /* the parser */
-                       char *curchar;
-                       int quote_open, escaped;
-                       size_t param_len;
-                       char param_buffer[1024];
-
                        /* reset the newargv */
                        newargc = 0;
 
@@ -371,69 +429,7 @@ int ip6tables_restore_main(int argc, char *argv[])
                                add_argv((char *) bcnt);
                        }
 
-                       /* After fighting with strtok enough, here's now
-                        * a 'real' parser. According to Rusty I'm now no
-                        * longer a real hacker, but I can live with that */
-
-                       quote_open = 0;
-                       escaped = 0;
-                       param_len = 0;
-
-                       for (curchar = parsestart; *curchar; curchar++) {
-                               if (quote_open) {
-                                       if (escaped) {
-                                               param_buffer[param_len++] = *curchar;
-                                               escaped = 0;
-                                               continue;
-                                       } else if (*curchar == '\\') {
-                                               escaped = 1;
-                                               continue;
-                                       } else if (*curchar == '"') {
-                                               quote_open = 0;
-                                               *curchar = ' ';
-                                       } else {
-                                               param_buffer[param_len++] = *curchar;
-                                               continue;
-                                       }
-                               } else {
-                                       if (*curchar == '"') {
-                                               quote_open = 1;
-                                               continue;
-                                       }
-                               }
-
-                               if (*curchar == ' '
-                                   || *curchar == '\t'
-                                   || * curchar == '\n') {
-                                       if (!param_len) {
-                                               /* two spaces? */
-                                               continue;
-                                       }
-
-                                       param_buffer[param_len] = '\0';
-
-                                       /* check if table name specified */
-                                       if (!strncmp(param_buffer, "-t", 2)
-                                            || !strncmp(param_buffer, "--table", 8)) {
-                                               xtables_error(PARAMETER_PROBLEM,
-                                                  "The -t option (seen in "
-                                                  "line %u) cannot be used "
-                                                  "in ip6tables-restore.\n",
-                                                  line);
-                                               exit(1);
-                                       }
-
-                                       add_argv(param_buffer);
-                                       param_len = 0;
-                               } else {
-                                       /* regular character, copy to buffer */
-                                       param_buffer[param_len++] = *curchar;
-
-                                       if (param_len >= sizeof(param_buffer))
-                                               xtables_error(PARAMETER_PROBLEM,
-                                                  "Parameter too long!");
-                               }
-                       }
+                       add_param_to_argv(parsestart);
 
                        DEBUGP("calling do_command6(%u, argv, &%s, handle):\n",
                                newargc, curtable);
index f21754a488fd2d637574019d51aa57d8950c57e1..082251390bf43d46ab9c22960a8bcd09f3bd1e62 100644 (file)
@@ -113,6 +113,70 @@ static void free_argv(void) {
                free(newargv[i]);
 }
 
+static void add_param_to_argv(char *parsestart)
+{
+       int quote_open = 0, escaped = 0, param_len = 0;
+       char param_buffer[1024], *curchar;
+
+       /* After fighting with strtok enough, here's now
+        * a 'real' parser. According to Rusty I'm now no
+        * longer a real hacker, but I can live with that */
+
+       for (curchar = parsestart; *curchar; curchar++) {
+               if (quote_open) {
+                       if (escaped) {
+                               param_buffer[param_len++] = *curchar;
+                               escaped = 0;
+                               continue;
+                       } else if (*curchar == '\\') {
+                               escaped = 1;
+                               continue;
+                       } else if (*curchar == '"') {
+                               quote_open = 0;
+                               *curchar = ' ';
+                       } else {
+                               param_buffer[param_len++] = *curchar;
+                               continue;
+                       }
+               } else {
+                       if (*curchar == '"') {
+                               quote_open = 1;
+                               continue;
+                       }
+               }
+
+               if (*curchar == ' '
+                   || *curchar == '\t'
+                   || * curchar == '\n') {
+                       if (!param_len) {
+                               /* two spaces? */
+                               continue;
+                       }
+
+                       param_buffer[param_len] = '\0';
+
+                       /* check if table name specified */
+                       if (!strncmp(param_buffer, "-t", 2)
+                           || !strncmp(param_buffer, "--table", 8)) {
+                               xtables_error(PARAMETER_PROBLEM,
+                               "The -t option (seen in line %u) cannot be "
+                               "used in iptables-restore.\n", line);
+                               exit(1);
+                       }
+
+                       add_argv(param_buffer);
+                       param_len = 0;
+               } else {
+                       /* regular character, copy to buffer */
+                       param_buffer[param_len++] = *curchar;
+
+                       if (param_len >= sizeof(param_buffer))
+                               xtables_error(PARAMETER_PROBLEM,
+                                  "Parameter too long!");
+               }
+       }
+}
+
 int
 iptables_restore_main(int argc, char *argv[])
 {
@@ -325,12 +389,6 @@ iptables_restore_main(int argc, char *argv[])
                        char *bcnt = NULL;
                        char *parsestart;
 
-                       /* the parser */
-                       char *curchar;
-                       int quote_open, escaped;
-                       size_t param_len;
-                       char param_buffer[1024];
-
                        /* reset the newargv */
                        newargc = 0;
 
@@ -371,69 +429,7 @@ iptables_restore_main(int argc, char *argv[])
                                add_argv((char *) bcnt);
                        }
 
-                       /* After fighting with strtok enough, here's now
-                        * a 'real' parser. According to Rusty I'm now no
-                        * longer a real hacker, but I can live with that */
-
-                       quote_open = 0;
-                       escaped = 0;
-                       param_len = 0;
-
-                       for (curchar = parsestart; *curchar; curchar++) {
-                               if (quote_open) {
-                                       if (escaped) {
-                                               param_buffer[param_len++] = *curchar;
-                                               escaped = 0;
-                                               continue;
-                                       } else if (*curchar == '\\') {
-                                               escaped = 1;
-                                               continue;
-                                       } else if (*curchar == '"') {
-                                               quote_open = 0;
-                                               *curchar = ' ';
-                                       } else {
-                                               param_buffer[param_len++] = *curchar;
-                                               continue;
-                                       }
-                               } else {
-                                       if (*curchar == '"') {
-                                               quote_open = 1;
-                                               continue;
-                                       }
-                               }
-
-                               if (*curchar == ' '
-                                   || *curchar == '\t'
-                                   || * curchar == '\n') {
-                                       if (!param_len) {
-                                               /* two spaces? */
-                                               continue;
-                                       }
-
-                                       param_buffer[param_len] = '\0';
-
-                                       /* check if table name specified */
-                                       if (!strncmp(param_buffer, "-t", 2)
-                                           || !strncmp(param_buffer, "--table", 8)) {
-                                               xtables_error(PARAMETER_PROBLEM,
-                                                  "The -t option (seen in "
-                                                  "line %u) cannot be used "
-                                                  "in iptables-restore.\n",
-                                                  line);
-                                               exit(1);
-                                       }
-
-                                       add_argv(param_buffer);
-                                       param_len = 0;
-                               } else {
-                                       /* regular character, copy to buffer */
-                                       param_buffer[param_len++] = *curchar;
-
-                                       if (param_len >= sizeof(param_buffer))
-                                               xtables_error(PARAMETER_PROBLEM,
-                                                  "Parameter too long!");
-                               }
-                       }
+                       add_param_to_argv(parsestart);
 
                        DEBUGP("calling do_command4(%u, argv, &%s, handle):\n",
                                newargc, curtable);