From: <ri...@us...> - 2007-04-04 20:55:06
|
Revision: 4510 http://bacula.svn.sourceforge.net/bacula/?rev=4510&view=rev Author: ricozz Date: 2007-04-04 13:55:02 -0700 (Wed, 04 Apr 2007) Log Message: ----------- ebl add build_where and escape_string functions Modified Paths: -------------- trunk/bacula/patches/testing/breg.c trunk/bacula/patches/testing/breg.h trunk/bacula/patches/testing/bregtest.c Modified: trunk/bacula/patches/testing/breg.c =================================================================== --- trunk/bacula/patches/testing/breg.c 2007-04-04 18:34:15 UTC (rev 4509) +++ trunk/bacula/patches/testing/breg.c 2007-04-04 20:55:02 UTC (rev 4510) @@ -173,6 +173,9 @@ 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 @@ 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); Modified: trunk/bacula/patches/testing/breg.h =================================================================== --- trunk/bacula/patches/testing/breg.h 2007-04-04 18:34:15 UTC (rev 4509) +++ trunk/bacula/patches/testing/breg.h 2007-04-04 20:55:02 UTC (rev 4510) @@ -98,4 +98,14 @@ /* 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_ */ Modified: trunk/bacula/patches/testing/bregtest.c =================================================================== --- trunk/bacula/patches/testing/bregtest.c 2007-04-04 18:34:15 UTC (rev 4509) +++ trunk/bacula/patches/testing/bregtest.c 2007-04-04 20:55:02 UTC (rev 4510) @@ -62,6 +62,10 @@ 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; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |