From 586ad210368b775c94acbad2b105483eaf4ca05a Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Tue, 8 May 2018 13:08:38 +0200 Subject: libnftables: Implement JSON parser If JSON output setting is active in current context, try parsing any input as JSON. If the initial loading of the buffer or filename by libjansson fails, fall back to regular syntax parser. Signed-off-by: Phil Sutter Signed-off-by: Pablo Neira Ayuso --- src/libnftables.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'src/libnftables.c') diff --git a/src/libnftables.c b/src/libnftables.c index 68e53f70..d9b2c081 100644 --- a/src/libnftables.c +++ b/src/libnftables.c @@ -452,13 +452,16 @@ int nft_run_cmd_from_buffer(struct nft_ctx *nft, char *buf, size_t buflen) LIST_HEAD(cmds); size_t nlbuflen; char *nlbuf; - int rc; + int rc = -EINVAL; nlbuflen = max(buflen + 1, strlen(buf) + 2); nlbuf = xzalloc(nlbuflen); snprintf(nlbuf, nlbuflen, "%s\n", buf); - rc = nft_parse_bison_buffer(nft, nlbuf, nlbuflen, &msgs, &cmds); + if (nft->output.json) + rc = nft_parse_json_buffer(nft, nlbuf, nlbuflen, &msgs, &cmds); + if (rc == -EINVAL) + rc = nft_parse_bison_buffer(nft, nlbuf, nlbuflen, &msgs, &cmds); if (rc) goto err; @@ -491,7 +494,11 @@ int nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename) if (!strcmp(filename, "-")) filename = "/dev/stdin"; - rc = nft_parse_bison_filename(nft, filename, &msgs, &cmds); + rc = -EINVAL; + if (nft->output.json) + rc = nft_parse_json_filename(nft, filename, &msgs, &cmds); + if (rc == -EINVAL) + rc = nft_parse_bison_filename(nft, filename, &msgs, &cmds); if (rc) goto err; -- cgit v1.2.3