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