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