]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/lex.c
322181ad4e34329891211b244f498322c00d7f15
[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_ALL)) != 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_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 "??????";
240    }
241 }
242
243 static uint32_t scan_pint(LEX *lf, char *str)
244 {
245    double dval;
246    if (!is_a_number(str)) {
247       scan_err1(lf, "expected a positive integer number, got: %s", str);
248    } else {
249       errno = 0;
250       dval = strtod(str, NULL);
251       if (errno != 0 || dval < 0) {
252          scan_err1(lf, "expected a postive integer number, got: %s", str);
253       }
254    }
255    return (uint32_t)dval;
256 }
257
258 /*        
259  * 
260  * Get the next token from the input
261  *
262  */
263 int
264 lex_get_token(LEX *lf, int expect)
265 {
266    int ch;
267    int token = T_NONE;
268    int esc_next = FALSE;
269
270    Dmsg0(290, "enter lex_get_token\n");
271    while (token == T_NONE) {
272       ch = lex_get_char(lf);
273       switch (lf->state) {
274          case lex_none:
275             Dmsg2(290, "Lex state lex_none ch=%d,%x\n", ch, ch);
276             if (ISSPACE(ch))  
277                break;
278             if (ISALPHA(ch)) {
279                if (lf->options & LOPT_NO_IDENT)
280                   lf->state = lex_string;
281                else
282                   lf->state = lex_identifier;
283                begin_str(lf, ch);
284                break;
285             }
286             if (ISDIGIT(ch)) {
287                lf->state = lex_number;
288                begin_str(lf, ch);
289                break;
290             }
291             Dmsg0(290, "Enter lex_none switch\n");
292             switch (ch) {
293                case L_EOF:
294                   token = T_EOF;
295                   Dmsg0(290, "got L_EOF set token=T_EOF\n");
296                   break;
297                case '#':
298                   lf->state = lex_comment;
299                   break;
300                case '{':
301                   token = T_BOB;
302                   begin_str(lf, ch);
303                   break;
304                case '}':
305                   token = T_EOB;
306                   begin_str(lf, ch);
307                   break;
308                case '"':
309                   lf->state = lex_quoted_string;
310                   begin_str(lf, 0);
311                   break;
312                case '=': 
313                   token = T_EQUALS;
314                   begin_str(lf, ch);
315                   break;
316                case ',':
317                   token = T_COMMA;
318                   begin_str(lf, ch);
319                   break;
320                case ';':
321                   token = T_EOL;      /* treat ; like EOL */
322                   break;
323                case L_EOL:
324                   Dmsg0(290, "got L_EOL set token=T_EOL\n");
325                   token = T_EOL;
326                   break;
327                case '@':
328                   lf->state = lex_include;
329                   begin_str(lf, 0);
330                   break;
331                default:
332                   lf->state = lex_string;
333                   begin_str(lf, ch);
334                   break;
335             }
336             break;
337          case lex_comment:
338             Dmsg1(290, "Lex state lex_comment ch=%x\n", ch);
339             if (ch == L_EOL) {
340                lf->state = lex_none;
341                token = T_EOL;
342             }
343             break;
344          case lex_number:
345             Dmsg2(290, "Lex state lex_number ch=%x %c\n", ch, ch);
346             /* Might want to allow trailing specifications here */
347             if (ISDIGIT(ch)) {
348                add_str(lf, ch);
349                break;
350             }
351
352             /* A valid number can be terminated by the following */
353             if (ISSPACE(ch) || ch == L_EOL || ch == ',' || ch == ';') {
354                token = T_NUMBER;
355                lf->state = lex_none;
356             } else {
357                lf->state = lex_string;
358             }
359             lex_unget_char(lf);
360             break;
361          case lex_ip_addr:
362             Dmsg1(290, "Lex state lex_ip_addr ch=%x\n", ch);
363             break;
364          case lex_string:
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)) ) {
368                lex_unget_char(lf);    
369                token = T_UNQUOTED_STRING;
370                lf->state = lex_none;
371                break;
372             } 
373             add_str(lf, ch);
374             break;
375          case lex_identifier:
376             Dmsg2(290, "Lex state lex_identifier ch=%x %c\n", ch, ch);
377             if (ISALPHA(ch)) {
378                add_str(lf, ch);
379                break;
380             } else if (ISSPACE(ch)) {
381                break;
382             } else if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
383                        ch == ';' || ch == ','   || ch == '"' || ch == '#') {
384                lex_unget_char(lf);    
385                token = T_IDENTIFIER;
386                lf->state = lex_none;
387                break;
388             } else if (ch == L_EOF) {
389                token = T_ERROR;
390                lf->state = lex_none;
391                begin_str(lf, ch);
392                break;
393             }
394             /* Some non-alpha character => string */
395             lf->state = lex_string;
396             add_str(lf, ch);
397             break;
398          case lex_quoted_string:
399             Dmsg2(290, "Lex state lex_quoted_string ch=%x %c\n", ch, ch);
400             if (ch == L_EOL) {
401                esc_next = FALSE;
402                break;
403             }
404             if (esc_next) {
405                add_str(lf, ch);
406                esc_next = FALSE;
407                break;
408             }
409             if (ch == '\\') {
410                esc_next = TRUE;
411                break;
412             }
413             if (ch == '"') {
414                token = T_QUOTED_STRING;
415                lf->state = lex_none;
416                break;
417             }
418             add_str(lf, ch);
419             break;
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);
425                break;
426             }
427             add_str(lf, ch);
428             break;
429       }
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);
432    }
433    Dmsg2(290, "lex returning: line %d token: %s\n", lf->line_no, lex_tok_to_str(token));
434    lf->token = token;
435
436    /* 
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).
440     */
441    switch (expect) {
442    case T_PINT32:
443       lf->pint32_val = scan_pint(lf, lf->str);
444       lf->pint32_val2 = lf->pint32_val;
445       token = T_PINT32;
446       break;
447
448    case T_PINT32_RANGE:
449       if (token == T_NUMBER) {
450          lf->pint32_val = scan_pint(lf, lf->str);
451          lf->pint32_val2 = lf->pint32_val;
452          token = T_PINT32;
453       } else {
454          char *p = strchr(lf->str, '-');
455          if (!p) {
456             scan_err1(lf, "expected an integer or a range, got: %s", lf->str);
457          }
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;
462       }
463       break;
464
465    case T_INT32:
466       if (token != T_NUMBER || !is_a_number(lf->str)) {
467          scan_err1(lf, "expected an integer number, got: %s", lf->str);
468       } else {
469          errno = 0;
470          lf->int32_val = (int32_t)strtod(lf->str, NULL);
471          if (errno != 0) {
472             scan_err1(lf, "expected an integer number, got: %s", lf->str);
473          }
474       }
475       token = T_INT32;
476       break;
477
478    case T_INT64:
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);
482       } else {
483          errno = 0;
484          lf->int64_val = (int64_t)strtod(lf->str, NULL);
485          if (errno != 0) {
486             scan_err1(lf, "expected an integer number, got: %s", lf->str);
487          }
488       }
489       token = T_INT64;
490       break;
491
492    case T_NAME:
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);
498       }
499       token = T_NAME;
500       break;
501
502    case T_STRING:
503       if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
504          scan_err1(lf, "expected a name: %s", lf->str);
505       }
506       token = T_STRING;
507       break;
508
509
510    default:
511       break;                          /* no expectation given */
512    }
513    lf->token = token;                 /* set possible new token */
514    return token;
515 }