netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: netfilter-devel@vger.kernel.org
Subject: [PATCH nft 2/3] main: split parsing from libnftables initialization
Date: Thu,  9 Jan 2020 18:21:14 +0100	[thread overview]
Message-ID: <20200109172115.229723-3-pablo@netfilter.org> (raw)
In-Reply-To: <20200109172115.229723-1-pablo@netfilter.org>

Parse options first, then initialize the nft context object.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
 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


  parent reply	other threads:[~2020-01-09 17:21 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-09 17:21 [PATCH nft 0/3] netns support Pablo Neira Ayuso
2020-01-09 17:21 ` [PATCH nft 1/3] libnftables: add nft_ctx_set_netns() Pablo Neira Ayuso
2020-01-10 12:53   ` Phil Sutter
2020-01-12 10:28     ` Pablo Neira Ayuso
2020-01-12 10:40       ` Pablo Neira Ayuso
2020-01-14 10:25         ` Phil Sutter
2020-01-14 10:38           ` Pablo Neira Ayuso
2020-01-14 17:04             ` Phil Sutter
2020-01-09 17:21 ` Pablo Neira Ayuso [this message]
2020-01-09 17:21 ` [PATCH nft 3/3] main: add -w/--netns option Pablo Neira Ayuso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200109172115.229723-3-pablo@netfilter.org \
    --to=pablo@netfilter.org \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).