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