]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/scan.c
Big backport from Enterprise
[bacula/bacula] / bacula / src / lib / scan.c
index 53c40242104bf875df494576396a809dad3f7ab2..3eafcd9bf53f0fbefe5362d493707fa111131054 100644 (file)
@@ -1,23 +1,26 @@
+/*
+   Bacula(R) - The Network Backup Solution
+
+   Copyright (C) 2000-2017 Kern Sibbald
+
+   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.
+*/
 /*
  *   scan.c -- scanning routines for Bacula
  *
  *    Kern Sibbald, MM  separated from util.c MMIII
  *
- *   Version $Id$
- */
-/*
-   Copyright (C) 2000-2005 Kern Sibbald
-
-   This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License
-   version 2 as amended with additional clauses defined in the
-   file LICENSE in the main source directory.
-
-   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 
-   the file LICENSE for additional details.
-
  */
 
 
@@ -32,42 +35,44 @@ void strip_leading_space(char *str)
    while (B_ISSPACE(*p)) {
       p++;
    }
-   if (p != str) {
-      strcpy(str, p);
+   if (str != p) {
+      do {
+         *str++ = *p;
+      } while (*p++ != 0);
    }
 }
 
-
 /* Strip any trailing junk from the command */
-void strip_trailing_junk(char *cmd)
+char *strip_trailing_junk(char *cmd)
 {
    char *p;
-   p = cmd + strlen(cmd) - 1;
 
    /* strip trailing junk from command */
-   while ((p >= cmd) && (*p == '\n' || *p == '\r' || *p == ' '))
+   p = cmd - 1 + strlen(cmd);
+   while ((p >= cmd) && (B_ISSPACE(*p) || *p == '\n' || *p == '\r')) {
       *p-- = 0;
+   } 
+   return cmd;
 }
 
 /* Strip any trailing newline characters from the string */
-void strip_trailing_newline(char *cmd)
+char *strip_trailing_newline(char *cmd)
 {
    char *p;
-   p = cmd + strlen(cmd) - 1;
-
-   while ((p >= cmd) && (*p == '\n' || *p == '\r'))
-      *p-- = 0;
+   p = cmd - 1 + strlen(cmd);
+   while ((p >= cmd) && (*p == '\n' || *p == '\r')) *p-- = 0;
+   return cmd;
 }
 
 /* Strip any trailing slashes from a directory path */
