]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bregtest.c
Add openssl-compat.h which went in wrong directory
[bacula/bacula] / bacula / src / tools / bregtest.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2016 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is 
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  * Test program for testing regular expressions.
21  *
22  *  Kern Sibbald, MMVI
23  *
24  */
25
26 /*
27  *  If you define BACULA_REGEX, bregex will be built with the
28  *  Bacula bregex library, which is the same code that we
29  *  use on Win32, thus using Linux, you can test your Win32
30  *  expressions. Otherwise, this program will link with the
31  *  system library routines.
32  */
33 //#define BACULA_REGEX
34
35 #include "bacula.h"
36 #include <stdio.h>
37 #include "lib/breg.h"
38
39
40 static void usage()
41 {
42    fprintf(stderr,
43 "\n"
44 "Usage: bregtest [-d debug_level] [-s] -f <data-file> -e /test/test2/\n"
45 "       -f          specify file of data to be matched\n"
46 "       -e          specify expression\n"
47 "       -s          sed output\n"
48 "       -d <nn>     set debug level to <nn>\n"
49 "       -dt         print timestamp in debug output\n"
50 "       -?          print this message.\n"
51 "\n");
52
53    exit(1);
54 }
55
56
57 int main(int argc, char *const *argv)
58 {
59    char *fname = NULL;
60    char *expr = NULL;
61    int ch;
62    bool sed=false;
63    char data[1000];
64    FILE *fd;
65
66    setlocale(LC_ALL, "");
67    bindtextdomain("bacula", LOCALEDIR);
68    textdomain("bacula");
69
70    while ((ch = getopt(argc, argv, "sd:f:e:")) != -1) {
71       switch (ch) {
72       case 'd':                       /* set debug level */
73          if (*optarg == 't') {
74             dbg_timestamp = true;
75          } else {
76             debug_level = atoi(optarg);
77             if (debug_level <= 0) {
78                debug_level = 1;
79             }
80          }
81          break;
82
83       case 'f':                       /* data */
84          fname = optarg;
85          break;
86
87       case 'e':
88          expr = optarg;
89          break;
90
91       case 's':
92          sed=true;
93          break;
94
95       case '?':
96       default:
97          usage();
98
99       }
100    }
101    argc -= optind;
102    argv += optind;
103
104    if (!fname) {
105       printf("A data file must be specified.\n");
106       usage();
107    }
108
109    if (!expr) {
110       printf("An expression must be specified.\n");
111       usage();
112    }
113
114    OSDependentInit();
115
116    alist *list;
117    char *p;
118    
119    list = get_bregexps(expr);
120
121    if (!list) {
122       printf("Can't use %s as 'sed' expression\n", expr);
123       exit (1);
124    }
125
126    fd = fopen(fname, "r");
127    if (!fd) {
128       printf(_("Could not open data file: %s\n"), fname);
129       exit(1);
130    }
131
132    while (fgets(data, sizeof(data)-1, fd)) {
133       strip_trailing_newline(data);
134       apply_bregexps(data, list, &p);
135       if (sed) {
136          printf("%s\n", p);
137       } else {
138          printf("%s => %s\n", data, p);
139       }
140    }
141    fclose(fd);
142    free_bregexps(list);
143    delete list;
144    exit(0);
145 }
146 /*
147   TODO: 
148    - ajout /g
149
150    - tests 
151    * test avec /i
152    * test avec un sed et faire un diff
153    * test avec une boucle pour voir les fuites
154
155 */