]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/bwild.c
This commit was manufactured by cvs2svn to create tag
[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
23 static void usage()
24 {
25    fprintf(stderr,
26 "\n"
27 "Usage: bwild [-d debug_level] -f <data-file>\n"
28 "       -f          specify file of data to be matched\n"
29 "       -i          use case insenitive match\n"
30 "       -l          suppress line numbers\n"
31 "       -n          print lines that do not match\n"
32 "       -?          print this message.\n"
33 "\n\n");
34
35    exit(1);
36 }
37
38 int main(int argc, char *const *argv)
39 {
40    char *fname = NULL;
41    int rc, ch;
42    char data[1000];
43    char pat[500];
44    FILE *fd;
45    bool match_only = true;
46    int lineno;
47    bool no_linenos = false;
48    int ic = 0;
49    
50
51    setlocale(LC_ALL, "");
52    bindtextdomain("bacula", LOCALEDIR);
53    textdomain("bacula");
54
55    while ((ch = getopt(argc, argv, "d:f:in?")) != -1) {
56       switch (ch) {
57       case 'd':                       /* set debug level */
58          debug_level = atoi(optarg);
59          if (debug_level <= 0) {
60             debug_level = 1;
61          }
62          break;
63
64       case 'f':                       /* data */
65          fname = optarg;
66          break;
67
68       case 'i':                       /* ignore case */
69          ic = FNM_CASEFOLD;
70          break;
71
72       case 'l':
73          no_linenos = true;
74          break;
75
76       case 'n':
77          match_only = false;
78          break;
79
80       case '?':
81       default:
82          usage();
83
84       }
85    }
86    argc -= optind;
87    argv += optind;
88
89    if (!fname) {
90       printf("A data file must be specified.\n");
91       usage();
92    }
93
94    for ( ;; ) {
95       printf("Enter a wild-card: ");
96       if (fgets(pat, sizeof(pat)-1, stdin) == NULL) {
97          break;
98       }
99       strip_trailing_newline(pat);
100       if (pat[0] == 0) {
101          exit(0);
102       }
103       fd = fopen(fname, "r");
104       if (!fd) {
105          printf(_("Could not open data file: %s\n"), fname);
106          exit(1);
107       }
108       lineno = 0;
109       while (fgets(data, sizeof(data)-1, fd)) {
110          strip_trailing_newline(data);
111          lineno++;
112          rc = fnmatch(pat, data, ic);
113          if ((match_only && rc == 0) || (!match_only && rc != 0)) {
114             if (no_linenos) {
115                printf("%s\n", data);
116             } else {
117                printf("%5d: %s\n", lineno, data);
118             }
119          }
120       }
121       fclose(fd);
122    }
123    exit(0);
124 }