-void strip_trailing_slashes(char *dir)
+char *strip_trailing_slashes(char *dir)
 {
    char *p;
-   p = dir + strlen(dir) - 1;
 
    /* strip trailing slashes */
-   while ((p >= dir) && (*p == '/'))
-      *p-- = 0;
+   p = dir -1 + strlen(dir);
+   while (p >= dir && IsPathSeparator(*p)) *p-- = 0;
+   return dir;
 }
 
 /*
@@ -141,7 +146,18 @@ fstrsch(const char *a, const char *b)   /* folded case search */
 
 /*
  * Return next argument from command line.  Note, this
- * routine is destructive.
+ *   routine is destructive because it stored 0 at the end
+ *   of each argument.
+ * Called with pointer to pointer to command line. This
+ *   pointer is updated to point to the remainder of the
+ *   command line.
+ *
+ * Returns pointer to next argument -- don't store the result
+ *   in the pointer you passed as an argument ...
+ *   The next argument is terminated by a space unless within
+ *   quotes. Double quote characters (unless preceded by a \) are
+ *   stripped.
+ *
  */
 char *next_arg(char **s)
 {
@@ -154,8 +170,8 @@ char *next_arg(char **s)
    }
    Dmsg1(900, "Next arg=%s\n", p);
    for (n = q = p; *p ; ) {
-      if (*p == '\\') {
-         p++;
+      if (*p == '\\') {                 /* slash? */
+         p++;                           /* yes, skip it */
          if (*p) {
             *q++ = *p++;
          } else {
@@ -164,16 +180,11 @@ char *next_arg(char **s)
          continue;
       }
       if (*p == '"') {                  /* start or end of quote */
-         if (in_quote) {
-            p++;                        /* skip quote */
-            in_quote = false;
-            continue;
-         }
-         in_quote = true;
          p++;
+         in_quote = !in_quote;          /* change state */
          continue;
       }
-      if (!in_quote && B_ISSPACE(*p)) {     /* end of field */
+      if (!in_quote && B_ISSPACE(*p)) { /* end of field */
          p++;
          break;
       }
@@ -188,7 +199,7 @@ char *next_arg(char **s)
 /*
  * This routine parses the input command line.
  * It makes a copy in args, then builds an
- *  argc, argv like list where
+ *  argc, argk, argv list where:
  *
  *  argc = count of arguments
  *  argk[i] = argument keyword (part preceding =)
@@ -204,11 +215,57 @@ char *next_arg(char **s)
  *  argk[2] = arg3
  *  argv[2] =
  */
-
 int parse_args(POOLMEM *cmd, POOLMEM **args, int *argc,
                char **argk, char **argv, int max_args)
 {
-   char *p, *q, *n;
+   char *p;
+
+   parse_args_only(cmd, args, argc, argk, argv, max_args);
+
+   /* Separate keyword and value */
+   for (int i=0; i < *argc; i++) {
+      p = strchr(argk[i], '=');
+      if (p) {
+         *p++ = 0;                    /* terminate keyword and point to value */
+      }
+      argv[i] = p;                    /* save ptr to value or NULL */
+   }
+#ifdef xxx_debug
+   for (int i=0; i < *argc; i++) {
+      Pmsg3(000, "Arg %d: kw=%s val=%s\n", i, argk[i], argv[i]?argv[i]:"NULL");
+   }
+#endif
+   return 1;
+}
+
+
+/*
+ * This routine parses the input command line.
+ *   It makes a copy in args, then builds an
+ *   argc, argk, but no argv (values).
+ *   This routine is useful for scanning command lines where the data
+ *   is a filename and no keywords are expected.  If we scan a filename
+ *   for keywords, any = in the filename will be interpreted as the
+ *   end of a keyword, and this is not good.
+ *
+ *  argc = count of arguments
+ *  argk[i] = argument keyword (part preceding =)
+ *  argv[i] = NULL
+ *
+ *  example:  arg1 arg2=abc arg3=
+ *
+ *  argc = c
+ *  argk[0] = arg1
+ *  argv[0] = NULL
+ *  argk[1] = arg2=abc
+ *  argv[1] = NULL
+ *  argk[2] = arg3
+ *  argv[2] =
+ */
+int parse_args_only(POOLMEM *cmd, POOLMEM **args, int *argc,
+                    char **argk, char **argv, int max_args)
+{
+   char *p, *n;
 
    pm_strcpy(args, cmd);
    strip_trailing_junk(*args);
@@ -224,36 +281,10 @@ int parse_args(POOLMEM *cmd, POOLMEM **args, int *argc,
          break;
       }
    }
-   /* Separate keyword and value */
-   for (int i=0; i < *argc; i++) {
-      p = strchr(argk[i], '=');
-      if (p) {
-         *p++ = 0;                    /* terminate keyword and point to value */
-         /* Unquote quoted values */
-         if (*p == '"') {
-            for (n = q = ++p; *p && *p != '"'; ) {
-               if (*p == '\\') {
-                  p++;
-               }
-               *q++ = *p++;
-            }
-            *q = 0;                   /* terminate string */
-            p = n;                    /* point to string */
-         }
-         if (strlen(p) > MAX_NAME_LENGTH-1) {
-            p[MAX_NAME_LENGTH-1] = 0; /* truncate to max len */
-         }
-      }
-      argv[i] = p;                    /* save ptr to value or NULL */
-   }
-#ifdef xxxx
-   for (int i=0; i < *argc; i++) {
-      Pmsg3(000, "Arg %d: kw=%s val=%s\n", i, argk[i], argv[i]?argv[i]:"NULL");
-   }
-#endif
    return 1;
 }
 
+
 /*
  * Given a full filename, split it into its path
  *  and filename parts. They are returned in pool memory
@@ -275,16 +306,16 @@ void split_path_and_filename(const char *fname, POOLMEM **path, int *pnl,
     */
    f = fname + len - 1;
    /* "strip" any trailing slashes */
-   while (slen > 1 && *f == '/') {
+   while (slen > 1 && IsPathSeparator(*f)) {
       slen--;
       f--;
    }
    /* Walk back to last slash -- begin of filename */
-   while (slen > 0 && *f != '/') {
+   while (slen > 0 && !IsPathSeparator(*f)) {
       slen--;
       f--;
    }
-   if (*f == '/') {                   /* did we find a slash? */
+   if (IsPathSeparator(*f)) {         /* did we find a slash? */
       f++;                            /* yes, point to filename */
    } else {                           /* no, whole thing must be path name */
       f = fname;
@@ -310,6 +341,10 @@ void split_path_and_filename(const char *fname, POOLMEM **path, int *pnl,
 
 /*
  * Extremely simple sscanf. Handles only %(u,d,ld,qd,qu,lu,lld,llu,c,nns)
+ *
+ * Note, BIG is the default maximum length when no length
+ *   has been specified for %s. If it is not big enough, then
+ *   simply add a length such as %10000s.
  */
 const int BIG = 1000;
 int bsscanf(const char *buf, const char *fmt, ...)
@@ -322,6 +357,7 @@ int bsscanf(const char *buf, const char *fmt, ...)
    int max_len = BIG;
    uint64_t value;
    bool error = false;
+   bool negative;
 
    va_start(ap, fmt);
    while (*fmt && !error) {
@@ -332,18 +368,47 @@ int bsscanf(const char *buf, const char *fmt, ...)
 switch_top:
          switch (*fmt++) {
          case 'u':
-         case 'd':
             value = 0;
             while (B_ISDIGIT(*buf)) {
                value = B_TIMES10(value) + *buf++ - '0';
             }
             vp = (void *)va_arg(ap, void *);
 //          Dmsg2(000, "val=%lld at 0x%lx\n", value, (long unsigned)vp);
-            if (l < 2) {
+            if (l == 0) {
+               *((int *)vp) = (int)value;
+            } else if (l == 1) {
                *((uint32_t *)vp) = (uint32_t)value;
 //             Dmsg0(000, "Store 32 bit int\n");
             } else {
                *((uint64_t *)vp) = (uint64_t)value;
+//             Dmsg0(000, "Store 64 bit int\n");
+            }
+            count++;
+            l = 0;
+            break;
+         case 'd':
+            value = 0;
+            if (*buf == '-') {
+               negative = true;
+               buf++;
+            } else {
+               negative = false;
+            }
+            while (B_ISDIGIT(*buf)) {
+               value = B_TIMES10(value) + *buf++ - '0';
+            }
+            if (negative) {
+               value = -value;
+            }
+            vp = (void *)va_arg(ap, void *);
+//          Dmsg2(000, "val=%lld at 0x%lx\n", value, (long unsigned)vp);
+            if (l == 0) {
+               *((int *)vp) = (int)value;
+            } else if (l == 1) {
+               *((int32_t *)vp) = (int32_t)value;
+//             Dmsg0(000, "Store 32 bit int\n");
+            } else {
+               *((int64_t *)vp) = (int64_t)value;
 //             Dmsg0(000, "Store 64 bit int\n");
             }
             count++;