]> git.sur5r.net Git - bacula/bacula/blob - bacula/patches/testing/bregtest.c
ebl update
[bacula/bacula] / bacula / patches / testing / bregtest.c
1 /*
2  * Test program for testing regular expressions.
3  *
4  *  Kern Sibbald, MMVI
5  *
6  */
7 /*
8    Bacula® - The Network Backup Solution
9
10    Copyright (C) 2006-2006 Free Software Foundation Europe e.V.
11
12    The main author of Bacula is Kern Sibbald, with contributions from
13    many others, a complete list can be found in the file AUTHORS.
14    This program is Free Software; you can redistribute it and/or
15    modify it under the terms of version two of the GNU General Public
16    License as published by the Free Software Foundation plus additions
17    that are listed in the file LICENSE.
18
19    This program is distributed in the hope that it will be useful, but
20    WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22    General Public License for more details.
23
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27    02110-1301, USA.
28
29    Bacula® is a registered trademark of John Walker.
30    The licensor of Bacula is the Free Software Foundation Europe
31    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
32    Switzerland, email:ftf@fsfeurope.org.
33 */
34
35 /*
36  *  If you define BACULA_REGEX, bregex will be built with the
37  *  Bacula bregex library, which is the same code that we
38  *  use on Win32, thus using Linux, you can test your Win32
39  *  expressions. Otherwise, this program will link with the
40  *  system library routines.
41  */
42 //#define BACULA_REGEX
43
44 #include "bacula.h"
45 #include <stdio.h>
46 #include "lib/breg.h"
47
48
49 static void usage()
50 {
51    fprintf(stderr,
52 "\n"
53 "Usage: bregex [-d debug_level] -f <data-file> -e /test/test2/\n"
54 "       -f          specify file of data to be matched\n"
55 "       -e          specify expression\n"
56 "       -?          print this message.\n"
57 "\n");
58
59    exit(1);
60 }
61
62
63 int main(int argc, char *const *argv)
64 {
65         printf("%s\n", bregexp_build_where("/tmp", NULL, ".old"));
66         exit(0);
67
68
69    regex_t preg;
70    char prbuf[500];
71    char *fname = NULL;
72    char *expr = NULL;
73    int rc, ch;
74    char data[1000];
75    char pat[500];
76    FILE *fd;
77    bool match_only = true;
78    int lineno;
79    bool no_linenos = false;
80    
81
82    setlocale(LC_ALL, "");
83    bindtextdomain("bacula", LOCALEDIR);
84    textdomain("bacula");
85
86    while ((ch = getopt(argc, argv, "d:f:e:")) != -1) {
87       switch (ch) {
88       case 'd':                       /* set debug level */
89          debug_level = atoi(optarg);
90          if (debug_level <= 0) {
91             debug_level = 1;
92          }
93          break;
94
95       case 'f':                       /* data */
96          fname = optarg;
97          break;
98
99       case 'e':
100          expr = optarg;
101          break;
102
103       case '?':
104       default:
105          usage();
106
107       }
108    }
109    argc -= optind;
110    argv += optind;
111
112    if (!fname) {
113       printf("A data file must be specified.\n");
114       usage();
115    }
116
117    if (!expr) {
118       printf("An expression must be specified.\n");
119       usage();
120    }
121
122    OSDependentInit();
123
124    alist *list;
125    char *p;
126    
127    list = get_bregexps(expr);
128
129    if (!list) {
130       printf("Can't use %s as 'sed' expression\n", expr);
131       exit (1);
132    }
133
134    fd = fopen(fname, "r");
135    if (!fd) {
136       printf(_("Could not open data file: %s\n"), fname);
137       exit(1);
138    }
139
140    while (fgets(data, sizeof(data)-1, fd)) {
141       strip_trailing_newline(data);
142       apply_bregexps(data, list, &p);
143       printf("%s => %s\n", data, p);
144    }
145    fclose(fd);
146    free_bregexps(list);
147    delete list;
148    exit(0);
149 }
150 /*
151   TODO: 
152    - ajout /g
153
154    - tests 
155    * test avec /i
156    * test avec un sed et faire un diff
157    * test avec une boucle pour voir les fuites
158
159 */