]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/lex.c
Fix over-zellous restriction of chars in Names
[bacula/bacula] / bacula / src / lib / lex.c
1 /*
2  * Lexical scanner for Bacula configuration file
3  *
4  *   Version $Id$
5  *
6  */
7
8 /*
9    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
10
11    This program is free software; you can redistribute it and/or
12    modify it under the terms of the GNU General Public License as
13    published by the Free Software Foundation; either version 2 of
14    the License, or (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19    General Public License for more details.
20
21    You should have received a copy of the GNU General Public
22    License along with this program; if not, write to the Free
23    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24    MA 02111-1307, USA.
25
26  */
27
28 #include "bacula.h"
29 #include "lex.h"
30
31 extern int debug_level;
32
33 /*
34  * Scan to "logical" end of line. I.e. end of line,
35  * or semicolon.
36  */
37 void scan_to_eol(LEX *lc)
38 {
39    Dmsg0(900, "start scan to eof\n");
40    while (lex_get_token(lc, T_ALL) != T_EOL)
41       { }
42    Dmsg0(900, "done scan to eof\n");
43 }
44
45    
46 /*
47  * Format a scanner error message 
48  */
49 static void s_err(char *file, int line, LEX *lc, char *msg, ...)
50 {
51    va_list arg_ptr;
52    char buf[MAXSTRING];
53    char more[MAXSTRING];
54
55    va_start(arg_ptr, msg);
56    bvsnprintf(buf, sizeof(buf), msg, arg_ptr);
57    va_end(arg_ptr);
58      
59    if (lc->line_no > lc->begin_line_no) {
60       sprintf(more, _("Problem probably begins at Line %d.\n"), lc->begin_line_no);
61    } else {
62       more[0] = 0;
63    }
64    e_msg(file, line, M_ERROR_TERM, 0, _("Config error: %s\n\
65             : Line %d, col %d of file %s\n%s\n%s"),
66       buf, lc->line_no, lc->col_no, lc->fname, lc->line, more);
67 }
68
69
70 /*
71  * Free the current file, and retrieve the contents
72  * of the previous packet if any.
73  */
74 LEX *lex_close_file(LEX *lf)
75 {
76    LEX *of;
77
78    Dmsg1(900, "Close lex file: %s\n", lf->fname);
79    if (lf == NULL) {
80       Emsg0(M_ABORT, 0, "Close of NULL file\n");
81    }
82    of = lf->next;
83    fclose(lf->fd);
84    Dmsg1(900, "Close cfg file %s\n", lf->fname);
85    free(lf->fname);
86    if (of) {
87       of->options = lf->options;      /* preserve options */
88       memcpy(lf, of, sizeof(LEX));
89       Dmsg1(900, "Restart scan of cfg file %s\n", of->fname);
90    } else {
91       of = lf;
92       lf = NULL;
93    }
94    free(of);
95    return lf;
96 }
97
98 /*     
99  * Open a new configuration file. We push the
100  * state of the current file (lf) so that we
101  * can do includes.  This is a bit of a hammer.
102  * Instead of passing back the pointer to the
103  * new packet, I simply replace the contents
104  * of the caller's packet with the new packet,
105  * and link the contents of the old packet into
106  * the next field.
107  *
108  */
109 LEX *lex_open_file(LEX *lf, char *filename, LEX_ERROR_HANDLER *scan_error) 
110               
111 {
112    LEX *nf;
113    FILE *fd;
114    char *fname = bstrdup(filename);
115
116    
117    if ((fd = fopen(fname, "r")) == NULL) {
118       Emsg2(M_ERROR_TERM, 0, _("Cannot open config file %s: %s\n"), 
119             fname, strerror(errno));
120    }
121    Dmsg1(900, "Open config file: %s\n", fname);
122    nf = (LEX *)malloc(sizeof(LEX));
123    if (lf) {     
124       memcpy(nf, lf, sizeof(LEX));
125       memset(lf, 0, sizeof(LEX));
126       lf->next = nf;                  /* if have lf, push it behind new one */
127       lf->options = nf->options;      /* preserve user options */
128    } else {
129       lf = nf;                        /* start new packet */
130       memset(lf, 0, sizeof(LEX));
131    }
132    lf->fd = fd;
133    lf->fname = fname;
134    lf->state = lex_none;
135    lf->ch = L_EOL;
136    if (scan_error) {
137       lf->scan_error = scan_error;
138    } else {
139       lf->scan_error = s_err;
140    }
141    Dmsg1(900, "Return lex=%x\n", lf);
142    return lf;
143 }
144
145 /*    
146  * Get the next character from the input.
147  *  Returns the character or
148  *    L_EOF if end of file
149  *    L_EOL if end of line
150  */
151 int lex_get_char(LEX *lf)
152 {
153    if (lf->ch == L_EOF) {
154       Emsg0(M_ABORT, 0, "get_char: called after EOF\n");
155    }
156    if (lf->ch == L_EOL) {
157       if (fgets(lf->line, MAXSTRING, lf->fd) == NULL) {
158          lf->ch = L_EOF;
159          if (lf->next) {
160             lex_close_file(lf);
161          }
162          return lf->ch;
163       }
164       lf->line_no++;
165       lf->col_no = 0;
166    }
167    lf->ch = lf->line[lf->col_no];
168    if (lf->ch == 0) {
169       lf->ch = L_EOL;
170    } else {
171       lf->col_no++;
172    }
173    Dmsg2(900, "lex_get_char: %c %d\n", lf->ch, lf->ch);
174    return lf->ch;
175 }
176
177 void lex_unget_char(LEX *lf)
178 {
179    lf->col_no--;      
180    if (lf->ch == L_EOL)
181       lf->ch = 0;
182 }
183
184
185 /*
186  * Add a character to the current string
187  */
188 static void add_str(LEX *lf, int ch)
189 {
190    if (lf->str_len >= MAXSTRING-3) {
191       Emsg3(M_ERROR_TERM, 0, _(
192             "Token too long, file: %s, line %d, begins at line %d\n"), 
193              lf->fname, lf->line_no, lf->begin_line_no);
194    }
195    lf->str[lf->str_len++] = ch;
196    lf->str[lf->str_len] = 0;
197 }
198
199 /*
200  * Begin the string
201  */
202 static void begin_str(LEX *lf, int ch)  
203 {
204    lf->str_len = 0;
205    lf->str[0] = 0;
206    if (ch != 0) {
207       add_str(lf, ch);
208    }
209    lf->begin_line_no = lf->line_no;   /* save start string line no */
210 }
211
212 #ifdef DEBUG
213 static char *lex_state_to_str(int state)
214 {
215    switch (state) {
216    case lex_none:          return "none";
217    case lex_comment:       return "comment";
218    case lex_number:        return "number";
219    case lex_ip_addr:       return "ip_addr";
220    case lex_identifier:    return "identifier";
221    case lex_string:        return "string";
222    case lex_quoted_string: return "quoted_string";
223    default:                return "??????";
224    }
225 }
226 #endif
227
228 /*
229  * Convert a lex token to a string
230  * used for debug/error printing.
231  */
232 char *lex_tok_to_str(int token)
233 {
234    switch(token) {
235    case L_EOF:             return "L_EOF";
236    case L_EOL:             return "L_EOL";
237    case T_NONE:            return "T_NONE";
238    case T_NUMBER:          return "T_NUMBER";
239    case T_IPADDR:          return "T_IPADDR";
240    case T_IDENTIFIER:      return "T_IDENTIFIER";
241    case T_UNQUOTED_STRING: return "T_UNQUOTED_STRING";
242    case T_QUOTED_STRING:   return "T_QUOTED_STRING";
243    case T_BOB:             return "T_BOB";
244    case T_EOB:             return "T_EOB";
245    case T_EQUALS:          return "T_EQUALS";
246    case T_ERROR:           return "T_ERROR";
247    case T_EOF:             return "T_EOF";
248    case T_COMMA:           return "T_COMMA";
249    case T_EOL:             return "T_EOL";
250    default:                return "??????";
251    }
252 }
253
254 static uint32_t scan_pint(LEX *lf, char *str)
255 {
256    double dval = 0;
257    if (!is_a_number(str)) {
258       scan_err1(lf, "expected a positive integer number, got: %s", str);
259       /* NOT REACHED */
260    } else {
261       errno = 0;
262       dval = strtod(str, NULL);
263       if (errno != 0 || dval < 0) {
264          scan_err1(lf, "expected a postive integer number, got: %s", str);
265          /* NOT REACHED */
266       }
267    }
268    return (uint32_t)dval;
269 }
270
271 /*        
272  * 
273  * Get the next token from the input
274  *
275  */
276 int
277 lex_get_token(LEX *lf, int expect)
278 {
279    int ch;
280    int token = T_NONE;
281    int esc_next = FALSE;
282
283    Dmsg0(900, "enter lex_get_token\n");
284    while (token == T_NONE) {
285       ch = lex_get_char(lf);
286       switch (lf->state) {
287       case lex_none:
288          Dmsg2(900, "Lex state lex_none ch=%d,%x\n", ch, ch);
289          if (B_ISSPACE(ch))  
290             break;
291          if (B_ISALPHA(ch)) {
292             if (lf->options & LOPT_NO_IDENT)
293                lf->state = lex_string;
294             else
295                lf->state = lex_identifier;
296             begin_str(lf, ch);
297             break;
298          }
299          if (B_ISDIGIT(ch)) {
300             lf->state = lex_number;
301             begin_str(lf, ch);
302             break;
303          }
304          Dmsg0(900, "Enter lex_none switch\n");
305          switch (ch) {
306          case L_EOF:
307             token = T_EOF;
308             Dmsg0(900, "got L_EOF set token=T_EOF\n");
309             break;
310          case '#':
311             lf->state = lex_comment;
312             break;
313          case '{':
314             token = T_BOB;
315             begin_str(lf, ch);
316             break;
317          case '}':
318             token = T_EOB;
319             begin_str(lf, ch);
320             break;
321          case '"':
322             lf->state = lex_quoted_string;
323             begin_str(lf, 0);
324             break;
325          case '=': 
326             token = T_EQUALS;
327             begin_str(lf, ch);
328             break;
329          case ',':
330             token = T_COMMA;
331             begin_str(lf, ch);
332             break;
333          case ';':
334             token = T_EOL;      /* treat ; like EOL */
335             break;
336          case L_EOL:
337             Dmsg0(900, "got L_EOL set token=T_EOL\n");
338             token = T_EOL;
339             break;
340          case '@':
341             lf->state = lex_include;
342             begin_str(lf, 0);
343             break;
344          default:
345             lf->state = lex_string;
346             begin_str(lf, ch);
347             break;
348          }
349          break;
350       case lex_comment:
351          Dmsg1(900, "Lex state lex_comment ch=%x\n", ch);
352          if (ch == L_EOL) {
353             lf->state = lex_none;
354             token = T_EOL;
355          } else if (ch == L_EOF) {
356             token = T_ERROR;
357          }
358          break;
359       case lex_number:
360          Dmsg2(900, "Lex state lex_number ch=%x %c\n", ch, ch);
361          if (ch == L_EOF) {
362             token = T_ERROR;
363             break;
364          }
365          /* Might want to allow trailing specifications here */
366          if (B_ISDIGIT(ch)) {
367             add_str(lf, ch);
368             break;
369          }
370
371          /* A valid number can be terminated by the following */
372          if (B_ISSPACE(ch) || ch == L_EOL || ch == ',' || ch == ';') {
373             token = T_NUMBER;
374             lf->state = lex_none;
375          } else {
376             lf->state = lex_string;
377          }
378          lex_unget_char(lf);
379          break;
380       case lex_ip_addr:
381          if (ch == L_EOF) {
382             token = T_ERROR;
383             break;
384          }
385          Dmsg1(900, "Lex state lex_ip_addr ch=%x\n", ch);
386          break;
387       case lex_string:
388          Dmsg1(900, "Lex state lex_string ch=%x\n", ch);
389          if (ch == L_EOF) {
390             token = T_ERROR;
391             break;
392          }
393          if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
394              ch == '\r' || ch == ';' || ch == ',' || ch == '#' || (B_ISSPACE(ch)) ) {
395             lex_unget_char(lf);    
396             token = T_UNQUOTED_STRING;
397             lf->state = lex_none;
398             break;
399          } 
400          add_str(lf, ch);
401          break;
402       case lex_identifier:
403          Dmsg2(900, "Lex state lex_identifier ch=%x %c\n", ch, ch);
404          if (B_ISALPHA(ch)) {
405             add_str(lf, ch);
406             break;
407          } else if (B_ISSPACE(ch)) {
408             break;
409          } else if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
410                     ch == '\r' || ch == ';' || ch == ','   || ch == '"' || ch == '#') {
411             lex_unget_char(lf);    
412             token = T_IDENTIFIER;
413             lf->state = lex_none;
414             break;
415          } else if (ch == L_EOF) {
416             token = T_ERROR;
417             lf->state = lex_none;
418             begin_str(lf, ch);
419             break;
420          }
421          /* Some non-alpha character => string */
422          lf->state = lex_string;
423          add_str(lf, ch);
424          break;
425       case lex_quoted_string:
426          Dmsg2(900, "Lex state lex_quoted_string ch=%x %c\n", ch, ch);
427          if (ch == L_EOF) {
428             token = T_ERROR;
429             break;
430          }
431          if (ch == L_EOL) {
432             esc_next = FALSE;
433             break;
434          }
435          if (esc_next) {
436             add_str(lf, ch);
437             esc_next = FALSE;
438             break;
439          }
440          if (ch == '\\') {
441             esc_next = TRUE;
442             break;
443          }
444          if (ch == '"') {
445             token = T_QUOTED_STRING;
446             lf->state = lex_none;
447             break;
448          }
449          add_str(lf, ch);
450          break;
451       case lex_include:            /* scanning a filename */
452          if (ch == L_EOF) {
453             token = T_ERROR;
454             break;
455          }
456          if (B_ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' ||
457              ch == ';' || ch == ','   || ch == '"' || ch == '#') {
458             lf->state = lex_none;
459             lf = lex_open_file(lf, lf->str, NULL);
460             break;
461          }
462          add_str(lf, ch);
463          break;
464       }
465       Dmsg4(900, "ch=%d state=%s token=%s %c\n", ch, lex_state_to_str(lf->state),
466         lex_tok_to_str(token), ch);
467    }
468    Dmsg2(900, "lex returning: line %d token: %s\n", lf->line_no, lex_tok_to_str(token));
469    lf->token = token;
470
471    /* 
472     * Here is where we check to see if the user has set certain 
473     *  expectations (e.g. 32 bit integer). If so, we do type checking
474     *  and possible additional scanning (e.g. for range).
475     */
476    switch (expect) {
477    case T_PINT32:
478       lf->pint32_val = scan_pint(lf, lf->str);
479       lf->pint32_val2 = lf->pint32_val;
480       token = T_PINT32;
481       break;
482
483    case T_PINT32_RANGE:
484       if (token == T_NUMBER) {
485          lf->pint32_val = scan_pint(lf, lf->str);
486          lf->pint32_val2 = lf->pint32_val;
487          token = T_PINT32;
488       } else {
489          char *p = strchr(lf->str, '-');
490          if (!p) {
491             scan_err2(lf, "expected an integer or a range, got %s: %s", 
492                lex_tok_to_str(token), lf->str);
493             token = T_ERROR;
494             break;
495          }
496          *p++ = 0;                       /* terminate first half of range */
497          lf->pint32_val  = scan_pint(lf, lf->str);
498          lf->pint32_val2 = scan_pint(lf, p);
499          token = T_PINT32_RANGE;
500       }
501       break;
502
503    case T_INT32:
504       if (token != T_NUMBER || !is_a_number(lf->str)) {
505          scan_err2(lf, "expected an integer number, got %s: %s",
506                lex_tok_to_str(token), lf->str);
507          token = T_ERROR;
508          break;
509       }
510       errno = 0;
511       lf->int32_val = (int32_t)strtod(lf->str, NULL);
512       if (errno != 0) {
513          scan_err2(lf, "expected an integer number, got %s: %s",
514                lex_tok_to_str(token), lf->str);
515          token = T_ERROR;
516       } else {
517          token = T_INT32;
518       }
519       break;
520
521    case T_INT64:
522       Dmsg2(900, "int64=:%s: %f\n", lf->str, strtod(lf->str, NULL)); 
523       if (token != T_NUMBER || !is_a_number(lf->str)) {
524          scan_err2(lf, "expected an integer number, got %s: %s",
525                lex_tok_to_str(token), lf->str);
526          token = T_ERROR;
527          break;
528       }
529       errno = 0;
530       lf->int64_val = (int64_t)strtod(lf->str, NULL);
531       if (errno != 0) {
532          scan_err2(lf, "expected an integer number, got %s: %s",
533                lex_tok_to_str(token), lf->str);
534          token = T_ERROR;
535       } else {
536          token = T_INT64;
537       }
538       break;
539
540    case T_NAME:
541       if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
542          scan_err2(lf, "expected a name, got %s: %s",
543                lex_tok_to_str(token), lf->str);
544          token = T_ERROR;
545       } else if (lf->str_len > MAX_RES_NAME_LENGTH) {
546          scan_err3(lf, "name %s length %d too long, max is %d\n", lf->str, 
547             lf->str_len, MAX_RES_NAME_LENGTH);
548          token = T_ERROR;
549       }
550       break;
551
552    case T_STRING:
553       if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
554          scan_err2(lf, "expected a string, got %s: %s",
555                lex_tok_to_str(token), lf->str);
556          token = T_ERROR;
557       } else {
558          token = T_STRING;
559       }
560       break;
561
562
563    default:
564       break;                          /* no expectation given */
565    }
566    lf->token = token;                 /* set possible new token */
567    return token;
568 }