-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathw3cgrep.c
144 lines (124 loc) · 3.07 KB
/
w3cgrep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* w3cgrep.c --
*
* A poor man's grep which uses XSD regular expressions instead of any
* of the other regular expressions.
*
* Use this tool to check yang patterns. Requires libxml2.
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#if !defined(WIN32)
/* Should be possible to support this
for win32 too, but it's not needed */
#include <libxml/xmlregexp.h>
#endif
static const char *progname = "w3cgrep";
static int invert_flag = 0;
static int count_flag = 0;
static int mult_flag = 0;
static void
grep_file(FILE *in, xmlRegexpPtr regex, const char *fname)
{
char line[1024];
int len;
unsigned count = 0;
while (! feof(in)) {
if (! fgets(line, sizeof(line), in)) {
break;
}
len = strlen(line);
if (len) line[len-1] = 0;
if (xmlRegexpExec(regex, BAD_CAST(line)) == ! invert_flag) {
count++;
if (! count_flag) {
if (mult_flag) {
fprintf(stdout, "%s:", fname);
}
fprintf(stdout, "%s\n", line);
}
}
}
if (count_flag) {
if (mult_flag) {
fprintf(stdout, "%s:", fname);
}
fprintf(stdout, "%u\n", count);
}
}
static int
grep(const char *filename, xmlRegexpPtr regex)
{
FILE *in;
const char *fname = filename;
if (! filename || strcmp(filename, "-") == 0) {
clearerr(stdin);
in = stdin;
fname = NULL;
} else {
in = fopen(filename, "r");
if (! in) {
perror(progname);
return -1;
}
}
grep_file(in, regex, fname);
if (fflush(stdout) || ferror(stdout) || ferror(in)) {
perror(progname);
exit(1);
}
if (in != stdin) {
fclose(in);
}
return 0;
}
int
main(int argc, char **argv)
{
const char *usage = "Usage: %s [-c] [-h] [-v] [V] pattern files ... \n";
xmlRegexpPtr regex;
int c, status = EXIT_SUCCESS;
while ((c = getopt(argc, argv, "chivV")) >= 0) {
switch (c) {
case 'c':
count_flag = 1;
break;
case 'h':
printf(usage, progname);
exit(EXIT_SUCCESS);
case 'v':
invert_flag = 1;
break;
case 'V':
printf("%s version 0.2.0\n", progname);
exit(EXIT_SUCCESS);
default:
exit(EXIT_FAILURE);
}
}
if (optind == argc) {
fprintf(stderr, usage, progname);
exit(EXIT_FAILURE);
}
regex = xmlRegexpCompile(BAD_CAST(argv[optind]));
if (! regex) {
fprintf(stderr, "%s: regex pattern compilation failed\n", progname);
return EXIT_FAILURE;
}
optind++;
if (optind == argc) {
if (grep(NULL, regex) < 0) {
status = EXIT_FAILURE;
}
} else {
mult_flag = (argc - optind) > 1;
for (; optind < argc; optind++) {
if (grep(argv[optind], regex) < 0) {
status = EXIT_FAILURE;
}
}
}
xmlRegFreeRegexp(regex);
return status;
}