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