From 2e060adf1a6ca67a1b2184e7ced4185e60177877 Mon Sep 17 00:00:00 2001 From: Eric Bollengier Date: Tue, 20 Mar 2007 22:36:28 +0000 Subject: [PATCH] ebl works with bregex.h and regex.h git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@4373 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/patches/testing/breg.c | 88 +++++++++++------ bacula/patches/testing/breg.h | 10 +- bacula/patches/testing/bregsed.c | 156 +++++++++++++++++++++++++++++++ 3 files changed, 219 insertions(+), 35 deletions(-) create mode 100644 bacula/patches/testing/bregsed.c diff --git a/bacula/patches/testing/breg.c b/bacula/patches/testing/breg.c index a62482e70b..261424fd4c 100644 --- a/bacula/patches/testing/breg.c +++ b/bacula/patches/testing/breg.c @@ -1,7 +1,7 @@ /* * Manipulation routines for BREGEXP list * - * Eric Bollengier, Mars 2007 + * Eric Bollengier, March 2007 * * Version $Id$ * @@ -46,8 +46,6 @@ BREGEXP *new_bregexp(const char *motif) BREGEXP *self = (BREGEXP *)malloc(sizeof(BREGEXP)); memset(self, 0, sizeof(BREGEXP)); - self->nmatch = 20; - if (!self->extract_regexp(motif)) { // Dmsg0(100, "bregexp: extract_regexp error\n"); printf("bregexp: extract_regexp error\n"); @@ -55,11 +53,52 @@ BREGEXP *new_bregexp(const char *motif) return NULL; } + static int _start[RE_NREGS]; + static int _end[RE_NREGS]; + self->result = get_pool_memory(PM_FNAME); + self->result[0] = '\0'; + +#ifdef HAVE_REGEX_H + /* TODO: que devient cette memoire... */ + self->_regs_match = (int *) malloc (2*RE_NREGS * sizeof(int)); + + self->regs.num_regs = RE_NREGS; + self->regs.start = self->_regs_match; + self->regs.end = self->_regs_match+(RE_NREGS * sizeof(int)); +#endif return self; } +void free_bregexp(BREGEXP *self) +{ + Dmsg0(500, "bregexp: freeing BREGEXP object\n"); + + if (self->expr) { + free(self->expr); + } + if (self->result) { + free_pool_memory(self->result); + } + if (self->_regs_match) { + free(self->_regs_match); + } + + regfree(&self->preg); + free(self); +} + +void free_bregexps(alist *bregexps) +{ + Dmsg0(500, "bregexp: freeing all BREGEXP object\n"); + + BREGEXP *elt; + foreach_alist(elt, bregexps) { + free_bregexp(elt); + } +} + bool BREGEXP::extract_regexp(const char *motif) { @@ -69,7 +108,7 @@ bool BREGEXP::extract_regexp(const char *motif) char sep = motif[0]; char *search = (char *) motif + 1; char *replace; - int options = REG_EXTENDED; + int options = REG_EXTENDED | REG_NEWLINE; bool ok = false; bool found_motif = false; @@ -104,6 +143,9 @@ bool BREGEXP::extract_regexp(const char *motif) if (*search == 'i') { options |= REG_ICASE; } + if (*search == 'g') { + /* recherche multiple*/ + } search++; } @@ -115,41 +157,21 @@ bool BREGEXP::extract_regexp(const char *motif) // Dmsg1(100, "bregexp: compile error: %s\n", prbuf); return false; } - - return true; -} - -void free_bregexp(BREGEXP *self) -{ - Dmsg0(500, "bregexp: freeing BREGEXP object\n"); - - if (self->expr) { - free(self->expr); - } - if (self->result) { - free_pool_memory(self->result); - } - regfree(&self->preg); - free(self); + return true; } -void free_bregexps(alist *bregexps) -{ - Dmsg0(500, "bregexp: freeing all BREGEXP object\n"); - - BREGEXP *elt; - foreach_alist(elt, bregexps) { - free_bregexp(elt); - } -} +#ifndef HAVE_REGEX_H + #define BREGEX_CAST unsigned +#else + #define BREGEX_CAST const +#endif /* return regexp->result */ char *BREGEXP::replace(const char *fname) { - struct re_registers regs; int flen = strlen(fname); - int rc = re_search(&preg, fname, flen, 0, flen, ®s); + int rc = re_search(&preg, (BREGEX_CAST char*) fname, flen, 0, flen, ®s); if (rc < 0) { printf("E: regex mismatch\n"); @@ -248,6 +270,7 @@ char *BREGEXP::edit_subst(const char *fname, struct re_registers *regs) } } + /* we copy what is out of the match */ strcpy(result + i, fname + regs->end[0]); return result; @@ -260,6 +283,8 @@ void BREGEXP::debug() printf("result=%s\n", result?result:"(null)"); } +#ifdef TEST + int main(int argc, char **argv) { BREGEXP *reg; @@ -274,3 +299,4 @@ int main(int argc, char **argv) exit(0); } +#endif diff --git a/bacula/patches/testing/breg.h b/bacula/patches/testing/breg.h index 746e04db42..12e907e332 100644 --- a/bacula/patches/testing/breg.h +++ b/bacula/patches/testing/breg.h @@ -1,6 +1,6 @@ /* * Bacula BREGEXP Structure definition for FileDaemon - * Eric Bollengier Mars 2007 + * Eric Bollengier March 2007 * Version $Id$ */ /* @@ -35,6 +35,7 @@ #ifndef __BREG_H_ #define __BREG_H_ 1 +//#undef HAVE_REGEX_H #ifndef HAVE_REGEX_H #include "bregex.h" @@ -49,7 +50,7 @@ * BREGEXP *breg = new_bregexp("!/prod!/test!"); * char *filename = breg->replace("/prod/data.dat"); * or - * filename = breg->result; + * char *filename = breg->result; * free_bregexp(breg); */ @@ -66,8 +67,9 @@ public: POOLMEM *expr; /* search epression */ POOLMEM *subst; /* substitution */ regex_t preg; /* regex_t result of regcomp() */ - - int nmatch; + struct re_registers regs; /* contains match */ + + int *_regs_match; char *return_fname(const char *fname, int len); /* return fname as result */ char *edit_subst(const char *fname, struct re_registers *regs); diff --git a/bacula/patches/testing/bregsed.c b/bacula/patches/testing/bregsed.c new file mode 100644 index 0000000000..f11e538519 --- /dev/null +++ b/bacula/patches/testing/bregsed.c @@ -0,0 +1,156 @@ +/* + * 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(); + + BREGEXP *reg; + + reg = new_bregexp(expr); + + if (!reg) { + 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); + reg->replace(data); + printf("%s\n", reg->result); + } + fclose(fd); + free_bregexp(reg); + exit(0); +} +/* + TODO: + - ajout /g + + - tests + * test avec /i (visiblement il ne marche pas sur bregexp.c) + * test avec un sed et faire un diff + * test avec une boucle pour voir les fuites + * tester les cas possibles pour la compilation d'une expression + - manque le depart, le milieu, la fin, utilise des groupes sans + reference... + +*/ -- 2.39.5