]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/lex.c
Integrate Phil's code, lex updates
[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    Dmsg0(150, "start scan to eof\n");
40    while (lex_get_token(lc, T_ALL) != T_EOL)
41       { }
42    Dmsg0(150, "done scan to eof\n");
43 }
44
45    
46 /*
47  * Format a scanner error message 
48  */
49 static void s_err(char *file, int line, LEX *lc, char *msg, ...)
50 {
51    va_list arg_ptr;
52    char buf[MAXSTRING];
53    char more[MAXSTRING];
54
55    va_start(arg_ptr, msg);
56    bvsnprintf(buf, sizeof(buf), msg, arg_ptr);
57    va_end(arg_ptr);
58      
59    if (lc->line_no > lc->begin_line_no) {
60       sprintf(more, _("Problem probably begins at Line %d.\n"), lc->begin_line_no);
61    } else {
62       more[0] = 0;
63    }
64    e_msg(file, line, M_ERROR_TERM, 0, _("Config error: %s\n\
65             : Line %d, col %d of file %s\n%s\n%s"),
66       buf, lc->line_no, lc->col_no, lc->fname, lc->line, more);
67 }
68
69
70 /*
71  * Free the current file, and retrieve the contents
72  * of the previous packet if any.
73  */
74 LEX *
75 lex_close_file(LEX *lf)
76 {
77    LEX *of;
78
79    Dmsg1(40, "Close lex file: %s\n", lf->fname);
80    if (lf == NULL) {
81       Emsg0(M_ABORT, 0, "Close of NULL file\n");
82    }
83    of = lf->next;
84    fclose(lf->fd);
85    Dmsg1(49, "Close cfg file %s\n", lf->fname);
86    free(lf->fname);
87    if (of) {
88       of->options = lf->options;      /* preserve options */
89       memcpy(lf, of, sizeof(LEX));
90       Dmsg1(49, "Restart scan of cfg file %s\n", of->fname);
91    } else {
92       of = lf;
93       lf = NULL;
94    }
95    free(of);
96    return lf;
97 }
98
99 /*     
100  * Open a new configuration file. We push the
101  * state of the current file (lf) so that we
102  * can do includes.  This is a bit of a hammer.
103  * Instead of passing back the pointer to the
104  * new packet, I simply replace the contents
105  * of the caller's packet with the new packet,
106  * and link the contents of the old packet into
107  * the next field.
108  *
109  */
110 LEX *
111 lex_open_file(LEX *lf, char *filename, LEX_ERROR_HANDLER *scan_error) 
112               
113 {
114    LEX *nf;
115    FILE *fd;
116    char *fname = bstrdup(filename);
117
118    
119    if ((fd = fopen(fname, "r")) == NULL) {
120       Emsg2(M_ERROR_TERM, 0, _("Cannot open config file %s: %s\n"), 
121             fname, strerror(errno));
122    }
123    Dmsg1(49, "Open config file: %s\n", fname);
124    nf = (LEX *)malloc(sizeof(LEX));
125    if (lf) {     
126       memcpy(nf, lf, sizeof(LEX));
127       memset(lf, 0, sizeof(LEX));
128       lf->next = nf;                  /* if have lf, push it behind new one */
129       lf->options = nf->options;      /* preserve user options */
130    } else {
131       lf = nf;                        /* start new packet */
132       memset(lf, 0, sizeof(LEX));
133    }
134    lf->fd = fd;
135    lf->fname = fname;
136    lf->state = lex_none;
137    lf->ch = L_EOL;
138    if (scan_error) {
139       lf->scan_error = scan_error;
140    } else {
141       lf->scan_error = s_err;
142    }
143    Dmsg1(49, "Return lex=%x\n", lf);
144    return lf;
145 }
146
147 /*    
148  * Get the next character from the input.
149  *  Returns the character or
150  *    L_EOF if end of file
151  *    L_EOL if end of line
152  */
153 int
154 lex_get_char(LEX *lf)
155 {
156    if (lf->ch == L_EOF) {
157       Emsg0(M_ABORT, 0, "get_char: called after EOF\n");
158    }
159    if (lf->ch == L_EOL) {
160       if (fgets(lf->line, MAXSTRING, lf->fd) == NULL) {
161          lf->ch = L_EOF;
162          if (lf->next) {
163             lex_close_file(lf);
164          }
165          return lf->ch;
166       }
167       lf->line_no++;
168       lf->col_no = 0;
169    }
170    lf->ch = lf->line[lf->col_no];
171    if (lf->ch == 0) {
172       lf->ch = L_EOL;
173    } else {
174       lf->col_no++;
175    }
176    Dmsg2(900, "lex_get_char: %c %d\n", lf->ch, lf->ch);
177    return lf->ch;
178 }
179
180 void
181 lex_unget_char(LEX *lf)
182 {
183    lf->col_no--;      
184    if (lf->ch == L_EOL)
185       lf->ch = 0;
186 }
187
188
189 /*
190  * Add a character to the current string
191  */
192 static void add_str(LEX *lf, int ch)
193 {
194    if (lf->str_len >= MAXSTRING-3) {
195       Emsg3(M_ERROR_TERM, 0, _(
196             "Token too long, file: %s, line %d, begins at line %d\n"), 
197              lf->fname, lf->line_no, lf->begin_line_no);
198    }
199    lf->str[lf->str_len++] = ch;
200    lf->str[lf->str_len] = 0;
201 }
202
203 /*
204  * Begin the string
205  */
206 static void begin_str(LEX *lf, int ch)  
207 {
208    lf->str_len = 0;
209    lf->str[0] = 0;
210    if (ch != 0) {
211       add_str(lf, ch);
212    }
213    lf->begin_line_no = lf->line_no;   /* save start string line no */
214 }
215
216 #ifdef DEBUG
217 static char *
218 lex_state_to_str(int state)
219 {
220    switch (state) {
221       case lex_none:          return "none";
222       case lex_comment:       return "comment";
223       case lex_number:        return "number";
224       case lex_ip_addr:       return "ip_addr";
225       case lex_identifier:    return "identifier";
226       case lex_string:        return "string";
227       case lex_quoted_string: return "quoted_string";
228       default:                return "??????";
229    }
230 }
231 #endif
232
233 /*
234  * Convert a lex token to a string
235  * used for debug/error printing.
236  */
237 char *
238 lex_tok_to_str(int token)
239 {
240    switch(token) {
241       case L_EOF:             return "L_EOF";
242       case L_EOL:             return "L_EOL";
243       case T_NONE:            return "T_NONE";
244       case T_NUMBER:          return "T_NUMBER";
245       case T_IPADDR:          return "T_IPADDR";
246       case T_IDENTIFIER:      return "T_IDENTIFIER";
247       case T_UNQUOTED_STRING: return "T_UNQUOTED_STRING";
248       case T_QUOTED_STRING:   return "T_QUOTED_STRING";
249       case T_BOB:             return "T_BOB";
250       case T_EOB:             return "T_EOB";
251       case T_EQUALS:          return "T_EQUALS";
252       case T_ERROR:           return "T_ERROR";
253       case T_EOF:             return "T_EOF";
254       case T_COMMA:           return "T_COMMA";
255       case T_EOL:             return "T_EOL";
256       default:                return "??????";
257    }
258 }
259
260 static uint32_t scan_pint(LEX *lf, char *str)
261 {
262    double dval = 0;
263    if (!is_a_number(str)) {
264       scan_err1(lf, "expected a positive integer number, got: %s", str);
265       /* NOT REACHED */
266    } else {
267       errno = 0;
268       dval = strtod(str, NULL);
269       if (errno != 0 || dval < 0) {
270          scan_err1(lf, "expected a postive integer number, got: %s", str);
271          /* NOT REACHED */
272       }
273    }
274    return (uint32_t)dval;
275 }
276
277 /*        
278  * 
279  * Get the next token from the input
280  *
281  */
282 int
283 lex_get_token(LEX *lf, int expect)
284 {
285    int ch;
286    int token = T_NONE;
287    int esc_next = FALSE;
288
289    Dmsg0(290, "enter lex_get_token\n");
290    while (token == T_NONE) {
291       ch = lex_get_char(lf);
292       switch (lf->state) {
293          case lex_none:
294             Dmsg2(290, "Lex state lex_none ch=%d,%x\n", ch, ch);
295             if (B_ISSPACE(ch))  
296                break;
297             if (B_ISALPHA(ch)) {
298                if (lf->options & LOPT_NO_IDENT)
299                   lf->state = lex_string;
300                else
301                   lf->state = lex_identifier;
302                begin_str(lf, ch);
303                break;
304             }
305             if (B_ISDIGIT(ch)) {
306                lf->state = lex_number;
307                begin_str(lf, ch);
308                break;
309             }
310             Dmsg0(290, "Enter lex_none switch\n");
311             switch (ch) {
312                case L_EOF:
313                   token = T_EOF;
314                   Dmsg0(290, "got L_EOF set token=T_EOF\n");
315                   break;
316                case '#':
317                   lf->state = lex_comment;
318                   break;
319                case '{':
320                   token = T_BOB;
321                   begin_str(lf, ch);
322                   break;
323                case '}':
324                   token = T_EOB;
325                   begin_str(lf, ch);
326                   break;
327                case '"':
328                   lf->state = lex_quoted_string;
329                   begin_str(lf, 0);
330                   break;
331                case '=': 
332                   token = T_EQUALS;
333                   begin_str(lf, ch);
334                   break;
335                case ',':
336                   token = T_COMMA;
337                   begin_str(lf, ch);
338                   break;
339                case ';':
340                   token = T_EOL;      /* treat ; like EOL */
341                   break;
342                case L_EOL:
343                   Dmsg0(290, "got L_EOL set token=T_EOL\n");
344                   token = T_EOL;
345                   break;
346                case '@':
347                   lf->state = lex_include;
348                   begin_str(lf, 0);
349                   break;
350                default:
351                   lf->state = lex_string;
352                   begin_str(lf, ch);
353                   break;
354             }
355             break;
356          case lex_comment:
357             Dmsg1(290, "Lex state lex_comment ch=%x\n", ch);
358             if (ch == L_EOL) {
359                lf->state = lex_none;
360                token = T_EOL;
361             } else if (ch == L_EOF) {
362                token = T_ERROR;
363             }
364             break;
365          case lex_number:
366             Dmsg2(290, "Lex state lex_number ch=%x %c\n", ch, ch);
367             if (ch == L_EOF) {
368                token = T_ERROR;
369                break;
370             }
371             /* Might want to allow trailing specifications here */
372             if (B_ISDIGIT(ch)) {
373                add_str(lf, ch);
374                break;
375             }
376
377             /* A valid number can be terminated by the following */
378             if (B_ISSPACE(ch) || ch == L_EOL || ch == ',' || ch == ';') {
379                token = T_NUMBER;
380                lf->state = lex_none;
381             } else {
382                lf->state = lex_string;
383             }
384             lex_unget_char(lf);
385             break;
386          case lex_ip_addr:
387             if (ch == L_EOF) {
388                token = T_ERROR;
389                break;
390             }
391             Dmsg1(290, "Lex state lex_ip_addr ch=%x\n", ch);
392             break;
393          case lex_string:
394             Dmsg1(290, "Lex state lex_string ch=%x\n", ch);
395             if (ch == L_EOF) {
396                token = T_ERROR;
397                break;
398             }
399             if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
400                 ch == ';' || ch == ',' || ch == '#' || (B_ISSPACE(ch)) ) {
401                lex_unget_char(lf);    
402                token = T_UNQUOTED_STRING;
403                lf->state = lex_none;
404                break;
405             } 
406             add_str(lf, ch);
407             break;
408          case lex_identifier:
409             Dmsg2(290, "Lex state lex_identifier ch=%x %c\n", ch, ch);
410             if (B_ISALPHA(ch)) {
411                add_str(lf, ch);
412                break;
413             } else if (B_ISSPACE(ch)) {
414                break;
415             } else if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
416                        ch == ';' || ch == ','   || ch == '"' || ch == '#') {
417                lex_unget_char(lf);    
418                token = T_IDENTIFIER;
419                lf->state = lex_none;
420                break;
421             } else if (ch == L_EOF) {
422                token = T_ERROR;
423                lf->state = lex_none;
424                begin_str(lf, ch);
425                break;
426             }
427             /* Some non-alpha character => string */
428             lf->state = lex_string;
429             add_str(lf, ch);
430             break;
431          case lex_quoted_string:
432             Dmsg2(290, "Lex state lex_quoted_string ch=%x %c\n", ch, ch);
433             if (ch == L_EOF) {
434                token = T_ERROR;
435                break;
436             }
437             if (ch == L_EOL) {
438                esc_next = FALSE;
439                break;
440             }
441             if (esc_next) {
442                add_str(lf, ch);
443                esc_next = FALSE;
444                break;
445             }
446             if (ch == '\\') {
447                esc_next = TRUE;
448                break;
449             }
450             if (ch == '"') {
451                token = T_QUOTED_STRING;
452                lf->state = lex_none;
453                break;
454             }
455             add_str(lf, ch);
456             break;
457          case lex_include:            /* scanning a filename */
458             if (ch == L_EOF) {
459                token = T_ERROR;
460                break;
461             }
462             if (B_ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' ||
463                 ch == ';' || ch == ','   || ch == '"' || ch == '#') {
464                lf->state = lex_none;
465                lf = lex_open_file(lf, lf->str, NULL);
466                break;
467             }
468             add_str(lf, ch);
469             break;
470       }
471       Dmsg4(290, "ch=%d state=%s token=%s %c\n", ch, lex_state_to_str(lf->state),
472         lex_tok_to_str(token), ch);
473    }
474    Dmsg2(290, "lex returning: line %d token: %s\n", lf->line_no, lex_tok_to_str(token));
475    lf->token = token;
476
477    /* 
478     * Here is where we check to see if the user has set certain 
479     *  expectations (e.g. 32 bit integer). If so, we do type checking
480     *  and possible additional scanning (e.g. for range).
481     */
482    switch (expect) {
483    case T_PINT32:
484       lf->pint32_val = scan_pint(lf, lf->str);
485       lf->pint32_val2 = lf->pint32_val;
486       token = T_PINT32;
487       break;
488
489    case T_PINT32_RANGE:
490       if (token == T_NUMBER) {
491          lf->pint32_val = scan_pint(lf, lf->str);
492          lf->pint32_val2 = lf->pint32_val;
493          token = T_PINT32;
494       } else {
495          char *p = strchr(lf->str, '-');
496          if (!p) {
497             scan_err2(lf, "expected an integer or a range, got %s: %s", 
498                lex_tok_to_str(token), lf->str);
499             token = T_ERROR;
500             break;
501          }
502          *p++ = 0;                       /* terminate first half of range */
503          lf->pint32_val  = scan_pint(lf, lf->str);
504          lf->pint32_val2 = scan_pint(lf, p);
505          token = T_PINT32_RANGE;
506       }
507       break;
508
509    case T_INT32:
510       if (token != T_NUMBER || !is_a_number(lf->str)) {
511          scan_err2(lf, "expected an integer number, got %s: %s",
512                lex_tok_to_str(token), lf->str);
513          token = T_ERROR;
514          break;
515       }
516       errno = 0;
517       lf->int32_val = (int32_t)strtod(lf->str, NULL);
518       if (errno != 0) {
519          scan_err2(lf, "expected an integer number, got %s: %s",
520                lex_tok_to_str(token), lf->str);
521          token = T_ERROR;
522       } else {
523          token = T_INT32;
524       }
525       break;
526
527    case T_INT64:
528       Dmsg2(400, "int64=:%s: %f\n", lf->str, strtod(lf->str, NULL)); 
529       if (token != T_NUMBER || !is_a_number(lf->str)) {
530          scan_err2(lf, "expected an integer number, got %s: %s",
531                lex_tok_to_str(token), lf->str);
532          token = T_ERROR;
533          break;
534       }
535       errno = 0;
536       lf->int64_val = (int64_t)strtod(lf->str, NULL);
537       if (errno != 0) {
538          scan_err2(lf, "expected an integer number, got %s: %s",
539                lex_tok_to_str(token), lf->str);
540          token = T_ERROR;
541       } else {
542          token = T_INT64;
543       }
544       break;
545
546    case T_NAME:
547       if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
548          scan_err2(lf, "expected a name, got %s: %s",
549                lex_tok_to_str(token), lf->str);
550          token = T_ERROR;
551       } else if (lf->str_len > MAX_RES_NAME_LENGTH) {
552          scan_err3(lf, "name %s length %d too long, max is %d\n", lf->str, 
553             lf->str_len, MAX_RES_NAME_LENGTH);
554          token = T_ERROR;
555       } else {
556          token = T_NAME;
557       }
558       break;
559
560    case T_STRING:
561       if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
562          scan_err2(lf, "expected a string, got %s: %s",
563                lex_tok_to_str(token), lf->str);
564          token = T_ERROR;
565       } else {
566          token = T_STRING;
567       }
568       break;
569
570
571    default:
572       break;                          /* no expectation given */
573    }
574    lf->token = token;                 /* set possible new token */
575    return token;
576 }