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