memset(self, 0, sizeof(BREGEXP));
if (!self->extract_regexp(motif)) {
-// Dmsg0(100, "bregexp: extract_regexp error\n");
- printf("bregexp: extract_regexp error\n");
+ Dmsg0(100, "bregexp: extract_regexp error\n");
free_bregexp(self);
return NULL;
}
return ret;
}
+char *get_next_bregexp(char *where)
+{
+ char sep;
+ char *after;
+ bool ok=false;
+
+ if (!where && !*where) {
+ return NULL;
+ }
+
+}
+
+/* return an alist of BREGEXP or return NULL if it's not a
+ * where=!tmp!opt!ig,!temp!opt!i
+ */
+alist *get_bregexps(const char *where)
+{
+ char *p = (char *)where;
+ alist *list = New(alist(10, not_owned_by_alist));
+ BREGEXP *reg;
+
+ reg = new_bregexp(p);
+
+ while(reg) {
+ p = reg->eor;
+ list->append(reg);
+ reg = new_bregexp(p);
+ }
+
+ if (list->size()) {
+ return list;
+ } else {
+ delete list;
+ return NULL;
+ }
+}
bool BREGEXP::extract_regexp(const char *motif)
{
-
- if (!motif) {
+ if (!motif || (*motif == '\0')) {
return false;
}
char sep = motif[0];
char *dest = expr = bstrdup(motif);
while (*search && !ok) {
- if (*search == '\\' && *dest == sep) {
+ if (search[0] == '\\' && search[1] == sep) {
*dest++ = *++search; /* we skip separator */
- } else if (*search == sep) {
+
+ } else if (*search == sep) { /* we found end of expression */
*dest++ = '\0';
- if (subst) { /* already have found motif */
+ if (subst) { /* already have found motif */
ok = true;
+
} else {
*dest++ = *++search; /* we skip separator */
subst = dest; /* get replaced string */
}
+
} else {
*dest++ = *search++;
}
return false;
}
+ ok = false;
/* find options */
- while (*search) {
+ while (*search && !ok) {
if (*search == 'i') {
options |= REG_ICASE;
- }
- if (*search == 'g') {
+
+ } else if (*search == 'g') {
/* recherche multiple*/
+
+ } else if (*search == sep) {
+ /* skip separator */
+
+ } else { /* end of options */
+ ok = true;
}
search++;
}
if (rc != 0) {
char prbuf[500];
regerror(rc, &preg, prbuf, sizeof(prbuf));
- printf("bregexp: compile error: %s\n", prbuf);
-// Dmsg1(100, "bregexp: compile error: %s\n", prbuf);
+ Dmsg1(100, "bregexp: compile error: %s\n", prbuf);
return false;
}
+ eor = search; /* useful to find the next regexp in where */
+
return true;
}
int rc = re_search(&preg, (BREGEX_CAST char*) fname, flen, 0, flen, ®s);
if (rc < 0) {
- printf("E: regex mismatch\n");
+ Dmsg0(100, "E: regex mismatch\n");
return return_fname(fname, flen);
}
edit_subst(fname, ®s);
} else { /* error in substitution */
- printf("E: error in substitution\n");
+ Dmsg0(100, "E: error in substitution\n");
return return_fname(fname, flen);
}
no = *psubst++ - '0';
/* we check if the back reference exists */
+ /* references can not match if we are using (..)? */
+
if (regs->start[no] >= 0 && regs->end[no] >= 0) {
len += regs->end[no] - regs->start[no];
- } else {
- return 0; /* back reference missing */
}
+
} else {
len++;
}
if ((*p == '$' || *p == '\\') && ('0' <= *psubst && *psubst <= '9')) {
no = *psubst++ - '0';
- len = regs->end[no] - regs->start[no];
- bstrncpy(result + i, fname + regs->start[no], len + 1);
- i += len ;
+ /* have a back reference ? */
+ if (regs->start[no] >= 0 && regs->end[no] >= 0) {
+ len = regs->end[no] - regs->start[no];
+ bstrncpy(result + i, fname + regs->start[no], len + 1);
+ i += len ;
+ }
} else {
result[i++] = *p;
POOLMEM *subst; /* substitution */
regex_t preg; /* regex_t result of regcomp() */
struct re_registers regs; /* contains match */
+ char *eor; /* end of regexp in expr */
int *_regs_match;
/* free BREGEXP (and all POOLMEM) */
void free_bregexp(BREGEXP *script);
+/* fill an alist with BREGEXP from where */
+alist *get_bregexps(const char *where);
+
+char *apply_bregexps(const char *fname, alist *bregexps);
+
/* foreach_alist free RUNSCRIPT */
void free_bregexps(alist *bregexps); /* you have to free alist */
--- /dev/null
+/*
+ * Test program for testing regular expressions.
+ *
+ * Kern Sibbald, MMVI
+ *
+ */
+/*
+ Bacula® - The Network Backup Solution
+
+ Copyright (C) 2006-2006 Free Software Foundation Europe e.V.
+
+ The main author of Bacula is Kern Sibbald, with contributions from
+ many others, a complete list can be found in the file AUTHORS.
+ This program is Free Software; you can redistribute it and/or
+ modify it under the terms of version two of the GNU General Public
+ License as published by the Free Software Foundation plus additions
+ that are listed in the file LICENSE.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301, USA.
+
+ Bacula® 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,
+ Switzerland, email:ftf@fsfeurope.org.
+*/
+
+/*
+ * If you define BACULA_REGEX, bregex will be built with the
+ * Bacula bregex library, which is the same code that we
+ * use on Win32, thus using Linux, you can test your Win32
+ * expressions. Otherwise, this program will link with the
+ * system library routines.
+ */
+//#define BACULA_REGEX
+
+#include "bacula.h"
+#include <stdio.h>
+#include "lib/breg.h"
+
+
+static void usage()
+{
+ fprintf(stderr,
+"\n"
+"Usage: bregex [-d debug_level] -f <data-file> -e /test/test2/\n"
+" -f specify file of data to be matched\n"
+" -e specify expression\n"
+" -? print this message.\n"
+"\n");
+
+ exit(1);
+}
+
+
+int main(int argc, char *const *argv)
+{
+ regex_t preg;
+ char prbuf[500];
+ char *fname = NULL;
+ char *expr = NULL;
+ int rc, ch;
+ char data[1000];
+ char pat[500];
+ FILE *fd;
+ bool match_only = true;
+ int lineno;
+ bool no_linenos = false;
+
+
+ setlocale(LC_ALL, "");
+ bindtextdomain("bacula", LOCALEDIR);
+ textdomain("bacula");
+
+ while ((ch = getopt(argc, argv, "d:f:e:")) != -1) {
+ switch (ch) {
+ case 'd': /* set debug level */
+ debug_level = atoi(optarg);
+ if (debug_level <= 0) {
+ debug_level = 1;
+ }
+ break;
+
+ case 'f': /* data */
+ fname = optarg;
+ break;
+
+ case 'e':
+ expr = optarg;
+ break;
+
+ case '?':
+ default:
+ usage();
+
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (!fname) {
+ printf("A data file must be specified.\n");
+ usage();
+ }
+
+ if (!expr) {
+ printf("An expression must be specified.\n");
+ usage();
+ }
+
+ OSDependentInit();
+
+ alist *list;
+ char *p;
+
+ list = get_bregexps(expr);
+
+ if (!list) {
+ printf("Can't use %s as 'sed' expression\n", expr);
+ exit (1);
+ }
+
+ fd = fopen(fname, "r");
+ if (!fd) {
+ printf(_("Could not open data file: %s\n"), fname);
+ exit(1);
+ }
+
+ while (fgets(data, sizeof(data)-1, fd)) {
+ strip_trailing_newline(data);
+ p = apply_bregexps(data, list);
+ printf("%s => %s\n", data, p);
+ }
+ fclose(fd);
+ free_bregexps(list);
+ delete list;
+ exit(0);
+}
+/*
+ TODO:
+ - ajout /g
+
+ - tests
+ * test avec /i
+ * test avec un sed et faire un diff
+ * test avec une boucle pour voir les fuites
+
+*/