]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bwild.c
Fix header file includes.
[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    OSDependentInit();
94
95    for ( ;; ) {
96       printf("Enter a wild-card: ");
97       if (fgets(pat, sizeof(pat)-1, stdin) == NULL) {
98          break;
99       }
100       strip_trailing_newline(pat);
101       if (pat[0] == 0) {
102          exit(0);
103       }
104       fd = fopen(fname, "r");
105       if (!fd) {
106          printf(_("Could not open data file: %s\n"), fname);
107          exit(1);
108       }
109       lineno = 0;
110       while (fgets(data, sizeof(data)-1, fd)) {
111          strip_trailing_newline(data);
112          lineno++;
113          rc = fnmatch(pat, data, ic);
114          if ((match_only && rc == 0) || (!match_only && rc != 0)) {
115             if (no_linenos) {
116                printf("%s\n", data);
117             } else {
118                printf("%5d: %s\n", lineno, data);
119             }
120          }
121       }
122       fclose(fd);
123    }
124    exit(0);
125 }