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