diff mbox series

[nft,3/7] JSON: Simplify verdict statement parsing

Message ID 20180601153212.26614-4-phil@nwl.cc
State Accepted
Delegated to: Pablo Neira
Headers show
Series Review wildcard expression parsing and some more | expand

Commit Message

Phil Sutter June 1, 2018, 3:32 p.m. UTC
Reuse verdict expression parsing routine. Since the statement simply
wraps an expression, this change is an obvious one.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 src/parser_json.c | 39 +++------------------------------------
 1 file changed, 3 insertions(+), 36 deletions(-)
diff mbox series

Patch

diff --git a/src/parser_json.c b/src/parser_json.c
index 1d60db47f718c..394e5039a4476 100644
--- a/src/parser_json.c
+++ b/src/parser_json.c
@@ -1403,45 +1403,12 @@  static struct stmt *json_parse_counter_stmt(struct json_ctx *ctx,
 static struct stmt *json_parse_verdict_stmt(struct json_ctx *ctx,
 					    const char *key, json_t *value)
 {
-	struct {
-		const char *name;
-		int val;
-	} verdict_type_tbl[] = {
-		{ "accept", NF_ACCEPT },
-		{ "drop", NF_DROP },
-		{ "continue", NFT_CONTINUE },
-		{ "jump", NFT_JUMP },
-		{ "goto", NFT_GOTO },
-		{ "return", NFT_RETURN },
-	};
-	const char *identifier = NULL;
 	struct expr *expr;
-	unsigned int i;
-	int type = 255;	/* NFT_* are negative, NF_* are max 5 (NF_STOP) */
 
-	for (i = 0; i < array_size(verdict_type_tbl); i++) {
-		if (!strcmp(verdict_type_tbl[i].name, key)) {
-			type = verdict_type_tbl[i].val;
-			break;
-		}
-	}
-	switch(type) {
-	case NFT_JUMP:
-	case NFT_GOTO:
-		if (!json_is_string(value)) {
-			json_error(ctx, "Verdict '%s' requires destination.", key);
-			return NULL;
-		}
-		identifier = xstrdup(json_string_value(value));
-		/* fall through */
-	case NF_ACCEPT:
-	case NF_DROP:
-	case NFT_CONTINUE:
-	case NFT_RETURN:
-		expr = verdict_expr_alloc(int_loc, type, identifier);
+	expr = json_parse_verdict_expr(ctx, key, value);
+	if (expr)
 		return verdict_stmt_alloc(int_loc, expr);
-	}
-	json_error(ctx, "Unknown verdict '%s'.", key);
+
 	return NULL;
 }