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(150, "start scan to eof\n");
40 while (lex_get_token(lc, T_ALL) != T_EOL)
42 Dmsg0(150, "done scan to eof\n");
47 * Format a scanner error message
49 static void s_err(char *file, int line, LEX *lc, char *msg, ...)
54 va_start(arg_ptr, msg);
55 bvsnprintf(buf, sizeof(buf), msg, arg_ptr);
58 e_msg(file, line, M_ERROR_TERM, 0, "Config error: %s\n\
59 : Line %d, col %d of file %s\n%s\n",
60 buf, lc->line_no, lc->col_no, lc->fname, lc->line);
65 * Free the current file, and retrieve the contents
66 * of the previous packet if any.
69 lex_close_file(LEX *lf)
73 Dmsg1(20, "Close lex file: %s\n", lf->fname);
75 Emsg0(M_ABORT, 0, "Close of NULL file\n");
79 Dmsg1(29, "Close cfg file %s\n", lf->fname);
82 of->options = lf->options; /* preserve options */
83 memcpy(lf, of, sizeof(LEX));
84 Dmsg1(29, "Restart scan of cfg file %s\n", of->fname);
94 * Open a new configuration file. We push the
95 * state of the current file (lf) so that we
96 * can do includes. This is a bit of a hammer.
97 * Instead of passing back the pointer to the
98 * new packet, I simply replace the contents
99 * of the caller's packet with the new packet,
100 * and link the contents of the old packet into
105 lex_open_file(LEX *lf, char *filename, LEX_ERROR_HANDLER *scan_error)
110 char *fname = bstrdup(filename);
113 if ((fd = fopen(fname, "r")) == NULL) {
114 Emsg2(M_ABORT, 0, "Cannot open config file %s: %s\n", fname, strerror(errno));
116 Dmsg1(29, "Open config file: %s\n", fname);
117 nf = (LEX *)malloc(sizeof(LEX));
119 memcpy(nf, lf, sizeof(LEX));
120 memset(lf, 0, sizeof(LEX));
121 lf->next = nf; /* if have lf, push it behind new one */
122 lf->options = nf->options; /* preserve user options */
124 lf = nf; /* start new packet */
125 memset(lf, 0, sizeof(LEX));
129 lf->state = lex_none;
132 lf->scan_error = scan_error;
134 lf->scan_error = s_err;
136 Dmsg1(29, "Return lex=%x\n", lf);
141 * Get the next character from the input.
142 * Returns the character or
143 * L_EOF if end of file
144 * L_EOL if end of line
147 lex_get_char(LEX *lf)
150 Emsg0(M_ABORT, 0, "get_char: called after EOF\n");
151 if (lf->ch == L_EOL) {
152 if (fgets(lf->line, MAXSTRING, lf->fd) == NULL) {
162 lf->ch = lf->line[lf->col_no];
168 Dmsg2(900, "lex_get_char: %c %d\n", lf->ch, lf->ch);
173 lex_unget_char(LEX *lf)
182 * Add a character to the current string
184 static void add_str(LEX *lf, int ch)
186 if (lf->str_len >= MAXSTRING-3) {
187 Emsg2(M_ABORT, 0, "Token too long, file: %s, line %s\n", lf->fname, lf->line_no);
189 lf->str[lf->str_len++] = ch;
190 lf->str[lf->str_len] = 0;
196 static void begin_str(LEX *lf, int ch)
206 lex_state_to_str(int state)
209 case lex_none: return "none";
210 case lex_comment: return "comment";
211 case lex_number: return "number";
212 case lex_ip_addr: return "ip_addr";
213 case lex_identifier: return "identifier";
214 case lex_string: return "string";
215 case lex_quoted_string: return "quoted_string";
216 default: return "??????";
222 * Convert a lex token to a string
223 * used for debug/error printing.
226 lex_tok_to_str(int token)
229 case L_EOF: return "L_EOF";
230 case L_EOL: return "L_EOL";
231 case T_NONE: return "T_NONE";
232 case T_NUMBER: return "T_NUMBER";
233 case T_IPADDR: return "T_IPADDR";
234 case T_IDENTIFIER: return "T_IDENTIFIER";
235 case T_UNQUOTED_STRING: return "T_UNQUOTED_STRING";
236 case T_QUOTED_STRING: return "T_QUOTED_STRING";
237 case T_BOB: return "T_BOB";
238 case T_EOB: return "T_EOB";
239 case T_EQUALS: return "T_EQUALS";
240 case T_ERROR: return "T_ERROR";
241 case T_EOF: return "T_EOF";
242 case T_COMMA: return "T_COMMA";
243 case T_EOL: return "T_EOL";
244 default: return "??????";
248 static uint32_t scan_pint(LEX *lf, char *str)
251 if (!is_a_number(str)) {
252 scan_err1(lf, "expected a positive integer number, got: %s", str);
255 dval = strtod(str, NULL);
256 if (errno != 0 || dval < 0) {
257 scan_err1(lf, "expected a postive integer number, got: %s", str);
260 return (uint32_t)dval;
265 * Get the next token from the input
269 lex_get_token(LEX *lf, int expect)
273 int esc_next = FALSE;
275 Dmsg0(290, "enter lex_get_token\n");
276 while (token == T_NONE) {
277 ch = lex_get_char(lf);
280 Dmsg2(290, "Lex state lex_none ch=%d,%x\n", ch, ch);
284 if (lf->options & LOPT_NO_IDENT)
285 lf->state = lex_string;
287 lf->state = lex_identifier;
292 lf->state = lex_number;
296 Dmsg0(290, "Enter lex_none switch\n");
300 Dmsg0(290, "got L_EOF set token=T_EOF\n");
303 lf->state = lex_comment;
314 lf->state = lex_quoted_string;
326 token = T_EOL; /* treat ; like EOL */
329 Dmsg0(290, "got L_EOL set token=T_EOL\n");
333 lf->state = lex_include;
337 lf->state = lex_string;
343 Dmsg1(290, "Lex state lex_comment ch=%x\n", ch);
345 lf->state = lex_none;
350 Dmsg2(290, "Lex state lex_number ch=%x %c\n", ch, ch);
351 /* Might want to allow trailing specifications here */
357 /* A valid number can be terminated by the following */
358 if (ISSPACE(ch) || ch == L_EOL || ch == ',' || ch == ';') {
360 lf->state = lex_none;
362 lf->state = lex_string;
367 Dmsg1(290, "Lex state lex_ip_addr ch=%x\n", ch);
370 Dmsg1(290, "Lex state lex_string ch=%x\n", ch);
371 if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
372 ch == ';' || ch == ',' || ch == '#' || (ISSPACE(ch)) ) {
374 token = T_UNQUOTED_STRING;
375 lf->state = lex_none;
381 Dmsg2(290, "Lex state lex_identifier ch=%x %c\n", ch, ch);
385 } else if (ISSPACE(ch)) {
387 } else if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
388 ch == ';' || ch == ',' || ch == '"' || ch == '#') {
390 token = T_IDENTIFIER;
391 lf->state = lex_none;
393 } else if (ch == L_EOF) {
395 lf->state = lex_none;
399 /* Some non-alpha character => string */
400 lf->state = lex_string;
403 case lex_quoted_string:
404 Dmsg2(290, "Lex state lex_quoted_string ch=%x %c\n", ch, ch);
419 token = T_QUOTED_STRING;
420 lf->state = lex_none;
425 case lex_include: /* scanning a filename */
426 if (ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' ||
427 ch == ';' || ch == ',' || ch == '"' || ch == '#') {
428 lf->state = lex_none;
429 lf = lex_open_file(lf, lf->str, NULL);
435 Dmsg4(290, "ch=%d state=%s token=%s %c\n", ch, lex_state_to_str(lf->state),
436 lex_tok_to_str(token), ch);
438 Dmsg2(290, "lex returning: line %d token: %s\n", lf->line_no, lex_tok_to_str(token));
442 * Here is where we check to see if the user has set certain
443 * expectations (e.g. 32 bit integer). If so, we do type checking
444 * and possible additional scanning (e.g. for range).
448 lf->pint32_val = scan_pint(lf, lf->str);
449 lf->pint32_val2 = lf->pint32_val;
454 if (token == T_NUMBER) {
455 lf->pint32_val = scan_pint(lf, lf->str);
456 lf->pint32_val2 = lf->pint32_val;
459 char *p = strchr(lf->str, '-');
461 scan_err2(lf, "expected an integer or a range, got %s: %s",
462 lex_tok_to_str(token), lf->str);
466 *p++ = 0; /* terminate first half of range */
467 lf->pint32_val = scan_pint(lf, lf->str);
468 lf->pint32_val2 = scan_pint(lf, p);
469 token = T_PINT32_RANGE;
474 if (token != T_NUMBER || !is_a_number(lf->str)) {
475 scan_err2(lf, "expected an integer number, got %s: %s",
476 lex_tok_to_str(token), lf->str);
481 lf->int32_val = (int32_t)strtod(lf->str, NULL);
483 scan_err2(lf, "expected an integer number, got %s: %s",
484 lex_tok_to_str(token), lf->str);
492 Dmsg2(400, "int64=:%s: %f\n", lf->str, strtod(lf->str, NULL));
493 if (token != T_NUMBER || !is_a_number(lf->str)) {
494 scan_err2(lf, "expected an integer number, got %s: %s",
495 lex_tok_to_str(token), lf->str);
500 lf->int64_val = (int64_t)strtod(lf->str, NULL);
502 scan_err2(lf, "expected an integer number, got %s: %s",
503 lex_tok_to_str(token), lf->str);
511 if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
512 scan_err2(lf, "expected a name, got %s: %s",
513 lex_tok_to_str(token), lf->str);
515 } else if (lf->str_len > MAX_RES_NAME_LENGTH) {
516 scan_err3(lf, "name %s length %d too long, max is %d\n", lf->str,
517 lf->str_len, MAX_RES_NAME_LENGTH);
525 if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
526 scan_err2(lf, "expected a name, got %s: %s",
527 lex_tok_to_str(token), lf->str);
536 break; /* no expectation given */
538 lf->token = token; /* set possible new token */