]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/lex.c
Make bfgets handle very long lines
[bacula/bacula] / bacula / src / lib / lex.c
index 2c438da08ca6f5dd88b497ed8fb3fe6eb5d1e562..cd9d7c5daeeaa70e69c848a75d1c16991b5abd53 100644 (file)
@@ -1,12 +1,12 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
+   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 two of the GNU General Public
+   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.
 
    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 General Public License
+   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 John Walker.
+   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.
@@ -30,8 +30,6 @@
  *
  *   Kern Sibbald, 2000
  *
- *   Version $Id$
- *
  */
 
 #include "bacula.h"
@@ -141,6 +139,8 @@ LEX *lex_close_file(LEX *lf)
    }
    Dmsg1(dbglvl, "Close cfg file %s\n", lf->fname);
    free(lf->fname);
+   free_memory(lf->line);
+   lf->line = NULL;
    if (of) {
       of->options = lf->options;      /* preserve options */
       memcpy(lf, of, sizeof(LEX));
@@ -172,9 +172,8 @@ LEX *lex_open_file(LEX *lf, const char *filename, LEX_ERROR_HANDLER *scan_error)
    BPIPE *bpipe = NULL;
    char *fname = bstrdup(filename);
 
-
    if (fname[0] == '|') {
-      if ((bpipe = open_bpipe(fname, 0, "rb")) == NULL) {
+      if ((bpipe = open_bpipe(fname+1, 0, "rb")) == NULL) {
          free(fname);
          return NULL;
       }
@@ -190,6 +189,11 @@ LEX *lex_open_file(LEX *lf, const char *filename, LEX_ERROR_HANDLER *scan_error)
       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));
@@ -203,6 +207,7 @@ LEX *lex_open_file(LEX *lf, const char *filename, LEX_ERROR_HANDLER *scan_error)
    lf->fd = fd;
    lf->bpipe = bpipe;
    lf->fname = fname;
+   lf->line = get_memory(5000);
    lf->state = lex_none;
    lf->ch = L_EOL;
    Dmsg1(dbglvl, "Return lex=%x\n", lf);
@@ -218,10 +223,11 @@ LEX *lex_open_file(LEX *lf, const char *filename, LEX_ERROR_HANDLER *scan_error)
 int lex_get_char(LEX *lf)
 {
    if (lf->ch == L_EOF) {
-      Emsg0(M_ABORT, 0, _("get_char: called after EOF\n"));
+      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 (bfgets(lf->line, MAXSTRING, lf->fd) == NULL) {
+      if (bfgets(lf->line, lf->fd) == NULL) {
          lf->ch = L_EOF;
          if (lf->next) {
             lex_close_file(lf);
@@ -291,6 +297,8 @@ static const char *lex_state_to_str(int state)
    case lex_identifier:    return _("identifier");
    case lex_string:        return _("string");
    case lex_quoted_string: return _("quoted_string");
+   case lex_include:       return _("include");
+   case lex_include_quoted_string: return _("include_quoted_string");
    case lex_utf8_bom:      return _("UTF-8 Byte Order Mark");
    case lex_utf16_le_bom:  return _("UTF-16le Byte Order Mark");
    default:                return "??????";
@@ -343,6 +351,23 @@ static uint32_t scan_pint(LEX *lf, char *str)
    return (uint32_t)val;
 }
 
+static uint64_t scan_pint64(LEX *lf, char *str)
+{
+   uint64_t val = 0;
+   if (!is_a_number(str)) {
+      scan_err1(lf, _("expected a positive integer number, got: %s"), str);
+      /* NOT REACHED */
+   } else {
+      errno = 0;
+      val = str_to_uint64(str);
+      if (errno != 0) {
+         scan_err1(lf, _("expected a positive integer number, got: %s"), str);
+         /* NOT REACHED */
+      }
+   }
+   return val;
+}
+
 /*
  *
  * Get the next token from the input
@@ -429,8 +454,14 @@ lex_get_token(LEX *lf, int expect)
             }
             break;
          case '@':
-            lf->state = lex_include;
-            begin_str(lf, 0);
+            /* In NO_EXTERN mode, @ is part of a string */
+            if (lf->options & LOPT_NO_EXTERN) {
+               lf->state = lex_string;
+               begin_str(lf, ch);
+            } else {
+               lf->state = lex_include;
+               begin_str(lf, 0);
+            }
             break;
          case 0xEF: /* probably a UTF-8 BOM */
          case 0xFF: /* probably a UTF-16le BOM */
@@ -561,11 +592,49 @@ lex_get_token(LEX *lf, int expect)
          }
          add_str(lf, ch);
          break;
+      case lex_include_quoted_string:
+         if (ch == L_EOF) {
+            token = T_ERROR;
+            break;
+         }
+         if (esc_next) {
+            add_str(lf, ch);
+            esc_next = false;
+            break;
+         }
+         if (ch == '\\') {
+            esc_next = true;
+            break;
+         }
+         if (ch == '"') {
+            /* Keep the original LEX so we can print an error if the included file can't be opened. */
+            LEX* lfori = lf;
+            /* Skip the double quote when restarting parsing */
+            lex_get_char(lf);
+
+            lf->state = lex_none;
+            lf = lex_open_file(lf, lf->str, lf->scan_error);
+            if (lf == NULL) {
+               berrno be;
+               scan_err2(lfori, _("Cannot open included config file %s: %s\n"),
+                  lfori->str, be.bstrerror());
+               return T_ERROR;
+            }
+            break;
+         }
+         add_str(lf, ch);
+         break;
       case lex_include:            /* scanning a filename */
          if (ch == L_EOF) {
             token = T_ERROR;
             break;
          }
+         if (ch == '"') {
+            lf->state = lex_include_quoted_string;
+            break;
+         }
+
+
          if (B_ISSPACE(ch) || ch == '\n' || ch == L_EOL || ch == '}' || ch == '{' ||
              ch == ';' || ch == ','   || ch == '"' || ch == '#') {
             /* Keep the original LEX so we can print an error if the included file can't be opened. */
@@ -594,18 +663,18 @@ lex_get_token(LEX *lf, int expect)
             lf->state = lex_none;
          } else {
             token = T_ERROR;
-        }
+         }
          break;
       case lex_utf16_le_bom:
          /* 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) {
-           token = T_UTF16_BOM;
-           lf->state = lex_none;
-        } else {
-           token = T_ERROR;
-        }
+         if (ch == 0xFE) {
+            token = T_UTF16_BOM;
+            lf->state = lex_none;
+         } else {
+            token = T_ERROR;
+         }
          break;
       }
       Dmsg4(dbglvl, "ch=%d state=%s token=%s %c\n", ch, lex_state_to_str(lf->state),
@@ -683,6 +752,26 @@ lex_get_token(LEX *lf, int expect)
       }
       break;
 
+   case T_PINT64_RANGE:
+      if (token == T_NUMBER) {
+         lf->pint64_val = scan_pint64(lf, lf->str);
+         lf->pint64_val2 = lf->pint64_val;
+         token = T_PINT64;
+      } else {
+         char *p = strchr(lf->str, '-');
+         if (!p) {
+            scan_err2(lf, _("expected an integer or a range, got %s: %s"),
+               lex_tok_to_str(token), lf->str);
+            token = T_ERROR;
+            break;
+         }
+         *p++ = 0;                       /* terminate first half of range */
+         lf->pint64_val  = scan_pint64(lf, lf->str);
+         lf->pint64_val2 = scan_pint64(lf, p);
+         token = T_PINT64_RANGE;
+      }
+      break;
+
    case T_NAME:
       if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
          scan_err2(lf, _("expected a name, got %s: %s"),