X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=bacula%2Fsrc%2Flib%2Flex.c;h=88dcfe1b4e7cd7bb885d86c4e175854fd15dd4c1;hb=458061b568c4451c7f1d2212879645b2625651fb;hp=51a6784842f636e429691aa0f2d935c9b1bd6779;hpb=14688fe152ffecdc82336decd897a0b5e56bb670;p=bacula%2Fbacula diff --git a/bacula/src/lib/lex.c b/bacula/src/lib/lex.c index 51a6784842..88dcfe1b4e 100644 --- a/bacula/src/lib/lex.c +++ b/bacula/src/lib/lex.c @@ -36,13 +36,9 @@ extern int debug_level; */ void scan_to_eol(LEX *lc) { - int token; Dmsg0(150, "start scan to eof\n"); - lc->expect = 0; /* clear expectations */ - if (token != T_EOL) { - while ((token = lex_get_token(lc)) != T_EOL) { - } - } + while (lex_get_token(lc, T_ALL) != T_EOL) + { } Dmsg0(150, "done scan to eof\n"); } @@ -50,18 +46,24 @@ void scan_to_eol(LEX *lc) /* * Format a scanner error message */ -void s_err(char *file, int line, LEX *lc, char *msg, ...) +static void s_err(char *file, int line, LEX *lc, char *msg, ...) { va_list arg_ptr; char buf[MAXSTRING]; + char more[MAXSTRING]; va_start(arg_ptr, msg); bvsnprintf(buf, sizeof(buf), msg, arg_ptr); va_end(arg_ptr); - e_msg(file, line, M_ERROR_TERM, 0, "Config error: %s\n\ - : Line %d, col %d of file %s\n%s\n", - buf, lc->line_no, lc->col_no, lc->fname, lc->line); + if (lc->line_no > lc->begin_line_no) { + sprintf(more, _("Problem probably begins at Line %d.\n"), lc->begin_line_no); + } else { + more[0] = 0; + } + e_msg(file, line, M_ERROR_TERM, 0, _("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); } @@ -69,23 +71,22 @@ void s_err(char *file, int line, LEX *lc, char *msg, ...) * Free the current file, and retrieve the contents * of the previous packet if any. */ -LEX * -lex_close_file(LEX *lf) +LEX *lex_close_file(LEX *lf) { LEX *of; - Dmsg1(20, "Close lex file: %s\n", lf->fname); + Dmsg1(40, "Close lex file: %s\n", lf->fname); if (lf == NULL) { Emsg0(M_ABORT, 0, "Close of NULL file\n"); } of = lf->next; fclose(lf->fd); - Dmsg1(29, "Close cfg file %s\n", lf->fname); + Dmsg1(49, "Close cfg file %s\n", lf->fname); free(lf->fname); if (of) { of->options = lf->options; /* preserve options */ memcpy(lf, of, sizeof(LEX)); - Dmsg1(29, "Restart scan of cfg file %s\n", of->fname); + Dmsg1(49, "Restart scan of cfg file %s\n", of->fname); } else { of = lf; lf = NULL; @@ -105,8 +106,8 @@ lex_close_file(LEX *lf) * the next field. * */ -LEX * -lex_open_file(LEX *lf, char *filename) +LEX *lex_open_file(LEX *lf, char *filename, LEX_ERROR_HANDLER *scan_error) + { LEX *nf; FILE *fd; @@ -114,9 +115,10 @@ lex_open_file(LEX *lf, char *filename) if ((fd = fopen(fname, "r")) == NULL) { - Emsg2(M_ABORT, 0, "Cannot open config file %s: %s\n", fname, strerror(errno)); + Emsg2(M_ERROR_TERM, 0, _("Cannot open config file %s: %s\n"), + fname, strerror(errno)); } - Dmsg1(29, "Open config file: %s\n", fname); + Dmsg1(49, "Open config file: %s\n", fname); nf = (LEX *)malloc(sizeof(LEX)); if (lf) { memcpy(nf, lf, sizeof(LEX)); @@ -131,7 +133,12 @@ lex_open_file(LEX *lf, char *filename) lf->fname = fname; lf->state = lex_none; lf->ch = L_EOL; - Dmsg1(29, "Return lex=%x\n", lf); + if (scan_error) { + lf->scan_error = scan_error; + } else { + lf->scan_error = s_err; + } + Dmsg1(49, "Return lex=%x\n", lf); return lf; } @@ -141,11 +148,11 @@ lex_open_file(LEX *lf, char *filename) * L_EOF if end of file * L_EOL if end of line */ -int -lex_get_char(LEX *lf) +int lex_get_char(LEX *lf) { - if (lf->ch == L_EOF) + if (lf->ch == L_EOF) { Emsg0(M_ABORT, 0, "get_char: called after EOF\n"); + } if (lf->ch == L_EOL) { if (fgets(lf->line, MAXSTRING, lf->fd) == NULL) { lf->ch = L_EOF; @@ -167,8 +174,7 @@ lex_get_char(LEX *lf) return lf->ch; } -void -lex_unget_char(LEX *lf) +void lex_unget_char(LEX *lf) { lf->col_no--; if (lf->ch == L_EOL) @@ -182,7 +188,9 @@ lex_unget_char(LEX *lf) static void add_str(LEX *lf, int ch) { if (lf->str_len >= MAXSTRING-3) { - Emsg2(M_ABORT, 0, "Token too long, file: %s, line %s\n", lf->fname, lf->line_no); + Emsg3(M_ERROR_TERM, 0, _( + "Token too long, file: %s, line %d, begins at line %d\n"), + lf->fname, lf->line_no, lf->begin_line_no); } lf->str[lf->str_len++] = ch; lf->str[lf->str_len] = 0; @@ -195,13 +203,14 @@ static void begin_str(LEX *lf, int ch) { lf->str_len = 0; lf->str[0] = 0; - if (ch != 0) + if (ch != 0) { add_str(lf, ch); + } + lf->begin_line_no = lf->line_no; /* save start string line no */ } #ifdef DEBUG -static char * -lex_state_to_str(int state) +static char *lex_state_to_str(int state) { switch (state) { case lex_none: return "none"; @@ -220,39 +229,40 @@ lex_state_to_str(int state) * Convert a lex token to a string * used for debug/error printing. */ -char * -lex_tok_to_str(int token) +char *lex_tok_to_str(int token) { switch(token) { - case L_EOF: return "L_EOF"; - case L_EOL: return "L_EOL"; - case T_NONE: return "T_NONE"; - case T_NUMBER: return "T_NUMBER"; - case T_IPADDR: return "T_IPADDR"; - case T_IDENTIFIER: return "T_IDENTIFIER"; - case T_STRING: return "T_STRING"; - case T_QUOTED_STRING: return "T_QUOTED_STRING"; - case T_BOB: return "T_BOB"; - case T_EOB: return "T_EOB"; - case T_EQUALS: return "T_EQUALS"; - case T_ERROR: return "T_ERROR"; - case T_EOF: return "T_EOF"; - case T_COMMA: return "T_COMMA"; - case T_EOL: return "T_EOL"; - default: return "??????"; + case L_EOF: return "L_EOF"; + case L_EOL: return "L_EOL"; + case T_NONE: return "T_NONE"; + case T_NUMBER: return "T_NUMBER"; + case T_IPADDR: return "T_IPADDR"; + case T_IDENTIFIER: return "T_IDENTIFIER"; + case T_UNQUOTED_STRING: return "T_UNQUOTED_STRING"; + case T_QUOTED_STRING: return "T_QUOTED_STRING"; + case T_BOB: return "T_BOB"; + case T_EOB: return "T_EOB"; + case T_EQUALS: return "T_EQUALS"; + case T_ERROR: return "T_ERROR"; + case T_EOF: return "T_EOF"; + case T_COMMA: return "T_COMMA"; + case T_EOL: return "T_EOL"; + default: return "??????"; } } static uint32_t scan_pint(LEX *lf, char *str) { - double dval; + double dval = 0; if (!is_a_number(str)) { scan_err1(lf, "expected a positive integer number, got: %s", str); + /* NOT REACHED */ } else { errno = 0; dval = strtod(str, NULL); if (errno != 0 || dval < 0) { scan_err1(lf, "expected a postive integer number, got: %s", str); + /* NOT REACHED */ } } return (uint32_t)dval; @@ -264,7 +274,7 @@ static uint32_t scan_pint(LEX *lf, char *str) * */ int -lex_get_token(LEX *lf) +lex_get_token(LEX *lf, int expect) { int ch; int token = T_NONE; @@ -276,9 +286,9 @@ lex_get_token(LEX *lf) switch (lf->state) { case lex_none: Dmsg2(290, "Lex state lex_none ch=%d,%x\n", ch, ch); - if (ISSPACE(ch)) + if (B_ISSPACE(ch)) break; - if (ISALPHA(ch)) { + if (B_ISALPHA(ch)) { if (lf->options & LOPT_NO_IDENT) lf->state = lex_string; else @@ -286,7 +296,7 @@ lex_get_token(LEX *lf) begin_str(lf, ch); break; } - if (ISDIGIT(ch)) { + if (B_ISDIGIT(ch)) { lf->state = lex_number; begin_str(lf, ch); break; @@ -342,18 +352,24 @@ lex_get_token(LEX *lf) if (ch == L_EOL) { lf->state = lex_none; token = T_EOL; + } else if (ch == L_EOF) { + token = T_ERROR; } break; case lex_number: Dmsg2(290, "Lex state lex_number ch=%x %c\n", ch, ch); + if (ch == L_EOF) { + token = T_ERROR; + break; + } /* Might want to allow trailing specifications here */ - if (ISDIGIT(ch)) { + if (B_ISDIGIT(ch)) { add_str(lf, ch); break; } /* A valid number can be terminated by the following */ - if (ISSPACE(ch) || ch == L_EOL || ch == ',' || ch == ';') { + if (B_ISSPACE(ch) || ch == L_EOL || ch == ',' || ch == ';') { token = T_NUMBER; lf->state = lex_none; } else { @@ -362,14 +378,22 @@ lex_get_token(LEX *lf) lex_unget_char(lf); break; case lex_ip_addr: + if (ch == L_EOF) { + token = T_ERROR; + break; + } Dmsg1(290, "Lex state lex_ip_addr ch=%x\n", ch); break; case lex_string: Dmsg1(290, "Lex state lex_string ch=%x\n", ch); + if (ch == L_EOF) { + token = T_ERROR; + break; + } if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' || - ch == ';' || ch == ',' || ch == '#' || (ISSPACE(ch)) ) { + ch == ';' || ch == ',' || ch == '#' || (B_ISSPACE(ch)) ) { lex_unget_char(lf); - token = T_STRING; + token = T_UNQUOTED_STRING; lf->state = lex_none; break; } @@ -377,10 +401,10 @@ lex_get_token(LEX *lf) break; case lex_identifier: Dmsg2(290, "Lex state lex_identifier ch=%x %c\n", ch, ch); - if (ISALPHA(ch)) { + if (B_ISALPHA(ch)) { add_str(lf, ch); break; - } else if (ISSPACE(ch)) { + } else if (B_ISSPACE(ch)) { break; } else if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' || ch == ';' || ch == ',' || ch == '"' || ch == '#') { @@ -400,6 +424,10 @@ lex_get_token(LEX *lf) break; case lex_quoted_string: Dmsg2(290, "Lex state lex_quoted_string ch=%x %c\n", ch, ch); + if (ch == L_EOF) { + token = T_ERROR; + break; + } if (ch == L_EOL) { esc_next = FALSE; break; @@ -421,10 +449,14 @@ lex_get_token(LEX *lf) add_str(lf, ch); break; case lex_include: /* scanning a filename */ - if (ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' || + if (ch == L_EOF) { + token = T_ERROR; + break; + } + if (B_ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' || ch == ';' || ch == ',' || ch == '"' || ch == '#') { lf->state = lex_none; - lf = lex_open_file(lf, lf->str); + lf = lex_open_file(lf, lf->str, NULL); break; } add_str(lf, ch); @@ -441,7 +473,7 @@ lex_get_token(LEX *lf) * expectations (e.g. 32 bit integer). If so, we do type checking * and possible additional scanning (e.g. for range). */ - switch (lf->expect) { + switch (expect) { case T_PINT32: lf->pint32_val = scan_pint(lf, lf->str); lf->pint32_val2 = lf->pint32_val; @@ -456,7 +488,10 @@ lex_get_token(LEX *lf) } else { char *p = strchr(lf->str, '-'); if (!p) { - scan_err1(lf, "expected an integer or a range, got: %s", lf->str); + scan_err2(lf, "expected an integer or a range, got %s: %s", + lex_tok_to_str(token), lf->str); + token = T_ERROR; + break; } *p++ = 0; /* terminate first half of range */ lf->pint32_val = scan_pint(lf, lf->str); @@ -467,41 +502,66 @@ lex_get_token(LEX *lf) case T_INT32: if (token != T_NUMBER || !is_a_number(lf->str)) { - scan_err1(lf, "expected an integer number, got: %s", lf->str); + scan_err2(lf, "expected an integer number, got %s: %s", + lex_tok_to_str(token), lf->str); + token = T_ERROR; + break; + } + errno = 0; + lf->int32_val = (int32_t)strtod(lf->str, NULL); + if (errno != 0) { + scan_err2(lf, "expected an integer number, got %s: %s", + lex_tok_to_str(token), lf->str); + token = T_ERROR; } else { - errno = 0; - lf->int32_val = (int32_t)strtod(lf->str, NULL); - if (errno != 0) { - scan_err1(lf, "expected an integer number, got: %s", lf->str); - } + token = T_INT32; } - token = T_INT32; break; case T_INT64: Dmsg2(400, "int64=:%s: %f\n", lf->str, strtod(lf->str, NULL)); if (token != T_NUMBER || !is_a_number(lf->str)) { - scan_err1(lf, "expected an integer number, got: %s", lf->str); + scan_err2(lf, "expected an integer number, got %s: %s", + lex_tok_to_str(token), lf->str); + token = T_ERROR; + break; + } + errno = 0; + lf->int64_val = (int64_t)strtod(lf->str, NULL); + if (errno != 0) { + scan_err2(lf, "expected an integer number, got %s: %s", + lex_tok_to_str(token), lf->str); + token = T_ERROR; } else { - errno = 0; - lf->int64_val = (int64_t)strtod(lf->str, NULL); - if (errno != 0) { - scan_err1(lf, "expected an integer number, got: %s", lf->str); - } + token = T_INT64; } - token = T_INT64; break; case T_NAME: - if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) { - scan_err1(lf, "expected a name: %s", lf->str); + if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) { + scan_err2(lf, "expected a name, got %s: %s", + lex_tok_to_str(token), lf->str); + token = T_ERROR; } else if (lf->str_len > MAX_RES_NAME_LENGTH) { scan_err3(lf, "name %s length %d too long, max is %d\n", lf->str, lf->str_len, MAX_RES_NAME_LENGTH); + token = T_ERROR; + } else { + token = T_NAME; } - token = T_NAME; break; + case T_STRING: + if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) { + scan_err2(lf, "expected a string, got %s: %s", + lex_tok_to_str(token), lf->str); + token = T_ERROR; + } else { + token = T_STRING; + } + break; + + default: break; /* no expectation given */ }