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