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(40, "Close lex file: %s\n", lf->fname);
75 Emsg0(M_ABORT, 0, "Close of NULL file\n");
79 Dmsg1(49, "Close cfg file %s\n", lf->fname);
82 of->options = lf->options; /* preserve options */
83 memcpy(lf, of, sizeof(LEX));
84 Dmsg1(49, "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(49, "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(49, "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);
256 dval = strtod(str, NULL);
257 if (errno != 0 || dval < 0) {
258 scan_err1(lf, "expected a postive integer number, got: %s", str);
262 return (uint32_t)dval;
267 * Get the next token from the input
271 lex_get_token(LEX *lf, int expect)
275 int esc_next = FALSE;
277 Dmsg0(290, "enter lex_get_token\n");
278 while (token == T_NONE) {
279 ch = lex_get_char(lf);
282 Dmsg2(290, "Lex state lex_none ch=%d,%x\n", ch, ch);
286 if (lf->options & LOPT_NO_IDENT)
287 lf->state = lex_string;
289 lf->state = lex_identifier;
294 lf->state = lex_number;
298 Dmsg0(290, "Enter lex_none switch\n");
302 Dmsg0(290, "got L_EOF set token=T_EOF\n");
305 lf->state = lex_comment;
316 lf->state = lex_quoted_string;
328 token = T_EOL; /* treat ; like EOL */
331 Dmsg0(290, "got L_EOL set token=T_EOL\n");
335 lf->state = lex_include;
339 lf->state = lex_string;
345 Dmsg1(290, "Lex state lex_comment ch=%x\n", ch);
347 lf->state = lex_none;
352 Dmsg2(290, "Lex state lex_number ch=%x %c\n", ch, ch);
353 /* Might want to allow trailing specifications here */
359 /* A valid number can be terminated by the following */
360 if (ISSPACE(ch) || ch == L_EOL || ch == ',' || ch == ';') {
362 lf->state = lex_none;
364 lf->state = lex_string;
369 Dmsg1(290, "Lex state lex_ip_addr ch=%x\n", ch);
372 Dmsg1(290, "Lex state lex_string ch=%x\n", ch);
373 if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
374 ch == ';' || ch == ',' || ch == '#' || (ISSPACE(ch)) ) {
376 token = T_UNQUOTED_STRING;
377 lf->state = lex_none;
383 Dmsg2(290, "Lex state lex_identifier ch=%x %c\n", ch, ch);
387 } else if (ISSPACE(ch)) {
389 } else if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
390 ch == ';' || ch == ',' || ch == '"' || ch == '#') {
392 token = T_IDENTIFIER;
393 lf->state = lex_none;
395 } else if (ch == L_EOF) {
397 lf->state = lex_none;
401 /* Some non-alpha character => string */
402 lf->state = lex_string;
405 case lex_quoted_string:
406 Dmsg2(290, "Lex state lex_quoted_string ch=%x %c\n", ch, ch);
421 token = T_QUOTED_STRING;
422 lf->state = lex_none;
427 case lex_include: /* scanning a filename */
428 if (ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' ||
429 ch == ';' || ch == ',' || ch == '"' || ch == '#') {
430 lf->state = lex_none;
431 lf = lex_open_file(lf, lf->str, NULL);
437 Dmsg4(290, "ch=%d state=%s token=%s %c\n", ch, lex_state_to_str(lf->state),
438 lex_tok_to_str(token), ch);
440 Dmsg2(290, "lex returning: line %d token: %s\n", lf->line_no, lex_tok_to_str(token));
444 * Here is where we check to see if the user has set certain
445 * expectations (e.g. 32 bit integer). If so, we do type checking
446 * and possible additional scanning (e.g. for range).
450 lf->pint32_val = scan_pint(lf, lf->str);
451 lf->pint32_val2 = lf->pint32_val;
456 if (token == T_NUMBER) {
457 lf->pint32_val = scan_pint(lf, lf->str);
458 lf->pint32_val2 = lf->pint32_val;
461 char *p = strchr(lf->str, '-');
463 scan_err2(lf, "expected an integer or a range, got %s: %s",
464 lex_tok_to_str(token), lf->str);
468 *p++ = 0; /* terminate first half of range */
469 lf->pint32_val = scan_pint(lf, lf->str);
470 lf->pint32_val2 = scan_pint(lf, p);
471 token = T_PINT32_RANGE;
476 if (token != T_NUMBER || !is_a_number(lf->str)) {
477 scan_err2(lf, "expected an integer number, got %s: %s",
478 lex_tok_to_str(token), lf->str);
483 lf->int32_val = (int32_t)strtod(lf->str, NULL);
485 scan_err2(lf, "expected an integer number, got %s: %s",
486 lex_tok_to_str(token), lf->str);
494 Dmsg2(400, "int64=:%s: %f\n", lf->str, strtod(lf->str, NULL));
495 if (token != T_NUMBER || !is_a_number(lf->str)) {
496 scan_err2(lf, "expected an integer number, got %s: %s",
497 lex_tok_to_str(token), lf->str);
502 lf->int64_val = (int64_t)strtod(lf->str, NULL);
504 scan_err2(lf, "expected an integer number, got %s: %s",
505 lex_tok_to_str(token), lf->str);
513 if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
514 scan_err2(lf, "expected a name, got %s: %s",
515 lex_tok_to_str(token), lf->str);
517 } else if (lf->str_len > MAX_RES_NAME_LENGTH) {
518 scan_err3(lf, "name %s length %d too long, max is %d\n", lf->str,
519 lf->str_len, MAX_RES_NAME_LENGTH);
527 if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
528 scan_err2(lf, "expected a name, got %s: %s",
529 lex_tok_to_str(token), lf->str);
538 break; /* no expectation given */
540 lf->token = token; /* set possible new token */