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)
40 Dmsg0(150, "start scan to eof\n");
42 while ((token = lex_get_token(lc, T_ALL)) != T_EOL) {
45 Dmsg0(150, "done scan to eof\n");
50 * Format a scanner error message
52 void s_err(char *file, int line, LEX *lc, char *msg, ...)
57 va_start(arg_ptr, msg);
58 bvsnprintf(buf, sizeof(buf), msg, arg_ptr);
61 e_msg(file, line, M_ERROR_TERM, 0, "Config error: %s\n\
62 : Line %d, col %d of file %s\n%s\n",
63 buf, lc->line_no, lc->col_no, lc->fname, lc->line);
68 * Free the current file, and retrieve the contents
69 * of the previous packet if any.
72 lex_close_file(LEX *lf)
76 Dmsg1(20, "Close lex file: %s\n", lf->fname);
78 Emsg0(M_ABORT, 0, "Close of NULL file\n");
82 Dmsg1(29, "Close cfg file %s\n", lf->fname);
85 of->options = lf->options; /* preserve options */
86 memcpy(lf, of, sizeof(LEX));
87 Dmsg1(29, "Restart scan of cfg file %s\n", of->fname);
97 * Open a new configuration file. We push the
98 * state of the current file (lf) so that we
99 * can do includes. This is a bit of a hammer.
100 * Instead of passing back the pointer to the
101 * new packet, I simply replace the contents
102 * of the caller's packet with the new packet,
103 * and link the contents of the old packet into
108 lex_open_file(LEX *lf, char *filename)
112 char *fname = bstrdup(filename);
115 if ((fd = fopen(fname, "r")) == NULL) {
116 Emsg2(M_ABORT, 0, "Cannot open config file %s: %s\n", fname, strerror(errno));
118 Dmsg1(29, "Open config file: %s\n", fname);
119 nf = (LEX *)malloc(sizeof(LEX));
121 memcpy(nf, lf, sizeof(LEX));
122 memset(lf, 0, sizeof(LEX));
123 lf->next = nf; /* if have lf, push it behind new one */
124 lf->options = nf->options; /* preserve user options */
126 lf = nf; /* start new packet */
127 memset(lf, 0, sizeof(LEX));
131 lf->state = lex_none;
133 Dmsg1(29, "Return lex=%x\n", lf);
138 * Get the next character from the input.
139 * Returns the character or
140 * L_EOF if end of file
141 * L_EOL if end of line
144 lex_get_char(LEX *lf)
147 Emsg0(M_ABORT, 0, "get_char: called after EOF\n");
148 if (lf->ch == L_EOL) {
149 if (fgets(lf->line, MAXSTRING, lf->fd) == NULL) {
159 lf->ch = lf->line[lf->col_no];
165 Dmsg2(900, "lex_get_char: %c %d\n", lf->ch, lf->ch);
170 lex_unget_char(LEX *lf)
179 * Add a character to the current string
181 static void add_str(LEX *lf, int ch)
183 if (lf->str_len >= MAXSTRING-3) {
184 Emsg2(M_ABORT, 0, "Token too long, file: %s, line %s\n", lf->fname, lf->line_no);
186 lf->str[lf->str_len++] = ch;
187 lf->str[lf->str_len] = 0;
193 static void begin_str(LEX *lf, int ch)
203 lex_state_to_str(int state)
206 case lex_none: return "none";
207 case lex_comment: return "comment";
208 case lex_number: return "number";
209 case lex_ip_addr: return "ip_addr";
210 case lex_identifier: return "identifier";
211 case lex_string: return "string";
212 case lex_quoted_string: return "quoted_string";
213 default: return "??????";
219 * Convert a lex token to a string
220 * used for debug/error printing.
223 lex_tok_to_str(int token)
226 case L_EOF: return "L_EOF";
227 case L_EOL: return "L_EOL";
228 case T_NONE: return "T_NONE";
229 case T_NUMBER: return "T_NUMBER";
230 case T_IPADDR: return "T_IPADDR";
231 case T_IDENTIFIER: return "T_IDENTIFIER";
232 case T_UNQUOTED_STRING: return "T_UNQUOTED_STRING";
233 case T_QUOTED_STRING: return "T_QUOTED_STRING";
234 case T_BOB: return "T_BOB";
235 case T_EOB: return "T_EOB";
236 case T_EQUALS: return "T_EQUALS";
237 case T_ERROR: return "T_ERROR";
238 case T_EOF: return "T_EOF";
239 case T_COMMA: return "T_COMMA";
240 case T_EOL: return "T_EOL";
241 default: return "??????";
245 static uint32_t scan_pint(LEX *lf, char *str)
248 if (!is_a_number(str)) {
249 scan_err1(lf, "expected a positive integer number, got: %s", str);
252 dval = strtod(str, NULL);
253 if (errno != 0 || dval < 0) {
254 scan_err1(lf, "expected a postive integer number, got: %s", str);
257 return (uint32_t)dval;
262 * Get the next token from the input
266 lex_get_token(LEX *lf, int expect)
270 int esc_next = FALSE;
272 Dmsg0(290, "enter lex_get_token\n");
273 while (token == T_NONE) {
274 ch = lex_get_char(lf);
277 Dmsg2(290, "Lex state lex_none ch=%d,%x\n", ch, ch);
281 if (lf->options & LOPT_NO_IDENT)
282 lf->state = lex_string;
284 lf->state = lex_identifier;
289 lf->state = lex_number;
293 Dmsg0(290, "Enter lex_none switch\n");
297 Dmsg0(290, "got L_EOF set token=T_EOF\n");
300 lf->state = lex_comment;
311 lf->state = lex_quoted_string;
323 token = T_EOL; /* treat ; like EOL */
326 Dmsg0(290, "got L_EOL set token=T_EOL\n");
330 lf->state = lex_include;
334 lf->state = lex_string;
340 Dmsg1(290, "Lex state lex_comment ch=%x\n", ch);
342 lf->state = lex_none;
347 Dmsg2(290, "Lex state lex_number ch=%x %c\n", ch, ch);
348 /* Might want to allow trailing specifications here */
354 /* A valid number can be terminated by the following */
355 if (ISSPACE(ch) || ch == L_EOL || ch == ',' || ch == ';') {
357 lf->state = lex_none;
359 lf->state = lex_string;
364 Dmsg1(290, "Lex state lex_ip_addr ch=%x\n", ch);
367 Dmsg1(290, "Lex state lex_string ch=%x\n", ch);
368 if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
369 ch == ';' || ch == ',' || ch == '#' || (ISSPACE(ch)) ) {
371 token = T_UNQUOTED_STRING;
372 lf->state = lex_none;
378 Dmsg2(290, "Lex state lex_identifier ch=%x %c\n", ch, ch);
382 } else if (ISSPACE(ch)) {
384 } else if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
385 ch == ';' || ch == ',' || ch == '"' || ch == '#') {
387 token = T_IDENTIFIER;
388 lf->state = lex_none;
390 } else if (ch == L_EOF) {
392 lf->state = lex_none;
396 /* Some non-alpha character => string */
397 lf->state = lex_string;
400 case lex_quoted_string:
401 Dmsg2(290, "Lex state lex_quoted_string ch=%x %c\n", ch, ch);
416 token = T_QUOTED_STRING;
417 lf->state = lex_none;
422 case lex_include: /* scanning a filename */
423 if (ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' ||
424 ch == ';' || ch == ',' || ch == '"' || ch == '#') {
425 lf->state = lex_none;
426 lf = lex_open_file(lf, lf->str);
432 Dmsg4(290, "ch=%d state=%s token=%s %c\n", ch, lex_state_to_str(lf->state),
433 lex_tok_to_str(token), ch);
435 Dmsg2(290, "lex returning: line %d token: %s\n", lf->line_no, lex_tok_to_str(token));
439 * Here is where we check to see if the user has set certain
440 * expectations (e.g. 32 bit integer). If so, we do type checking
441 * and possible additional scanning (e.g. for range).
445 lf->pint32_val = scan_pint(lf, lf->str);
446 lf->pint32_val2 = lf->pint32_val;
451 if (token == T_NUMBER) {
452 lf->pint32_val = scan_pint(lf, lf->str);
453 lf->pint32_val2 = lf->pint32_val;
456 char *p = strchr(lf->str, '-');
458 scan_err1(lf, "expected an integer or a range, got: %s", lf->str);
460 *p++ = 0; /* terminate first half of range */
461 lf->pint32_val = scan_pint(lf, lf->str);
462 lf->pint32_val2 = scan_pint(lf, p);
463 token = T_PINT32_RANGE;
468 if (token != T_NUMBER || !is_a_number(lf->str)) {
469 scan_err1(lf, "expected an integer number, got: %s", lf->str);
472 lf->int32_val = (int32_t)strtod(lf->str, NULL);
474 scan_err1(lf, "expected an integer number, got: %s", lf->str);
481 Dmsg2(400, "int64=:%s: %f\n", lf->str, strtod(lf->str, NULL));
482 if (token != T_NUMBER || !is_a_number(lf->str)) {
483 scan_err1(lf, "expected an integer number, got: %s", lf->str);
486 lf->int64_val = (int64_t)strtod(lf->str, NULL);
488 scan_err1(lf, "expected an integer number, got: %s", lf->str);
495 if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
496 scan_err1(lf, "expected a name: %s", lf->str);
497 } else if (lf->str_len > MAX_RES_NAME_LENGTH) {
498 scan_err3(lf, "name %s length %d too long, max is %d\n", lf->str,
499 lf->str_len, MAX_RES_NAME_LENGTH);
505 if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
506 scan_err1(lf, "expected a name: %s", lf->str);
513 break; /* no expectation given */
515 lf->token = token; /* set possible new token */