2 * Lexical scanner for Bacula configuration file
9 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation; either version 2 of
14 the License, or (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public
22 License along with this program; if not, write to the Free
23 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31 extern int debug_level;
34 * Scan to "logical" end of line. I.e. end of line,
37 void scan_to_eol(LEX *lc)
39 Dmsg0(900, "start scan to eof\n");
40 while (lex_get_token(lc, T_ALL) != T_EOL)
42 Dmsg0(900, "done scan to eof\n");
47 * Format a scanner error message
49 static void s_err(char *file, int line, LEX *lc, char *msg, ...)
55 va_start(arg_ptr, msg);
56 bvsnprintf(buf, sizeof(buf), msg, arg_ptr);
59 if (lc->line_no > lc->begin_line_no) {
60 bsnprintf(more, sizeof(more),
61 _("Problem probably begins at line %d.\n"), lc->begin_line_no);
65 e_msg(file, line, M_ERROR_TERM, 0, _("Config error: %s\n\
66 : line %d, col %d of file %s\n%s\n%s"),
67 buf, lc->line_no, lc->col_no, lc->fname, lc->line, more);
72 * Free the current file, and retrieve the contents
73 * of the previous packet if any.
75 LEX *lex_close_file(LEX *lf)
79 Dmsg1(900, "Close lex file: %s\n", lf->fname);
81 Emsg0(M_ABORT, 0, "Close of NULL file\n");
85 Dmsg1(900, "Close cfg file %s\n", lf->fname);
88 of->options = lf->options; /* preserve options */
89 memcpy(lf, of, sizeof(LEX));
90 Dmsg1(900, "Restart scan of cfg file %s\n", of->fname);
100 * Open a new configuration file. We push the
101 * state of the current file (lf) so that we
102 * can do includes. This is a bit of a hammer.
103 * Instead of passing back the pointer to the
104 * new packet, I simply replace the contents
105 * of the caller's packet with the new packet,
106 * and link the contents of the old packet into
110 LEX *lex_open_file(LEX *lf, char *filename, LEX_ERROR_HANDLER *scan_error)
115 char *fname = bstrdup(filename);
118 if ((fd = fopen(fname, "r")) == NULL) {
119 Emsg2(M_ERROR_TERM, 0, _("Cannot open config file %s: %s\n"),
120 fname, strerror(errno));
122 Dmsg1(900, "Open config file: %s\n", fname);
123 nf = (LEX *)malloc(sizeof(LEX));
125 memcpy(nf, lf, sizeof(LEX));
126 memset(lf, 0, sizeof(LEX));
127 lf->next = nf; /* if have lf, push it behind new one */
128 lf->options = nf->options; /* preserve user options */
130 lf = nf; /* start new packet */
131 memset(lf, 0, sizeof(LEX));
135 lf->state = lex_none;
138 lf->scan_error = scan_error;
140 lf->scan_error = s_err;
142 Dmsg1(900, "Return lex=%x\n", lf);
147 * Get the next character from the input.
148 * Returns the character or
149 * L_EOF if end of file
150 * L_EOL if end of line
152 int lex_get_char(LEX *lf)
154 if (lf->ch == L_EOF) {
155 Emsg0(M_ABORT, 0, "get_char: called after EOF\n");
157 if (lf->ch == L_EOL) {
158 if (fgets(lf->line, MAXSTRING, lf->fd) == NULL) {
168 lf->ch = lf->line[lf->col_no];
174 Dmsg2(900, "lex_get_char: %c %d\n", lf->ch, lf->ch);
178 void lex_unget_char(LEX *lf)
187 * Add a character to the current string
189 static void add_str(LEX *lf, int ch)
191 if (lf->str_len >= MAXSTRING-3) {
192 Emsg3(M_ERROR_TERM, 0, _(
193 _("Config token too long, file: %s, line %d, begins at line %d\n")),
194 lf->fname, lf->line_no, lf->begin_line_no);
196 lf->str[lf->str_len++] = ch;
197 lf->str[lf->str_len] = 0;
203 static void begin_str(LEX *lf, int ch)
210 lf->begin_line_no = lf->line_no; /* save start string line no */
214 static char *lex_state_to_str(int state)
217 case lex_none: return "none";
218 case lex_comment: return "comment";
219 case lex_number: return "number";
220 case lex_ip_addr: return "ip_addr";
221 case lex_identifier: return "identifier";
222 case lex_string: return "string";
223 case lex_quoted_string: return "quoted_string";
224 default: return "??????";
230 * Convert a lex token to a string
231 * used for debug/error printing.
233 char *lex_tok_to_str(int token)
236 case L_EOF: return "L_EOF";
237 case L_EOL: return "L_EOL";
238 case T_NONE: return "T_NONE";
239 case T_NUMBER: return "T_NUMBER";
240 case T_IPADDR: return "T_IPADDR";
241 case T_IDENTIFIER: return "T_IDENTIFIER";
242 case T_UNQUOTED_STRING: return "T_UNQUOTED_STRING";
243 case T_QUOTED_STRING: return "T_QUOTED_STRING";
244 case T_BOB: return "T_BOB";
245 case T_EOB: return "T_EOB";
246 case T_EQUALS: return "T_EQUALS";
247 case T_ERROR: return "T_ERROR";
248 case T_EOF: return "T_EOF";
249 case T_COMMA: return "T_COMMA";
250 case T_EOL: return "T_EOL";
251 default: return "??????";
255 static uint32_t scan_pint(LEX *lf, char *str)
258 if (!is_a_number(str)) {
259 scan_err1(lf, "expected a positive integer number, got: %s", str);
263 val = str_to_int64(str);
264 if (errno != 0 || val < 0) {
265 scan_err1(lf, "expected a postive integer number, got: %s", str);
269 return (uint32_t)val;
274 * Get the next token from the input
278 lex_get_token(LEX *lf, int expect)
282 int esc_next = FALSE;
284 Dmsg0(900, "enter lex_get_token\n");
285 while (token == T_NONE) {
286 ch = lex_get_char(lf);
289 Dmsg2(900, "Lex state lex_none ch=%d,%x\n", ch, ch);
293 if (lf->options & LOPT_NO_IDENT)
294 lf->state = lex_string;
296 lf->state = lex_identifier;
301 lf->state = lex_number;
305 Dmsg0(900, "Enter lex_none switch\n");
309 Dmsg0(900, "got L_EOF set token=T_EOF\n");
312 lf->state = lex_comment;
323 lf->state = lex_quoted_string;
335 token = T_EOL; /* treat ; like EOL */
338 Dmsg0(900, "got L_EOL set token=T_EOL\n");
342 lf->state = lex_include;
346 lf->state = lex_string;
352 Dmsg1(900, "Lex state lex_comment ch=%x\n", ch);
354 lf->state = lex_none;
356 } else if (ch == L_EOF) {
361 Dmsg2(900, "Lex state lex_number ch=%x %c\n", ch, ch);
366 /* Might want to allow trailing specifications here */
372 /* A valid number can be terminated by the following */
373 if (B_ISSPACE(ch) || ch == L_EOL || ch == ',' || ch == ';') {
375 lf->state = lex_none;
377 lf->state = lex_string;
386 Dmsg1(900, "Lex state lex_ip_addr ch=%x\n", ch);
389 Dmsg1(900, "Lex state lex_string ch=%x\n", ch);
394 if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
395 ch == '\r' || ch == ';' || ch == ',' || ch == '#' || (B_ISSPACE(ch)) ) {
397 token = T_UNQUOTED_STRING;
398 lf->state = lex_none;
404 Dmsg2(900, "Lex state lex_identifier ch=%x %c\n", ch, ch);
408 } else if (B_ISSPACE(ch)) {
410 } else if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
411 ch == '\r' || ch == ';' || ch == ',' || ch == '"' || ch == '#') {
413 token = T_IDENTIFIER;
414 lf->state = lex_none;
416 } else if (ch == L_EOF) {
418 lf->state = lex_none;
422 /* Some non-alpha character => string */
423 lf->state = lex_string;
426 case lex_quoted_string:
427 Dmsg2(900, "Lex state lex_quoted_string ch=%x %c\n", ch, ch);
446 token = T_QUOTED_STRING;
447 lf->state = lex_none;
452 case lex_include: /* scanning a filename */
457 if (B_ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' ||
458 ch == ';' || ch == ',' || ch == '"' || ch == '#') {
459 lf->state = lex_none;
460 lf = lex_open_file(lf, lf->str, NULL);
466 Dmsg4(900, "ch=%d state=%s token=%s %c\n", ch, lex_state_to_str(lf->state),
467 lex_tok_to_str(token), ch);
469 Dmsg2(900, "lex returning: line %d token: %s\n", lf->line_no, lex_tok_to_str(token));
473 * Here is where we check to see if the user has set certain
474 * expectations (e.g. 32 bit integer). If so, we do type checking
475 * and possible additional scanning (e.g. for range).
479 lf->pint32_val = scan_pint(lf, lf->str);
480 lf->pint32_val2 = lf->pint32_val;
485 if (token == T_NUMBER) {
486 lf->pint32_val = scan_pint(lf, lf->str);
487 lf->pint32_val2 = lf->pint32_val;
490 char *p = strchr(lf->str, '-');
492 scan_err2(lf, "expected an integer or a range, got %s: %s",
493 lex_tok_to_str(token), lf->str);
497 *p++ = 0; /* terminate first half of range */
498 lf->pint32_val = scan_pint(lf, lf->str);
499 lf->pint32_val2 = scan_pint(lf, p);
500 token = T_PINT32_RANGE;
505 if (token != T_NUMBER || !is_a_number(lf->str)) {
506 scan_err2(lf, "expected an integer number, got %s: %s",
507 lex_tok_to_str(token), lf->str);
512 lf->int32_val = (int32_t)str_to_int64(lf->str);
514 scan_err2(lf, "expected an integer number, got %s: %s",
515 lex_tok_to_str(token), lf->str);
523 Dmsg2(900, "int64=:%s: %f\n", lf->str, strtod(lf->str, NULL));
524 if (token != T_NUMBER || !is_a_number(lf->str)) {
525 scan_err2(lf, "expected an integer number, got %s: %s",
526 lex_tok_to_str(token), lf->str);
531 lf->int64_val = str_to_int64(lf->str);
533 scan_err2(lf, "expected an integer number, got %s: %s",
534 lex_tok_to_str(token), lf->str);
542 if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
543 scan_err2(lf, "expected a name, got %s: %s",
544 lex_tok_to_str(token), lf->str);
546 } else if (lf->str_len > MAX_RES_NAME_LENGTH) {
547 scan_err3(lf, "name %s length %d too long, max is %d\n", lf->str,
548 lf->str_len, MAX_RES_NAME_LENGTH);
554 if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
555 scan_err2(lf, "expected a string, got %s: %s",
556 lex_tok_to_str(token), lf->str);
565 break; /* no expectation given */
567 lf->token = token; /* set possible new token */