2 * Manipulation routines for BREGEXP list
4 * Eric Bollengier, March 2007
10 Bacula® - The Network Backup Solution
12 Copyright (C) 2006-2006 Free Software Foundation Europe e.V.
14 The main author of Bacula is Kern Sibbald, with contributions from
15 many others, a complete list can be found in the file AUTHORS.
16 This program is Free Software; you can redistribute it and/or
17 modify it under the terms of version two of the GNU General Public
18 License as published by the Free Software Foundation plus additions
19 that are listed in the file LICENSE.
21 This program is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31 Bacula® is a registered trademark of John Walker.
32 The licensor of Bacula is the Free Software Foundation Europe
33 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
34 Switzerland, email:ftf@fsfeurope.org.
43 BREGEXP *new_bregexp(const char *motif)
45 Dmsg0(500, "bregexp: creating new bregexp object\n");
46 BREGEXP *self = (BREGEXP *)malloc(sizeof(BREGEXP));
47 memset(self, 0, sizeof(BREGEXP));
49 if (!self->extract_regexp(motif)) {
50 Dmsg0(100, "bregexp: extract_regexp error\n");
55 self->result = get_pool_memory(PM_FNAME);
56 self->result[0] = '\0';
59 /* TODO: que devient cette memoire... */
60 self->_regs_match = (int *) malloc (2*RE_NREGS * sizeof(int));
62 self->regs.num_regs = RE_NREGS;
63 self->regs.start = self->_regs_match;
64 self->regs.end = self->_regs_match+(RE_NREGS * sizeof(int));
70 void free_bregexp(BREGEXP *self)
72 Dmsg0(500, "bregexp: freeing BREGEXP object\n");
78 free_pool_memory(self->result);
80 if (self->_regs_match) {
81 free(self->_regs_match);
88 /* Free a bregexps alist
90 void free_bregexps(alist *bregexps)
92 Dmsg0(500, "bregexp: freeing all BREGEXP object\n");
95 foreach_alist(elt, bregexps) {
100 /* Apply all regexps to fname
102 bool apply_bregexps(const char *fname, alist *bregexps, char **result)
107 char *ret = (char *) fname;
108 foreach_alist(elt, bregexps) {
109 ret = elt->replace(ret);
110 ok = ok || elt->success;
112 Dmsg2(500, "bregexp: fname=%s ret=%s\n", fname, ret);
118 /* return an alist of BREGEXP or return NULL if it's not a
119 * where=!tmp!opt!ig,!temp!opt!i
121 alist *get_bregexps(const char *where)
123 char *p = (char *)where;
124 alist *list = New(alist(10, not_owned_by_alist));
127 reg = new_bregexp(p);
132 reg = new_bregexp(p);
143 bool BREGEXP::extract_regexp(const char *motif)
165 char *search = (char *) motif + 1;
166 int options = REG_EXTENDED | REG_NEWLINE;
169 /* extract 1st part */
170 char *dest = expr = bstrdup(motif);
172 while (*search && !ok) {
173 if (search[0] == '\\' && search[1] == sep) {
174 *dest++ = *++search; /* we skip separator */
176 } else if (*search == sep) { /* we found end of expression */
179 if (subst) { /* already have found motif */
183 *dest++ = *++search; /* we skip separator */
184 subst = dest; /* get replaced string */
191 *dest = '\0'; /* in case of */
200 while (*search && !ok) {
201 if (*search == 'i') {
202 options |= REG_ICASE;
204 } else if (*search == 'g') {
205 /* recherche multiple*/
207 } else if (*search == sep) {
210 } else { /* end of options */
216 int rc = regcomp(&preg, expr, options);
219 regerror(rc, &preg, prbuf, sizeof(prbuf));
220 Dmsg1(100, "bregexp: compile error: %s\n", prbuf);
224 eor = search; /* useful to find the next regexp in where */
230 #define BREGEX_CAST unsigned
232 #define BREGEX_CAST const
235 /* return regexp->result */
236 char *BREGEXP::replace(const char *fname)
238 success = false; /* use this.success to known if it's ok */
239 int flen = strlen(fname);
240 int rc = re_search(&preg, (BREGEX_CAST char*) fname, flen, 0, flen, ®s);
243 Dmsg0(500, "bregexp: regex mismatch\n");
244 return return_fname(fname, flen);
247 int len = compute_dest_len(fname, ®s);
250 result = check_pool_memory_size(result, len);
251 edit_subst(fname, ®s);
253 Dmsg2(500, "bregexp: len = %i, result_len = %i\n", len, strlen(result));
255 } else { /* error in substitution */
256 Dmsg0(100, "bregexp: error in substitution\n");
257 return return_fname(fname, flen);
263 char *BREGEXP::return_fname(const char *fname, int len)
265 result = check_pool_memory_size(result, len+1);
266 strcpy(result,fname);
270 int BREGEXP::compute_dest_len(const char *fname, struct re_registers *regs)
274 char *psubst = subst;
277 if (!fname || !regs) {
282 if (regs->start[0] < 0) {
286 for (p = psubst++; *p ; p = psubst++) {
287 /* match $1 \1 back references */
288 if ((*p == '$' || *p == '\\') && ('0' <= *psubst && *psubst <= '9')) {
289 no = *psubst++ - '0';
291 /* we check if the back reference exists */
292 /* references can not match if we are using (..)? */
294 if (regs->start[no] >= 0 && regs->end[no] >= 0) {
295 len += regs->end[no] - regs->start[no];
303 /* $0 is replaced by subst */
304 len -= regs->end[0] - regs->start[0];
305 len += strlen(fname) + 1;
310 char *BREGEXP::edit_subst(const char *fname, struct re_registers *regs)
314 char *psubst = subst;
318 /* il faut recopier fname dans dest
319 * on recopie le debut fname -> regs->start[0]
322 for (i = 0; i < regs->start[0] ; i++) {
323 result[i] = fname[i];
326 /* on recopie le motif de remplacement (avec tous les $x) */
328 for (p = psubst++; *p ; p = psubst++) {
329 /* match $1 \1 back references */
330 if ((*p == '$' || *p == '\\') && ('0' <= *psubst && *psubst <= '9')) {
331 no = *psubst++ - '0';
333 /* have a back reference ? */
334 if (regs->start[no] >= 0 && regs->end[no] >= 0) {
335 len = regs->end[no] - regs->start[no];
336 bstrncpy(result + i, fname + regs->start[no], len + 1);
345 /* we copy what is out of the match */
346 strcpy(result + i, fname + regs->end[0]);
351 void BREGEXP::debug()
353 printf("expr=[%s]\n", expr);
354 printf("subst=[%s]\n", subst);
355 printf("result=%s\n", result?result:"(null)");