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