]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bwild.c
Backport from BEE
[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-2014 Free Software Foundation Europe e.V.
11
12    The main author of Bacula is Kern Sibbald, with contributions from many
13    others, a complete list can be found in the file AUTHORS.
14
15    You may use this file and others of this release according to the
16    license defined in the LICENSE file, which includes the Affero General
17    Public License, v3.0 ("AGPLv3") and some additional permissions and
18    terms pursuant to its AGPLv3 Section 7.
19
20    Bacula® is a registered trademark of Kern Sibbald.
21 */
22
23 #include "bacula.h"
24 #include "lib/fnmatch.h"
25
26 static void usage()
27 {
28    fprintf(stderr,
29 "\n"
30 "Usage: bwild [-d debug_level] -f <data-file>\n"
31 "       -f          specify file of data to be matched\n"
32 "       -i          use case insenitive match\n"
33 "       -l          suppress line numbers\n"
34 "       -n          print lines that do not match\n"
35 "       -?          print this message.\n"
36 "\n\n");
37
38    exit(1);
39 }
40
41 int main(int argc, char *const *argv)
42 {
43    char *fname = NULL;
44    int rc, ch;
45    char data[1000];
46    char pat[500];
47    FILE *fd;
48    bool match_only = true;
49    int lineno;
50    bool no_linenos = false;
51    int ic = 0;
52
53
54    setlocale(LC_ALL, "");
55    bindtextdomain("bacula", LOCALEDIR);
56    textdomain("bacula");
57
58    while ((ch = getopt(argc, argv, "d:f:in?")) != -1) {
59       switch (ch) {
60       case 'd':                       /* set debug level */
61          debug_level = atoi(optarg);
62          if (debug_level <= 0) {
63             debug_level = 1;
64          }
65          break;
66
67       case 'f':                       /* data */
68          fname = optarg;
69          break;
70
71       case 'i':                       /* ignore case */
72          ic = FNM_CASEFOLD;
73          break;
74
75       case 'l':
76          no_linenos = true;
77          break;
78
79       case 'n':
80          match_only = false;
81          break;
82
83       case '?':
84       default:
85          usage();
86
87       }
88    }
89    argc -= optind;
90    argv += optind;
91
92    if (!fname) {
93       printf("A data file must be specified.\n");
94       usage();
95    }
96
97    OSDependentInit();
98
99    for ( ;; ) {
100       printf("Enter a wild-card: ");
101       if (fgets(pat, sizeof(pat)-1, stdin) == NULL) {
102          break;
103       }
104       strip_trailing_newline(pat);
105       if (pat[0] == 0) {
106          exit(0);
107       }
108       fd = fopen(fname, "r");
109       if (!fd) {
110          printf(_("Could not open data file: %s\n"), fname);
111          exit(1);
112       }
113       lineno = 0;
114       while (fgets(data, sizeof(data)-1, fd)) {
115          strip_trailing_newline(data);
116          lineno++;
117          rc = fnmatch(pat, data, ic);
118          if ((match_only && rc == 0) || (!match_only && rc != 0)) {
119             if (no_linenos) {
120                printf("%s\n", data);
121             } else {
122                printf("%5d: %s\n", lineno, data);
123             }
124          }
125       }
126       fclose(fd);
127    }
128    exit(0);
129 }