diff options
author | Phil Sutter <phil@nwl.cc> | 2018-05-08 13:08:38 +0200 |
---|---|---|
committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2018-05-11 12:17:02 +0200 |
commit | 586ad210368b775c94acbad2b105483eaf4ca05a (patch) | |
tree | 2d6204f59b8d9f7ed7ebed8492739b5e79ec4e0d /src/libnftables.c | |
parent | e70354f53e9f6be4a4be31dbc46c5e23291d3587 (diff) |
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 <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/libnftables.c')
-rw-r--r-- | src/libnftables.c | 13 |
1 files changed, 10 insertions, 3 deletions
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; |