2 * Test program for testing regular expressions.
5 Copyright (C) 2006 Kern Sibbald
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 version 2 as amended with additional clauses defined in the
10 file LICENSE in the main source directory.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 the file LICENSE for additional details.
22 * If you define BACULA_REGEX, bregex will be built with the
23 * Bacula bregex library, which is the same code that we
24 * use on Win32, thus using Linux, you can test your Win32
25 * expressions. Otherwise, this program will link with the
26 * system library routines.
28 //#define BACULA_REGEX
32 #include "lib/bregex.h"
36 #include "lib/bregex.h"
48 "Usage: bregex [-d debug_level] -f <data-file>\n"
49 " -f specify file of data to be matched\n"
50 " -l suppress line numbers\n"
51 " -n print lines that do not match\n"
52 " -? print this message.\n"
59 int main(int argc, char *const *argv)
68 bool match_only = true;
70 bool no_linenos = false;
73 setlocale(LC_ALL, "");
74 bindtextdomain("bacula", LOCALEDIR);
77 while ((ch = getopt(argc, argv, "d:f:n?")) != -1) {
79 case 'd': /* set debug level */
80 debug_level = atoi(optarg);
81 if (debug_level <= 0) {
108 printf("A data file must be specified.\n");
113 printf("Enter regex pattern: ");
114 if (fgets(pat, sizeof(pat)-1, stdin) == NULL) {
117 strip_trailing_newline(pat);
121 rc = regcomp(&preg, pat, REG_EXTENDED);
123 regerror(rc, &preg, prbuf, sizeof(prbuf));
124 printf("Regex compile error: %s\n", prbuf);
127 fd = fopen(fname, "r");
129 printf(_("Could not open data file: %s\n"), fname);
133 while (fgets(data, sizeof(data)-1, fd)) {
134 const int nmatch = 30;
135 regmatch_t pmatch[nmatch];
136 strip_trailing_newline(data);
138 rc = regexec(&preg, data, nmatch, pmatch, 0);
139 if ((match_only && rc == 0) || (!match_only && rc != 0)) {
141 printf("%s\n", data);
143 printf("%5d: %s\n", lineno, data);