From 4d62bdd636e7dff1f9a72c794ef20fe0f5514772 Mon Sep 17 00:00:00 2001 From: Pavel Tvrdik Date: Fri, 11 Nov 2016 18:40:03 +0100 Subject: [PATCH] Parser: Display token where parser fell down For example I misspelled device protocol bird: bird.conf, line 11 (nearby 'demice'): syntax error It would be better to display whole err line. We can read it from config->err_file_name again and display the line at config->err_lino. --- conf/conf.c | 3 +++ conf/conf.h | 1 + nest/cli.c | 2 +- sysdep/unix/main.c | 6 +++--- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/conf/conf.c b/conf/conf.c index 885e2e7e..80bf6b8b 100644 --- a/conf/conf.c +++ b/conf/conf.c @@ -61,6 +61,8 @@ static jmp_buf conf_jmpbuf; struct config *config, *new_config; +char *cf_text; /* Actual position from parser */ + static struct config *old_config; /* Old configuration */ static struct config *future_config; /* New config held here if recon requested during recon */ static int old_cftype; /* Type of transition old_config -> config (RECONFIG_SOFT/HARD) */ @@ -511,6 +513,7 @@ cf_error(const char *msg, ...) strcpy(buf, ""); va_end(args); new_config->err_msg = cfg_strdup(buf); + new_config->err_token = cfg_strdup(cf_text); new_config->err_lino = ifs->lino; new_config->err_file_name = ifs->file_name; cf_lex_unwind(); diff --git a/conf/conf.h b/conf/conf.h index f174d352..ab940033 100644 --- a/conf/conf.h +++ b/conf/conf.h @@ -49,6 +49,7 @@ struct config { int err_lino; /* Line containing error */ char *err_file_name; /* File name containing error */ char *file_name; /* Name of main configuration file */ + char *err_token; /* Token where parser fell down */ int file_fd; /* File descriptor of main configuration file */ HASH(struct symbol) sym_hash; /* Lexer: symbol hash table */ struct config *fallback; /* Link to regular config for CLI parsing */ diff --git a/nest/cli.c b/nest/cli.c index c421cc7e..148a626c 100644 --- a/nest/cli.c +++ b/nest/cli.c @@ -270,7 +270,7 @@ cli_command(struct cli *c) lp_flush(c->parser_pool); res = cli_parse(&f); if (!res) - cli_printf(c, 9001, f.err_msg); + cli_printf(c, 9001, "Bad command (nearby '%s'): %s", f.err_token, f.err_msg); config_free(&f); } diff --git a/sysdep/unix/main.c b/sysdep/unix/main.c index ea1476e7..dcfb9b4b 100644 --- a/sysdep/unix/main.c +++ b/sysdep/unix/main.c @@ -213,7 +213,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, line %d (nearby '%s'): %s", conf->err_file_name, conf->err_lino, conf->err_token, conf->err_msg); else die("Unable to open configuration file %s: %m", config_name); } @@ -230,7 +230,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, line %d (nearby '%s'): %s", conf->err_file_name, conf->err_lino, conf->err_token, conf->err_msg); else log(L_ERR "Unable to open configuration file %s: %m", config_name); config_free(conf); @@ -251,7 +251,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, line %d (nearby '%s'): %s", conf->err_file_name, conf->err_lino, conf->err_token, conf->err_msg); else cli_msg(8002, "%s: %m", name); config_free(conf);