]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/lex.c
Initial Irix support
[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(20, "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(29, "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(29, "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(29, "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(29, "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;
251    if (!is_a_number(str)) {
252       scan_err1(lf, "expected a positive integer number, got: %s", str);
253    } else {
254       errno = 0;
255       dval = strtod(str, NULL);
256       if (errno != 0 || dval < 0) {
257          scan_err1(lf, "expected a postive integer number, got: %s", str);
258       }
259    }
260    return (uint32_t)dval;
261 }
262
263 /*        
264  * 
265  * Get the next token from the input
266  *
267  */
268 int
269 lex_get_token(LEX *lf, int expect)
270 {
271    int ch;
272    int token = T_NONE;
273    int esc_next = FALSE;
274
275    Dmsg0(290, "enter lex_get_token\n");
276    while (token == T_NONE) {
277       ch = lex_get_char(lf);
278       switch (lf->state) {
279          case lex_none:
280             Dmsg2(290, "Lex state lex_none ch=%d,%x\n", ch, ch);
281             if (ISSPACE(ch))  
282                break;
283             if (ISALPHA(ch)) {
284                if (lf->options & LOPT_NO_IDENT)
285                   lf->state = lex_string;
286                else
287                   lf->state = lex_identifier;
288                begin_str(lf, ch);
289                break;
290             }
291             if (ISDIGIT(ch)) {
292                lf->state = lex_number;
293                begin_str(lf, ch);
294                break;
295             }
296             Dmsg0(290, "Enter lex_none switch\n");
297             switch (ch) {
298                case L_EOF:
299                   token = T_EOF;
300                   Dmsg0(290, "got L_EOF set token=T_EOF\n");
301                   break;
302                case '#':
303                   lf->state = lex_comment;
304                   break;
305                case '{':
306                   token = T_BOB;
307                   begin_str(lf, ch);
308                   break;
309                case '}':
310                   token = T_EOB;
311                   begin_str(lf, ch);
312                   break;
313                case '"':
314                   lf->state = lex_quoted_string;
315                   begin_str(lf, 0);
316                   break;
317                case '=': 
318                   token = T_EQUALS;
319                   begin_str(lf, ch);
320                   break;
321                case ',':
322                   token = T_COMMA;
323                   begin_str(lf, ch);
324                   break;
325                case ';':
326                   token = T_EOL;      /* treat ; like EOL */
327                   break;
328                case L_EOL:
329                   Dmsg0(290, "got L_EOL set token=T_EOL\n");
330                   token = T_EOL;
331                   break;
332                case '@':
333                   lf->state = lex_include;
334                   begin_str(lf, 0);
335                   break;
336                default:
337                   lf->state = lex_string;
338                   begin_str(lf, ch);
339                   break;
340             }
341             break;
342          case lex_comment:
343             Dmsg1(290, "Lex state lex_comment ch=%x\n", ch);
344             if (ch == L_EOL) {
345                lf->state = lex_none;
346                token = T_EOL;
347             }
348             break;
349          case lex_number:
350             Dmsg2(290, "Lex state lex_number ch=%x %c\n", ch, ch);
351             /* Might want to allow trailing specifications here */
352             if (ISDIGIT(ch)) {
353                add_str(lf, ch);
354                break;
355             }
356
357             /* A valid number can be terminated by the following */
358             if (ISSPACE(ch) || ch == L_EOL || ch == ',' || ch == ';') {
359                token = T_NUMBER;
360                lf->state = lex_none;
361             } else {
362                lf->state = lex_string;
363             }
364             lex_unget_char(lf);
365             break;
366          case lex_ip_addr:
367             Dmsg1(290, "Lex state lex_ip_addr ch=%x\n", ch);
368             break;
369          case lex_string:
370             Dmsg1(290, "Lex state lex_string ch=%x\n", ch);
371             if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
372                 ch == ';' || ch == ',' || ch == '#' || (ISSPACE(ch)) ) {
373                lex_unget_char(lf);    
374                token = T_UNQUOTED_STRING;
375                lf->state = lex_none;
376                break;
377             } 
378             add_str(lf, ch);
379             break;
380          case lex_identifier:
381             Dmsg2(290, "Lex state lex_identifier ch=%x %c\n", ch, ch);
382             if (ISALPHA(ch)) {
383                add_str(lf, ch);
384                break;
385             } else if (ISSPACE(ch)) {
386                break;
387             } else if (ch == '\n' || ch == L_EOL || ch == '=' || ch == '}' || ch == '{' ||
388                        ch == ';' || ch == ','   || ch == '"' || ch == '#') {
389                lex_unget_char(lf);    
390                token = T_IDENTIFIER;
391                lf->state = lex_none;
392                break;
393             } else if (ch == L_EOF) {
394                token = T_ERROR;
395                lf->state = lex_none;
396                begin_str(lf, ch);
397                break;
398             }
399             /* Some non-alpha character => string */
400             lf->state = lex_string;
401             add_str(lf, ch);
402             break;
403          case lex_quoted_string:
404             Dmsg2(290, "Lex state lex_quoted_string ch=%x %c\n", ch, ch);
405             if (ch == L_EOL) {
406                esc_next = FALSE;
407                break;
408             }
409             if (esc_next) {
410                add_str(lf, ch);
411                esc_next = FALSE;
412                break;
413             }
414             if (ch == '\\') {
415                esc_next = TRUE;
416                break;
417             }
418             if (ch == '"') {
419                token = T_QUOTED_STRING;
420                lf->state = lex_none;
421                break;
422             }
423             add_str(lf, ch);
424             break;
425          case lex_include:            /* scanning a filename */
426             if (ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' ||
427                 ch == ';' || ch == ','   || ch == '"' || ch == '#') {
428                lf->state = lex_none;
429                lf = lex_open_file(lf, lf->str, NULL);
430                break;
431             }
432             add_str(lf, ch);
433             break;
434       }
435       Dmsg4(290, "ch=%d state=%s token=%s %c\n", ch, lex_state_to_str(lf->state),
436         lex_tok_to_str(token), ch);
437    }
438    Dmsg2(290, "lex returning: line %d token: %s\n", lf->line_no, lex_tok_to_str(token));
439    lf->token = token;
440
441    /* 
442     * Here is where we check to see if the user has set certain 
443     *  expectations (e.g. 32 bit integer). If so, we do type checking
444     *  and possible additional scanning (e.g. for range).
445     */
446    switch (expect) {
447    case T_PINT32:
448       lf->pint32_val = scan_pint(lf, lf->str);
449       lf->pint32_val2 = lf->pint32_val;
450       token = T_PINT32;
451       break;
452
453    case T_PINT32_RANGE:
454       if (token == T_NUMBER) {
455          lf->pint32_val = scan_pint(lf, lf->str);
456          lf->pint32_val2 = lf->pint32_val;
457          token = T_PINT32;
458       } else {
459          char *p = strchr(lf->str, '-');
460          if (!p) {
461             scan_err2(lf, "expected an integer or a range, got %s: %s", 
462                lex_tok_to_str(token), lf->str);
463             token = T_ERROR;
464             break;
465          }
466          *p++ = 0;                       /* terminate first half of range */
467          lf->pint32_val  = scan_pint(lf, lf->str);
468          lf->pint32_val2 = scan_pint(lf, p);
469          token = T_PINT32_RANGE;
470       }
471       break;
472
473    case T_INT32:
474       if (token != T_NUMBER || !is_a_number(lf->str)) {
475          scan_err2(lf, "expected an integer number, got %s: %s",
476                lex_tok_to_str(token), lf->str);
477          token = T_ERROR;
478          break;
479       }
480       errno = 0;
481       lf->int32_val = (int32_t)strtod(lf->str, NULL);
482       if (errno != 0) {
483          scan_err2(lf, "expected an integer number, got %s: %s",
484                lex_tok_to_str(token), lf->str);
485          token = T_ERROR;
486       } else {
487          token = T_INT32;
488       }
489       break;
490
491    case T_INT64:
492       Dmsg2(400, "int64=:%s: %f\n", lf->str, strtod(lf->str, NULL)); 
493       if (token != T_NUMBER || !is_a_number(lf->str)) {
494          scan_err2(lf, "expected an integer number, got %s: %s",
495                lex_tok_to_str(token), lf->str);
496          token = T_ERROR;
497          break;
498       }
499       errno = 0;
500       lf->int64_val = (int64_t)strtod(lf->str, NULL);
501       if (errno != 0) {
502          scan_err2(lf, "expected an integer number, got %s: %s",
503                lex_tok_to_str(token), lf->str);
504          token = T_ERROR;
505       } else {
506          token = T_INT64;
507       }
508       break;
509
510    case T_NAME:
511       if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
512          scan_err2(lf, "expected a name, got %s: %s",
513                lex_tok_to_str(token), lf->str);
514          token = T_ERROR;
515       } else if (lf->str_len > MAX_RES_NAME_LENGTH) {
516          scan_err3(lf, "name %s length %d too long, max is %d\n", lf->str, 
517             lf->str_len, MAX_RES_NAME_LENGTH);
518          token = T_ERROR;
519       } else {
520          token = T_NAME;
521       }
522       break;
523
524    case T_STRING:
525       if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
526          scan_err2(lf, "expected a name, got %s: %s",
527                lex_tok_to_str(token), lf->str);
528          token = T_ERROR;
529       } else {
530          token = T_STRING;
531       }
532       break;
533
534
535    default:
536       break;                          /* no expectation given */
537    }
538    lf->token = token;                 /* set possible new token */
539    return token;
540 }