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