From 67c86fb7ab1cc3a04bd6e48ac4ea062e5aabe803 Mon Sep 17 00:00:00 2001 From: Eric Bollengier Date: Sun, 22 Apr 2007 18:17:16 +0000 Subject: [PATCH] ebl add a tool to test regexp git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@4595 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/src/tools/bregtest.c | 162 ++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 bacula/src/tools/bregtest.c diff --git a/bacula/src/tools/bregtest.c b/bacula/src/tools/bregtest.c new file mode 100644 index 0000000000..52073ea00f --- /dev/null +++ b/bacula/src/tools/bregtest.c @@ -0,0 +1,162 @@ +/* + * 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) +{ + char tab[500]; + int len = bregexp_get_build_where_size("/tmp", "/tmp/toto", ".old"); + + printf("%s\n", bregexp_build_where(tab, len, "/tmp", "/tmp/toto!", ".old")); + exit(0); + + + 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); + apply_bregexps(data, list, &p); + 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