]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/patches/testing/breg.c
ebl fix compilation
[bacula/bacula] / bacula / patches / testing / breg.c
index 07bca726e75038fc09a864f489933dfaac2f8cbf..7468c6572d0a17f04ace7d84e6113604ea4ea8ea 100644 (file)
@@ -7,7 +7,7 @@
  *
  */
 /*
-   Bacula® - The Network Backup Solution
+   Bacula\81Â\81® - The Network Backup Solution
 
    Copyright (C) 2006-2006 Free Software Foundation Europe e.V.
 
@@ -28,9 +28,9 @@
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Bacula® is a registered trademark of John Walker.
+   Bacula\81Â\81® is a registered trademark of John Walker.
    The licensor of Bacula is the Free Software Foundation Europe
-   (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
+   (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Z\81Ã\81¼rich,
    Switzerland, email:ftf@fsfeurope.org.
 */
 
@@ -43,7 +43,7 @@
 BREGEXP *new_bregexp(const char *motif)
 {
    Dmsg0(500, "bregexp: creating new bregexp object\n");
-   BREGEXP *self = (BREGEXP *)malloc(sizeof(BREGEXP));
+   BREGEXP *self = (BREGEXP *)bmalloc(sizeof(BREGEXP));
    memset(self, 0, sizeof(BREGEXP));
    
    if (!self->extract_regexp(motif)) {
@@ -57,7 +57,7 @@ BREGEXP *new_bregexp(const char *motif)
 
 #ifdef HAVE_REGEX_H
    /* TODO: que devient cette memoire... */
-   self->_regs_match = (int *) malloc (2*RE_NREGS * sizeof(int));
+   self->_regs_match = (int *) bmalloc (2*RE_NREGS * sizeof(int));
 
    self->regs.num_regs = RE_NREGS;
    self->regs.start = self->_regs_match;
@@ -71,18 +71,22 @@ void free_bregexp(BREGEXP *self)
 {
    Dmsg0(500, "bregexp: freeing BREGEXP object\n");
 
+   if (!self) {
+      return;
+   }
+
    if (self->expr) {
-      free(self->expr);
+      bfree(self->expr);
    }
    if (self->result) {
       free_pool_memory(self->result);
    }
    if (self->_regs_match) {
-      free(self->_regs_match);
+      bfree(self->_regs_match);
    }
 
    regfree(&self->preg);
-   free(self);
+   bfree(self);
 }
 
 /* Free a bregexps alist
@@ -173,6 +177,9 @@ bool BREGEXP::extract_regexp(const char *motif)
       if (search[0] == '\\' && search[1] == sep) {
         *dest++ = *++search;       /* we skip separator */ 
 
+      } else if (search[0] == '\\' && search[1] == '\\') {
+        *dest++ = *++search;       /* we skip the second \ */
+
       } else if (*search == sep) {  /* we found end of expression */
         *dest++ = '\0';
 
@@ -348,6 +355,88 @@ char *BREGEXP::edit_subst(const char *fname, struct re_registers *regs)
    return result;
 }
 
+/* escape sep char and \
+ * dest must be long enough (src*2+1)
+ * return end of the string */
+char *bregexp_escape_string(char *dest, char *src, char sep)
+{
+   char *ret = dest;
+   while (*src)
+   {
+      if (*src == sep) {
+        *dest++ = '\\';
+      } else if (*src == '\\') {
+        *dest++ = '\\';
+      }
+      *dest++ = *src++;
+   }
+   *dest = '\0';
+
+   return ret; 
+}
+
+int bregexp_get_build_where_size(char *strip_prefix, 
+                                char *add_prefix, 
+                                char *add_suffix)
+{
+   /* strip_prefix = !strip_prefix!!i        4 bytes
+    * add_prefix   = !^!add_prefix!          5 bytes
+    * add_suffix   = !([^/])$!$1add_suffix! 13 bytes
+    */
+   int str_size = ((strip_prefix?strlen(strip_prefix)+4:0) +
+                  (add_prefix?strlen(add_prefix)+5    :0) + /* escape + 3*, + \0 */ 
+                  (add_suffix?strlen(add_suffix)+14   :0) )   * 2     + 3   + 1;
+
+   Dmsg1(1, "bregexp_get_build_where_size = %i\n", str_size);
+   return str_size;
+}
+
+/* build a regexp string with user arguments
+ * Usage :
+ * 
+ * int len = bregexp_get_build_where_size(a,b,c) ;
+ * char *dest = (char *) bmalloc (len * sizeof(char));
+ * bregexp_build_where(dest, len, a, b, c);
+ * free(dest);
+ * 
+ */
+char *bregexp_build_where(char *dest, int str_size,
+                         char *strip_prefix, 
+                          char *add_prefix, 
+                          char *add_suffix)
+{
+   int len=0;
+   char sep = '!';
+
+   POOLMEM *str_tmp = get_memory(str_size);
+
+   *str_tmp = *dest = '\0';
+   
+   if (strip_prefix) {
+      len += bsnprintf(dest, str_size - len, "!%s!!i",
+                      bregexp_escape_string(str_tmp, strip_prefix, sep));
+   }
+
+   if (add_suffix) {
+      if (len) dest[len++] = ',';
+
+      len += bsnprintf(dest + len,  str_size - len, "!([^/])$!$1%s!",
+                      bregexp_escape_string(str_tmp, add_suffix, sep));
+   }
+
+   if (add_prefix) {
+      if (len) dest[len++] = ',';
+
+      len += bsnprintf(dest + len, str_size - len, "!^!%s!", 
+                      bregexp_escape_string(str_tmp, add_prefix, sep));
+   }
+
+   free_pool_memory(str_tmp);
+
+   return dest;
+}
+
+
 void BREGEXP::debug()
 {
    printf("expr=[%s]\n", expr);