]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/lex.c
c25697fc3086caaf913314b5c099bcb6edd6aaf4
[bacula/bacula] / bacula / src / lib / lex.c
1 /*
2  * Lexical scanner for Bacula configuration file
3  *
4  *   Version $Id$
5  *
6  */
7
8 /*
9    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
10
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.
15
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.
20
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,
24    MA 02111-1307, USA.
25
26  */
27
28 #include "bacula.h"
29 #include "lex.h"
30
31 extern int debug_level;
32
33 /*
34  * Scan to "logical" end of line. I.e. end of line,
35  * or semicolon.
36  */
37 void scan_to_eol(LEX *lc)
38 {
39    int token;
40    Dmsg0(150, "start scan to eof\n");
41    while ((token = lex_get_token(lc)) != T_EOL) {
42    }
43    Dmsg0(150, "done scan to eof\n");
44 }
45
46    
47 /*
48  * Format a scanner error message 
49  */
50 void s_err(char *file, int line, LEX *lc, char *msg, ...)
51 {
52    va_list arg_ptr;
53    char buf[MAXSTRING];
54
55    va_start(arg_ptr, msg);
56    bvsnprintf(buf, sizeof(buf), msg, arg_ptr);
57    va_end(arg_ptr);
58      
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);
62 }
63
64
65 /*
66  * Free the current file, and retrieve the contents
67  * of the previous packet if any.
68  */
69 LEX *
70 lex_close_file(LEX *lf)
71 {
72    LEX *of;
73
74    Dmsg1(20, "Close lex file: %s\n", lf->fname);
75    if (lf == NULL) {
76       Emsg0(M_ABORT, 0, "Close of NULL file\n");
77    }
78    of = lf->next;
79    fclose(lf->fd);
80    Dmsg1(29, "Close cfg file %s\n", lf->fname);
81    free(lf->fname);
82    if (of) {
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);
86    } else {
87       of = lf;
88       lf = NULL;
89    }
90    free(of);
91    return lf;
92 }
93
94 /*     
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
102  * the next field.
103  *
104  */
105 LEX *
106 lex_open_file(LEX *lf, char *filename) 
107 {
108    LEX *nf;
109    FILE *fd;
110    char *fname = bstrdup(filename);
111
112    
113    if ((fd = fopen(fname, "r")) == NULL) {
114       Emsg2(M_ABORT, 0, "Cannot open config file %s: %s\n", fname, strerror(errno));
115    }
116    Dmsg1(29, "Open config file: %s\n", fname);
117    nf = (LEX *)malloc(sizeof(LEX));
118    if (lf) {     
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 */
123    } else {
124       lf = nf;                        /* start new packet */
125       memset(lf, 0, sizeof(LEX));
126    }
127    lf->fd = fd;
128    lf->fname = fname;
129    lf->state = lex_none;
130    lf->ch = L_EOL;
131    Dmsg1(29, "Return lex=%x\n", lf);
132    return lf;
133 }
134
135 /*    
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
140  */
141 int
142 lex_get_char(LEX *lf)
143 {
144    if (lf->ch == L_EOF)
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) {
148          lf->ch = L_EOF;
149          if (lf->next) {
150             lex_close_file(lf);
151          }
152          return lf->ch;
153       }
154       lf->line_no++;
155       lf->col_no = 0;
156    }
157    lf->ch = lf->line[lf->col_no];
158    if (lf->ch == 0) {
159       lf->ch = L_EOL;
160    } else {
161       lf->col_no++;
162    }
163    Dmsg2(900, "lex_get_char: %c %d\n", lf->ch, lf->ch);
164    return lf->ch;
165 }
166
167 void
168 lex_unget_char(LEX *lf)
169 {
170    lf->col_no--;      
171    if (lf->ch == L_EOL)
172       lf->ch = 0;
173 }
174
175
176 /*
177  * Add a character to the current string
178  */
179 static void add_str(LEX *lf, int ch)
180 {
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);
183    }
184    lf->str[lf->str_len++] = ch;
185    lf->str[lf->str_len] = 0;
186 }
187
188 /*
189  * Begin the string
190  */
191 static void begin_str(LEX *lf, int ch)  
192 {
193    lf->str_len = 0;
194    lf->str[0] = 0;
195    if (ch != 0)
196       add_str(lf, ch);
197 }
198
199 #ifdef DEBUG
200 static char *
201 lex_state_to_str(int state)
202 {
203    switch (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 "??????";
212    }
213 }
214 #endif
215
216 /*
217  * Convert a lex token to a string
218  * used for debug/error printing.
219  */
220 char *
221 lex_tok_to_str(int token)
222 {
223    switch(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_STRING:        return "T_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 "??????";
240    }
241 }
242
243 /*        
244  * 
245  * Get the next token from the input
246  *
247  */
248 int
249 lex_get_token(LEX *lf)
250 {
251    int ch;
252    int token = T_NONE;
253    int esc_next = FALSE;
254
255    Dmsg0(290, "enter lex_get_token\n");
256    while (token == T_NONE) {
257       ch = lex_get_char(lf);
258       switch (lf->state) {
259          case lex_none:
260             Dmsg2(290, "Lex state lex_none ch=%d,%x\n", ch, ch);
261             if (ISSPACE(ch))  
262                break;
263             if (ISALPHA(ch)) {
264                if (lf->options & LOPT_NO_IDENT)
265                   lf->state = lex_string;
266                else
267                   lf->state = lex_identifier;
268                begin_str(lf, ch);
269                break;
270             }
271             if (ISDIGIT(ch)) {
272                lf->state = lex_number;
273                begin_str(lf, ch);
274                break;
275             }
276             Dmsg0(290, "Enter lex_none switch\n");
277             switch (ch) {
278                case L_EOF:
279                   token = T_EOF;
280                   Dmsg0(290, "got L_EOF set token=T_EOF\n");
281                   break;
282                case '#':
283                   lf->state = lex_comment;
284                   break;
285                case '{':
286                   token = T_BOB;
287                   begin_str(lf, ch);
288                   break;
289                case '}':
290                   token = T_EOB;
291                   begin_str(lf, ch);
292                   break;
293                case '"':
294                   lf->state = lex_quoted_string;
295                   begin_str(lf, 0);
296                   break;
297                case '=': 
298                   token = T_EQUALS;
299                   begin_str(lf, ch);
300                   break;
301                case ',':
302                   token = T_COMMA;
303                   begin_str(lf, ch);
304                   break;
305                case ';':
306                   token = T_EOL;      /* treat ; like EOL */
307                   break;
308                case L_EOL:
309                   Dmsg0(290, "got L_EOL set token=T_EOL\n");
310                   token = T_EOL;
311                   break;
312                case '@':
313                   lf->state = lex_include;
314                   begin_str(lf, 0);
315                   break;
316                default:
317                   lf->state = lex_string;
318                   begin_str(lf, ch);
319                   break;
320             }
321             break;
322          case lex_comment:
323             Dmsg1(290, "Lex state lex_comment ch=%x\n", ch);
324             if (ch == L_EOL) {
325                lf->state = lex_none;
326                token = T_EOL;
327             }
328             break;
329          case lex_number:
330             Dmsg2(290, "Lex state lex_number ch=%x %c\n", ch, ch);
331             /* Might want to allow trailing specifications here */
332             if (ISDIGIT(ch)) {
333                add_str(lf, ch);
334                break;
335             }
336
337             /* A valid number can be terminated by the following */
338             if (ISSPACE(ch) || ch == L_EOL || ch == ',' || ch == ';') {
339                token = T_NUMBER;
340                lf->state = lex_none;
341             } else {
342                lf->state = lex_string;
343             }
344             lex_unget_char(lf);
345             break;
346          case lex_ip_addr:
347             Dmsg1(290, "Lex state lex_ip_addr ch=%x\n", ch);
348             break;
349          case lex_string:
350             Dmsg1(290, "Lex state lex_string ch=%x\n", ch);
351             if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
352                 ch == ';' || ch == ',' || ch == '#' || (ISSPACE(ch)) ) {
353                lex_unget_char(lf);    
354                token = T_STRING;
355                lf->state = lex_none;
356                break;
357             } 
358             add_str(lf, ch);
359             break;
360          case lex_identifier:
361             Dmsg2(290, "Lex state lex_identifier ch=%x %c\n", ch, ch);
362             if (ISALPHA(ch)) {
363                add_str(lf, ch);
364                break;
365             } else if (ISSPACE(ch)) {
366                break;
367             } else if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
368                        ch == ';' || ch == ','   || ch == '"' || ch == '#') {
369                lex_unget_char(lf);    
370                token = T_IDENTIFIER;
371                lf->state = lex_none;
372                break;
373             } else if (ch == L_EOF) {
374                token = T_ERROR;
375                lf->state = lex_none;
376                begin_str(lf, ch);
377                break;
378             }
379             /* Some non-alpha character => string */
380             lf->state = lex_string;
381             add_str(lf, ch);
382             break;
383          case lex_quoted_string:
384             Dmsg2(290, "Lex state lex_quoted_string ch=%x %c\n", ch, ch);
385             if (ch == L_EOL) {
386                esc_next = FALSE;
387                break;
388             }
389             if (esc_next) {
390                add_str(lf, ch);
391                esc_next = FALSE;
392                break;
393             }
394             if (ch == '\\') {
395                esc_next = TRUE;
396                break;
397             }
398             if (ch == '"') {
399                token = T_QUOTED_STRING;
400                lf->state = lex_none;
401                break;
402             }
403             add_str(lf, ch);
404             break;
405          case lex_include:            /* scanning a filename */
406             if (ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' ||
407                 ch == ';' || ch == ','   || ch == '"' || ch == '#') {
408                lf->state = lex_none;
409                lf = lex_open_file(lf, lf->str);
410                break;
411             }
412             add_str(lf, ch);
413             break;
414       }
415       Dmsg4(290, "ch=%d state=%s token=%s %c\n", ch, lex_state_to_str(lf->state),
416         lex_tok_to_str(token), ch);
417    }
418    Dmsg2(290, "lex returning: line %d token: %s\n", lf->line_no, lex_tok_to_str(token));
419    lf->token = token;
420    return token;
421 }