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);
66 * Format a scanner error message
68 void s_err(const char *file, int line, LEX *lc, const char *msg, ...)
74 va_start(arg_ptr, msg);
75 bvsnprintf(buf, sizeof(buf), msg, arg_ptr);
78 if (lc->line_no > lc->begin_line_no) {
79 bsnprintf(more, sizeof(more),
80 _("Problem probably begins at line %d.\n"), lc->begin_line_no);
84 if (lc->line_no > 0) {
85 e_msg(file, line, M_ERROR_TERM, 0, _("Config error: %s\n"
86 " : line %d, col %d of file %s\n%s\n%s"),
87 buf, lc->line_no, lc->col_no, lc->fname, lc->line, more);
89 e_msg(file, line, M_ERROR_TERM, 0, _("Config error: %s\n"), buf);
95 * Free the current file, and retrieve the contents
96 * of the previous packet if any.
98 LEX *lex_close_file(LEX *lf)
102 Dmsg1(2000, "Close lex file: %s\n", lf->fname);
104 Emsg0(M_ABORT, 0, "Close of NULL file\n");
108 Dmsg1(2000, "Close cfg file %s\n", lf->fname);
111 of->options = lf->options; /* preserve options */
112 memcpy(lf, of, sizeof(LEX));
113 Dmsg1(2000, "Restart scan of cfg file %s\n", of->fname);
123 * Open a new configuration file. We push the
124 * state of the current file (lf) so that we
125 * can do includes. This is a bit of a hammer.
126 * Instead of passing back the pointer to the
127 * new packet, I simply replace the contents
128 * of the caller's packet with the new packet,
129 * and link the contents of the old packet into
133 LEX *lex_open_file(LEX *lf, const char *filename, LEX_ERROR_HANDLER *scan_error)
138 char *fname = bstrdup(filename);
141 if ((fd = fopen(fname, "r")) == NULL) {
144 Dmsg1(2000, "Open config file: %s\n", fname);
145 nf = (LEX *)malloc(sizeof(LEX));
147 memcpy(nf, lf, sizeof(LEX));
148 memset(lf, 0, sizeof(LEX));
149 lf->next = nf; /* if have lf, push it behind new one */
150 lf->options = nf->options; /* preserve user options */
152 lf = nf; /* start new packet */
153 memset(lf, 0, sizeof(LEX));
157 lf->state = lex_none;
160 lf->scan_error = scan_error;
162 lf->scan_error = s_err;
164 Dmsg1(2000, "Return lex=%x\n", lf);
169 * Get the next character from the input.
170 * Returns the character or
171 * L_EOF if end of file
172 * L_EOL if end of line
174 int lex_get_char(LEX *lf)
176 if (lf->ch == L_EOF) {
177 Emsg0(M_ABORT, 0, "get_char: called after EOF\n");
179 if (lf->ch == L_EOL) {
180 if (bfgets(lf->line, MAXSTRING, lf->fd) == NULL) {
190 lf->ch = (uint8_t)lf->line[lf->col_no];
196 Dmsg2(2000, "lex_get_char: %c %d\n", lf->ch, lf->ch);
200 void lex_unget_char(LEX *lf)
209 * Add a character to the current string
211 static void add_str(LEX *lf, int ch)
213 if (lf->str_len >= MAXSTRING-3) {
214 Emsg3(M_ERROR_TERM, 0, _(
215 _("Config token too long, file: %s, line %d, begins at line %d\n")),
216 lf->fname, lf->line_no, lf->begin_line_no);
218 lf->str[lf->str_len++] = ch;
219 lf->str[lf->str_len] = 0;
225 static void begin_str(LEX *lf, int ch)
232 lf->begin_line_no = lf->line_no; /* save start string line no */
236 static const char *lex_state_to_str(int state)
239 case lex_none: return "none";
240 case lex_comment: return "comment";
241 case lex_number: return "number";
242 case lex_ip_addr: return "ip_addr";
243 case lex_identifier: return "identifier";
244 case lex_string: return "string";
245 case lex_quoted_string: return "quoted_string";
246 default: return "??????";
252 * Convert a lex token to a string
253 * used for debug/error printing.
255 const char *lex_tok_to_str(int token)
258 case L_EOF: return "L_EOF";
259 case L_EOL: return "L_EOL";
260 case T_NONE: return "T_NONE";
261 case T_NUMBER: return "T_NUMBER";
262 case T_IPADDR: return "T_IPADDR";
263 case T_IDENTIFIER: return "T_IDENTIFIER";
264 case T_UNQUOTED_STRING: return "T_UNQUOTED_STRING";
265 case T_QUOTED_STRING: return "T_QUOTED_STRING";
266 case T_BOB: return "T_BOB";
267 case T_EOB: return "T_EOB";
268 case T_EQUALS: return "T_EQUALS";
269 case T_ERROR: return "T_ERROR";
270 case T_EOF: return "T_EOF";
271 case T_COMMA: return "T_COMMA";
272 case T_EOL: return "T_EOL";
273 default: return "??????";
277 static uint32_t scan_pint(LEX *lf, char *str)
280 if (!is_a_number(str)) {
281 scan_err1(lf, _("expected a positive integer number, got: %s"), str);
285 val = str_to_int64(str);
286 if (errno != 0 || val < 0) {
287 scan_err1(lf, _("expected a postive integer number, got: %s"), str);
291 return (uint32_t)val;
296 * Get the next token from the input
300 lex_get_token(LEX *lf, int expect)
304 bool esc_next = false;
306 Dmsg0(2000, "enter lex_get_token\n");
307 while (token == T_NONE) {
308 ch = lex_get_char(lf);
311 Dmsg2(2000, "Lex state lex_none ch=%d,%x\n", ch, ch);
315 if (lf->options & LOPT_NO_IDENT || lf->options & LOPT_STRING) {
316 lf->state = lex_string;
318 lf->state = lex_identifier;
324 if (lf->options & LOPT_STRING) {
325 lf->state = lex_string;
327 lf->state = lex_number;
332 Dmsg0(2000, "Enter lex_none switch\n");
336 Dmsg0(2000, "got L_EOF set token=T_EOF\n");
339 lf->state = lex_comment;
350 lf->state = lex_quoted_string;
362 if (expect != T_SKIP_EOL) {
363 token = T_EOL; /* treat ; like EOL */
367 Dmsg0(2000, "got L_EOL set token=T_EOL\n");
368 if (expect != T_SKIP_EOL) {
373 lf->state = lex_include;
377 lf->state = lex_string;
383 Dmsg1(2000, "Lex state lex_comment ch=%x\n", ch);
385 lf->state = lex_none;
386 if (expect != T_SKIP_EOL) {
389 } else if (ch == L_EOF) {
394 Dmsg2(2000, "Lex state lex_number ch=%x %c\n", ch, ch);
399 /* Might want to allow trailing specifications here */
405 /* A valid number can be terminated by the following */
406 if (B_ISSPACE(ch) || ch == L_EOL || ch == ',' || ch == ';') {
408 lf->state = lex_none;
410 lf->state = lex_string;
419 Dmsg1(2000, "Lex state lex_ip_addr ch=%x\n", ch);
422 Dmsg1(2000, "Lex state lex_string ch=%x\n", ch);
427 if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
428 ch == '\r' || ch == ';' || ch == ',' || ch == '#' || (B_ISSPACE(ch)) ) {
430 token = T_UNQUOTED_STRING;
431 lf->state = lex_none;
437 Dmsg2(2000, "Lex state lex_identifier ch=%x %c\n", ch, ch);
441 } else if (B_ISSPACE(ch)) {
443 } else if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
444 ch == '\r' || ch == ';' || ch == ',' || ch == '"' || ch == '#') {
446 token = T_IDENTIFIER;
447 lf->state = lex_none;
449 } else if (ch == L_EOF) {
451 lf->state = lex_none;
455 /* Some non-alpha character => string */
456 lf->state = lex_string;
459 case lex_quoted_string:
460 Dmsg2(2000, "Lex state lex_quoted_string ch=%x %c\n", ch, ch);
479 token = T_QUOTED_STRING;
480 lf->state = lex_none;
485 case lex_include: /* scanning a filename */
490 if (B_ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' ||
491 ch == ';' || ch == ',' || ch == '"' || ch == '#') {
492 /* Keep the original LEX so we can print an error if the included file can't be opened. */
495 lf->state = lex_none;
496 lf = lex_open_file(lf, lf->str, NULL);
499 scan_err2(lfori, _("Cannot open included config file %s: %s\n"),
500 lfori->str, be.strerror());
508 Dmsg4(2000, "ch=%d state=%s token=%s %c\n", ch, lex_state_to_str(lf->state),
509 lex_tok_to_str(token), ch);
511 Dmsg2(2000, "lex returning: line %d token: %s\n", lf->line_no, lex_tok_to_str(token));
515 * Here is where we check to see if the user has set certain
516 * expectations (e.g. 32 bit integer). If so, we do type checking
517 * and possible additional scanning (e.g. for range).
521 lf->pint32_val = scan_pint(lf, lf->str);
522 lf->pint32_val2 = lf->pint32_val;
527 if (token == T_NUMBER) {
528 lf->pint32_val = scan_pint(lf, lf->str);
529 lf->pint32_val2 = lf->pint32_val;
532 char *p = strchr(lf->str, '-');
534 scan_err2(lf, _("expected an integer or a range, got %s: %s"),
535 lex_tok_to_str(token), lf->str);
539 *p++ = 0; /* terminate first half of range */
540 lf->pint32_val = scan_pint(lf, lf->str);
541 lf->pint32_val2 = scan_pint(lf, p);
542 token = T_PINT32_RANGE;
547 if (token != T_NUMBER || !is_a_number(lf->str)) {
548 scan_err2(lf, _("expected an integer number, got %s: %s"),
549 lex_tok_to_str(token), lf->str);
554 lf->int32_val = (int32_t)str_to_int64(lf->str);
556 scan_err2(lf, _("expected an integer number, got %s: %s"),
557 lex_tok_to_str(token), lf->str);
565 Dmsg2(2000, "int64=:%s: %f\n", lf->str, strtod(lf->str, NULL));
566 if (token != T_NUMBER || !is_a_number(lf->str)) {
567 scan_err2(lf, _("expected an integer number, got %s: %s"),
568 lex_tok_to_str(token), lf->str);
573 lf->int64_val = str_to_int64(lf->str);
575 scan_err2(lf, _("expected an integer number, got %s: %s"),
576 lex_tok_to_str(token), lf->str);
584 if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
585 scan_err2(lf, _("expected a name, got %s: %s"),
586 lex_tok_to_str(token), lf->str);
588 } else if (lf->str_len > MAX_RES_NAME_LENGTH) {
589 scan_err3(lf, _("name %s length %d too long, max is %d\n"), lf->str,
590 lf->str_len, MAX_RES_NAME_LENGTH);
596 if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
597 scan_err2(lf, _("expected a string, got %s: %s"),
598 lex_tok_to_str(token), lf->str);
607 break; /* no expectation given */
609 lf->token = token; /* set possible new token */