]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bwild.c
Remove obsolete .cvsignore files.
[bacula/bacula] / bacula / src / tools / bwild.c
1 /*
2  * Test program for testing wild card expressions
3  * 
4  *  Kern Sibbald, MMVI
5  *
6  */
7 /*
8    Bacula® - 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® 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ürich,
32    Switzerland, email:ftf@fsfeurope.org.
33 */
34
35 #include "bacula.h"
36 #include "lib/fnmatch.h"
37
38 static void usage()
39 {
40    fprintf(stderr,
41 "\n"
42 "Usage: bwild [-d debug_level] -f <data-file>\n"
43 "       -f          specify file of data to be matched\n"
44 "       -i          use case insenitive match\n"
45 "       -l          suppress line numbers\n"
46 "       -n          print lines that do not match\n"
47 "       -?          print this message.\n"
48 "\n\n");
49
50    exit(1);
51 }
52
53 int main(int argc, char *const *argv)
54 {
55    char *fname = NULL;
56    int rc, ch;
57    char data[1000];
58    char pat[500];
59    FILE *fd;
60    bool match_only = true;
61    int lineno;
62    bool no_linenos = false;
63    int ic = 0;
64    
65
66    setlocale(LC_ALL, "");
67    bindtextdomain("bacula", LOCALEDIR);
68    textdomain("bacula");
69
70    while ((ch = getopt(argc, argv, "d:f:in?")) != -1) {
71       switch (ch) {
72       case 'd':                       /* set debug level */
73          debug_level = atoi(optarg);
74          if (debug_level <= 0) {
75             debug_level = 1;
76          }
77          break;
78
79       case 'f':                       /* data */
80          fname = optarg;
81          break;
82
83       case 'i':                       /* ignore case */
84          ic = FNM_CASEFOLD;
85          break;
86
87       case 'l':
88          no_linenos = true;
89          break;
90
91       case 'n':
92          match_only = false;
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    OSDependentInit();
110
111    for ( ;; ) {
112       printf("Enter a wild-card: ");
113       if (fgets(pat, sizeof(pat)-1, stdin) == NULL) {
114          break;
115       }
116       strip_trailing_newline(pat);
117       if (pat[0] == 0) {
118          exit(0);
119       }
120       fd = fopen(fname, "r");
121       if (!fd) {
122          printf(_("Could not open data file: %s\n"), fname);
123          exit(1);
124       }
125       lineno = 0;
126       while (fgets(data, sizeof(data)-1, fd)) {
127          strip_trailing_newline(data);
128          lineno++;
129          rc = fnmatch(pat, data, ic);
130          if ((match_only && rc == 0) || (!match_only && rc != 0)) {
131             if (no_linenos) {
132                printf("%s\n", data);
133             } else {
134                printf("%5d: %s\n", lineno, data);
135             }
136          }
137       }
138       fclose(fd);
139    }
140    exit(0);
141 }