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 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)
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;
131 Dmsg1(29, "Return lex=%x\n", lf);
136 * Get the next character from the input.
137 * Returns the character or
138 * L_EOF if end of file
139 * L_EOL if end of line
142 lex_get_char(LEX *lf)
145 Emsg0(M_ABORT, 0, "get_char: called after EOF\n");
146 if (lf->ch == L_EOL) {
147 if (fgets(lf->line, MAXSTRING, lf->fd) == NULL) {
157 lf->ch = lf->line[lf->col_no];
163 Dmsg2(900, "lex_get_char: %c %d\n", lf->ch, lf->ch);
168 lex_unget_char(LEX *lf)
177 * Add a character to the current string
179 static void add_str(LEX *lf, int ch)
181 if (lf->str_len >= MAXSTRING-3) {
182 Emsg2(M_ABORT, 0, "Token too long, file: %s, line %s\n", lf->fname, lf->line_no);
184 lf->str[lf->str_len++] = ch;
185 lf->str[lf->str_len] = 0;
191 static void begin_str(LEX *lf, int ch)
201 lex_state_to_str(int state)
204 case lex_none: return "none";
205 case lex_comment: return "comment";
206 case lex_number: return "number";
207 case lex_ip_addr: return "ip_addr";
208 case lex_identifier: return "identifier";
209 case lex_string: return "string";
210 case lex_quoted_string: return "quoted_string";
211 default: return "??????";
217 * Convert a lex token to a string
218 * used for debug/error printing.
221 lex_tok_to_str(int token)
224 case L_EOF: return "L_EOF";
225 case L_EOL: return "L_EOL";
226 case T_NONE: return "T_NONE";
227 case T_NUMBER: return "T_NUMBER";
228 case T_IPADDR: return "T_IPADDR";
229 case T_IDENTIFIER: return "T_IDENTIFIER";
230 case T_UNQUOTED_STRING: return "T_UNQUOTED_STRING";
231 case T_QUOTED_STRING: return "T_QUOTED_STRING";
232 case T_BOB: return "T_BOB";
233 case T_EOB: return "T_EOB";
234 case T_EQUALS: return "T_EQUALS";
235 case T_ERROR: return "T_ERROR";
236 case T_EOF: return "T_EOF";
237 case T_COMMA: return "T_COMMA";
238 case T_EOL: return "T_EOL";
239 default: return "??????";
243 static uint32_t scan_pint(LEX *lf, char *str)
246 if (!is_a_number(str)) {
247 scan_err1(lf, "expected a positive integer number, got: %s", str);
250 dval = strtod(str, NULL);
251 if (errno != 0 || dval < 0) {
252 scan_err1(lf, "expected a postive integer number, got: %s", str);
255 return (uint32_t)dval;
260 * Get the next token from the input
264 lex_get_token(LEX *lf, int expect)
268 int esc_next = FALSE;
270 Dmsg0(290, "enter lex_get_token\n");
271 while (token == T_NONE) {
272 ch = lex_get_char(lf);
275 Dmsg2(290, "Lex state lex_none ch=%d,%x\n", ch, ch);
279 if (lf->options & LOPT_NO_IDENT)
280 lf->state = lex_string;
282 lf->state = lex_identifier;
287 lf->state = lex_number;
291 Dmsg0(290, "Enter lex_none switch\n");
295 Dmsg0(290, "got L_EOF set token=T_EOF\n");
298 lf->state = lex_comment;
309 lf->state = lex_quoted_string;
321 token = T_EOL; /* treat ; like EOL */
324 Dmsg0(290, "got L_EOL set token=T_EOL\n");
328 lf->state = lex_include;
332 lf->state = lex_string;
338 Dmsg1(290, "Lex state lex_comment ch=%x\n", ch);
340 lf->state = lex_none;
345 Dmsg2(290, "Lex state lex_number ch=%x %c\n", ch, ch);
346 /* Might want to allow trailing specifications here */
352 /* A valid number can be terminated by the following */
353 if (ISSPACE(ch) || ch == L_EOL || ch == ',' || ch == ';') {
355 lf->state = lex_none;
357 lf->state = lex_string;
362 Dmsg1(290, "Lex state lex_ip_addr ch=%x\n", ch);
365 Dmsg1(290, "Lex state lex_string ch=%x\n", ch);
366 if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
367 ch == ';' || ch == ',' || ch == '#' || (ISSPACE(ch)) ) {
369 token = T_UNQUOTED_STRING;
370 lf->state = lex_none;
376 Dmsg2(290, "Lex state lex_identifier ch=%x %c\n", ch, ch);
380 } else if (ISSPACE(ch)) {
382 } else if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
383 ch == ';' || ch == ',' || ch == '"' || ch == '#') {
385 token = T_IDENTIFIER;
386 lf->state = lex_none;
388 } else if (ch == L_EOF) {
390 lf->state = lex_none;
394 /* Some non-alpha character => string */
395 lf->state = lex_string;
398 case lex_quoted_string:
399 Dmsg2(290, "Lex state lex_quoted_string ch=%x %c\n", ch, ch);
414 token = T_QUOTED_STRING;
415 lf->state = lex_none;
420 case lex_include: /* scanning a filename */
421 if (ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' ||
422 ch == ';' || ch == ',' || ch == '"' || ch == '#') {
423 lf->state = lex_none;
424 lf = lex_open_file(lf, lf->str);
430 Dmsg4(290, "ch=%d state=%s token=%s %c\n", ch, lex_state_to_str(lf->state),
431 lex_tok_to_str(token), ch);
433 Dmsg2(290, "lex returning: line %d token: %s\n", lf->line_no, lex_tok_to_str(token));
437 * Here is where we check to see if the user has set certain
438 * expectations (e.g. 32 bit integer). If so, we do type checking
439 * and possible additional scanning (e.g. for range).
443 lf->pint32_val = scan_pint(lf, lf->str);
444 lf->pint32_val2 = lf->pint32_val;
449 if (token == T_NUMBER) {
450 lf->pint32_val = scan_pint(lf, lf->str);
451 lf->pint32_val2 = lf->pint32_val;
454 char *p = strchr(lf->str, '-');
456 scan_err1(lf, "expected an integer or a range, got: %s", lf->str);
458 *p++ = 0; /* terminate first half of range */
459 lf->pint32_val = scan_pint(lf, lf->str);
460 lf->pint32_val2 = scan_pint(lf, p);
461 token = T_PINT32_RANGE;
466 if (token != T_NUMBER || !is_a_number(lf->str)) {
467 scan_err1(lf, "expected an integer number, got: %s", lf->str);
470 lf->int32_val = (int32_t)strtod(lf->str, NULL);
472 scan_err1(lf, "expected an integer number, got: %s", lf->str);
479 Dmsg2(400, "int64=:%s: %f\n", lf->str, strtod(lf->str, NULL));
480 if (token != T_NUMBER || !is_a_number(lf->str)) {
481 scan_err1(lf, "expected an integer number, got: %s", lf->str);
484 lf->int64_val = (int64_t)strtod(lf->str, NULL);
486 scan_err1(lf, "expected an integer number, got: %s", lf->str);
493 if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
494 scan_err1(lf, "expected a name: %s", lf->str);
495 } else if (lf->str_len > MAX_RES_NAME_LENGTH) {
496 scan_err3(lf, "name %s length %d too long, max is %d\n", lf->str,
497 lf->str_len, MAX_RES_NAME_LENGTH);
503 if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
504 scan_err1(lf, "expected a name: %s", lf->str);
511 break; /* no expectation given */
513 lf->token = token; /* set possible new token */