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