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