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.
29 "Usage: regex [-d debug_level] -f <data-file>\n"
30 " -f specify file of data to be matched\n"
31 " -l suppress line numbers\n"
32 " -n print lines that do not match\n"
33 " -? print this message.\n"
40 int main(int argc, char *const *argv)
43 printf("The regex libraries don't seem to be available.\n");
53 bool match_only = true;
55 bool no_linenos = false;
58 setlocale(LC_ALL, "");
59 bindtextdomain("bacula", LOCALEDIR);
62 while ((ch = getopt(argc, argv, "d:f:n?")) != -1) {
64 case 'd': /* set debug level */
65 debug_level = atoi(optarg);
66 if (debug_level <= 0) {
93 printf("A data file must be specified.\n");
98 printf("Enter regex pattern: ");
99 if (fgets(pat, sizeof(pat)-1, stdin) == NULL) {
102 strip_trailing_newline(pat);
106 rc = regcomp(&preg, pat, REG_EXTENDED);
108 regerror(rc, &preg, prbuf, sizeof(prbuf));
109 printf("Regex compile error: %s\n", prbuf);
112 fd = fopen(fname, "r");
114 printf(_("Could not open data file: %s\n"), fname);
118 while (fgets(data, sizeof(data)-1, fd)) {
119 const int nmatch = 30;
120 regmatch_t pmatch[nmatch];
121 strip_trailing_newline(data);
123 rc = regexec(&preg, data, nmatch, pmatch, 0);
124 if ((match_only && rc == 0) || (!match_only && rc != 0)) {
126 printf("%s\n", data);
128 printf("%5d: %s\n", lineno, data);