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