]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bregex.c
Convert to pure GPL v2 license.
[bacula/bacula] / bacula / src / tools / bregex.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 and included
17    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 #include "bacula.h"
36
37 /*
38  *  If you define BACULA_REGEX, bregex will be built with the
39  *  Bacula bregex library, which is the same code that we
40  *  use on Win32, thus using Linux, you can test your Win32
41  *  expressions. Otherwise, this program will link with the
42  *  system library routines.
43  */
44 //#define BACULA_REGEX
45
46 #ifdef BACULA_REGEX
47
48 #include "lib/bregex.h"
49
50 #else
51 #ifndef HAVE_REGEX_H
52 #include "lib/bregex.h"
53 #else
54 #include <regex.h>
55 #endif
56
57 #endif
58
59
60 static void usage()
61 {
62    fprintf(stderr,
63 "\n"
64 "Usage: bregex [-d debug_level] -f <data-file>\n"
65 "       -f          specify file of data to be matched\n"
66 "       -l          suppress line numbers\n"
67 "       -n          print lines that do not match\n"
68 "       -?          print this message.\n"
69 "\n\n");
70
71    exit(1);
72 }
73
74
75 int main(int argc, char *const *argv)
76 {
77    regex_t preg;
78    char prbuf[500];
79    char *fname = NULL;
80    int rc, ch;
81    char data[1000];
82    char pat[500];
83    FILE *fd;
84    bool match_only = true;
85    int lineno;
86    bool no_linenos = false;
87    
88
89    setlocale(LC_ALL, "");
90    bindtextdomain("bacula", LOCALEDIR);
91    textdomain("bacula");
92
93    while ((ch = getopt(argc, argv, "d:f:n?")) != -1) {
94       switch (ch) {
95       case 'd':                       /* set debug level */
96          debug_level = atoi(optarg);
97          if (debug_level <= 0) {
98             debug_level = 1;
99          }
100          break;
101
102       case 'f':                       /* data */
103          fname = optarg;
104          break;
105
106       case 'l':
107          no_linenos = true;
108          break;
109
110       case 'n':
111          match_only = false;
112          break;
113
114       case '?':
115       default:
116          usage();
117
118       }
119    }
120    argc -= optind;
121    argv += optind;
122
123    if (!fname) {
124       printf("A data file must be specified.\n");
125       usage();
126    }
127
128    OSDependentInit();
129
130    for ( ;; ) {
131       printf("Enter regex pattern: ");
132       if (fgets(pat, sizeof(pat)-1, stdin) == NULL) {
133          break;
134       }
135       strip_trailing_newline(pat);
136       if (pat[0] == 0) {
137          exit(0);
138       }
139       rc = regcomp(&preg, pat, REG_EXTENDED);
140       if (rc != 0) {
141          regerror(rc, &preg, prbuf, sizeof(prbuf));
142          printf("Regex compile error: %s\n", prbuf);
143          continue;
144       }
145       fd = fopen(fname, "r");
146       if (!fd) {
147          printf(_("Could not open data file: %s\n"), fname);
148          exit(1);
149       }
150       lineno = 0;
151       while (fgets(data, sizeof(data)-1, fd)) {
152          const int nmatch = 30;
153          regmatch_t pmatch[nmatch];
154          strip_trailing_newline(data);
155          lineno++;
156          rc = regexec(&preg, data, nmatch, pmatch,  0);
157          if ((match_only && rc == 0) || (!match_only && rc != 0)) {
158             if (no_linenos) {
159                printf("%s\n", data);
160             } else {
161                printf("%5d: %s\n", lineno, data);
162             }
163          }
164       }
165       fclose(fd);
166       regfree(&preg);
167    }
168    exit(0);
169 }