]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/lex.c
Backport from Bacula Enterprise
[bacula/bacula] / bacula / src / lib / lex.c
index cd9d7c5daeeaa70e69c848a75d1c16991b5abd53..a337757742ba20b9548c1f0d5f31053fb4902d48 100644 (file)
@@ -1,29 +1,21 @@
 /*
-   Bacula® - The Network Backup Solution
-
-   Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
-
-   The main author of Bacula is Kern Sibbald, with contributions from
-   many others, a complete list can be found in the file AUTHORS.
-   This program is Free Software; you can redistribute it and/or
-   modify it under the terms of version three of the GNU Affero General Public
-   License as published by the Free Software Foundation and included
-   in the file LICENSE.
-
-   This program is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-   General Public License for more details.
-
-   You should have received a copy of the GNU Affero General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-   02110-1301, USA.
-
-   Bacula® is a registered trademark of Kern Sibbald.
-   The licensor of Bacula is the Free Software Foundation Europe
-   (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
-   Switzerland, email:ftf@fsfeurope.org.
+   Bacula(R) - The Network Backup Solution
+
+   Copyright (C) 2000-2015 Kern Sibbald
+   Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
+
+   The original author of Bacula is Kern Sibbald, with contributions
+   from many others, a complete list can be found in the file AUTHORS.
+
+   You may use this file and others of this release according to the
+   license defined in the LICENSE file, which includes the Affero General
+   Public License, v3.0 ("AGPLv3") and some additional permissions and
+   terms pursuant to its AGPLv3 Section 7.
+
+   This notice must be preserved when any source code is 
+   conveyed and/or propagated.
+
+   Bacula(R) is a registered trademark of Kern Sibbald.
 */
 /*
  * Lexical scanner for Bacula configuration file
@@ -35,8 +27,6 @@
 #include "bacula.h"
 #include "lex.h"
 
-extern int debug_level;
-
 /* Debug level for this source file */
 static const int dbglvl = 5000;
 
@@ -48,12 +38,15 @@ static const int dbglvl = 5000;
 void scan_to_eol(LEX *lc)
 {
    int token;
-   Dmsg0(dbglvl, "start scan to eof\n");
+   Dmsg0(dbglvl, "start scan to eol\n");
    while ((token = lex_get_token(lc, T_ALL)) != T_EOL) {
       if (token == T_EOB) {
          lex_unget_char(lc);
          return;
       }
+      if (token == T_EOF) {
+         return;
+      }
    }
 }
 
@@ -91,7 +84,7 @@ static void s_err(const char *file, int line, LEX *lc, const char *msg, ...)
                 _("Problem probably begins at line %d.\n"), lc->begin_line_no);
    } else {
       more[0] = 0;
-   }  
+   }
    if (lc->line_no > 0) {
       e_msg(file, line, lc->err_type, 0, _("Config error: %s\n"
 "            : line %d, col %d of file %s\n%s\n%s"),
@@ -134,13 +127,17 @@ LEX *lex_close_file(LEX *lf)
    if (lf->bpipe) {
       close_bpipe(lf->bpipe);
       lf->bpipe = NULL;
-   } else {
+   } else if (lf->fd) {
       fclose(lf->fd);
    }
    Dmsg1(dbglvl, "Close cfg file %s\n", lf->fname);
-   free(lf->fname);
+   if (lf->fname) {
+      free(lf->fname);
+   }
    free_memory(lf->line);
    lf->line = NULL;
+   free_memory(lf->str);
+   lf->str = NULL;
    if (of) {
       of->options = lf->options;      /* preserve options */
       memcpy(lf, of, sizeof(LEX));
@@ -149,7 +146,59 @@ LEX *lex_close_file(LEX *lf)
       of = lf;
       lf = NULL;
    }
-   free(of);
+   if (of) {
+      free(of);
+   }
+   return lf;
+}
+
+/*
+ * Open a configuration in memory buffer. We push the
+ * state of the current file (lf) so that we
+ * can do includes.  This is a bit of a hammer.
+ * Instead of passing back the pointer to the
+ * new packet, I simply replace the contents
+ * of the caller's packet with the new packet,
+ * and link the contents of the old packet into
+ * the next field.
+ *
+ */
+LEX *lex_open_buf(LEX *lf, const char *buffer, LEX_ERROR_HANDLER *scan_error)
+
+{
+   LEX *nf;
+
+   Dmsg0(400, "Open config buffer\n");
+   nf = (LEX *)malloc(sizeof(LEX));
+   if (lf) {
+      memcpy(nf, lf, sizeof(LEX));
+      memset(lf, 0, sizeof(LEX));
+      lf->next = nf;                  /* if have lf, push it behind new one */
+      lf->options = nf->options;      /* preserve user options */
+      /*
+       * preserve err_type to prevent bacula exiting on 'reload'
+       * if config is invalid. Fixes bug #877
+       */
+      lf->err_type = nf->err_type;
+   } else {
+      lf = nf;                        /* start new packet */
+      memset(lf, 0, sizeof(LEX));
+      lex_set_error_handler_error_type(lf, M_ERROR_TERM);
+   }
+   if (scan_error) {
+      lf->scan_error = scan_error;
+   } else {
+      lex_set_default_error_handler(lf);
+   }
+   lf->fd = NULL;
+   lf->bpipe = NULL;
+   lf->fname = NULL;
+   lf->line = get_memory(5000);
+   pm_strcpy(lf->line, buffer);
+   pm_strcat(lf->line, "");
+   lf->state = lex_none;
+   lf->ch = 0;
+   lf->str = get_memory(5000);
    return lf;
 }
 
@@ -190,10 +239,10 @@ LEX *lex_open_file(LEX *lf, const char *filename, LEX_ERROR_HANDLER *scan_error)
       lf->next = nf;                  /* if have lf, push it behind new one */
       lf->options = nf->options;      /* preserve user options */
       /*
-       * preserve err_type to prevent bacula exiting on 'reload' 
-       * if config is invalid. Fixes bug #877         
+       * preserve err_type to prevent bacula exiting on 'reload'
+       * if config is invalid. Fixes bug #877
        */
-      lf->err_type = nf->err_type;    
+      lf->err_type = nf->err_type;
    } else {
       lf = nf;                        /* start new packet */
       memset(lf, 0, sizeof(LEX));
@@ -210,6 +259,7 @@ LEX *lex_open_file(LEX *lf, const char *filename, LEX_ERROR_HANDLER *scan_error)
    lf->line = get_memory(5000);
    lf->state = lex_none;
    lf->ch = L_EOL;
+   lf->str = get_memory(5000);
    Dmsg1(dbglvl, "Return lex=%x\n", lf);
    return lf;
 }
