]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bregtest.c
Backport from Bacula Enterprise
[bacula/bacula] / bacula / src / tools / bregtest.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 /*
28  *  If you define BACULA_REGEX, bregex will be built with the
29  *  Bacula bregex library, which is the same code that we
30  *  use on Win32, thus using Linux, you can test your Win32
31  *  expressions. Otherwise, this program will link with the
32  *  system library routines.
33  */
34 //#define BACULA_REGEX
35
36 #include "bacula.h"
37 #include <stdio.h>
38 #include "lib/breg.h"
39
40
41 static void usage()
42 {
43    fprintf(stderr,
44 "\n"
45 "Usage: bregtest [-d debug_level] [-s] -f <data-file> -e /test/test2/\n"
46 "       -f          specify file of data to be matched\n"
47 "       -e          specify expression\n"
48 "       -s          sed output\n"
49 "       -d <nn>     set debug level to <nn>\n"
50 "       -dt         print timestamp in debug output\n"
51 "       -?          print this message.\n"
52 "\n");
53
54    exit(1);
55 }
56
57
58 int main(int argc, char *const *argv)
59 {
60    char *fname = NULL;
61    char *expr = NULL;
62    int ch;
63    bool sed=false;
64    char data[1000];
65    FILE *fd;
66
67    setlocale(LC_ALL, "");
68    bindtextdomain("bacula", LOCALEDIR);
69    textdomain("bacula");
70
71    while ((ch = getopt(argc, argv, "sd:f:e:")) != -1) {
72       switch (ch) {
73       case 'd':                       /* set debug level */
74          if (*optarg == 't') {
75             dbg_timestamp = true;
76          } else {
77             debug_level = atoi(optarg);
78             if (debug_level <= 0) {
79                debug_level = 1;
80             }
81          }
82          break;
83
84       case 'f':                       /* data */
85          fname = optarg;
86          break;
87
88       case 'e':
89          expr = optarg;
90          break;
91
92       case 's':
93          sed=true;
94          break;
95
96       case '?':
97       default:
98          usage();
99
100       }
101    }
102    argc -= optind;
103    argv += optind;
104
105    if (!fname) {
106       printf("A data file must be specified.\n");
107       usage();
108    }
109
110    if (!expr) {
111       printf("An expression must be specified.\n");
112       usage();
113    }
114
115    OSDependentInit();
116
117    alist *list;
118    char *p;
119    
120    list = get_bregexps(expr);
121
122    if (!list) {
123       printf("Can't use %s as 'sed' expression\n", expr);
124       exit (1);
125    }
126
127    fd = fopen(fname, "r");
128    if (!fd) {
129       printf(_("Could not open data file: %s\n"), fname);
130       exit(1);
131    }
132
133    while (fgets(data, sizeof(data)-1, fd)) {
134       strip_trailing_newline(data);
135       apply_bregexps(data, list, &p);
136       if (sed) {
137          printf("%s\n", p);
138       } else {
139          printf("%s => %s\n", data, p);
140       }
141    }
142    fclose(fd);
143    free_bregexps(list);
144    delete list;
145    exit(0);
146 }
147 /*
148   TODO: 
149    - ajout /g
150
151    - tests 
152    * test avec /i
153    * test avec un sed et faire un diff
154    * test avec une boucle pour voir les fuites
155
156 */