From 5a8bbed0b1e9b22526fd23946593f47487709497 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Thu, 4 Aug 2016 16:18:31 +0000 Subject: [PATCH] lib: Add support for negative ranges And convert range delimiters to signed int Signed-off-by: Quentin Young --- lib/command_graph.c | 2 +- lib/command_graph.h | 2 +- lib/command_lex.l | 6 +++--- lib/command_match.c | 9 ++++----- lib/command_parse.y | 16 ++++++---------- 5 files changed, 15 insertions(+), 20 deletions(-) diff --git a/lib/command_graph.c b/lib/command_graph.c index 05570d5c7..d420b1c06 100644 --- a/lib/command_graph.c +++ b/lib/command_graph.c @@ -185,7 +185,7 @@ dump_node (struct graph_node *node) fprintf(stderr, "\t->value: %ld\n", node->value); fprintf(stderr, "\t->is_start: %d\n", node->is_start); fprintf(stderr, "\t->element: %p\n", node->element); - fprintf(stderr, "\t->min: %lld\n->max: %lld\n", node->min, node->max); + fprintf(stderr, "\t->min: %d\n->max: %d\n", node->min, node->max); fprintf(stderr, "\t->arg: %s\n", node->arg); fprintf(stderr, "\t->refs: %d\n", node->refs); fprintf(stderr, "\tnum children: %d\n", vector_active(node->children)); diff --git a/lib/command_graph.h b/lib/command_graph.h index 35a0eee27..925133c20 100644 --- a/lib/command_graph.h +++ b/lib/command_graph.h @@ -29,7 +29,7 @@ struct graph_node char* text; // for WORD_GN and VARIABLE_GN long value; // for NUMBER_GN - signed long long min, max;// for RANGE_GN + int min, max; // for RANGE_GN /* cmd_element struct pointer, only valid for END_GN */ struct cmd_element *element; diff --git a/lib/command_lex.l b/lib/command_lex.l index ff951149b..0a997de8d 100644 --- a/lib/command_lex.l +++ b/lib/command_lex.l @@ -4,13 +4,13 @@ extern void set_buffer_string(const char *); %} -WORD [-+a-z\*][-+_a-zA-Z0-9\*]* +WORD [-|+]?[a-z\*][-+_a-zA-Z0-9\*]* IPV4 A\.B\.C\.D IPV4_PREFIX A\.B\.C\.D\/M IPV6 X:X::X:X IPV6_PREFIX X:X::X:X\/M VARIABLE [A-Z][-_a-zA-Z:0-9]+ -NUMBER [0-9]{1,20} +NUMBER [-|+]?[0-9]{1,20} RANGE \({NUMBER}\-{NUMBER}\) /* yytext shall be a pointer */ @@ -30,7 +30,7 @@ RANGE \({NUMBER}\-{NUMBER}\) {VARIABLE} {yylval.string = XSTRDUP(MTYPE_TMP, yytext); return VARIABLE;} {NUMBER} { char *endptr; - yylval.integer = strtoll(yytext, &endptr, 10); + yylval.integer = strtoimax(yytext, &endptr, 10); return NUMBER; } {RANGE} {yylval.string = XSTRDUP(MTYPE_TMP, yytext); return RANGE;} diff --git a/lib/command_match.c b/lib/command_match.c index 305e3b1a6..aac2edc48 100644 --- a/lib/command_match.c +++ b/lib/command_match.c @@ -622,7 +622,7 @@ match_ipv6_prefix (const char *str) return no_match; /* validate mask */ - nmask = strtol (mask, &endptr, 10); + nmask = strtoimax (mask, &endptr, 10); if (*endptr != '\0' || nmask < 0 || nmask > 128) return no_match; @@ -636,15 +636,14 @@ static enum match_type match_range (struct graph_node *rangenode, const char *str) { char *endptr = NULL; - signed long long val; + signed int val; if (str == NULL) return 1; - val = strtoll (str, &endptr, 10); + val = strtoimax (str, &endptr, 10); if (*endptr != '\0') return 0; - val = llabs(val); if (val < rangenode->min || val > rangenode->max) return no_match; @@ -675,7 +674,7 @@ match_number(struct graph_node *numnode, const char *word) { if (!strcmp("\0", word)) return no_match; char *endptr; - long num = strtol(word, &endptr, 10); + int num = strtoimax(word, &endptr, 10); if (endptr != '\0') return no_match; return num == numnode->value ? exact_match : no_match; } diff --git a/lib/command_parse.y b/lib/command_parse.y index 0c36c2a6d..9d7432ca4 100644 --- a/lib/command_parse.y +++ b/lib/command_parse.y @@ -16,6 +16,8 @@ node_exists(struct graph_node *, struct graph_node *); struct graph_node * node_replace(struct graph_node *, struct graph_node *); +#define DECIMAL_STRLEN_MAX 20 + // compile with debugging facilities #define YYDEBUG 1 %} @@ -34,7 +36,7 @@ node_replace(struct graph_node *, struct graph_node *); /* valid types for tokens */ %union{ - signed long long integer; + signed int integer; char *string; struct graph_node *node; } @@ -203,10 +205,8 @@ placeholder_token: $$->text = XSTRDUP(MTYPE_CMD_TOKENS, $1); // get the numbers out - strsep(&yylval.string, "(-)"); - char *endptr; - $$->min = strtoll( strsep(&yylval.string, "(-)"), &endptr, 10 ); - $$->max = strtoll( strsep(&yylval.string, "(-)"), &endptr, 10 ); + $$->min = strtoimax( yylval.string+1, &yylval.string, 10 ); + $$->max = strtoimax( yylval.string+1, &yylval.string, 10 ); free ($1); } @@ -333,11 +333,7 @@ option_token: void yyerror(char const *message) { // fail on bad parse fprintf(stderr, "Grammar error: %s\n", message); - fprintf(stderr, "Token on error: "); - if (yylval.string) fprintf(stderr, "%s\n", yylval.string); - else if (yylval.node) fprintf(stderr, "%s\n", yylval.node->text); - else fprintf(stderr, "%lld\n", yylval.integer); - + exit(EXIT_FAILURE); } struct graph_node * -- 2.39.2