2 * Test program for testing regular expressions.
8 Bacula® - The Network Backup Solution
10 Copyright (C) 2006-2014 Free Software Foundation Europe e.V.
12 The main author of Bacula is Kern Sibbald, with contributions from many
13 others, a complete list can be found in the file AUTHORS.
15 You may use this file and others of this release according to the
16 license defined in the LICENSE file, which includes the Affero General
17 Public License, v3.0 ("AGPLv3") and some additional permissions and
18 terms pursuant to its AGPLv3 Section 7.
20 Bacula® is a registered trademark of Kern Sibbald.
26 * If you define BACULA_REGEX, bregex will be built with the
27 * Bacula bregex library, which is the same code that we
28 * use on Win32, thus using Linux, you can test your Win32
29 * expressions. Otherwise, this program will link with the
30 * system library routines.
32 //#define BACULA_REGEX
36 #include "lib/bregex.h"
40 #include "lib/bregex.h"
52 "Usage: bregex [-d debug_level] -f <data-file>\n"
53 " -f specify file of data to be matched\n"
54 " -l suppress line numbers\n"
55 " -n print lines that do not match\n"
56 " -d <nn> set debug level to <nn>\n"
57 " -dt print timestamp in debug output\n"
58 " -? print this message.\n"
65 int main(int argc, char *const *argv)
74 bool match_only = true;
76 bool no_linenos = false;
79 setlocale(LC_ALL, "");
80 bindtextdomain("bacula", LOCALEDIR);
83 while ((ch = getopt(argc, argv, "d:f:n?")) != -1) {
85 case 'd': /* set debug level */
89 debug_level = atoi(optarg);
90 if (debug_level <= 0) {
118 printf("A data file must be specified.\n");
125 printf("Enter regex pattern: ");
126 if (fgets(pat, sizeof(pat)-1, stdin) == NULL) {
129 strip_trailing_newline(pat);
133 rc = regcomp(&preg, pat, REG_EXTENDED);
135 regerror(rc, &preg, prbuf, sizeof(prbuf));
136 printf("Regex compile error: %s\n", prbuf);
139 fd = fopen(fname, "r");
141 printf(_("Could not open data file: %s\n"), fname);
145 while (fgets(data, sizeof(data)-1, fd)) {
146 const int nmatch = 30;
147 regmatch_t pmatch[nmatch];
148 strip_trailing_newline(data);
150 rc = regexec(&preg, data, nmatch, pmatch, 0);
151 if ((match_only && rc == 0) || (!match_only && rc != 0)) {
153 printf("%s\n", data);
155 printf("%5d: %s\n", lineno, data);