2 Bacula® - The Network Backup Solution
4 Copyright (C) 2006-2011 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version three of the GNU Affero General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zuerich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Manipulation routines for BREGEXP list
31 * Eric Bollengier, March 2007
41 BREGEXP *new_bregexp(const char *motif)
43 Dmsg0(500, "bregexp: creating new bregexp object\n");
44 BREGEXP *self = (BREGEXP *)bmalloc(sizeof(BREGEXP));
45 memset(self, 0, sizeof(BREGEXP));
47 if (!self->extract_regexp(motif)) {
48 Dmsg0(100, "bregexp: extract_regexp error\n");
53 self->result = get_pool_memory(PM_FNAME);
54 self->result[0] = '\0';
59 void free_bregexp(BREGEXP *self)
61 Dmsg0(500, "bregexp: freeing BREGEXP object\n");
71 free_pool_memory(self->result);
77 /* Free a bregexps alist
79 void free_bregexps(alist *bregexps)
81 Dmsg0(500, "bregexp: freeing all BREGEXP object\n");
84 foreach_alist(elt, bregexps) {
89 /* Apply all regexps to fname
91 bool apply_bregexps(const char *fname, alist *bregexps, char **result)
96 char *ret = (char *) fname;
97 foreach_alist(elt, bregexps) {
98 ret = elt->replace(ret);
99 ok = ok || elt->success;
101 Dmsg2(500, "bregexp: fname=%s ret=%s\n", fname, ret);
107 /* return an alist of BREGEXP or return NULL if it's not a
108 * where=!tmp!opt!ig,!temp!opt!i
110 alist *get_bregexps(const char *where)
112 char *p = (char *)where;
113 alist *list = New(alist(10, not_owned_by_alist));
116 reg = new_bregexp(p);
121 reg = new_bregexp(p);
132 bool BREGEXP::extract_regexp(const char *motif)
155 char *search = (char *) motif + 1;
156 int options = REG_EXTENDED | REG_NEWLINE;
159 /* extract 1st part */
160 char *dest = expr = bstrdup(motif);
162 while (*search && !ok) {
163 if (search[0] == '\\' && search[1] == sep) {
164 *dest++ = *++search; /* we skip separator */
166 } else if (search[0] == '\\' && search[1] == '\\') {
167 *dest++ = *++search; /* we skip the second \ */
169 } else if (*search == sep) { /* we found end of expression */
172 if (subst) { /* already have found motif */
176 *dest++ = *++search; /* we skip separator */
177 subst = dest; /* get replaced string */
184 *dest = '\0'; /* in case of */
193 while (*search && !ok) {
194 if (*search == 'i') {
195 options |= REG_ICASE;
197 } else if (*search == 'g') {
198 /* recherche multiple*/
200 } else if (*search == sep) {
203 } else { /* end of options */
209 int rc = regcomp(&preg, expr, options);
212 regerror(rc, &preg, prbuf, sizeof(prbuf));
213 Dmsg1(100, "bregexp: compile error: %s\n", prbuf);
217 eor = search; /* useful to find the next regexp in where */
222 /* return regexp->result */
223 char *BREGEXP::replace(const char *fname)
225 success = false; /* use this.success to known if it's ok */
226 int flen = strlen(fname);
227 int rc = regexec(&preg, fname, BREG_NREGS, regs, 0);
229 if (rc == REG_NOMATCH) {
230 Dmsg0(500, "bregexp: regex mismatch\n");
231 return return_fname(fname, flen);
234 int len = compute_dest_len(fname, regs);
237 result = check_pool_memory_size(result, len);
238 edit_subst(fname, regs);
240 Dmsg2(500, "bregexp: len = %i, result_len = %i\n", len, strlen(result));
242 } else { /* error in substitution */
243 Dmsg0(100, "bregexp: error in substitution\n");
244 return return_fname(fname, flen);
250 char *BREGEXP::return_fname(const char *fname, int len)
252 result = check_pool_memory_size(result, len+1);
253 strcpy(result,fname);
257 int BREGEXP::compute_dest_len(const char *fname, regmatch_t pmatch[])
261 char *psubst = subst;
264 if (!fname || !pmatch) {
269 if (pmatch[0].rm_so < 0) {
273 for (p = psubst++; *p ; p = psubst++) {
274 /* match $1 \1 back references */
275 if ((*p == '$' || *p == '\\') && ('0' <= *psubst && *psubst <= '9')) {
276 no = *psubst++ - '0';
278 /* we check if the back reference exists */
279 /* references can not match if we are using (..)? */
281 if (pmatch[no].rm_so >= 0 && pmatch[no].rm_eo >= 0) {
282 len += pmatch[no].rm_eo - pmatch[no].rm_so;
290 /* $0 is replaced by subst */
291 len -= pmatch[0].rm_eo - pmatch[0].rm_so;
292 len += strlen(fname) + 1;
297 char *BREGEXP::edit_subst(const char *fname, regmatch_t pmatch[])
301 char *psubst = subst;
305 /* il faut recopier fname dans dest
306 * on recopie le debut fname -> pmatch->start[0]
309 for (i = 0; i < pmatch[0].rm_so ; i++) {
310 result[i] = fname[i];
313 /* on recopie le motif de remplacement (avec tous les $x) */
315 for (p = psubst++; *p ; p = psubst++) {
316 /* match $1 \1 back references */
317 if ((*p == '$' || *p == '\\') && ('0' <= *psubst && *psubst <= '9')) {
318 no = *psubst++ - '0';
320 /* have a back reference ? */
321 if (pmatch[no].rm_so >= 0 && pmatch[no].rm_eo >= 0) {
322 len = pmatch[no].rm_eo - pmatch[no].rm_so;
323 bstrncpy(result + i, fname + pmatch[no].rm_so, len + 1);
332 /* we copy what is out of the match */
333 strcpy(result + i, fname + pmatch[0].rm_eo);
338 /* escape sep char and \
339 * dest must be long enough (src*2+1)
340 * return end of the string */
341 char *bregexp_escape_string(char *dest, const char *src, const char sep)
348 } else if (*src == '\\') {
358 static const char regexp_sep = '!';
359 static const char *str_strip_prefix = "!%s!!i";
360 static const char *str_add_prefix = "!^!%s!";
361 static const char *str_add_suffix = "!([^/])$!$1%s!";
363 int bregexp_get_build_where_size(char *strip_prefix,
367 int str_size = ((strip_prefix?strlen(strip_prefix)+strlen(str_strip_prefix):0) +
368 (add_prefix?strlen(add_prefix)+strlen(str_add_prefix) :0) +
369 (add_suffix?strlen(add_suffix)+strlen(str_add_suffix) :0) )
370 /* escape + 3*, + \0 */
373 Dmsg1(200, "bregexp_get_build_where_size = %i\n", str_size);
377 /* build a regexp string with user arguments
380 * int len = bregexp_get_build_where_size(a,b,c) ;
381 * char *dest = (char *) bmalloc (len * sizeof(char));
382 * bregexp_build_where(dest, len, a, b, c);
386 char *bregexp_build_where(char *dest, int str_size,
393 POOLMEM *str_tmp = get_memory(str_size);
395 *str_tmp = *dest = '\0';
398 len += bsnprintf(dest, str_size - len, str_strip_prefix,
399 bregexp_escape_string(str_tmp, strip_prefix, regexp_sep));
403 if (len) dest[len++] = ',';
405 len += bsnprintf(dest + len, str_size - len, str_add_suffix,
406 bregexp_escape_string(str_tmp, add_suffix, regexp_sep));
410 if (len) dest[len++] = ',';
412 len += bsnprintf(dest + len, str_size - len, str_add_prefix,
413 bregexp_escape_string(str_tmp, add_prefix, regexp_sep));
416 free_pool_memory(str_tmp);
422 void BREGEXP::debug()
424 printf("expr=[%s]\n", expr);
425 printf("subst=[%s]\n", subst);
426 printf("result=%s\n", NPRT(result));