[PATCH nft 3/3] scanner: remove indescs and indescs_idx attributes from the parser, and directly use indesc_list

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

 



parser_state.indescs (static array) and parser_state.indesc_list are in fact duplicating information.

This commit removes the static array and uses the list for everything.

Signed-off-by: Laurent Fasnacht <fasnacht@xxxxxxxxxxxxx>
---
 include/parser.h |  2 --
 src/scanner.l    | 58 ++++++++++++++++++++----------------------------
 2 files changed, 24 insertions(+), 36 deletions(-)

diff --git a/include/parser.h b/include/parser.h
index 949284d9..636d1c88 100644
--- a/include/parser.h
+++ b/include/parser.h
@@ -15,8 +15,6 @@
 
 struct parser_state {
 	struct input_descriptor		*indesc;
-	struct input_descriptor		*indescs[MAX_INCLUDE_DEPTH];
-	unsigned int			indesc_idx;
 	struct list_head		indesc_list;
 
 	struct list_head		*msgs;
diff --git a/src/scanner.l b/src/scanner.l
index 837bc476..4b06cb99 100644
--- a/src/scanner.l
+++ b/src/scanner.l
@@ -668,18 +668,23 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 static void scanner_push_indesc(struct parser_state *state,
 				struct input_descriptor *indesc)
 {
-	state->indescs[state->indesc_idx] = indesc;
-	state->indesc = state->indescs[state->indesc_idx++];
+	list_add_tail(&indesc->list, &state->indesc_list);
+	state->indesc = indesc;
 }
 
 static void scanner_pop_indesc(struct parser_state *state)
 {
-	state->indesc_idx--;
-
-	if (state->indesc_idx > 0)
-		state->indesc = state->indescs[state->indesc_idx - 1];
-	else
+	struct input_descriptor *indesc;
+	indesc = state->indesc;
+	if (!list_empty(&state->indesc_list)) {
+		state->indesc = list_entry(state->indesc->list.prev, struct input_descriptor, list);
+	} else {
 		state->indesc = NULL;
+	}
+	if (indesc->f) {
+		fclose(indesc->f);
+		indesc->f = NULL;
+	}
 }
 
 static void scanner_pop_buffer(yyscan_t scanner)
@@ -716,7 +721,6 @@ static void scanner_push_file(struct nft_ctx *nft, void *scanner,
 	init_pos(indesc);
 
 	scanner_push_indesc(state, indesc);
-	list_add_tail(&indesc->list, &state->indesc_list);
 }
 
 static int include_file(struct nft_ctx *nft, void *scanner,
@@ -915,15 +919,14 @@ void scanner_push_buffer(void *scanner, const struct input_descriptor *indesc,
 {
 	struct parser_state *state = yyget_extra(scanner);
 	YY_BUFFER_STATE b;
+	struct input_descriptor *new_indesc;
 
-	state->indesc = xzalloc(sizeof(struct input_descriptor));
-	state->indescs[state->indesc_idx] = state->indesc;
-	state->indesc_idx++;
+	new_indesc = xzalloc(sizeof(struct input_descriptor));
 
-	memcpy(state->indesc, indesc, sizeof(*state->indesc));
-	state->indesc->data = buffer;
-	state->indesc->name = NULL;
-	list_add_tail(&state->indesc->list, &state->indesc_list);
+	memcpy(new_indesc, indesc, sizeof(*new_indesc));
+	new_indesc->data = buffer;
+	new_indesc->name = NULL;
+	scanner_push_indesc(state, new_indesc);
 
 	b = yy_scan_string(buffer, scanner);
 	assert(b != NULL);
@@ -940,35 +943,22 @@ void *scanner_init(struct parser_state *state)
 	return scanner;
 }
 
-static void input_descriptor_destroy(const struct input_descriptor *indesc)
-{
-	if (indesc->name)
-		xfree(indesc->name);
-	xfree(indesc);
-}
 
-static void input_descriptor_list_destroy(struct parser_state *state)
+static void input_descriptor_list_destroy(yyscan_t *scanner)
 {
+	struct parser_state *state = yyget_extra(scanner);
 	struct input_descriptor *indesc, *next;
 
 	list_for_each_entry_safe(indesc, next, &state->indesc_list, list) {
-		if (indesc->f) {
-			fclose(indesc->f);
-			indesc->f = NULL;
-		}
+		if (indesc->name)
+			xfree(indesc->name);
 		list_del(&indesc->list);
-		input_descriptor_destroy(indesc);
+		xfree(indesc);
 	}
 }
 
 void scanner_destroy(struct nft_ctx *nft)
 {
-	struct parser_state *state = yyget_extra(nft->scanner);
-
-	do {
-		yypop_buffer_state(nft->scanner);
-	} while (state->indesc_idx--);
-
-	input_descriptor_list_destroy(state);
+	input_descriptor_list_destroy(nft->scanner);
 	yylex_destroy(nft->scanner);
 }
-- 
2.20.1






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

  Powered by Linux