]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bregtest.c
kes Make explicit exception to GPL in LICENSE to permit linking
[bacula/bacula] / bacula / src / tools / bregtest.c
1 /*
2  * Test program for testing regular expressions.
3  *
4  *  Kern Sibbald, MMVI
5  *
6  */
7 /*
8    Bacula\81Â\81® - The Network Backup Solution
9
10    Copyright (C) 2006-2006 Free Software Foundation Europe e.V.
11
12    The main author of Bacula is Kern Sibbald, with contributions from
13    many others, a complete list can be found in the file AUTHORS.
14    This program is Free Software; you can redistribute it and/or
15    modify it under the terms of version two of the GNU General Public
16    License as published by the Free Software Foundation and included
17    in the file LICENSE.
18
19    This program is distributed in the hope that it will be useful, but
20    WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22    General Public License for more details.
23
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27    02110-1301, USA.
28
29    Bacula\81Â\81® is a registered trademark of John Walker.
30    The licensor of Bacula is the Free Software Foundation Europe
31    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Z\81Ã\81¼rich,
32    Switzerland, email:ftf@fsfeurope.org.
33 */
34
35 /*
36  *  If you define BACULA_REGEX, bregex will be built with the
37  *  Bacula bregex library, which is the same code that we
38  *  use on Win32, thus using Linux, you can test your Win32
39  *  expressions. Otherwise, this program will link with the
40  *  system library routines.
41  */
42 //#define BACULA_REGEX
43
44 #include "bacula.h"
45 #include <stdio.h>
46 #include "lib/breg.h"
47
48
49 static void usage()
50 {
51    fprintf(stderr,
52 "\n"
53 "Usage: bregtest [-d debug_level] [-s] -f <data-file> -e /test/test2/\n"
54 "       -f          specify file of data to be matched\n"
55 "       -e          specify expression\n"
56 "       -s          sed output\n"
57 "       -d <nn>     set debug level to <nn>\n"
58 "       -dt         print timestamp in debug output\n"
59 "       -?          print this message.\n"
60 "\n");
61
62    exit(1);
63 }
64
65
66 int main(int argc, char *const *argv)
67 {
68    char *fname = NULL;
69    char *expr = NULL;
70    int ch;
71    bool sed=false;
72    char data[1000];
73    FILE *fd;
74
75    setlocale(LC_ALL, "");
76    bindtextdomain("bacula", LOCALEDIR);
77    textdomain("bacula");
78
79    while ((ch = getopt(argc, argv, "sd:f:e:")) != -1) {
80       switch (ch) {
81       case 'd':                       /* set debug level */
82          if (*optarg == 't') {
83             dbg_timestamp = true;
84          } else {
85             debug_level = atoi(optarg);
86             if (debug_level <= 0) {
87                debug_level = 1;
88             }
89          }
90          break;
91
92       case 'f':                       /* data */
93          fname = optarg;
94          break;
95
96       case 'e':
97          expr = optarg;
98          break;
99
100       case 's':
101          sed=true;
102          break;
103
104       case '?':
105       default:
106          usage();
107
108       }
109    }
110    argc -= optind;
111    argv += optind;
112
113    if (!fname) {
114       printf("A data file must be specified.\n");
115       usage();
116    }
117
118    if (!expr) {
119       printf("An expression must be specified.\n");
120       usage();
121    }
122
123    OSDependentInit();
124
125    alist *list;
126    char *p;
127    
128    list = get_bregexps(expr);
129
130    if (!list) {
131       printf("Can't use %s as 'sed' expression\n", expr);
132       exit (1);
133    }
134
135    fd = fopen(fname, "r");
136    if (!fd) {
137       printf(_("Could not open data file: %s\n"), fname);
138       exit(1);
139    }
140
141    while (fgets(data, sizeof(data)-1, fd)) {
142       strip_trailing_newline(data);
143       apply_bregexps(data, list, &p);
144       if (sed) {
145          printf("%s\n", p);
146       } else {
147          printf("%s => %s\n", data, p);
148       }
149    }
150    fclose(fd);
151    free_bregexps(list);
152    delete list;
153    exit(0);
154 }
155 /*
156   TODO: 
157    - ajout /g
158
159    - tests 
160    * test avec /i
161    * test avec un sed et faire un diff
162    * test avec une boucle pour voir les fuites
163
164 */