]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/lex.c
b3445d7f4ed20cce60c715c68e1474b0c10b7de8
[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       berrno be;
139       Emsg2(M_ERROR_TERM, 0, _("Cannot open config file %s: %s\n"), 
140             fname, be.strerror());
141       return NULL; /* Never reached if exit_on_error == 1 */
142    }
143    Dmsg1(2000, "Open config file: %s\n", fname);
144    nf = (LEX *)malloc(sizeof(LEX));
145    if (lf) {     
146       memcpy(nf, lf, sizeof(LEX));
147       memset(lf, 0, sizeof(LEX));
148       lf->next = nf;                  /* if have lf, push it behind new one */
149       lf->options = nf->options;      /* preserve user options */
150    } else {
151       lf = nf;                        /* start new packet */
152       memset(lf, 0, sizeof(LEX));
153    }
154    lf->fd = fd;
155    lf->fname = fname;
156    lf->state = lex_none;
157    lf->ch = L_EOL;
158    if (scan_error) {
159       lf->scan_error = scan_error;
160    } else {
161       lf->scan_error = s_err;
162    }
163    Dmsg1(2000, "Return lex=%x\n", lf);
164    return lf;
165 }
166
167 /*    
168  * Get the next character from the input.
169  *  Returns the character or
170  *    L_EOF if end of file
171  *    L_EOL if end of line
172  */
173 int lex_get_char(LEX *lf)
174 {
175    if (lf->ch == L_EOF) {
176       Emsg0(M_ABORT, 0, "get_char: called after EOF\n");
177    }
178    if (lf->ch == L_EOL) {
179       if (bfgets(lf->line, MAXSTRING, lf->fd) == NULL) {
180          lf->ch = L_EOF;
181          if (lf->next) {
182             lex_close_file(lf);
183          }
184          return lf->ch;
185       }
186       lf->line_no++;
187       lf->col_no = 0;
188    }
189    lf->ch = lf->line[lf->col_no];
190    if (lf->ch == 0) {
191       lf->ch = L_EOL;
192    } else {
193       lf->col_no++;
194    }
195    Dmsg2(2000, "lex_get_char: %c %d\n", lf->ch, lf->ch);
196    return lf->ch;
197 }
198
199 void lex_unget_char(LEX *lf)
200 {
201    lf->col_no--;      
202    if (lf->ch == L_EOL)
203       lf->ch = 0;
204 }
205
206
207 /*
208  * Add a character to the current string
209  */
210 static void add_str(LEX *lf, int ch)
211 {
212    if (lf->str_len >= MAXSTRING-3) {
213       Emsg3(M_ERROR_TERM, 0, _(
214            _("Config token too long, file: %s, line %d, begins at line %d\n")), 
215              lf->fname, lf->line_no, lf->begin_line_no);
216    }
217    lf->str[lf->str_len++] = ch;
218    lf->str[lf->str_len] = 0;
219 }
220
221 /*
222  * Begin the string
223  */
224 static void begin_str(LEX *lf, int ch)  
225 {
226    lf->str_len = 0;
227    lf->str[0] = 0;
228    if (ch != 0) {
229       add_str(lf, ch);
230    }
231    lf->begin_line_no = lf->line_no;   /* save start string line no */
232 }
233
234 #ifdef DEBUG
235 static const char *lex_state_to_str(int state)
236 {
237    switch (state) {
238    case lex_none:          return "none";
239    case lex_comment:       return "comment";
240    case lex_number:        return "number";
241    case lex_ip_addr:       return "ip_addr";
242    case lex_identifier:    return "identifier";
243    case lex_string:        return "string";
244    case lex_quoted_string: return "quoted_string";
245    default:                return "??????";
246    }
247 }
248 #endif
249
250 /*
251  * Convert a lex token to a string
252  * used for debug/error printing.
253  */
254 const char *lex_tok_to_str(int token)
255 {
256    switch(token) {
257    case L_EOF:             return "L_EOF";
258    case L_EOL:             return "L_EOL";
259    case T_NONE:            return "T_NONE";
260    case T_NUMBER:          return "T_NUMBER";
261    case T_IPADDR:          return "T_IPADDR";
262    case T_IDENTIFIER:      return "T_IDENTIFIER";
263    case T_UNQUOTED_STRING: return "T_UNQUOTED_STRING";
264    case T_QUOTED_STRING:   return "T_QUOTED_STRING";
265    case T_BOB:             return "T_BOB";
266    case T_EOB:             return "T_EOB";
267    case T_EQUALS:          return "T_EQUALS";
268    case T_ERROR:           return "T_ERROR";
269    case T_EOF:             return "T_EOF";
270    case T_COMMA:           return "T_COMMA";
271    case T_EOL:             return "T_EOL";
272    default:                return "??????";
273    }
274 }
275
276 static uint32_t scan_pint(LEX *lf, char *str)
277 {
278    int64_t val = 0;
279    if (!is_a_number(str)) {
280       scan_err1(lf, _("expected a positive integer number, got: %s"), str);
281       /* NOT REACHED */
282    } else {
283       errno = 0;
284       val = str_to_int64(str);
285       if (errno != 0 || val < 0) {
286          scan_err1(lf, _("expected a postive integer number, got: %s"), str);
287          /* NOT REACHED */
288       }
289    }
290    return (uint32_t)val;
291 }
292
293 /*        
294  * 
295  * Get the next token from the input
296  *
297  */
298 int
299 lex_get_token(LEX *lf, int expect)
300 {
301    int ch;
302    int token = T_NONE;
303    bool esc_next = false;
304
305    Dmsg0(2000, "enter lex_get_token\n");
306    while (token == T_NONE) {
307       ch = lex_get_char(lf);
308       switch (lf->state) {
309       case lex_none:
310          Dmsg2(2000, "Lex state lex_none ch=%d,%x\n", ch, ch);
311          if (B_ISSPACE(ch))  
312             break;
313          if (B_ISALPHA(ch)) {
314             if (lf->options & LOPT_NO_IDENT)
315                lf->state = lex_string;
316             else
317                lf->state = lex_identifier;
318             begin_str(lf, ch);
319             break;
320          }
321          if (B_ISDIGIT(ch)) {
322             lf->state = lex_number;
323             begin_str(lf, ch);
324             break;
325          }
326          Dmsg0(2000, "Enter lex_none switch\n");
327          switch (ch) {
328          case L_EOF:
329             token = T_EOF;
330             Dmsg0(2000, "got L_EOF set token=T_EOF\n");
331             break;
332          case '#':
333             lf->state = lex_comment;
334             break;
335          case '{':
336             token = T_BOB;
337             begin_str(lf, ch);
338             break;
339          case '}':
340             token = T_EOB;
341             begin_str(lf, ch);
342             break;
343          case '"':
344             lf->state = lex_quoted_string;
345             begin_str(lf, 0);
346             break;
347          case '=': 
348             token = T_EQUALS;
349             begin_str(lf, ch);
350             break;
351          case ',':
352             token = T_COMMA;
353             begin_str(lf, ch);
354             break;
355          case ';':
356             if (expect != T_SKIP_EOL) {
357                token = T_EOL;      /* treat ; like EOL */
358             }
359             break;
360          case L_EOL:
361             Dmsg0(2000, "got L_EOL set token=T_EOL\n");
362             if (expect != T_SKIP_EOL) {
363                token = T_EOL;
364             }
365             break;
366          case '@':
367             lf->state = lex_include;
368             begin_str(lf, 0);
369             break;
370          default:
371             lf->state = lex_string;
372             begin_str(lf, ch);
373             break;
374          }
375          break;
376       case lex_comment:
377          Dmsg1(2000, "Lex state lex_comment ch=%x\n", ch);
378          if (ch == L_EOL) {
379             lf->state = lex_none;
380             if (expect != T_SKIP_EOL) {
381                token = T_EOL;
382             }
383          } else if (ch == L_EOF) {
384             token = T_ERROR;
385          }
386          break;
387       case lex_number:
388          Dmsg2(2000, "Lex state lex_number ch=%x %c\n", ch, ch);
389          if (ch == L_EOF) {
390             token = T_ERROR;
391             break;
392          }
393          /* Might want to allow trailing specifications here */
394          if (B_ISDIGIT(ch)) {
395             add_str(lf, ch);
396             break;
397          }
398
399          /* A valid number can be terminated by the following */
400          if (B_ISSPACE(ch) || ch == L_EOL || ch == ',' || ch == ';') {
401             token = T_NUMBER;
402             lf->state = lex_none;
403          } else {
404             lf->state = lex_string;
405          }
406          lex_unget_char(lf);
407          break;
408       case lex_ip_addr:
409          if (ch == L_EOF) {
410             token = T_ERROR;
411             break;
412          }
413          Dmsg1(2000, "Lex state lex_ip_addr ch=%x\n", ch);
414          break;
415       case lex_string:
416          Dmsg1(2000, "Lex state lex_string ch=%x\n", ch);
417          if (ch == L_EOF) {
418             token = T_ERROR;
419             break;
420          }
421          if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
422              ch == '\r' || ch == ';' || ch == ',' || ch == '#' || (B_ISSPACE(ch)) ) {
423             lex_unget_char(lf);    
424             token = T_UNQUOTED_STRING;
425             lf->state = lex_none;
426             break;
427          } 
428          add_str(lf, ch);
429          break;
430       case lex_identifier:
431          Dmsg2(2000, "Lex state lex_identifier ch=%x %c\n", ch, ch);
432          if (B_ISALPHA(ch)) {
433             add_str(lf, ch);
434             break;
435          } else if (B_ISSPACE(ch)) {
436             break;
437          } else if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
438                     ch == '\r' || ch == ';' || ch == ','   || ch == '"' || ch == '#') {
439             lex_unget_char(lf);    
440             token = T_IDENTIFIER;
441             lf->state = lex_none;
442             break;
443          } else if (ch == L_EOF) {
444             token = T_ERROR;
445             lf->state = lex_none;
446             begin_str(lf, ch);
447             break;
448          }
449          /* Some non-alpha character => string */
450          lf->state = lex_string;
451          add_str(lf, ch);
452          break;
453       case lex_quoted_string:
454          Dmsg2(2000, "Lex state lex_quoted_string ch=%x %c\n", ch, ch);
455          if (ch == L_EOF) {
456             token = T_ERROR;
457             break;
458          }
459          if (ch == L_EOL) {
460             esc_next = false;
461             break;
462          }
463          if (esc_next) {
464             add_str(lf, ch);
465             esc_next = false;
466             break;
467          }
468          if (ch == '\\') {
469             esc_next = true;
470             break;
471          }
472          if (ch == '"') {
473             token = T_QUOTED_STRING;
474             lf->state = lex_none;
475             break;
476          }
477          add_str(lf, ch);
478          break;
479       case lex_include:            /* scanning a filename */
480          if (ch == L_EOF) {
481             token = T_ERROR;
482             break;
483          }
484          if (B_ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' ||
485              ch == ';' || ch == ','   || ch == '"' || ch == '#') {
486             lf->state = lex_none;
487             lf = lex_open_file(lf, lf->str, NULL);
488        if (lf == NULL) {
489          return T_ERROR;
490        }
491             break;
492          }
493          add_str(lf, ch);
494          break;
495       }
496       Dmsg4(2000, "ch=%d state=%s token=%s %c\n", ch, lex_state_to_str(lf->state),
497         lex_tok_to_str(token), ch);
498    }
499    Dmsg2(2000, "lex returning: line %d token: %s\n", lf->line_no, lex_tok_to_str(token));
500    lf->token = token;
501
502    /* 
503     * Here is where we check to see if the user has set certain 
504     *  expectations (e.g. 32 bit integer). If so, we do type checking
505     *  and possible additional scanning (e.g. for range).
506     */
507    switch (expect) {
508    case T_PINT32:
509       lf->pint32_val = scan_pint(lf, lf->str);
510       lf->pint32_val2 = lf->pint32_val;
511       token = T_PINT32;
512       break;
513
514    case T_PINT32_RANGE:
515       if (token == T_NUMBER) {
516          lf->pint32_val = scan_pint(lf, lf->str);
517          lf->pint32_val2 = lf->pint32_val;
518          token = T_PINT32;
519       } else {
520          char *p = strchr(lf->str, '-');
521          if (!p) {
522             scan_err2(lf, _("expected an integer or a range, got %s: %s"), 
523                lex_tok_to_str(token), lf->str);
524             token = T_ERROR;
525             break;
526          }
527          *p++ = 0;                       /* terminate first half of range */
528          lf->pint32_val  = scan_pint(lf, lf->str);
529          lf->pint32_val2 = scan_pint(lf, p);
530          token = T_PINT32_RANGE;
531       }
532       break;
533
534    case T_INT32:
535       if (token != T_NUMBER || !is_a_number(lf->str)) {
536          scan_err2(lf, _("expected an integer number, got %s: %s"),
537                lex_tok_to_str(token), lf->str);
538          token = T_ERROR;
539          break;
540       }
541       errno = 0;
542       lf->int32_val = (int32_t)str_to_int64(lf->str);
543       if (errno != 0) {
544          scan_err2(lf, _("expected an integer number, got %s: %s"),
545                lex_tok_to_str(token), lf->str);
546          token = T_ERROR;
547       } else {
548          token = T_INT32;
549       }
550       break;
551
552    case T_INT64:
553       Dmsg2(2000, "int64=:%s: %f\n", lf->str, strtod(lf->str, NULL)); 
554       if (token != T_NUMBER || !is_a_number(lf->str)) {
555          scan_err2(lf, _("expected an integer number, got %s: %s"),
556                lex_tok_to_str(token), lf->str);
557          token = T_ERROR;
558          break;
559       }
560       errno = 0;
561       lf->int64_val = str_to_int64(lf->str);
562       if (errno != 0) {
563          scan_err2(lf, _("expected an integer number, got %s: %s"),
564                lex_tok_to_str(token), lf->str);
565          token = T_ERROR;
566       } else {
567          token = T_INT64;
568       }
569       break;
570
571    case T_NAME:
572       if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
573          scan_err2(lf, _("expected a name, got %s: %s"),
574                lex_tok_to_str(token), lf->str);
575          token = T_ERROR;
576       } else if (lf->str_len > MAX_RES_NAME_LENGTH) {
577          scan_err3(lf, _("name %s length %d too long, max is %d\n"), lf->str, 
578             lf->str_len, MAX_RES_NAME_LENGTH);
579          token = T_ERROR;
580       }
581       break;
582
583    case T_STRING:
584       if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
585          scan_err2(lf, _("expected a string, got %s: %s"),
586                lex_tok_to_str(token), lf->str);
587          token = T_ERROR;
588       } else {
589          token = T_STRING;
590       }
591       break;
592
593
594    default:
595       break;                          /* no expectation given */
596    }
597    lf->token = token;                 /* set possible new token */
598    return token;
599 }