2 * Lexical scanner for Bacula configuration file
7 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of
12 the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public
20 License along with this program; if not, write to the Free
21 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29 extern int debug_level;
32 * Free the current file, and retrieve the contents
33 * of the previous packet if any.
36 lex_close_file(LEX *lf)
40 Dmsg1(20, "Close lex file: %s\n", lf->fname);
42 Emsg0(M_ABORT, 0, "Close of NULL file\n");
46 Dmsg1(29, "Close cfg file %s\n", lf->fname);
49 of->options = lf->options; /* preserve options */
50 memcpy(lf, of, sizeof(LEX));
51 Dmsg1(29, "Restart scan of cfg file %s\n", of->fname);
61 * Open a new configuration file. We push the
62 * state of the current file (lf) so that we
63 * can do includes. This is a bit of a hammer.
64 * Instead of passing back the pointer to the
65 * new packet, I simply replace the contents
66 * of the caller's packet with the new packet,
67 * and link the contents of the old packet into
72 lex_open_file(LEX *lf, char *filename)
76 char *fname = bstrdup(filename);
79 if ((fd = fopen(fname, "r")) == NULL) {
80 Emsg2(M_ABORT, 0, "Cannot open config file %s: %s\n", fname, strerror(errno));
82 Dmsg1(29, "Open config file: %s\n", fname);
83 nf = (LEX *)malloc(sizeof(LEX));
85 memcpy(nf, lf, sizeof(LEX));
86 memset(lf, 0, sizeof(LEX));
87 lf->next = nf; /* if have lf, push it behind new one */
88 lf->options = nf->options; /* preserve user options */
90 lf = nf; /* start new packet */
91 memset(lf, 0, sizeof(LEX));
97 Dmsg1(29, "Return lex=%x\n", lf);
102 * Get the next character from the input.
103 * Returns the character or
104 * L_EOF if end of file
105 * L_EOL if end of line
108 lex_get_char(LEX *lf)
111 Emsg0(M_ABORT, 0, "get_char: called after EOF\n");
112 if (lf->ch == L_EOL) {
113 if (fgets(lf->line, MAXSTRING, lf->fd) == NULL) {
123 lf->ch = lf->line[lf->col_no];
129 Dmsg2(900, "lex_get_char: %c %d\n", lf->ch, lf->ch);
134 lex_unget_char(LEX *lf)
143 * Add a character to the current string
145 static void add_str(LEX *lf, int ch)
147 if (lf->str_len >= MAXSTRING-3) {
148 Emsg2(M_ABORT, 0, "Token too long, file: %s, line %s\n", lf->fname, lf->line_no);
150 lf->str[lf->str_len++] = ch;
151 lf->str[lf->str_len] = 0;
157 static void begin_str(LEX *lf, int ch)
167 lex_state_to_str(int state)
170 case lex_none: return "none";
171 case lex_comment: return "comment";
172 case lex_number: return "number";
173 case lex_ip_addr: return "ip_addr";
174 case lex_identifier: return "identifier";
175 case lex_string: return "string";
176 case lex_quoted_string: return "quoted_string";
177 default: return "??????";
183 * Convert a lex token to a string
184 * used for debug/error printing.
187 lex_tok_to_str(int token)
190 case L_EOF: return "L_EOF";
191 case L_EOL: return "L_EOL";
192 case T_NONE: return "T_NONE";
193 case T_NUMBER: return "T_NUMBER";
194 case T_IPADDR: return "T_IPADDR";
195 case T_IDENTIFIER: return "T_IDENTIFIER";
196 case T_STRING: return "T_STRING";
197 case T_QUOTED_STRING: return "T_QUOTED_STRING";
198 case T_BOB: return "T_BOB";
199 case T_EOB: return "T_EOB";
200 case T_EQUALS: return "T_EQUALS";
201 case T_ERROR: return "T_ERROR";
202 case T_EOF: return "T_EOF";
203 case T_COMMA: return "T_COMMA";
204 case T_EOL: return "T_EOL";
205 default: return "??????";
211 * Get the next token from the input
215 lex_get_token(LEX *lf)
219 int esc_next = FALSE;
221 Dmsg0(290, "enter lex_get_token\n");
222 while (token == T_NONE) {
223 ch = lex_get_char(lf);
226 Dmsg2(290, "Lex state lex_none ch=%d,%x\n", ch, ch);
230 if (lf->options & LOPT_NO_IDENT)
231 lf->state = lex_string;
233 lf->state = lex_identifier;
238 lf->state = lex_number;
242 Dmsg0(290, "Enter lex_none switch\n");
246 Dmsg0(290, "got L_EOF set token=T_EOF\n");
249 lf->state = lex_comment;
260 lf->state = lex_quoted_string;
272 token = T_EOL; /* treat ; like EOL */
275 Dmsg0(290, "got L_EOL set token=T_EOL\n");
279 lf->state = lex_include;
283 lf->state = lex_string;
289 Dmsg1(290, "Lex state lex_comment ch=%x\n", ch);
291 lf->state = lex_none;
296 Dmsg2(290, "Lex state lex_number ch=%x %c\n", ch, ch);
297 /* Might want to allow trailing specifications here */
303 /* A valid number can be terminated by the following */
304 if (ISSPACE(ch) || ch == L_EOL || ch == ',' || ch == ';') {
306 lf->state = lex_none;
308 lf->state = lex_string;
313 Dmsg1(290, "Lex state lex_ip_addr ch=%x\n", ch);
316 Dmsg1(290, "Lex state lex_string ch=%x\n", ch);
317 if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
318 ch == ';' || ch == ',' || ch == '#' || (ISSPACE(ch)) ) {
321 lf->state = lex_none;
327 Dmsg2(290, "Lex state lex_identifier ch=%x %c\n", ch, ch);
331 } else if (ISSPACE(ch)) {
333 } else if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
334 ch == ';' || ch == ',' || ch == '"' || ch == '#') {
336 token = T_IDENTIFIER;
337 lf->state = lex_none;
339 } else if (ch == L_EOF) {
341 lf->state = lex_none;
345 /* Some non-alpha character => string */
346 lf->state = lex_string;
349 case lex_quoted_string:
350 Dmsg2(290, "Lex state lex_quoted_string ch=%x %c\n", ch, ch);
365 token = T_QUOTED_STRING;
366 lf->state = lex_none;
371 case lex_include: /* scanning a filename */
372 if (ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' ||
373 ch == ';' || ch == ',' || ch == '"' || ch == '#') {
374 lf->state = lex_none;
375 lf = lex_open_file(lf, lf->str);
381 Dmsg4(290, "ch=%d state=%s token=%s %c\n", ch, lex_state_to_str(lf->state),
382 lex_tok_to_str(token), ch);
384 Dmsg2(290, "lex returning: line %d token: %s\n", lf->line_no, lex_tok_to_str(token));