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