From 8c0f6b494e89ee7cd8825b032043b09b46ed34f9 Mon Sep 17 00:00:00 2001 From: Eric Bollengier Date: Sat, 31 Mar 2007 11:32:32 +0000 Subject: [PATCH] ebl update git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@4472 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/patches/testing/breg.c | 87 +++++++++++++---- bacula/patches/testing/breg.h | 6 ++ bacula/patches/testing/bregtest.c | 155 ++++++++++++++++++++++++++++++ 3 files changed, 229 insertions(+), 19 deletions(-) create mode 100644 bacula/patches/testing/bregtest.c diff --git a/bacula/patches/testing/breg.c b/bacula/patches/testing/breg.c index 88449991ed..a997942b90 100644 --- a/bacula/patches/testing/breg.c +++ b/bacula/patches/testing/breg.c @@ -47,8 +47,7 @@ BREGEXP *new_bregexp(const char *motif) 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; } @@ -109,11 +108,46 @@ char *apply_bregexps(const char *fname, alist *bregexps) 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]; @@ -127,17 +161,20 @@ bool BREGEXP::extract_regexp(const char *motif) 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++; } @@ -149,13 +186,20 @@ bool BREGEXP::extract_regexp(const char *motif) 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++; } @@ -164,11 +208,12 @@ bool BREGEXP::extract_regexp(const char *motif) 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; } @@ -185,7 +230,7 @@ char *BREGEXP::replace(const char *fname) 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); } @@ -196,7 +241,7 @@ char *BREGEXP::replace(const char *fname) 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); } @@ -232,11 +277,12 @@ int BREGEXP::compute_dest_len(const char *fname, struct re_registers *regs) 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++; } @@ -272,9 +318,12 @@ char *BREGEXP::edit_subst(const char *fname, struct re_registers *regs) 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; diff --git a/bacula/patches/testing/breg.h b/bacula/patches/testing/breg.h index 12e907e332..606575af84 100644 --- a/bacula/patches/testing/breg.h +++ b/bacula/patches/testing/breg.h @@ -68,6 +68,7 @@ public: 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; @@ -86,6 +87,11 @@ int run_bregexp(alist *bregexps, const char *fname); /* 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 */ diff --git a/bacula/patches/testing/bregtest.c b/bacula/patches/testing/bregtest.c new file mode 100644 index 0000000000..150bcb2394 --- /dev/null +++ b/bacula/patches/testing/bregtest.c @@ -0,0 +1,155 @@ +/* + * 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 +#include "lib/breg.h" + + +static void usage() +{ + fprintf(stderr, +"\n" +"Usage: bregex [-d debug_level] -f -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 + +*/ -- 2.39.5