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 * Free the current file, and retrieve the contents
35 * of the previous packet if any.
38 lex_close_file(LEX *lf)
42 Dmsg1(20, "Close lex file: %s\n", lf->fname);
44 Emsg0(M_ABORT, 0, "Close of NULL file\n");
48 Dmsg1(29, "Close cfg file %s\n", lf->fname);
51 of->options = lf->options; /* preserve options */
52 memcpy(lf, of, sizeof(LEX));
53 Dmsg1(29, "Restart scan of cfg file %s\n", of->fname);
63 * Open a new configuration file. We push the
64 * state of the current file (lf) so that we
65 * can do includes. This is a bit of a hammer.
66 * Instead of passing back the pointer to the
67 * new packet, I simply replace the contents
68 * of the caller's packet with the new packet,
69 * and link the contents of the old packet into
74 lex_open_file(LEX *lf, char *filename)
78 char *fname = bstrdup(filename);
81 if ((fd = fopen(fname, "r")) == NULL) {
82 Emsg2(M_ABORT, 0, "Cannot open config file %s: %s\n", fname, strerror(errno));
84 Dmsg1(29, "Open config file: %s\n", fname);
85 nf = (LEX *)malloc(sizeof(LEX));
87 memcpy(nf, lf, sizeof(LEX));
88 memset(lf, 0, sizeof(LEX));
89 lf->next = nf; /* if have lf, push it behind new one */
90 lf->options = nf->options; /* preserve user options */
92 lf = nf; /* start new packet */
93 memset(lf, 0, sizeof(LEX));
99 Dmsg1(29, "Return lex=%x\n", lf);
104 * Get the next character from the input.
105 * Returns the character or
106 * L_EOF if end of file
107 * L_EOL if end of line
110 lex_get_char(LEX *lf)
113 Emsg0(M_ABORT, 0, "get_char: called after EOF\n");
114 if (lf->ch == L_EOL) {
115 if (fgets(lf->line, MAXSTRING, lf->fd) == NULL) {
125 lf->ch = lf->line[lf->col_no];
131 Dmsg2(900, "lex_get_char: %c %d\n", lf->ch, lf->ch);
136 lex_unget_char(LEX *lf)
145 * Add a character to the current string
147 static void add_str(LEX *lf, int ch)
149 if (lf->str_len >= MAXSTRING-3) {
150 Emsg2(M_ABORT, 0, "Token too long, file: %s, line %s\n", lf->fname, lf->line_no);
152 lf->str[lf->str_len++] = ch;
153 lf->str[lf->str_len] = 0;
159 static void begin_str(LEX *lf, int ch)
169 lex_state_to_str(int state)
172 case lex_none: return "none";
173 case lex_comment: return "comment";
174 case lex_number: return "number";
175 case lex_ip_addr: return "ip_addr";
176 case lex_identifier: return "identifier";
177 case lex_string: return "string";
178 case lex_quoted_string: return "quoted_string";
179 default: return "??????";
185 * Convert a lex token to a string
186 * used for debug/error printing.
189 lex_tok_to_str(int token)
192 case L_EOF: return "L_EOF";
193 case L_EOL: return "L_EOL";
194 case T_NONE: return "T_NONE";
195 case T_NUMBER: return "T_NUMBER";
196 case T_IPADDR: return "T_IPADDR";
197 case T_IDENTIFIER: return "T_IDENTIFIER";
198 case T_STRING: return "T_STRING";
199 case T_QUOTED_STRING: return "T_QUOTED_STRING";
200 case T_BOB: return "T_BOB";
201 case T_EOB: return "T_EOB";
202 case T_EQUALS: return "T_EQUALS";
203 case T_ERROR: return "T_ERROR";
204 case T_EOF: return "T_EOF";
205 case T_COMMA: return "T_COMMA";
206 case T_EOL: return "T_EOL";
207 default: return "??????";
213 * Get the next token from the input
217 lex_get_token(LEX *lf)
221 int esc_next = FALSE;
223 Dmsg0(290, "enter lex_get_token\n");
224 while (token == T_NONE) {
225 ch = lex_get_char(lf);
228 Dmsg2(290, "Lex state lex_none ch=%d,%x\n", ch, ch);
232 if (lf->options & LOPT_NO_IDENT)
233 lf->state = lex_string;
235 lf->state = lex_identifier;
240 lf->state = lex_number;
244 Dmsg0(290, "Enter lex_none switch\n");
248 Dmsg0(290, "got L_EOF set token=T_EOF\n");
251 lf->state = lex_comment;
262 lf->state = lex_quoted_string;
274 token = T_EOL; /* treat ; like EOL */
277 Dmsg0(290, "got L_EOL set token=T_EOL\n");
281 lf->state = lex_include;
285 lf->state = lex_string;
291 Dmsg1(290, "Lex state lex_comment ch=%x\n", ch);
293 lf->state = lex_none;
298 Dmsg2(290, "Lex state lex_number ch=%x %c\n", ch, ch);
299 /* Might want to allow trailing specifications here */
305 /* A valid number can be terminated by the following */
306 if (ISSPACE(ch) || ch == L_EOL || ch == ',' || ch == ';') {
308 lf->state = lex_none;
310 lf->state = lex_string;
315 Dmsg1(290, "Lex state lex_ip_addr ch=%x\n", ch);
318 Dmsg1(290, "Lex state lex_string ch=%x\n", ch);
319 if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
320 ch == ';' || ch == ',' || ch == '#' || (ISSPACE(ch)) ) {
323 lf->state = lex_none;
329 Dmsg2(290, "Lex state lex_identifier ch=%x %c\n", ch, ch);
333 } else if (ISSPACE(ch)) {
335 } else if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
336 ch == ';' || ch == ',' || ch == '"' || ch == '#') {
338 token = T_IDENTIFIER;
339 lf->state = lex_none;
341 } else if (ch == L_EOF) {
343 lf->state = lex_none;
347 /* Some non-alpha character => string */
348 lf->state = lex_string;
351 case lex_quoted_string:
352 Dmsg2(290, "Lex state lex_quoted_string ch=%x %c\n", ch, ch);
367 token = T_QUOTED_STRING;
368 lf->state = lex_none;
373 case lex_include: /* scanning a filename */
374 if (ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' ||
375 ch == ';' || ch == ',' || ch == '"' || ch == '#') {
376 lf->state = lex_none;
377 lf = lex_open_file(lf, lf->str);
383 Dmsg4(290, "ch=%d state=%s token=%s %c\n", ch, lex_state_to_str(lf->state),
384 lex_tok_to_str(token), ch);
386 Dmsg2(290, "lex returning: line %d token: %s\n", lf->line_no, lex_tok_to_str(token));