[PATCH nft 2/3] main: split parsing from libnftables initialization

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Parse options first, then initialize the nft context object.

Signed-off-by: Pablo Neira Ayuso <pablo@xxxxxxxxxxxxx>
---
 src/main.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)

diff --git a/src/main.c b/src/main.c
index 6ab1b89f4dd5..0f2b57cbacbb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -247,18 +247,14 @@ static bool nft_options_check(int argc, char * const argv[])
 
 int main(int argc, char * const *argv)
 {
-	char *buf = NULL, *filename = NULL;
-	unsigned int output_flags = 0;
-	bool interactive = false;
-	unsigned int debug_mask;
-	unsigned int len;
+	char *buf = NULL, *filename = NULL, *includepath = NULL;
+	unsigned int output_flags = 0, debug_mask = 0, len;
+	bool interactive = false, dry_run = false;
 	int i, val, rc;
 
 	if (!nft_options_check(argc, argv))
 		exit(EXIT_FAILURE);
 
-	nft = nft_ctx_new(NFT_CTX_DEFAULT);
-
 	while (1) {
 		val = getopt_long(argc, argv, OPTSTRING, options, NULL);
 		if (val == -1)
@@ -273,7 +269,7 @@ int main(int argc, char * const *argv)
 			       PACKAGE_NAME, PACKAGE_VERSION, RELEASE_NAME);
 			exit(EXIT_SUCCESS);
 		case OPT_CHECK:
-			nft_ctx_set_dry_run(nft, true);
+			dry_run = true;
 			break;
 		case OPT_FILE:
 			filename = optarg;
@@ -282,12 +278,7 @@ int main(int argc, char * const *argv)
 			interactive = true;
 			break;
 		case OPT_INCLUDEPATH:
-			if (nft_ctx_add_include_path(nft, optarg)) {
-				fprintf(stderr,
-					"Failed to add include path '%s'\n",
-					optarg);
-				exit(EXIT_FAILURE);
-			}
+			includepath = optarg;
 			break;
 		case OPT_NUMERIC:
 			output_flags |= NFT_CTX_OUTPUT_NUMERIC_ALL;
@@ -302,7 +293,6 @@ int main(int argc, char * const *argv)
 			output_flags |= NFT_CTX_OUTPUT_SERVICE;
 			break;
 		case OPT_DEBUG:
-			debug_mask = nft_ctx_output_get_debug(nft);
 			for (;;) {
 				unsigned int i;
 				char *end;
@@ -328,7 +318,6 @@ int main(int argc, char * const *argv)
 					break;
 				optarg = end + 1;
 			}
-			nft_ctx_output_set_debug(nft, debug_mask);
 			break;
 		case OPT_HANDLE_OUTPUT:
 			output_flags |= NFT_CTX_OUTPUT_HANDLE;
@@ -364,6 +353,14 @@ int main(int argc, char * const *argv)
 		}
 	}
 
+	nft = nft_ctx_new(NFT_CTX_DEFAULT);
+	if (includepath && nft_ctx_add_include_path(nft, includepath) < 0) {
+		fprintf(stderr, "Failed to add include path '%s'\n", optarg);
+		exit(EXIT_FAILURE);
+	}
+
+	nft_ctx_set_dry_run(nft, dry_run);
+	nft_ctx_output_set_debug(nft, debug_mask);
 	nft_ctx_output_set_flags(nft, output_flags);
 
 	if (optind != argc) {
-- 
2.11.0




[Index of Archives]     [Netfitler Users]     [Berkeley Packet Filter]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux