]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bregex.c
Change copyright as per agreement with FSFE + update copyright year
[bacula/bacula] / bacula / src / tools / bregex.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2016 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is 
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  * Test program for testing regular expressions.
21  *
22  *  Kern Sibbald, MMVI
23  *
24  */
25
26 #include "bacula.h"
27
28 /*
29  *  If you define BACULA_REGEX, bregex will be built with the
30  *  Bacula bregex library, which is the same code that we
31  *  use on Win32, thus using Linux, you can test your Win32
32  *  expressions. Otherwise, this program will link with the
33  *  system library routines.
34  */
35 //#define BACULA_REGEX
36
37 #ifdef BACULA_REGEX
38
39 #include "lib/bregex.h"
40
41 #else
42 #ifndef HAVE_REGEX_H
43 #include "lib/bregex.h"
44 #else
45 #include <regex.h>
46 #endif
47
48 #endif
49
50
51 static void usage()
52 {
53    fprintf(stderr,
54 "\n"
55 "Usage: bregex [-d debug_level] -f <data-file>\n"
56 "       -f          specify file of data to be matched\n"
57 "       -l          suppress line numbers\n"
58 "       -n          print lines that do not match\n"
59 "       -d <nn>     set debug level to <nn>\n"
60 "       -dt         print timestamp in debug output\n"
61 "       -?          print this message.\n"
62 "\n\n");
63
64    exit(1);
65 }
66
67
68 int main(int argc, char *const *argv)
69 {
70    regex_t preg;
71    char prbuf[500];
72    char *fname = NULL;
73    int rc, ch;
74    char data[1000];
75    char pat[500];
76    FILE *fd;
77    bool match_only = true;
78    int lineno;
79    bool no_linenos = false;
80    
81
82    setlocale(LC_ALL, "");
83    bindtextdomain("bacula", LOCALEDIR);
84    textdomain("bacula");
85
86    while ((ch = getopt(argc, argv, "d:f:n?")) != -1) {
87       switch (ch) {
88       case 'd':                       /* set debug level */
89          if (*optarg == 't') {
90             dbg_timestamp = true;
91          } else {
92             debug_level = atoi(optarg);
93             if (debug_level <= 0) {
94                debug_level = 1;
95             }
96          }
97          break;
98
99       case 'f':                       /* data */
100          fname = optarg;
101          break;
102
103       case 'l':
104          no_linenos = true;
105          break;
106
107       case 'n':
108          match_only = false;
109          break;
110
111       case '?':
112       default:
113          usage();
114
115       }
116    }
117    argc -= optind;
118    argv += optind;
119
120    if (!fname) {
121       printf("A data file must be specified.\n");
122       usage();
123    }
124
125    OSDependentInit();
126
127    for ( ;; ) {
128       printf("Enter regex pattern: ");
129       if (fgets(pat, sizeof(pat)-1, stdin) == NULL) {
130          break;
131       }
132       strip_trailing_newline(pat);
133       if (pat[0] == 0) {
134          exit(0);
135       }
136       rc = regcomp(&preg, pat, REG_EXTENDED);
137       if (rc != 0) {
138          regerror(rc, &preg, prbuf, sizeof(prbuf));
139          printf("Regex compile error: %s\n", prbuf);
140          continue;
141       }
142       fd = fopen(fname, "r");
143       if (!fd) {
144          printf(_("Could not open data file: %s\n"), fname);
145          exit(1);
146       }
147       lineno = 0;
148       while (fgets(data, sizeof(data)-1, fd)) {
149          const int nmatch = 30;
150          regmatch_t pmatch[nmatch];
151          strip_trailing_newline(data);
152          lineno++;
153          rc = regexec(&preg, data, nmatch, pmatch,  0);
154          if ((match_only && rc == 0) || (!match_only && rc != 0)) {
155             if (no_linenos) {
156                printf("%s\n", data);
157             } else {
158                printf("%5d: %s\n", lineno, data);
159             }
160          }
161       }
162       fclose(fd);
163       regfree(&preg);
164    }
165    exit(0);
166 }