]> git.sur5r.net Git - bacula/bacula/blob - bacula/patches/testing/bregtest.c
ebl fix compilation
[bacula/bacula] / bacula / patches / testing / 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 plus additions
17    that are listed 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: bregex [-d debug_level] -f <data-file> -e /test/test2/\n"
54 "       -f          specify file of data to be matched\n"
55 "       -e          specify expression\n"
56 "       -?          print this message.\n"
57 "\n");
58
59    exit(1);
60 }
61
62
63 int main(int argc, char *const *argv)
64 {
65    char tab[500];
66    int len = bregexp_get_build_where_size("/tmp", "/tmp/toto", ".old");
67    
68    printf("%s\n", bregexp_build_where(tab, len, "/tmp", "/tmp/toto!", ".old"));
69    exit(0);
70
71
72    regex_t preg;
73    char prbuf[500];
74    char *fname = NULL;
75    char *expr = NULL;
76    int rc, ch;
77    char data[1000];
78    char pat[500];
79    FILE *fd;
80    bool match_only = true;
81    int lineno;
82    bool no_linenos = false;
83    
84
85    setlocale(LC_ALL, "");
86    bindtextdomain("bacula", LOCALEDIR);
87    textdomain("bacula");
88
89    while ((ch = getopt(argc, argv, "d:f:e:")) != -1) {
90       switch (ch) {
91       case 'd':                       /* set debug level */
92          debug_level = atoi(optarg);
93          if (debug_level <= 0) {
94             debug_level = 1;
95          }
96          break;
97
98       case 'f':                       /* data */
99          fname = optarg;
100          break;
101
102       case 'e':
103          expr = optarg;
104          break;
105
106       case '?':
107       default:
108          usage();
109
110       }
111    }
112    argc -= optind;
113    argv += optind;
114
115    if (!fname) {
116       printf("A data file must be specified.\n");
117       usage();
118    }
119
120    if (!expr) {
121       printf("An expression must be specified.\n");
122       usage();
123    }
124
125    OSDependentInit();
126
127    alist *list;
128    char *p;
129    
130    list = get_bregexps(expr);
131
132    if (!list) {
133       printf("Can't use %s as 'sed' expression\n", expr);
134       exit (1);
135    }
136
137    fd = fopen(fname, "r");
138    if (!fd) {
139       printf(_("Could not open data file: %s\n"), fname);
140       exit(1);
141    }
142
143    while (fgets(data, sizeof(data)-1, fd)) {
144       strip_trailing_newline(data);
145       apply_bregexps(data, list, &p);
146       printf("%s => %s\n", data, p);
147    }
148    fclose(fd);
149    free_bregexps(list);
150    delete list;
151    exit(0);
152 }
153 /*
154   TODO: 
155    - ajout /g
156
157    - tests 
158    * test avec /i
159    * test avec un sed et faire un diff
160    * test avec une boucle pour voir les fuites
161
162 */