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