@@ -226,7 +276,7 @@ int lex_get_char(LEX *lf)
       Emsg0(M_ABORT, 0, _("get_char: called after EOF."
          " You may have a open double quote without the closing double quote.\n"));
    }
-   if (lf->ch == L_EOL) {
+   if (lf->fd && lf->ch == L_EOL) {
       if (bfgets(lf->line, lf->fd) == NULL) {
          lf->ch = L_EOF;
          if (lf->next) {
@@ -237,14 +287,32 @@ int lex_get_char(LEX *lf)
       lf->line_no++;
       lf->col_no = 0;
       Dmsg2(1000, "fget line=%d %s", lf->line_no, lf->line);
+   } else if (lf->ch == L_EOL) {
+      lf->line_no++;
+      lf->col_no++;
    }
    lf->ch = (uint8_t)lf->line[lf->col_no];
-   if (lf->ch == 0) {
-      lf->ch = L_EOL;
+   if (lf->fd) {
+      if (lf->ch == 0) {
+         lf->ch = L_EOL;           /* reached end of line, force bfgets */
+      } else {
+         lf->col_no++;
+      }
    } else {
-      lf->col_no++;
+      if (lf->ch == 0) {           /* End of buffer, stop scan */
+         lf->ch = L_EOF;
+         if (lf->next) {
+            lex_close_file(lf);
+         }
+         return lf->ch;
+      } else if (lf->ch == '\n') {  /* End of line */
+         Dmsg0(dbglvl, "Found newline return L_EOL\n");
+         lf->ch = L_EOL;
+      } else {
+         lf->col_no++;
+      }
    }
-   Dmsg2(dbglvl, "lex_get_char: %c %d\n", lf->ch, lf->ch);
+   Dmsg3(dbglvl, "lex_get_char: %c %d col=%d\n", lf->ch, lf->ch, lf->col_no);
    return lf->ch;
 }
 
@@ -255,7 +323,6 @@ void lex_unget_char(LEX *lf)
    } else {
       lf->col_no--;                   /* Backup to re-read char */
    }
-
 }
 
 
@@ -264,7 +331,7 @@ void lex_unget_char(LEX *lf)
  */
 static void add_str(LEX *lf, int ch)
 {
-   if (lf->str_len >= MAXSTRING-3) {
+   if (lf->str_len >= sizeof_pool_memory(lf->str)) {
       Emsg3(M_ERROR_TERM, 0, _(
            _("Config token too long, file: %s, line %d, begins at line %d\n")),
              lf->fname, lf->line_no, lf->begin_line_no);
@@ -387,12 +454,12 @@ lex_get_token(LEX *lf, int expect)
       to tell which byte we are expecting. */
    int bom_bytes_seen = 0;
 
-   Dmsg0(dbglvl, "enter lex_get_token\n");
+   Dmsg1(dbglvl, "enter lex_get_token state=%s\n", lex_state_to_str(lf->state));
    while (token == T_NONE) {
       ch = lex_get_char(lf);
       switch (lf->state) {
       case lex_none:
-         Dmsg2(dbglvl, "Lex state lex_none ch=%d,%x\n", ch, ch);
+         Dmsg2(dbglvl, "Lex state lex_none ch=%c,%d\n", ch, ch);
          if (B_ISSPACE(ch))
             break;
          if (B_ISALPHA(ch)) {
@@ -587,6 +654,13 @@ lex_get_token(LEX *lf, int expect)
          }
          if (ch == '"') {
             token = T_QUOTED_STRING;
+            /*
+             * Since we may be scanning a quoted list of names,
+             *  we get the next character (a comma indicates another
+             *  one), then we put it back for rescanning.
+             */
+            lex_get_char(lf);
+            lex_unget_char(lf);
             lf->state = lex_none;
             break;
          }
@@ -653,7 +727,7 @@ lex_get_token(LEX *lf, int expect)
          add_str(lf, ch);
          break;
       case lex_utf8_bom:
-         /* we only end up in this state if we have read an 0xEF 
+         /* we only end up in this state if we have read an 0xEF
             as the first byte of the file, indicating we are probably
             reading a UTF-8 file */
          if (ch == 0xBB && bom_bytes_seen == 1) {
@@ -666,7 +740,7 @@ lex_get_token(LEX *lf, int expect)
          }
          break;
       case lex_utf16_le_bom:
-         /* we only end up in this state if we have read an 0xFF 
+         /* we only end up in this state if we have read an 0xFF
             as the first byte of the file -- indicating that we are
             probably dealing with an Intel based (little endian) UTF-16 file*/
          if (ch == 0xFE) {