From 55b587a417a305132946994923755ce9a87e3cec Mon Sep 17 00:00:00 2001 From: Pierre POMES Date: Mon, 26 Aug 2024 17:14:22 -0400 Subject: [PATCH] Flex (config and dump parser): manually handle line number, yylineno not always reporting good value --- main/configparser.y | 8 ++++---- main/configscanner.l | 4 ++-- main/dumpparser.y | 4 ++-- main/dumpscanner.l | 9 ++++++--- main/myanon.c | 6 ++++-- main/myanon.h | 8 ++++++-- 6 files changed, 24 insertions(+), 15 deletions(-) diff --git a/main/configparser.y b/main/configparser.y index 6ed7449..c8892d9 100644 --- a/main/configparser.y +++ b/main/configparser.y @@ -129,7 +129,7 @@ pypathline: } } #else - fprintf(stderr, "Python support disabled, ignoring pypath directive at line %d\n",config_lineno); + fprintf(stderr, "Python support disabled, ignoring pypath directive at line %d\n",config_line_nb); #endif } @@ -138,7 +138,7 @@ pyscriptline: #ifdef HAVE_PYTHON remove_quote(pyscript,$3,sizeof(secret)); #else - fprintf(stderr, "Python support disabled, ignoring pyscript directive at line %d\n",config_lineno); + fprintf(stderr, "Python support disabled, ignoring pyscript directive at line %d\n",config_line_nb); #endif } @@ -286,7 +286,7 @@ pydef: workinfos.type = AM_PY; remove_quote(workinfos.pydef,$2,sizeof(workinfos.pydef)); #else - fprintf(stderr, "Python support disabled, ignoring pydef directive at line %d\n",config_lineno); + fprintf(stderr, "Python support disabled, ignoring pydef directive at line %d\n",config_line_nb); #endif } @@ -295,7 +295,7 @@ json: #ifdef HAVE_JQ workinfos.type = AM_JSON; #else - fprintf(stderr, "JQ support disabled, ignoring json directive at line %d\n",config_lineno); + fprintf(stderr, "JQ support disabled, ignoring json directive at line %d\n",config_line_nb); #endif } diff --git a/main/configscanner.l b/main/configscanner.l index 944b0aa..5604b83 100644 --- a/main/configscanner.l +++ b/main/configscanner.l @@ -42,7 +42,6 @@ %option prefix="config_" %option outfile="lex.yy.c" %option noyywrap -%option yylineno %% @@ -88,7 +87,8 @@ separated\ by { return SEPARATEDBY;} COPYSTR return IDENTIFIER; } -[ \n] +[ ] +\n { config_line_nb ++; } #.* . { config_error("Syntax error"); diff --git a/main/dumpparser.y b/main/dumpparser.y index 2ec07ce..1de270f 100644 --- a/main/dumpparser.y +++ b/main/dumpparser.y @@ -240,7 +240,7 @@ singlefield : VALUE { } else { - fprintf(stderr, "WARNING! Table/field %s: Unable to parse seperated field '%s'at line %d, skip anonimyzation",cur->key,dump_text,dump_lineno); + fprintf(stderr, "WARNING! Table/field %s: Unable to parse seperated field '%s'at line %d, skip anonimyzation",cur->key,dump_text,dump_line_nb); fwrite(dump_text,dump_leng,1,stdout); bDone=true; continue; @@ -343,7 +343,7 @@ singlefield : VALUE { jv_free(input_json); } else { - fprintf(stderr, "WARNING! Table/field %s: Unable to parse json field '%s' at line %d, skip anonimyzation\n",cur->key, unbackslash_json_str,dump_lineno); + fprintf(stderr, "WARNING! Table/field %s: Unable to parse json field '%s' at line %d, skip anonimyzation\n",cur->key, unbackslash_json_str,dump_line_nb); fwrite(dump_text,dump_leng,1,stdout); } break; diff --git a/main/dumpscanner.l b/main/dumpscanner.l index 72cca2f..f57e8a0 100644 --- a/main/dumpscanner.l +++ b/main/dumpscanner.l @@ -102,7 +102,6 @@ bool is_truncated() { %option noyywrap %option full %option pointer -%option yylineno %x ST_TABLE ST_TRUNCATE ST_VALUES @@ -126,6 +125,7 @@ bool is_truncated() { return INSERT_INTO; } } +\n { DUPOUT dump_line_nb++; } . { DUPOUT } ^CREATE\ TABLE\ `[^`]+` { DUPOUT @@ -144,6 +144,7 @@ bool is_truncated() { } ^(INSERT|REPLACE).* { /* In truncate mode, skip statements */} +\n { DUPOUT dump_line_nb++; } . { DUPOUT } @@ -177,7 +178,8 @@ bool is_truncated() { KEY.* { DUPOUT } CONSTRAINT.* { DUPOUT } DELIMITER.* { DUPOUT } -[ \n] { DUPOUT } +[ ] { DUPOUT } +\n { DUPOUT dump_line_nb++; } ENGINE.* { BEGIN(INITIAL); DEBUG_MSG("\nENTERING STATE INITIAL") DUPOUT @@ -200,7 +202,8 @@ bool is_truncated() { _binary\ '(\\.|[^'\\])*' { return VALUE; } '(\\.|[^'\\])*' { return VALUE; } [0-9\-\.e]+ { return VALUE; } -[ \n] { DUPOUT } +[ ] { DUPOUT } +\n { DUPOUT dump_line_nb++;} ^(INSERT|REPLACE)\ (\ IGNORE\ )?\INTO\ `[^`]+`(?:\ \([^\)]+\))? { DUPOUT if (is_anonymized()) { return INSERT_INTO; diff --git a/main/myanon.c b/main/myanon.c index 0d61f93..db54754 100644 --- a/main/myanon.c +++ b/main/myanon.c @@ -284,7 +284,7 @@ anonymized_res_st anonymize_token(bool quoted, anon_base_st *config, char *token void config_error(const char *s) { fprintf(stderr, "Config parsing error at line %d: %s - Unexpected [%s]\n", - config_lineno, s, config_text); + config_line_nb, s, config_text); } void dump_error(const char *s) @@ -292,7 +292,7 @@ void dump_error(const char *s) // flush (buffered) stdout and report error fflush(stdout); fprintf(stderr, "\nDump parsing error at line %d: %s - Unexpected [%s]\n", - dump_lineno, s, dump_text); + dump_line_nb, s, dump_text); } /* @@ -322,6 +322,8 @@ int main(int argc, char **argv) stats = false; debug = false; anon_time = 0; + config_line_nb = 1; + dump_line_nb = 1; /* For stats */ ts_beg = get_ts_in_ms(); diff --git a/main/myanon.h b/main/myanon.h index 95b85ad..d479543 100644 --- a/main/myanon.h +++ b/main/myanon.h @@ -225,7 +225,6 @@ int config_lex(); /* Config Lexer */ int config_parse(); /* Config Parser */ int config_lex_destroy(); /* Config Lexer 'destructor' */ void config_error(const char *s); /* Config parser error function */ -extern int config_lineno; /* Config Line number */ extern char *config_text; /* Config last token found */ extern int config_leng; /* Config last token length */ extern FILE *config_in; /* Config Lexer input */ @@ -237,8 +236,13 @@ int dump_lex(); /* Dump Lexer */ int dump_parse(); /* Dump Parser */ int dump_lex_destroy(); /* Dump Lexer 'destructor' */ void dump_error(const char *s); /* Dump parser error function */ -extern int dump_lineno; /* Dump line numner */ extern char *dump_text; /* Dump last token found */ extern int dump_leng; /* Dump last token length */ +/* + * Parser line numbers + */ +EXTERN int config_line_nb; /* Config file line numer */ +EXTERN int dump_line_nb; /* Dump line number */ + #endif