]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bregex.c
This commit was manufactured by cvs2svn to create tag
[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 //#define TESTLIB
22 #ifdef TESTLIB
23
24 #include "lib/bregex.h"
25
26 #else
27 #ifndef HAVE_REGEX_H
28 #include "lib/bregex.h"
29 #else
30 #include <regex.h>
31 #endif
32
33 #endif
34
35
36 static void usage()
37 {
38    fprintf(stderr,
39 "\n"
40 "Usage: bregex [-d debug_level] -f <data-file>\n"
41 "       -f          specify file of data to be matched\n"
42 "       -l          suppress line numbers\n"
43 "       -n          print lines that do not match\n"
44 "       -?          print this message.\n"
45 "\n\n");
46
47    exit(1);
48 }
49
50
51 int main(int argc, char *const *argv)
52 {
53    regex_t preg;
54    char prbuf[500];
55    char *fname = NULL;
56    int rc, ch;
57    char data[1000];
58    char pat[500];
59    FILE *fd;
60    bool match_only = true;
61    int lineno;
62    bool no_linenos = false;
63    
64
65    setlocale(LC_ALL, "");
66    bindtextdomain("bacula", LOCALEDIR);
67    textdomain("bacula");
68
69    while ((ch = getopt(argc, argv, "d:f:n?")) != -1) {
70       switch (ch) {
71       case 'd':                       /* set debug level */
72          debug_level = atoi(optarg);
73          if (debug_level <= 0) {
74             debug_level = 1;
75          }
76          break;
77
78       case 'f':                       /* data */
79          fname = optarg;
80          break;
81
82       case 'l':
83          no_linenos = true;
84          break;
85
86       case 'n':
87          match_only = false;
88          break;
89
90       case '?':
91       default:
92          usage();
93
94       }
95    }
96    argc -= optind;
97    argv += optind;
98
99    if (!fname) {
100       printf("A data file must be specified.\n");
101       usage();
102    }
103
104    for ( ;; ) {
105       printf("Enter regex pattern: ");
106       if (fgets(pat, sizeof(pat)-1, stdin) == NULL) {
107          break;
108       }
109       strip_trailing_newline(pat);
110       if (pat[0] == 0) {
111          exit(0);
112       }
113       rc = regcomp(&preg, pat, REG_EXTENDED);
114       if (rc != 0) {
115          regerror(rc, &preg, prbuf, sizeof(prbuf));
116          printf("Regex compile error: %s\n", prbuf);
117          continue;
118       }
119       fd = fopen(fname, "r");
120       if (!fd) {
121          printf(_("Could not open data file: %s\n"), fname);
122          exit(1);
123       }
124       lineno = 0;
125       while (fgets(data, sizeof(data)-1, fd)) {
126          const int nmatch = 30;
127          regmatch_t pmatch[nmatch];
128          strip_trailing_newline(data);
129          lineno++;
130          rc = regexec(&preg, data, nmatch, pmatch,  0);
131          if ((match_only && rc == 0) || (!match_only && rc != 0)) {
132             if (no_linenos) {
133                printf("%s\n", data);
134             } else {
135                printf("%5d: %s\n", lineno, data);
136             }
137          }
138       }
139       fclose(fd);
140       regfree(&preg);
141    }
142    exit(0);
143 }