summaryrefslogtreecommitdiff
path: root/Userland/Utilities
diff options
context:
space:
mode:
authorAli Mohammad Pur <ali.mpfard@gmail.com>2021-07-10 13:20:44 +0430
committerAndreas Kling <kling@serenityos.org>2021-07-10 13:33:08 +0200
commiteca74088a0185aac7b7ca56b1b14128daacc6fe0 (patch)
treede848981af88f88b10bcb65a65ef78d21cd6d3f0 /Userland/Utilities
parent97f7132b822d0b36cbd47690fa4558480d4a7755 (diff)
downloadserenity-eca74088a0185aac7b7ca56b1b14128daacc6fe0.zip
grep: Use Basic POSIX regexps by default and make -E not the default
Diffstat (limited to 'Userland/Utilities')
-rw-r--r--Userland/Utilities/grep.cpp179
1 files changed, 91 insertions, 88 deletions
diff --git a/Userland/Utilities/grep.cpp b/Userland/Utilities/grep.cpp
index 3210412f61..ceb15fe4d6 100644
--- a/Userland/Utilities/grep.cpp
+++ b/Userland/Utilities/grep.cpp
@@ -42,7 +42,7 @@ int main(int argc, char** argv)
Vector<const char*> files;
bool recursive { false };
- bool use_ere { true };
+ bool use_ere { false };
const char* pattern = nullptr;
BinaryFileMode binary_mode { BinaryFileMode::Binary };
bool case_insensitive = false;
@@ -50,7 +50,7 @@ int main(int argc, char** argv)
Core::ArgsParser args_parser;
args_parser.add_option(recursive, "Recursively scan files starting in working directory", "recursive", 'r');
- args_parser.add_option(use_ere, "Extended regular expressions (default)", "extended-regexp", 'E');
+ args_parser.add_option(use_ere, "Extended regular expressions", "extended-regexp", 'E');
args_parser.add_option(pattern, "Pattern", "regexp", 'e', "Pattern");
args_parser.add_option(case_insensitive, "Make matches case-insensitive", nullptr, 'i');
args_parser.add_option(invert_match, "Select non-matching lines", "invert-match", 'v');
@@ -93,9 +93,6 @@ int main(int argc, char** argv)
args_parser.add_positional_argument(files, "File(s) to process", "file", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv);
- if (!use_ere)
- return 0;
-
// mock grep behaviour: if -e is omitted, use first positional argument as pattern
if (pattern == nullptr && files.size())
pattern = files.take_first();
@@ -104,104 +101,110 @@ int main(int argc, char** argv)
if (case_insensitive)
options |= PosixFlags::Insensitive;
- Regex<PosixExtended> re(pattern, options);
- if (re.parser_result.error != Error::NoError) {
- return 1;
- }
+ auto grep_logic = [&](auto&& re) {
+ if (re.parser_result.error != Error::NoError) {
+ return 1;
+ }
- auto matches = [&](StringView str, StringView filename = "", bool print_filename = false, bool is_binary = false) {
- size_t last_printed_char_pos { 0 };
- if (is_binary && binary_mode == BinaryFileMode::Skip)
- return false;
+ auto matches = [&](StringView str, StringView filename = "", bool print_filename = false, bool is_binary = false) {
+ size_t last_printed_char_pos { 0 };
+ if (is_binary && binary_mode == BinaryFileMode::Skip)
+ return false;
- auto result = re.match(str, PosixFlags::Global);
- if (result.success ^ invert_match) {
- if (is_binary && binary_mode == BinaryFileMode::Binary) {
- outln("binary file \x1B[34m{}\x1B[0m matches", filename);
- } else {
- if ((result.matches.size() || invert_match) && print_filename) {
- out("\x1B[34m{}:\x1B[0m", filename);
+ auto result = re.match(str, PosixFlags::Global);
+ if (result.success ^ invert_match) {
+ if (is_binary && binary_mode == BinaryFileMode::Binary) {
+ outln("binary file \x1B[34m{}\x1B[0m matches", filename);
+ } else {
+ if ((result.matches.size() || invert_match) && print_filename) {
+ out("\x1B[34m{}:\x1B[0m", filename);
+ }
+
+ for (auto& match : result.matches) {
+
+ out("{}\x1B[32m{}\x1B[0m",
+ StringView(&str[last_printed_char_pos], match.global_offset - last_printed_char_pos),
+ match.view.to_string());
+ last_printed_char_pos = match.global_offset + match.view.length();
+ }
+ outln("{}", StringView(&str[last_printed_char_pos], str.length() - last_printed_char_pos));
}
- for (auto& match : result.matches) {
-
- out("{}\x1B[32m{}\x1B[0m",
- StringView(&str[last_printed_char_pos], match.global_offset - last_printed_char_pos),
- match.view.to_string());
- last_printed_char_pos = match.global_offset + match.view.length();
- }
- outln("{}", StringView(&str[last_printed_char_pos], str.length() - last_printed_char_pos));
+ return true;
}
- return true;
- }
+ return false;
+ };
- return false;
- };
+ auto handle_file = [&matches, binary_mode](StringView filename, bool print_filename) -> bool {
+ auto file = Core::File::construct(filename);
+ if (!file->open(Core::OpenMode::ReadOnly)) {
+ warnln("Failed to open {}: {}", filename, file->error_string());
+ return false;
+ }
- auto handle_file = [&matches, binary_mode](StringView filename, bool print_filename) -> bool {
- auto file = Core::File::construct(filename);
- if (!file->open(Core::OpenMode::ReadOnly)) {
- warnln("Failed to open {}: {}", filename, file->error_string());
- return false;
- }
+ while (file->can_read_line()) {
+ auto line = file->read_line();
+ auto is_binary = memchr(line.characters(), 0, line.length()) != nullptr;
- while (file->can_read_line()) {
- auto line = file->read_line();
- auto is_binary = memchr(line.characters(), 0, line.length()) != nullptr;
+ if (matches(line, filename, print_filename, is_binary) && is_binary && binary_mode == BinaryFileMode::Binary)
+ return true;
+ }
+ return true;
+ };
+
+ auto add_directory = [&handle_file](String base, Optional<String> recursive, auto handle_directory) -> void {
+ Core::DirIterator it(recursive.value_or(base), Core::DirIterator::Flags::SkipDots);
+ while (it.has_next()) {
+ auto path = it.next_full_path();
+ if (!Core::File::is_directory(path)) {
+ auto key = path.substring_view(base.length() + 1, path.length() - base.length() - 1);
+ handle_file(key, true);
+ } else {
+ handle_directory(base, path, handle_directory);
+ }
+ }
+ };
+
+ bool did_match_something = false;
+ if (!files.size() && !recursive) {
+ char* line = nullptr;
+ size_t line_len = 0;
+ ssize_t nread = 0;
+ ScopeGuard free_line = [line] { free(line); };
+ while ((nread = getline(&line, &line_len, stdin)) != -1) {
+ VERIFY(nread > 0);
+ if (line[nread - 1] == '\n')
+ --nread;
+ StringView line_view(line, nread);
+ bool is_binary = line_view.contains(0);
+
+ if (is_binary && binary_mode == BinaryFileMode::Skip)
+ return 1;
- if (matches(line, filename, print_filename, is_binary) && is_binary && binary_mode == BinaryFileMode::Binary)
- return true;
- }
- return true;
- };
+ auto matched = matches(line_view, "stdin", false, is_binary);
+ did_match_something = did_match_something || matched;
+ if (matched && is_binary && binary_mode == BinaryFileMode::Binary)
+ return 0;
+ }
+ } else {
+ if (recursive) {
+ add_directory(".", {}, add_directory);
- auto add_directory = [&handle_file](String base, Optional<String> recursive, auto handle_directory) -> void {
- Core::DirIterator it(recursive.value_or(base), Core::DirIterator::Flags::SkipDots);
- while (it.has_next()) {
- auto path = it.next_full_path();
- if (!Core::File::is_directory(path)) {
- auto key = path.substring_view(base.length() + 1, path.length() - base.length() - 1);
- handle_file(key, true);
} else {
- handle_directory(base, path, handle_directory);
+ bool print_filename { files.size() > 1 };
+ for (auto& filename : files) {
+ if (!handle_file(filename, print_filename))
+ return 1;
+ }
}
}
- };
- bool did_match_something = false;
- if (!files.size() && !recursive) {
- char* line = nullptr;
- size_t line_len = 0;
- ssize_t nread = 0;
- ScopeGuard free_line = [line] { free(line); };
- while ((nread = getline(&line, &line_len, stdin)) != -1) {
- VERIFY(nread > 0);
- if (line[nread - 1] == '\n')
- --nread;
- StringView line_view(line, nread);
- bool is_binary = line_view.contains(0);
-
- if (is_binary && binary_mode == BinaryFileMode::Skip)
- return 1;
-
- auto matched = matches(line_view, "stdin", false, is_binary);
- did_match_something = did_match_something || matched;
- if (matched && is_binary && binary_mode == BinaryFileMode::Binary)
- return 0;
- }
- } else {
- if (recursive) {
- add_directory(".", {}, add_directory);
+ return did_match_something ? 0 : 1;
+ };
- } else {
- bool print_filename { files.size() > 1 };
- for (auto& filename : files) {
- if (!handle_file(filename, print_filename))
- return 1;
- }
- }
- }
+ if (use_ere)
+ return grep_logic(Regex<PosixExtended>(pattern, options));
- return did_match_something ? 0 : 1;
+ return grep_logic(Regex<PosixBasic>(pattern, options));
}