From afa14f1868f2c753efdc81ce8e2c2d44e6bdd80e Mon Sep 17 00:00:00 2001 From: Jan Maria Matejka Date: Tue, 11 Sep 2018 16:55:41 +0200 Subject: Conf: Show the line:char position where the syntax error happens --- conf/cf-lex.l | 6 ++++-- conf/conf.c | 1 + conf/conf.h | 4 +++- sysdep/unix/main.c | 6 +++--- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/conf/cf-lex.l b/conf/cf-lex.l index 66be3811..6d534c19 100644 --- a/conf/cf-lex.l +++ b/conf/cf-lex.l @@ -100,6 +100,7 @@ static struct include_file_stack *ifs_head; #define YY_INPUT(buf,result,max) result = cf_read_hook(buf, max, ifs->fd); #define YY_NO_UNPUT #define YY_FATAL_ERROR(msg) cf_error(msg) +#define YY_USER_ACTION ifs->chno += yyleng; ifs->toklen = yyleng; static void cf_include(char *arg, int alen); static int check_eof(void); @@ -237,7 +238,7 @@ else: { {WHITE}+ -\n ifs->lino++; +\n ifs->lino++; ifs->chno = 0; # BEGIN(COMMENT); @@ -247,13 +248,14 @@ else: { \n { ifs->lino++; + ifs->chno = 0; BEGIN(INITIAL); } . \*\/ BEGIN(INITIAL); -\n ifs->lino++; +\n ifs->lino++; ifs->chno = 0; \/\* cf_error("Comment nesting not supported"); <> cf_error("Unterminated comment"); . diff --git a/conf/conf.c b/conf/conf.c index 7f4eb7e8..f64932f5 100644 --- a/conf/conf.c +++ b/conf/conf.c @@ -515,6 +515,7 @@ cf_error(char *msg, ...) va_end(args); new_config->err_msg = cfg_strdup(buf); new_config->err_lino = ifs->lino; + new_config->err_chno = ifs->chno - ifs->toklen + 1; new_config->err_file_name = ifs->file_name; cf_lex_unwind(); longjmp(conf_jmpbuf, 1); diff --git a/conf/conf.h b/conf/conf.h index bf74b76b..6d8ed5ca 100644 --- a/conf/conf.h +++ b/conf/conf.h @@ -48,6 +48,7 @@ struct config { u32 watchdog_timeout; /* Watchdog timeout (in seconds, 0 = disabled) */ char *err_msg; /* Parser error message */ int err_lino; /* Line containing error */ + int err_chno; /* Character where the parser stopped */ char *err_file_name; /* File name containing error */ char *file_name; /* Name of main configuration file */ int file_fd; /* File descriptor of main configuration file */ @@ -141,6 +142,8 @@ struct include_file_stack { char *file_name; /* File name */ int fd; /* File descriptor */ int lino; /* Current line num */ + int chno; /* Current char num (on current line) */ + int toklen; /* Current token length */ int depth; /* Include depth, 0 = cannot include */ struct include_file_stack *prev; /* Previous record in stack */ @@ -149,7 +152,6 @@ struct include_file_stack { extern struct include_file_stack *ifs; - int cf_lex(void); void cf_lex_init(int is_cli, struct config *c); void cf_lex_unwind(void); diff --git a/sysdep/unix/main.c b/sysdep/unix/main.c index 8aa19fce..99cfab17 100644 --- a/sysdep/unix/main.c +++ b/sysdep/unix/main.c @@ -212,7 +212,7 @@ read_config(void) if (!unix_read_config(&conf, config_name)) { if (conf->err_msg) - die("%s, line %d: %s", conf->err_file_name, conf->err_lino, conf->err_msg); + die("%s:%d:%d %s", conf->err_file_name, conf->err_lino, conf->err_chno, conf->err_msg); else die("Unable to open configuration file %s: %m", config_name); } @@ -229,7 +229,7 @@ async_config(void) if (!unix_read_config(&conf, config_name)) { if (conf->err_msg) - log(L_ERR "%s, line %d: %s", conf->err_file_name, conf->err_lino, conf->err_msg); + log(L_ERR "%s:%d:%d %s", conf->err_file_name, conf->err_lino, conf->err_chno, conf->err_msg); else log(L_ERR "Unable to open configuration file %s: %m", config_name); config_free(conf); @@ -250,7 +250,7 @@ cmd_read_config(char *name) if (!unix_read_config(&conf, name)) { if (conf->err_msg) - cli_msg(8002, "%s, line %d: %s", conf->err_file_name, conf->err_lino, conf->err_msg); + cli_msg(8002, "%s:%d:%d %s", conf->err_file_name, conf->err_lino, conf->err_chno, conf->err_msg); else cli_msg(8002, "%s: %m", name); config_free(conf); -- cgit v1.2.3