1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:48:12 +00:00

grep: Use Basic POSIX regexps by default and make -E not the default

This commit is contained in:
Ali Mohammad Pur 2021-07-10 13:20:44 +04:30 committed by Andreas Kling
parent 97f7132b82
commit eca74088a0

View file

@ -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,7 +101,7 @@ int main(int argc, char** argv)
if (case_insensitive)
options |= PosixFlags::Insensitive;
Regex<PosixExtended> re(pattern, options);
auto grep_logic = [&](auto&& re) {
if (re.parser_result.error != Error::NoError) {
return 1;
}
@ -204,4 +201,10 @@ int main(int argc, char** argv)
}
return did_match_something ? 0 : 1;
};
if (use_ere)
return grep_logic(Regex<PosixExtended>(pattern, options));
return grep_logic(Regex<PosixBasic>(pattern, options));
}