2 * Lexical scanner for Bacula configuration file
11 Copyright (C) 2000-2005 Kern Sibbald
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of
16 the License, or (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public
24 License along with this program; if not, write to the Free
25 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
33 extern int debug_level;
36 * Scan to "logical" end of line. I.e. end of line,
37 * or semicolon, but stop on T_EOB (same as end of
38 * line except it is not eaten).
40 void scan_to_eol(LEX *lc)
43 Dmsg0(2000, "start scan to eof\n");
44 while ((token = lex_get_token(lc, T_ALL)) != T_EOL) {
53 * Get next token, but skip EOL
55 int scan_to_next_not_eol(LEX * lc)
59 token = lex_get_token(lc, T_ALL);
60 } while (token == T_EOL);
65 * Format a scanner error message
67 static void s_err(const char *file, int line, LEX *lc, const char *msg, ...)
73 va_start(arg_ptr, msg);
74 bvsnprintf(buf, sizeof(buf), msg, arg_ptr);
77 if (lc->line_no > lc->begin_line_no) {
78 bsnprintf(more, sizeof(more),
79 _("Problem probably begins at line %d.\n"), lc->begin_line_no);
83 if (lc->line_no > 0) {
84 e_msg(file, line, M_ERROR_TERM, 0, _("Config error: %s\n"
85 " : line %d, col %d of file %s\n%s\n%s"),
86 buf, lc->line_no, lc->col_no, lc->fname, lc->line, more);
88 e_msg(file, line, M_ERROR_TERM, 0, _("Config error: %s\n"), buf);
92 void lex_set_default_error_handler(LEX *lf)
94 lf->scan_error = s_err;
99 * Free the current file, and retrieve the contents
100 * of the previous packet if any.
102 LEX *lex_close_file(LEX *lf)
107 Emsg0(M_ABORT, 0, _("Close of NULL file\n"));
109 Dmsg1(2000, "Close lex file: %s\n", lf->fname);
113 Dmsg1(2000, "Close cfg file %s\n", lf->fname);
116 of->options = lf->options; /* preserve options */
117 memcpy(lf, of, sizeof(LEX));
118 Dmsg1(2000, "Restart scan of cfg file %s\n", of->fname);
128 * Open a new configuration file. We push the
129 * state of the current file (lf) so that we
130 * can do includes. This is a bit of a hammer.
131 * Instead of passing back the pointer to the
132 * new packet, I simply replace the contents
133 * of the caller's packet with the new packet,
134 * and link the contents of the old packet into
138 LEX *lex_open_file(LEX *lf, const char *filename, LEX_ERROR_HANDLER *scan_error)
143 char *fname = bstrdup(filename);
146 if ((fd = fopen(fname, "r")) == NULL) {
149 Dmsg1(400, "Open config file: %s\n", fname);
150 nf = (LEX *)malloc(sizeof(LEX));
152 memcpy(nf, lf, sizeof(LEX));
153 memset(lf, 0, sizeof(LEX));
154 lf->next = nf; /* if have lf, push it behind new one */
155 lf->options = nf->options; /* preserve user options */
157 lf = nf; /* start new packet */
158 memset(lf, 0, sizeof(LEX));
161 lf->scan_error = scan_error;
163 lex_set_default_error_handler(lf);
167 lf->state = lex_none;
169 Dmsg1(2000, "Return lex=%x\n", lf);
174 * Get the next character from the input.
175 * Returns the character or
176 * L_EOF if end of file
177 * L_EOL if end of line
179 int lex_get_char(LEX *lf)
181 if (lf->ch == L_EOF) {
182 Emsg0(M_ABORT, 0, _("get_char: called after EOF\n"));
184 if (lf->ch == L_EOL) {
185 if (bfgets(lf->line, MAXSTRING, lf->fd) == NULL) {
194 Dmsg2(1000, "fget line=%d %s", lf->line_no, lf->line);
196 lf->ch = (uint8_t)lf->line[lf->col_no];
202 Dmsg2(2000, "lex_get_char: %c %d\n", lf->ch, lf->ch);
206 void lex_unget_char(LEX *lf)
215 * Add a character to the current string
217 static void add_str(LEX *lf, int ch)
219 if (lf->str_len >= MAXSTRING-3) {
220 Emsg3(M_ERROR_TERM, 0, _(
221 _("Config token too long, file: %s, line %d, begins at line %d\n")),
222 lf->fname, lf->line_no, lf->begin_line_no);
224 lf->str[lf->str_len++] = ch;
225 lf->str[lf->str_len] = 0;
231 static void begin_str(LEX *lf, int ch)
238 lf->begin_line_no = lf->line_no; /* save start string line no */
242 static const char *lex_state_to_str(int state)
245 case lex_none: return _("none");
246 case lex_comment: return _("comment");
247 case lex_number: return _("number");
248 case lex_ip_addr: return _("ip_addr");
249 case lex_identifier: return _("identifier");
250 case lex_string: return _("string");
251 case lex_quoted_string: return _("quoted_string");
252 default: return "??????";
258 * Convert a lex token to a string
259 * used for debug/error printing.
261 const char *lex_tok_to_str(int token)
264 case L_EOF: return "L_EOF";
265 case L_EOL: return "L_EOL";
266 case T_NONE: return "T_NONE";
267 case T_NUMBER: return "T_NUMBER";
268 case T_IPADDR: return "T_IPADDR";
269 case T_IDENTIFIER: return "T_IDENTIFIER";
270 case T_UNQUOTED_STRING: return "T_UNQUOTED_STRING";
271 case T_QUOTED_STRING: return "T_QUOTED_STRING";
272 case T_BOB: return "T_BOB";
273 case T_EOB: return "T_EOB";
274 case T_EQUALS: return "T_EQUALS";
275 case T_ERROR: return "T_ERROR";
276 case T_EOF: return "T_EOF";
277 case T_COMMA: return "T_COMMA";
278 case T_EOL: return "T_EOL";
279 default: return "??????";
283 static uint32_t scan_pint(LEX *lf, char *str)
286 if (!is_a_number(str)) {
287 scan_err1(lf, _("expected a positive integer number, got: %s"), str);
291 val = str_to_int64(str);
292 if (errno != 0 || val < 0) {
293 scan_err1(lf, _("expected a positive integer number, got: %s"), str);
297 return (uint32_t)val;
302 * Get the next token from the input
306 lex_get_token(LEX *lf, int expect)
310 bool esc_next = false;
312 Dmsg0(2000, "enter lex_get_token\n");
313 while (token == T_NONE) {
314 ch = lex_get_char(lf);
317 Dmsg2(2000, "Lex state lex_none ch=%d,%x\n", ch, ch);
321 if (lf->options & LOPT_NO_IDENT || lf->options & LOPT_STRING) {
322 lf->state = lex_string;
324 lf->state = lex_identifier;
330 if (lf->options & LOPT_STRING) {
331 lf->state = lex_string;
333 lf->state = lex_number;
338 Dmsg0(2000, "Enter lex_none switch\n");
342 Dmsg0(2000, "got L_EOF set token=T_EOF\n");
345 lf->state = lex_comment;
356 lf->state = lex_quoted_string;
368 if (expect != T_SKIP_EOL) {
369 token = T_EOL; /* treat ; like EOL */
373 Dmsg0(2000, "got L_EOL set token=T_EOL\n");
374 if (expect != T_SKIP_EOL) {
379 lf->state = lex_include;
383 lf->state = lex_string;
389 Dmsg1(2000, "Lex state lex_comment ch=%x\n", ch);
391 lf->state = lex_none;
392 if (expect != T_SKIP_EOL) {
395 } else if (ch == L_EOF) {
400 Dmsg2(2000, "Lex state lex_number ch=%x %c\n", ch, ch);
405 /* Might want to allow trailing specifications here */
411 /* A valid number can be terminated by the following */
412 if (B_ISSPACE(ch) || ch == L_EOL || ch == ',' || ch == ';') {
414 lf->state = lex_none;
416 lf->state = lex_string;
425 Dmsg1(2000, "Lex state lex_ip_addr ch=%x\n", ch);
428 Dmsg1(2000, "Lex state lex_string ch=%x\n", ch);
433 if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
434 ch == '\r' || ch == ';' || ch == ',' || ch == '#' || (B_ISSPACE(ch)) ) {
436 token = T_UNQUOTED_STRING;
437 lf->state = lex_none;
443 Dmsg2(2000, "Lex state lex_identifier ch=%x %c\n", ch, ch);
447 } else if (B_ISSPACE(ch)) {
449 } else if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
450 ch == '\r' || ch == ';' || ch == ',' || ch == '"' || ch == '#') {
452 token = T_IDENTIFIER;
453 lf->state = lex_none;
455 } else if (ch == L_EOF) {
457 lf->state = lex_none;
461 /* Some non-alpha character => string */
462 lf->state = lex_string;
465 case lex_quoted_string:
466 Dmsg2(2000, "Lex state lex_quoted_string ch=%x %c\n", ch, ch);
485 token = T_QUOTED_STRING;
486 lf->state = lex_none;
491 case lex_include: /* scanning a filename */
496 if (B_ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' ||
497 ch == ';' || ch == ',' || ch == '"' || ch == '#') {
498 /* Keep the original LEX so we can print an error if the included file can't be opened. */
501 lf->state = lex_none;
502 lf = lex_open_file(lf, lf->str, NULL);
505 scan_err2(lfori, _("Cannot open included config file %s: %s\n"),
506 lfori->str, be.strerror());
514 Dmsg4(2000, "ch=%d state=%s token=%s %c\n", ch, lex_state_to_str(lf->state),
515 lex_tok_to_str(token), ch);
517 Dmsg2(2000, "lex returning: line %d token: %s\n", lf->line_no, lex_tok_to_str(token));
521 * Here is where we check to see if the user has set certain
522 * expectations (e.g. 32 bit integer). If so, we do type checking
523 * and possible additional scanning (e.g. for range).
527 lf->pint32_val = scan_pint(lf, lf->str);
528 lf->pint32_val2 = lf->pint32_val;
533 if (token == T_NUMBER) {
534 lf->pint32_val = scan_pint(lf, lf->str);
535 lf->pint32_val2 = lf->pint32_val;
538 char *p = strchr(lf->str, '-');
540 scan_err2(lf, _("expected an integer or a range, got %s: %s"),
541 lex_tok_to_str(token), lf->str);
545 *p++ = 0; /* terminate first half of range */
546 lf->pint32_val = scan_pint(lf, lf->str);
547 lf->pint32_val2 = scan_pint(lf, p);
548 token = T_PINT32_RANGE;
553 if (token != T_NUMBER || !is_a_number(lf->str)) {
554 scan_err2(lf, _("expected an integer number, got %s: %s"),
555 lex_tok_to_str(token), lf->str);
560 lf->int32_val = (int32_t)str_to_int64(lf->str);
562 scan_err2(lf, _("expected an integer number, got %s: %s"),
563 lex_tok_to_str(token), lf->str);
571 Dmsg2(2000, "int64=:%s: %f\n", lf->str, strtod(lf->str, NULL));
572 if (token != T_NUMBER || !is_a_number(lf->str)) {
573 scan_err2(lf, _("expected an integer number, got %s: %s"),
574 lex_tok_to_str(token), lf->str);
579 lf->int64_val = str_to_int64(lf->str);
581 scan_err2(lf, _("expected an integer number, got %s: %s"),
582 lex_tok_to_str(token), lf->str);
590 if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
591 scan_err2(lf, _("expected a name, got %s: %s"),
592 lex_tok_to_str(token), lf->str);
594 } else if (lf->str_len > MAX_RES_NAME_LENGTH) {
595 scan_err3(lf, _("name %s length %d too long, max is %d\n"), lf->str,
596 lf->str_len, MAX_RES_NAME_LENGTH);
602 if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
603 scan_err2(lf, _("expected a string, got %s: %s"),
604 lex_tok_to_str(token), lf->str);
613 break; /* no expectation given */
615 lf->token = token; /* set possible new token */