From 459106f0a04248b61dad024e405fffbde4fee5e1 Mon Sep 17 00:00:00 2001 From: Eric Bollengier Date: Wed, 4 Apr 2007 20:55:02 +0000 Subject: [PATCH] ebl add build_where and escape_string functions git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@4510 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/patches/testing/breg.c | 68 +++++++++++++++++++++++++++++++ bacula/patches/testing/breg.h | 10 +++++ bacula/patches/testing/bregtest.c | 4 ++ 3 files changed, 82 insertions(+) diff --git a/bacula/patches/testing/breg.c b/bacula/patches/testing/breg.c index 07bca726e7..1dee7128bb 100644 --- a/bacula/patches/testing/breg.c +++ b/bacula/patches/testing/breg.c @@ -173,6 +173,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 +351,71 @@ 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; +} + +/* build a regexp string with user arguments + * don't forget to free ret + */ +char *bregexp_build_where(char *strip_prefix, + char *add_prefix, + char *add_suffix) +{ + /* strip_prefix = !strip_prefix!! 4 bytes + * add_prefix = !^!add_prefix! 5 bytes + * add_suffix = !([^/])$!$1.add_suffix! 14 bytes + */ + int len=0; + char sep = '!'; + int str_size = (strlen(strip_prefix) + 4 + + strlen(add_prefix) + 5 + + strlen(add_suffix) + 14) * 2 + 1; + + POOLMEM *ret = get_memory(str_size); + POOLMEM *str_tmp = get_memory(str_size); + + *str_tmp = *ret = '\0'; + + if (*strip_prefix) { + len += bsnprintf(ret, str_size - len, "!%s!!", + bregexp_escape_string(str_tmp, strip_prefix, sep)); + } + + if (*add_suffix) { + if (len) ret[len++] = ','; + + len += bsnprintf(ret + len, str_size - len, "!([^/])$!$1%s!", + bregexp_escape_string(str_tmp, add_suffix, sep)); + } + + if (*add_prefix) { + if (len) ret[len++] = ','; + + len += bsnprintf(ret + len, str_size - len, "!^!%s!", + bregexp_escape_string(str_tmp, add_prefix, sep)); + } + + return ret; +} + + void BREGEXP::debug() { printf("expr=[%s]\n", expr); diff --git a/bacula/patches/testing/breg.h b/bacula/patches/testing/breg.h index 991ad5a2d3..101f50eee6 100644 --- a/bacula/patches/testing/breg.h +++ b/bacula/patches/testing/breg.h @@ -98,4 +98,14 @@ bool apply_bregexps(const char *fname, alist *bregexps, char **result); /* foreach_alist free RUNSCRIPT */ void free_bregexps(alist *bregexps); /* you have to free alist */ +/* get a bregexp string from user arguments */ +char *bregexp_build_where(char *strip_prefix, + char *add_prefix, + char *add_suffix); + +/* escape a string to regexp format (sep and \) + * dest must be long enough (dest = 2*src + 1) + */ +char *bregexp_escape_string(char *dest, char *src, char sep); + #endif /* __BREG_H_ */ diff --git a/bacula/patches/testing/bregtest.c b/bacula/patches/testing/bregtest.c index f8cbb92c25..71eaad3b6b 100644 --- a/bacula/patches/testing/bregtest.c +++ b/bacula/patches/testing/bregtest.c @@ -62,6 +62,10 @@ static void usage() int main(int argc, char *const *argv) { + printf("%s\n", bregexp_build_where("/tmp", "/opt", ".old")); + exit(0); + + regex_t preg; char prbuf[500]; char *fname = NULL; -- 2.39.5