Skip to contentSkip to conversation

Review nft_options_check()

PATCH New Phil Sutter 1 participant 3 messages Dec 2, 2023 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

cover Review nft_options_check()

The hard-coded list of nft options which take an argument is a maintenance burden and also needless since the information is already present in struct nft_options array.

After some indenting cleanup in patch 1, turn the list of options to compare against into a loop over nft_options' elements.

Phil Sutter (2):
main: Reduce indenting in nft_options_check()
main: Refer to nft_options in nft_options_check()

 src/main.c | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
Signature · 1 line
2.41.0
author reviewer
#1 lore raw

PATCH 1/2 main: Reduce indenting in nft_options_check()

No functional change intended.

---
 src/main.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
src/main.c +16 -15
index 9485b193cd34d..c3c7fe23baa37 100644
@@ -333,23 +333,24 @@ static bool nft_options_check(int argc, char * const argv[])
 		} else if (skip) {
 			skip = false;
 			continue;
-		} else if (argv[i][0] == '-') {
-			if (nonoption) {
-				nft_options_error(argc, argv, pos);
-				return false;
-			} else if (argv[i][1] == 'd' ||
-				   argv[i][1] == 'I' ||
-				   argv[i][1] == 'f' ||
-				   argv[i][1] == 'D' ||
-				   !strcmp(argv[i], "--debug") ||
-				   !strcmp(argv[i], "--includepath") ||
-				   !strcmp(argv[i], "--define") ||
-				   !strcmp(argv[i], "--file")) {
-				skip = true;
-				continue;
-			}
 		} else if (argv[i][0] != '-') {
 			nonoption = true;
+			continue;
+		}
+		if (nonoption) {
+			nft_options_error(argc, argv, pos);
+			return false;
+		}
+		if (argv[i][1] == 'd' ||
+		    argv[i][1] == 'I' ||
+		    argv[i][1] == 'f' ||
+		    argv[i][1] == 'D' ||
+		    !strcmp(argv[i], "--debug") ||
+		    !strcmp(argv[i], "--includepath") ||
+		    !strcmp(argv[i], "--define") ||
+		    !strcmp(argv[i], "--file")) {
+			skip = true;
+			continue;
 		}
 	}
 
Signature · 1 line
2.41.0
author reviewer
#2 lore raw

PATCH 2/2 main: Refer to nft_options in nft_options_check()

In reply to Phil Sutter

Consult the array when determining whether a given option is followed by an argument or not instead of hard-coding those that do. The array holds both short and long option name, so one extra pitfall removed.

---
 src/main.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
src/main.c +9 -10
index c3c7fe23baa37..d3491cda7f8db 100644
@@ -325,6 +325,7 @@ static bool nft_options_check(int argc, char * const argv[])
 {
 	bool skip = false, nonoption = false;
 	int pos = 0, i;
+	size_t j;
 
 	for (i = 1; i < argc; i++) {
 		pos += strlen(argv[i - 1]) + 1;
@@ -341,16 +342,14 @@ static bool nft_options_check(int argc, char * const argv[])
 			nft_options_error(argc, argv, pos);
 			return false;
 		}
-		if (argv[i][1] == 'd' ||
-		    argv[i][1] == 'I' ||
-		    argv[i][1] == 'f' ||
-		    argv[i][1] == 'D' ||
-		    !strcmp(argv[i], "--debug") ||
-		    !strcmp(argv[i], "--includepath") ||
-		    !strcmp(argv[i], "--define") ||
-		    !strcmp(argv[i], "--file")) {
-			skip = true;
-			continue;
+		for (j = 0; j < NR_NFT_OPTIONS; j++) {
+			if (nft_options[j].arg &&
+			    (argv[i][1] == (char)nft_options[j].val ||
+			     (argv[i][1] == '-' &&
+			      !strcmp(argv[i] + 2, nft_options[j].name)))) {
+				skip = true;
+				break;
+			}
 		}
 	}
 
Signature · 1 line
2.41.0