From: Nicolas Boichat Date: Tue, 26 Apr 2005 21:21:35 +0000 (+0000) Subject: wx-console: Improve error handling when reading configuration file. X-Git-Tag: Release-1.38.0~535 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=11913ce5400511449d124bbe2d18fb30e61476b5;p=bacula%2Fbacula wx-console: Improve error handling when reading configuration file. git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@1963 91ce42f0-d328-0410-95d8-f526ca767f89 --- diff --git a/bacula/src/lib/lex.c b/bacula/src/lib/lex.c index c73016e138..714b0f8a42 100644 --- a/bacula/src/lib/lex.c +++ b/bacula/src/lib/lex.c @@ -135,10 +135,7 @@ LEX *lex_open_file(LEX *lf, const char *filename, LEX_ERROR_HANDLER *scan_error) if ((fd = fopen(fname, "r")) == NULL) { - berrno be; - Emsg2(M_ERROR_TERM, 0, _("Cannot open config file %s: %s\n"), - fname, be.strerror()); - return NULL; /* Never reached if exit_on_error == 1 */ + return NULL; } Dmsg1(2000, "Open config file: %s\n", fname); nf = (LEX *)malloc(sizeof(LEX)); @@ -488,11 +485,17 @@ lex_get_token(LEX *lf, int expect) } if (B_ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' || ch == ';' || ch == ',' || ch == '"' || ch == '#') { + /* Keep the original LEX so we can print an error if the included file can't be opened. */ + LEX* lfori = lf; + lf->state = lex_none; lf = lex_open_file(lf, lf->str, NULL); - if (lf == NULL) { - return T_ERROR; - } + if (lf == NULL) { + berrno be; + scan_err2(lfori, _("Cannot open included config file %s: %s\n"), + lfori->str, be.strerror()); + return T_ERROR; + } break; } add_str(lf, ch); diff --git a/bacula/src/lib/message.c b/bacula/src/lib/message.c index 65079394b2..2e14810576 100755 --- a/bacula/src/lib/message.c +++ b/bacula/src/lib/message.c @@ -91,8 +91,6 @@ static bool trace = false; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static MSGS *daemon_msgs; /* global messages */ -/* Define if e_msg must exit when M_ERROR_TERM is received */ -static int exit_on_error = 1; /* * Set daemon name. Also, find canonical execution @@ -596,14 +594,6 @@ void dispatch_message(JCR *jcr, int type, time_t mtime, char *msg) fputs(dt, stdout); fputs(msg, stdout); /* print this here to INSURE that it is printed */ fflush(stdout); -#endif -#if !defined(HAVE_CONSOLE) -#if defined(HAVE_CYGWIN) || defined(HAVE_WIN32) - /* If we don't exit on error, error messages are parsed by UA */ - if (exit_on_error) { - MessageBox(NULL, msg, "Bacula", MB_OK); - } -#endif #endif } @@ -982,7 +972,7 @@ e_msg(const char *file, int line, int type, int level, const char *fmt,...) char *p = 0; p[0] = 0; /* generate segmentation violation */ } - if ((type == M_ERROR_TERM) && exit_on_error) { + if (type == M_ERROR_TERM) { exit(1); } } @@ -1079,7 +1069,7 @@ Jmsg(JCR *jcr, int type, time_t mtime, const char *fmt,...) char *p = 0; p[0] = 0; /* generate segmentation violation */ } - if ((type == M_ERROR_TERM) && exit_on_error) { + if (type == M_ERROR_TERM) { exit(1); } } @@ -1316,10 +1306,3 @@ void q_msg(const char *file, int line, JCR *jcr, int type, time_t mtime, const c Qmsg(jcr, type, mtime, "%s", pool_buf); free_memory(pool_buf); } - -/* - * Define if e_msg must exit when M_ERROR_TERM is received - */ -void set_exit_on_error(int value) { - exit_on_error = value; -} diff --git a/bacula/src/lib/parse_conf.c b/bacula/src/lib/parse_conf.c index ae9fe194fa..5213d73aa7 100755 --- a/bacula/src/lib/parse_conf.c +++ b/bacula/src/lib/parse_conf.c @@ -734,14 +734,13 @@ enum parse_state { /********************************************************************* * - * Parse configuration file + * Parse configuration file * * Return 0 if reading failed, 1 otherwise */ int -parse_config(const char *cf, int exit_on_error) +parse_config(const char *cf, LEX_ERROR_HANDLER *scan_error) { - set_exit_on_error(exit_on_error); LEX *lc = NULL; int token, i, pass; int res_type = 0; @@ -755,9 +754,15 @@ parse_config(const char *cf, int exit_on_error) Dmsg0(900, "Enter parse_config()\n"); for (pass=1; pass <= 2; pass++) { Dmsg1(900, "parse_config pass %d\n", pass); - if ((lc = lex_open_file(lc, cf, NULL)) == NULL) { - set_exit_on_error(1); /* Never reached if exit_on_error == 1 */ - return 0; + if ((lc = lex_open_file(lc, cf, scan_error)) == NULL) { + lc = (LEX *)malloc(sizeof(LEX)); + memset(lc, 0, sizeof(LEX)); + lc->scan_error = scan_error; + berrno be; + scan_err2(lc, _("Cannot open config file %s: %s\n"), + lc->str, be.strerror()); + free(lc); + return 0; } while ((token=lex_get_token(lc, T_ALL)) != T_EOF) { Dmsg1(900, "parse got token=%s\n", lex_tok_to_str(token)); @@ -768,7 +773,6 @@ parse_config(const char *cf, int exit_on_error) } if (token != T_IDENTIFIER) { scan_err1(lc, _("Expected a Resource name identifier, got: %s"), lc->str); - set_exit_on_error(1); /* Never reached if exit_on_error == 1 */ return 0; } for (i=0; resources[i].name; i++) @@ -781,8 +785,7 @@ parse_config(const char *cf, int exit_on_error) } if (state == p_none) { scan_err1(lc, _("expected resource name, got: %s"), lc->str); - set_exit_on_error(1); /* Never reached if exit_on_error == 1 */ - return 0; + return 0; } break; case p_resource: @@ -793,7 +796,6 @@ parse_config(const char *cf, int exit_on_error) case T_IDENTIFIER: if (level != 1) { scan_err1(lc, _("not in resource definition: %s"), lc->str); - set_exit_on_error(1); /* Never reached if exit_on_error == 1 */ return 0; } for (i=0; items[i].name; i++) { @@ -805,7 +807,6 @@ parse_config(const char *cf, int exit_on_error) Dmsg1 (900, "in T_IDENT got token=%s\n", lex_tok_to_str(token)); if (token != T_EQUALS) { scan_err1(lc, _("expected an equals, got: %s"), lc->str); - set_exit_on_error(1); /* Never reached if exit_on_error == 1 */ return 0; } } @@ -821,7 +822,6 @@ parse_config(const char *cf, int exit_on_error) Dmsg1(900, "Keyword = %s\n", lc->str); scan_err1(lc, _("Keyword \"%s\" not permitted in this resource.\n" "Perhaps you left the trailing brace off of the previous resource."), lc->str); - set_exit_on_error(1); /* Never reached if exit_on_error == 1 */ return 0; } break; @@ -839,19 +839,16 @@ parse_config(const char *cf, int exit_on_error) default: scan_err2(lc, _("unexpected token %d %s in resource definition"), token, lex_tok_to_str(token)); - set_exit_on_error(1); /* Never reached if exit_on_error == 1 */ return 0; } break; default: scan_err1(lc, _("Unknown parser state %d\n"), state); - set_exit_on_error(1); /* Never reached if exit_on_error == 1 */ return 0; } } if (state != p_none) { scan_err0(lc, _("End of conf file reached with unclosed resource.")); - set_exit_on_error(1); /* Never reached if exit_on_error == 1 */ return 0; } if (debug_level >= 900 && pass == 2) { @@ -863,7 +860,6 @@ parse_config(const char *cf, int exit_on_error) lc = lex_close_file(lc); } Dmsg0(900, "Leave parse_config()\n"); - set_exit_on_error(1); return 1; } diff --git a/bacula/src/lib/parse_conf.h b/bacula/src/lib/parse_conf.h index 46f08f2eec..af81738e57 100644 --- a/bacula/src/lib/parse_conf.h +++ b/bacula/src/lib/parse_conf.h @@ -96,7 +96,7 @@ union CURES { /* Configuration routines */ -int parse_config(const char *cf, int exit_on_error = 1); +int parse_config(const char *cf, LEX_ERROR_HANDLER *scan_error = NULL); void free_config_resources(void); RES **save_config_resources(void); RES **new_res_head(); diff --git a/bacula/src/lib/protos.h b/bacula/src/lib/protos.h index 1ca895a29d..ba13ad44bf 100644 --- a/bacula/src/lib/protos.h +++ b/bacula/src/lib/protos.h @@ -163,7 +163,6 @@ void init_console_msg (const char *wd); void free_msgs_res (MSGS *msgs); void dequeue_messages (JCR *jcr); void set_trace (int trace_flag); -void set_exit_on_error (int value); /* bnet_server.c */ void bnet_thread_server(dlist *addr, int max_clients, workq_t *client_wq, diff --git a/bacula/src/stored/parse_bsr.c b/bacula/src/stored/parse_bsr.c index 69df50cc4f..4aa77a03f1 100755 --- a/bacula/src/stored/parse_bsr.c +++ b/bacula/src/stored/parse_bsr.c @@ -129,7 +129,11 @@ BSR *parse_bsr(JCR *jcr, char *fname) BSR *bsr = root_bsr; Dmsg1(200, "Enter parse_bsf %s\n", fname); - lc = lex_open_file(lc, fname, s_err); + if ((lc = lex_open_file(lc, fname, s_err)) == NULL) { + berrno be; + Emsg2(M_ERROR_TERM, 0, _("Cannot open bootstrap file %s: %s\n"), + fname, be.strerror()); + } lc->caller_ctx = (void *)jcr; while ((token=lex_get_token(lc, T_ALL)) != T_EOF) { Dmsg1(200, "parse got token=%s\n", lex_tok_to_str(token)); diff --git a/bacula/src/wx-console/CHANGELOG b/bacula/src/wx-console/CHANGELOG index 408838d084..6816822d8e 100644 --- a/bacula/src/wx-console/CHANGELOG +++ b/bacula/src/wx-console/CHANGELOG @@ -1,4 +1,5 @@ 26-04-2005 : + - Improve error handling when reading configuration file. - Fixes compile error with unicode-enabled wxWidgets. 25-04-2005 : diff --git a/bacula/src/wx-console/console_thread.cpp b/bacula/src/wx-console/console_thread.cpp index ba51097f30..0eed96f451 100644 --- a/bacula/src/wx-console/console_thread.cpp +++ b/bacula/src/wx-console/console_thread.cpp @@ -85,6 +85,34 @@ void console_thread::FreeLib() { } } +wxString errmsg; + +/* + * Format a scanner error message + */ +static void scan_err(const char *file, int line, LEX *lc, const char *msg, ...) +{ + va_list arg_ptr; + char buf[MAXSTRING]; + char more[MAXSTRING]; + char err[MAXSTRING]; + + va_start(arg_ptr, msg); + bvsnprintf(buf, sizeof(buf), msg, arg_ptr); + va_end(arg_ptr); + + if (lc->line_no > lc->begin_line_no) { + bsnprintf(more, sizeof(more), + _("Problem probably begins at line %d.\n"), lc->begin_line_no); + } else { + more[0] = 0; + } + bsnprintf(err, sizeof(err), _("Config error: %s\n" +" : line %d, col %d of file %s\n%s\n%s"), + buf, lc->line_no, lc->col_no, lc->fname, lc->line, more); + errmsg << err; +} + wxString console_thread::LoadConfig(wxString configfile) { if (!inited) { InitLib(); @@ -107,24 +135,11 @@ wxString console_thread::LoadConfig(wxString configfile) { init_msg(NULL, msgs); init_console_msg(console_thread::working_dir); - if (!parse_config(configfile.c_str(), 0)) { + errmsg = ""; + if (!parse_config(configfile.c_str(), &scan_err)) { configloaded = false; - wxFile file(console_thread::working_dir + "/wx-console.conmsg"); - if (!file.IsOpened()) - return "Unable to retrieve error message."; - wxString err = ""; - wxChar buffer[513]; - off_t len; - while ((len = file.Read(buffer, 512)) > -1) { - buffer[len] = (wxChar)0; - err += buffer; - if (file.Eof()) - break; - } - file.Close(); term_msg(); - wxRemoveFile(console_thread::working_dir + "/wx-console.conmsg"); - return err; + return errmsg; } term_msg();