-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrep.cpp
More file actions
180 lines (156 loc) · 5.48 KB
/
grep.cpp
File metadata and controls
180 lines (156 loc) · 5.48 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// grep — search patterns in text
// Supported flags: -E (extended regex), -i (ignore case), -v (invert match),
// -n (line numbers), -r (recursive), -c (count only),
// -l (files with matches), -q (quiet)
#include <cstdio>
#include <filesystem>
#include <string>
#include <string_view>
#include <vector>
#include <cfbox/args.hpp>
#include <cfbox/help.hpp>
#include <cfbox/io.hpp>
#include <cfbox/regex.hpp>
#include <cfbox/error.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "grep",
.version = CFBOX_VERSION_STRING,
.one_line = "search patterns in text",
.usage = "grep [OPTIONS] PATTERN [FILE]...",
.options = " -E extended regex\n"
" -i ignore case\n"
" -v invert match\n"
" -n print line numbers\n"
" -r recursive search\n"
" -c print only a count of matching lines\n"
" -l print only names of files with matches\n"
" -q quiet mode",
.extra = "",
};
struct GrepOptions {
bool extended = false;
bool ignore_case = false;
bool invert = false;
bool line_numbers = false;
bool recursive = false;
bool count_only = false;
bool files_with_matches = false;
bool quiet = false;
};
auto grep_file(const std::string& pattern, const GrepOptions& opts,
std::string_view path, bool print_filename) -> int {
int cflags = opts.extended ? REG_EXTENDED : 0;
if (opts.ignore_case) cflags |= REG_ICASE;
cfbox::util::scoped_regex re;
if (re.compile(pattern.c_str(), cflags) != 0) {
CFBOX_ERR("grep", "invalid regex: %s", pattern.c_str());
return 2;
}
int match_count = 0;
int found_any = 0;
std::size_t line_num = 0;
auto process_line = [&](const std::string& line) -> bool {
++line_num;
bool matched = re.exec(line.c_str(), 0, nullptr, 0) == 0;
if (opts.invert) matched = !matched;
if (matched) {
++match_count;
found_any = 1;
if (opts.quiet) return false;
if (opts.files_with_matches) {
std::printf("%s\n", path.data());
return false;
}
if (!opts.count_only) {
if (print_filename) {
std::printf("%s:", path.data());
}
if (opts.line_numbers) {
std::printf("%zu:", line_num);
}
std::printf("%s\n", line.c_str());
}
}
return true;
};
auto result = cfbox::io::for_each_line(path, process_line);
if (!result) {
CFBOX_ERR("grep", "%s", result.error().msg.c_str());
return 2;
}
if (opts.count_only) {
if (print_filename) {
std::printf("%s:", std::string{path}.c_str());
}
std::printf("%d\n", match_count);
}
return found_any ? 0 : 1;
}
auto grep_recursive(const std::string& pattern, const GrepOptions& opts,
const std::filesystem::path& dir) -> int {
int final_rc = 1;
std::error_code ec;
for (const auto& entry : std::filesystem::recursive_directory_iterator(dir, ec)) {
if (ec) continue;
if (!entry.is_regular_file()) continue;
int rc = grep_file(pattern, opts, entry.path().string(), true);
if (rc == 2) return 2;
if (rc == 0) final_rc = 0;
}
return final_rc;
}
} // namespace
auto grep_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'E', false, "extended-regexp"},
cfbox::args::OptSpec{'i', false, "ignore-case"},
cfbox::args::OptSpec{'v', false, "invert-match"},
cfbox::args::OptSpec{'n', false, "line-number"},
cfbox::args::OptSpec{'r', false, "recursive"},
cfbox::args::OptSpec{'c', false, "count"},
cfbox::args::OptSpec{'l', false, "files-with-matches"},
cfbox::args::OptSpec{'q', false, "quiet"},
});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
GrepOptions opts;
opts.extended = parsed.has('E');
opts.ignore_case = parsed.has('i');
opts.invert = parsed.has('v');
opts.line_numbers = parsed.has('n');
opts.recursive = parsed.has('r');
opts.count_only = parsed.has('c');
opts.files_with_matches = parsed.has('l');
opts.quiet = parsed.has('q');
const auto& pos = parsed.positional();
if (pos.empty()) {
CFBOX_ERR("grep", "missing pattern");
return 2;
}
std::string pattern{pos[0]};
if (opts.recursive) {
if (pos.size() < 2) {
return grep_recursive(pattern, opts, std::filesystem::current_path());
}
int final_rc = 1;
for (std::size_t i = 1; i < pos.size(); ++i) {
int rc = grep_recursive(pattern, opts, std::filesystem::path{pos[i]});
if (rc == 2) return 2;
if (rc == 0) final_rc = 0;
}
return final_rc;
}
if (pos.size() < 2) {
// grep PATTERN (from stdin)
return grep_file(pattern, opts, "-", false);
}
bool multi = pos.size() > 2;
int final_rc = 1;
for (std::size_t i = 1; i < pos.size(); ++i) {
int rc = grep_file(pattern, opts, std::string{pos[i]}, multi);
if (rc == 2) return 2;
if (rc == 0) final_rc = 0;
}
return final_rc;
}