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");
41 while ((token = lex_get_token(lc, T_ALL)) != T_EOL)
43 Dmsg0(150, "done scan to eof\n");
48 * Format a scanner error message
50 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 e_msg(file, line, M_ERROR_TERM, 0, "Config error: %s\n\
60 : Line %d, col %d of file %s\n%s\n",
61 buf, lc->line_no, lc->col_no, lc->fname, lc->line);
66 * Free the current file, and retrieve the contents
67 * of the previous packet if any.
70 lex_close_file(LEX *lf)
74 Dmsg1(20, "Close lex file: %s\n", lf->fname);
76 Emsg0(M_ABORT, 0, "Close of NULL file\n");
80 Dmsg1(29, "Close cfg file %s\n", lf->fname);
83 of->options = lf->options; /* preserve options */
84 memcpy(lf, of, sizeof(LEX));
85 Dmsg1(29, "Restart scan of cfg file %s\n", of->fname);
95 * Open a new configuration file. We push the
96 * state of the current file (lf) so that we
97 * can do includes. This is a bit of a hammer.
98 * Instead of passing back the pointer to the
99 * new packet, I simply replace the contents
100 * of the caller's packet with the new packet,
101 * and link the contents of the old packet into
106 lex_open_file(LEX *lf, char *filename, LEX_ERROR_HANDLER *scan_error)
111 char *fname = bstrdup(filename);
114 if ((fd = fopen(fname, "r")) == NULL) {
115 Emsg2(M_ABORT, 0, "Cannot open config file %s: %s\n", fname, strerror(errno));
117 Dmsg1(29, "Open config file: %s\n", fname);
118 nf = (LEX *)malloc(sizeof(LEX));
120 memcpy(nf, lf, sizeof(LEX));
121 memset(lf, 0, sizeof(LEX));
122 lf->next = nf; /* if have lf, push it behind new one */
123 lf->options = nf->options; /* preserve user options */
125 lf = nf; /* start new packet */
126 memset(lf, 0, sizeof(LEX));
130 lf->state = lex_none;
133 lf->scan_error = scan_error;
135 lf->scan_error = s_err;
137 Dmsg1(29, "Return lex=%x\n", lf);
142 * Get the next character from the input.
143 * Returns the character or
144 * L_EOF if end of file
145 * L_EOL if end of line
148 lex_get_char(LEX *lf)
151 Emsg0(M_ABORT, 0, "get_char: called after EOF\n");
152 if (lf->ch == L_EOL) {
153 if (fgets(lf->line, MAXSTRING, lf->fd) == NULL) {
163 lf->ch = lf->line[lf->col_no];
169 Dmsg2(900, "lex_get_char: %c %d\n", lf->ch, lf->ch);
174 lex_unget_char(LEX *lf)
183 * Add a character to the current string
185 static void add_str(LEX *lf, int ch)
187 if (lf->str_len >= MAXSTRING-3) {
188 Emsg2(M_ABORT, 0, "Token too long, file: %s, line %s\n", lf->fname, lf->line_no);
190 lf->str[lf->str_len++] = ch;
191 lf->str[lf->str_len] = 0;
197 static void begin_str(LEX *lf, int ch)
207 lex_state_to_str(int state)
210 case lex_none: return "none";
211 case lex_comment: return "comment";
212 case lex_number: return "number";
213 case lex_ip_addr: return "ip_addr";
214 case lex_identifier: return "identifier";
215 case lex_string: return "string";
216 case lex_quoted_string: return "quoted_string";
217 default: return "??????";
223 * Convert a lex token to a string
224 * used for debug/error printing.
227 lex_tok_to_str(int token)
230 case L_EOF: return "L_EOF";
231 case L_EOL: return "L_EOL";
232 case T_NONE: return "T_NONE";
233 case T_NUMBER: return "T_NUMBER";
234 case T_IPADDR: return "T_IPADDR";
235 case T_IDENTIFIER: return "T_IDENTIFIER";
236 case T_UNQUOTED_STRING: return "T_UNQUOTED_STRING";
237 case T_QUOTED_STRING: return "T_QUOTED_STRING";
238 case T_BOB: return "T_BOB";
239 case T_EOB: return "T_EOB";
240 case T_EQUALS: return "T_EQUALS";
241 case T_ERROR: return "T_ERROR";
242 case T_EOF: return "T_EOF";
243 case T_COMMA: return "T_COMMA";
244 case T_EOL: return "T_EOL";
245 default: return "??????";
249 static uint32_t scan_pint(LEX *lf, char *str)
252 if (!is_a_number(str)) {
253 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);
261 return (uint32_t)dval;
266 * Get the next token from the input
270 lex_get_token(LEX *lf, int expect)
274 int esc_next = FALSE;
276 Dmsg0(290, "enter lex_get_token\n");
277 while (token == T_NONE) {
278 ch = lex_get_char(lf);
281 Dmsg2(290, "Lex state lex_none ch=%d,%x\n", ch, ch);
285 if (lf->options & LOPT_NO_IDENT)
286 lf->state = lex_string;
288 lf->state = lex_identifier;
293 lf->state = lex_number;
297 Dmsg0(290, "Enter lex_none switch\n");
301 Dmsg0(290, "got L_EOF set token=T_EOF\n");
304 lf->state = lex_comment;
315 lf->state = lex_quoted_string;
327 token = T_EOL; /* treat ; like EOL */
330 Dmsg0(290, "got L_EOL set token=T_EOL\n");
334 lf->state = lex_include;
338 lf->state = lex_string;
344 Dmsg1(290, "Lex state lex_comment ch=%x\n", ch);
346 lf->state = lex_none;
351 Dmsg2(290, "Lex state lex_number ch=%x %c\n", ch, ch);
352 /* Might want to allow trailing specifications here */
358 /* A valid number can be terminated by the following */
359 if (ISSPACE(ch) || ch == L_EOL || ch == ',' || ch == ';') {
361 lf->state = lex_none;
363 lf->state = lex_string;
368 Dmsg1(290, "Lex state lex_ip_addr ch=%x\n", ch);
371 Dmsg1(290, "Lex state lex_string ch=%x\n", ch);
372 if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
373 ch == ';' || ch == ',' || ch == '#' || (ISSPACE(ch)) ) {
375 token = T_UNQUOTED_STRING;
376 lf->state = lex_none;
382 Dmsg2(290, "Lex state lex_identifier ch=%x %c\n", ch, ch);
386 } else if (ISSPACE(ch)) {
388 } else if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
389 ch == ';' || ch == ',' || ch == '"' || ch == '#') {
391 token = T_IDENTIFIER;
392 lf->state = lex_none;
394 } else if (ch == L_EOF) {
396 lf->state = lex_none;
400 /* Some non-alpha character => string */
401 lf->state = lex_string;
404 case lex_quoted_string:
405 Dmsg2(290, "Lex state lex_quoted_string ch=%x %c\n", ch, ch);
420 token = T_QUOTED_STRING;
421 lf->state = lex_none;
426 case lex_include: /* scanning a filename */
427 if (ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' ||
428 ch == ';' || ch == ',' || ch == '"' || ch == '#') {
429 lf->state = lex_none;
430 lf = lex_open_file(lf, lf->str, NULL);
436 Dmsg4(290, "ch=%d state=%s token=%s %c\n", ch, lex_state_to_str(lf->state),
437 lex_tok_to_str(token), ch);
439 Dmsg2(290, "lex returning: line %d token: %s\n", lf->line_no, lex_tok_to_str(token));
443 * Here is where we check to see if the user has set certain
444 * expectations (e.g. 32 bit integer). If so, we do type checking
445 * and possible additional scanning (e.g. for range).
449 lf->pint32_val = scan_pint(lf, lf->str);
450 lf->pint32_val2 = lf->pint32_val;
455 if (token == T_NUMBER) {
456 lf->pint32_val = scan_pint(lf, lf->str);
457 lf->pint32_val2 = lf->pint32_val;
460 char *p = strchr(lf->str, '-');
462 scan_err2(lf, "expected an integer or a range, got %s: %s",
463 lex_tok_to_str(token), lf->str);
467 *p++ = 0; /* terminate first half of range */
468 lf->pint32_val = scan_pint(lf, lf->str);
469 lf->pint32_val2 = scan_pint(lf, p);
470 token = T_PINT32_RANGE;
475 if (token != T_NUMBER || !is_a_number(lf->str)) {
476 scan_err2(lf, "expected an integer number, got %s: %s",
477 lex_tok_to_str(token), lf->str);
482 lf->int32_val = (int32_t)strtod(lf->str, NULL);
484 scan_err2(lf, "expected an integer number, got %s: %s",
485 lex_tok_to_str(token), lf->str);
493 Dmsg2(400, "int64=:%s: %f\n", lf->str, strtod(lf->str, NULL));
494 if (token != T_NUMBER || !is_a_number(lf->str)) {
495 scan_err2(lf, "expected an integer number, got %s: %s",
496 lex_tok_to_str(token), lf->str);
501 lf->int64_val = (int64_t)strtod(lf->str, NULL);
503 scan_err2(lf, "expected an integer number, got %s: %s",
504 lex_tok_to_str(token), lf->str);
512 if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
513 scan_err2(lf, "expected a name, got %s: %s",
514 lex_tok_to_str(token), lf->str);
516 } else if (lf->str_len > MAX_RES_NAME_LENGTH) {
517 scan_err3(lf, "name %s length %d too long, max is %d\n", lf->str,
518 lf->str_len, MAX_RES_NAME_LENGTH);
526 if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
527 scan_err2(lf, "expected a name, got %s: %s",
528 lex_tok_to_str(token), lf->str);
537 break; /* no expectation given */
539 lf->token = token; /* set possible new token */