]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bwild.c
Merge in all the low-risk changes from the Windows branch.
[bacula/bacula] / bacula / src / tools / bwild.c
1 /*
2  * Test program for testing wild card expressions
3  */
4 /*
5    Copyright (C) 2006 Kern Sibbald
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as amended with additional clauses defined in the
10    file LICENSE in the main source directory.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
15    the file LICENSE for additional details.
16
17  */
18
19 #include "bacula.h"
20 #include "lib/fnmatch.h"
21
22 static void usage()
23 {
24    fprintf(stderr,
25 "\n"
26 "Usage: bwild [-d debug_level] -f <data-file>\n"
27 "       -f          specify file of data to be matched\n"
28 "       -i          use case insenitive match\n"
29 "       -l          suppress line numbers\n"
30 "       -n          print lines that do not match\n"
31 "       -?          print this message.\n"
32 "\n\n");
33
34    exit(1);
35 }
36
37 int main(int argc, char *const *argv)
38 {
39    char *fname = NULL;
40    int rc, ch;
41    char data[1000];
42    char pat[500];
43    FILE *fd;
44    bool match_only = true;
45    int lineno;
46    bool no_linenos = false;
47    int ic = 0;
48    
49
50    setlocale(LC_ALL, "");
51    bindtextdomain("bacula", LOCALEDIR);
52    textdomain("bacula");
53
54    while ((ch = getopt(argc, argv, "d:f:in?")) != -1) {
55       switch (ch) {
56       case 'd':                       /* set debug level */
57          debug_level = atoi(optarg);
58          if (debug_level <= 0) {
59             debug_level = 1;
60          }
61          break;
62
63       case 'f':                       /* data */
64          fname = optarg;
65          break;
66
67       case 'i':                       /* ignore case */
68          ic = FNM_CASEFOLD;
69          break;
70
71       case 'l':
72          no_linenos = true;
73          break;
74
75       case 'n':
76          match_only = false;
77          break;
78
79       case '?':
80       default:
81          usage();
82
83       }
84    }
85    argc -= optind;
86    argv += optind;
87
88    if (!fname) {
89       printf("A data file must be specified.\n");
90       usage();
91    }
92
93    for ( ;; ) {
94       printf("Enter a wild-card: ");
95       if (fgets(pat, sizeof(pat)-1, stdin) == NULL) {
96          break;
97       }
98       strip_trailing_newline(pat);
99       if (pat[0] == 0) {
100          exit(0);
101       }
102       fd = fopen(fname, "r");
103       if (!fd) {
104          printf(_("Could not open data file: %s\n"), fname);
105          exit(1);
106       }
107       lineno = 0;
108       while (fgets(data, sizeof(data)-1, fd)) {
109          strip_trailing_newline(data);
110          lineno++;
111          rc = fnmatch(pat, data, ic);
112          if ((match_only && rc == 0) || (!match_only && rc != 0)) {
113             if (no_linenos) {
114                printf("%s\n", data);
115             } else {
116                printf("%5d: %s\n", lineno, data);
117             }
118          }
119       }
120       fclose(fd);
121    }
122    exit(0);
123 }