/
bregtest.cc
158 lines (130 loc) · 3.56 KB
/
bregtest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2006-2008 Free Software Foundation Europe e.V.
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation, which is
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
Affero General Public License for more details.
You should have received a copy of the GNU Affero 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.
*/
/*
* Test program for testing regular expressions.
*
* Kern Sibbald, MMVI
*/
/*
* If you define BAREOS_REGEX, bregex will be built with the
* Bareos 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 BAREOS_REGEX
/* pull in dummy functions that libbareos depends ons */
#include "tools/dummy_config_parser.cc"
#include "include/bareos.h"
#include <stdio.h>
#include "lib/breg.h"
static void usage()
{
fprintf(stderr,
"\n"
"Usage: bregtest [-d debug_level] [-s] -f <data-file> -e /test/test2/\n"
" -f specify file of data to be matched\n"
" -e specify expression\n"
" -s sed output\n"
" -d <nn> set debug level to <nn>\n"
" -dt print timestamp in debug output\n"
" -? print this message.\n"
"\n");
exit(1);
}
int main(int argc, char *const *argv)
{
char *fname = NULL;
char *expr = NULL;
int ch;
bool sed=false;
char data[1000];
FILE *fd;
setlocale(LC_ALL, "");
bindtextdomain("bareos", LOCALEDIR);
textdomain("bareos");
while ((ch = getopt(argc, argv, "sd:f:e:")) != -1) {
switch (ch) {
case 'd': /* set debug level */
if (*optarg == 't') {
dbg_timestamp = true;
} else {
debug_level = atoi(optarg);
if (debug_level <= 0) {
debug_level = 1;
}
}
break;
case 'f': /* data */
fname = optarg;
break;
case 'e':
expr = optarg;
break;
case 's':
sed=true;
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)) {
StripTrailingNewline(data);
ApplyBregexps(data, list, &p);
if (sed) {
printf("%s\n", p);
} else {
printf("%s => %s\n", data, p);
}
}
fclose(fd);
FreeBregexps(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
*/