2020-11-27 20:44:25 +03:30
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Emanuel Sprung <emanuel.sprung@gmail.com>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-11-27 20:44:25 +03:30
|
|
|
*/
|
|
|
|
|
2021-05-15 12:34:40 +02:00
|
|
|
#include <AK/Assertions.h>
|
2020-11-27 20:44:25 +03:30
|
|
|
#include <AK/ByteBuffer.h>
|
2021-01-23 21:58:14 -07:00
|
|
|
#include <AK/ScopeGuard.h>
|
2020-11-27 20:44:25 +03:30
|
|
|
#include <AK/String.h>
|
|
|
|
#include <AK/Utf8View.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/DirIterator.h>
|
|
|
|
#include <LibCore/File.h>
|
|
|
|
#include <LibRegex/Regex.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-11-28 18:10:56 +03:30
|
|
|
enum class BinaryFileMode {
|
|
|
|
Binary,
|
|
|
|
Text,
|
|
|
|
Skip,
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename... Ts>
|
|
|
|
void fail(StringView format, Ts... args)
|
|
|
|
{
|
2021-05-31 15:43:25 +01:00
|
|
|
warn("\x1b[31m");
|
2020-11-28 18:10:56 +03:30
|
|
|
warnln(format, forward<Ts>(args)...);
|
2021-05-31 15:43:25 +01:00
|
|
|
warn("\x1b[0m");
|
2020-11-28 18:10:56 +03:30
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2020-11-27 20:44:25 +03:30
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if (pledge("stdio rpath", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<const char*> files;
|
|
|
|
|
|
|
|
bool recursive { false };
|
2021-07-10 13:20:44 +04:30
|
|
|
bool use_ere { false };
|
2020-11-27 20:44:25 +03:30
|
|
|
const char* pattern = nullptr;
|
2020-11-28 18:10:56 +03:30
|
|
|
BinaryFileMode binary_mode { BinaryFileMode::Binary };
|
|
|
|
bool case_insensitive = false;
|
2021-11-05 00:35:31 +01:00
|
|
|
bool line_numbers = false;
|
2021-01-23 22:11:19 -07:00
|
|
|
bool invert_match = false;
|
2021-10-30 12:54:26 +02:00
|
|
|
bool quiet_mode = false;
|
2021-10-30 13:04:12 +02:00
|
|
|
bool suppress_errors = false;
|
2021-08-12 19:12:38 +08:00
|
|
|
bool colored_output = isatty(STDOUT_FILENO);
|
2020-11-27 20:44:25 +03:30
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
2021-09-03 21:41:17 +02:00
|
|
|
args_parser.add_option(recursive, "Recursively scan files", "recursive", 'r');
|
2021-07-10 13:20:44 +04:30
|
|
|
args_parser.add_option(use_ere, "Extended regular expressions", "extended-regexp", 'E');
|
2020-11-27 20:44:25 +03:30
|
|
|
args_parser.add_option(pattern, "Pattern", "regexp", 'e', "Pattern");
|
2020-11-28 18:10:56 +03:30
|
|
|
args_parser.add_option(case_insensitive, "Make matches case-insensitive", nullptr, 'i');
|
2021-11-05 00:35:31 +01:00
|
|
|
args_parser.add_option(line_numbers, "Output line-numbers", "line-numbers", 'n');
|
2021-01-23 22:11:19 -07:00
|
|
|
args_parser.add_option(invert_match, "Select non-matching lines", "invert-match", 'v');
|
2021-10-30 12:54:26 +02:00
|
|
|
args_parser.add_option(quiet_mode, "Do not write anything to standard output", "quiet", 'q');
|
2021-10-30 13:04:12 +02:00
|
|
|
args_parser.add_option(suppress_errors, "Suppress error messages for nonexistent or unreadable files", "no-messages", 's');
|
2020-11-28 18:10:56 +03:30
|
|
|
args_parser.add_option(Core::ArgsParser::Option {
|
|
|
|
.requires_argument = true,
|
|
|
|
.help_string = "Action to take for binary files ([binary], text, skip)",
|
|
|
|
.long_name = "binary-mode",
|
|
|
|
.accept_value = [&](auto* str) {
|
2021-07-04 11:08:46 +02:00
|
|
|
if ("text"sv == str)
|
2020-11-28 18:10:56 +03:30
|
|
|
binary_mode = BinaryFileMode::Text;
|
2021-07-04 11:08:46 +02:00
|
|
|
else if ("binary"sv == str)
|
2020-11-28 18:10:56 +03:30
|
|
|
binary_mode = BinaryFileMode::Binary;
|
2021-07-04 11:08:46 +02:00
|
|
|
else if ("skip"sv == str)
|
2020-11-28 18:10:56 +03:30
|
|
|
binary_mode = BinaryFileMode::Skip;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
args_parser.add_option(Core::ArgsParser::Option {
|
|
|
|
.requires_argument = false,
|
|
|
|
.help_string = "Treat binary files as text (same as --binary-mode text)",
|
|
|
|
.long_name = "text",
|
|
|
|
.short_name = 'a',
|
|
|
|
.accept_value = [&](auto) {
|
|
|
|
binary_mode = BinaryFileMode::Text;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
args_parser.add_option(Core::ArgsParser::Option {
|
|
|
|
.requires_argument = false,
|
|
|
|
.help_string = "Ignore binary files (same as --binary-mode skip)",
|
|
|
|
.long_name = nullptr,
|
|
|
|
.short_name = 'I',
|
|
|
|
.accept_value = [&](auto) {
|
|
|
|
binary_mode = BinaryFileMode::Skip;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
});
|
2021-08-12 19:12:38 +08:00
|
|
|
args_parser.add_option(Core::ArgsParser::Option {
|
|
|
|
.requires_argument = true,
|
|
|
|
.help_string = "When to use colored output for the matching text ([auto], never, always)",
|
|
|
|
.long_name = "color",
|
|
|
|
.short_name = 0,
|
|
|
|
.value_name = "WHEN",
|
|
|
|
.accept_value = [&](auto* str) {
|
|
|
|
if ("never"sv == str)
|
|
|
|
colored_output = false;
|
|
|
|
else if ("always"sv == str)
|
|
|
|
colored_output = true;
|
|
|
|
else if ("auto"sv != str)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
});
|
2020-11-27 20:44:25 +03:30
|
|
|
args_parser.add_positional_argument(files, "File(s) to process", "file", Core::ArgsParser::Required::No);
|
|
|
|
args_parser.parse(argc, argv);
|
|
|
|
|
2021-09-07 12:56:50 +02:00
|
|
|
// mock grep behavior: if -e is omitted, use first positional argument as pattern
|
2020-11-27 20:44:25 +03:30
|
|
|
if (pattern == nullptr && files.size())
|
|
|
|
pattern = files.take_first();
|
|
|
|
|
2021-09-03 21:41:17 +02:00
|
|
|
auto user_has_specified_files = !files.is_empty();
|
|
|
|
|
2020-11-28 18:10:56 +03:30
|
|
|
PosixOptions options {};
|
|
|
|
if (case_insensitive)
|
|
|
|
options |= PosixFlags::Insensitive;
|
|
|
|
|
2021-07-10 13:20:44 +04:30
|
|
|
auto grep_logic = [&](auto&& re) {
|
2021-11-06 10:33:46 +01:00
|
|
|
if (re.parser_result.error != regex::Error::NoError) {
|
2021-07-10 13:20:44 +04:30
|
|
|
return 1;
|
|
|
|
}
|
2020-11-27 20:44:25 +03:30
|
|
|
|
2021-11-05 00:35:31 +01:00
|
|
|
auto matches = [&](StringView str, StringView filename, size_t line_number, bool print_filename, bool is_binary) {
|
2021-07-10 13:20:44 +04:30
|
|
|
size_t last_printed_char_pos { 0 };
|
|
|
|
if (is_binary && binary_mode == BinaryFileMode::Skip)
|
|
|
|
return false;
|
2020-11-28 18:10:56 +03:30
|
|
|
|
2021-07-10 13:20:44 +04:30
|
|
|
auto result = re.match(str, PosixFlags::Global);
|
|
|
|
if (result.success ^ invert_match) {
|
2021-10-30 12:54:26 +02:00
|
|
|
if (quiet_mode)
|
|
|
|
return true;
|
|
|
|
|
2021-07-10 13:20:44 +04:30
|
|
|
if (is_binary && binary_mode == BinaryFileMode::Binary) {
|
2021-08-12 19:12:38 +08:00
|
|
|
outln(colored_output ? "binary file \x1B[34m{}\x1B[0m matches" : "binary file {} matches", filename);
|
2021-07-10 13:20:44 +04:30
|
|
|
} else {
|
2021-08-12 19:12:38 +08:00
|
|
|
if ((result.matches.size() || invert_match) && print_filename)
|
|
|
|
out(colored_output ? "\x1B[34m{}:\x1B[0m" : "{}:", filename);
|
2021-11-05 00:35:31 +01:00
|
|
|
if ((result.matches.size() || invert_match) && line_numbers)
|
|
|
|
out(colored_output ? "\x1B[35m{}:\x1B[0m" : "{}:", line_number);
|
2021-07-10 13:20:44 +04:30
|
|
|
|
|
|
|
for (auto& match : result.matches) {
|
2021-08-12 19:12:38 +08:00
|
|
|
out(colored_output ? "{}\x1B[32m{}\x1B[0m" : "{}{}",
|
2021-07-10 13:20:44 +04:30
|
|
|
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));
|
2020-11-28 18:10:56 +03:30
|
|
|
}
|
|
|
|
|
2021-07-10 13:20:44 +04:30
|
|
|
return true;
|
2020-11-27 20:44:25 +03:30
|
|
|
}
|
|
|
|
|
2021-07-10 13:20:44 +04:30
|
|
|
return false;
|
|
|
|
};
|
2020-11-27 20:44:25 +03:30
|
|
|
|
2021-10-30 13:04:12 +02:00
|
|
|
auto handle_file = [&matches, binary_mode, suppress_errors](StringView filename, bool print_filename) -> bool {
|
2021-07-10 13:20:44 +04:30
|
|
|
auto file = Core::File::construct(filename);
|
|
|
|
if (!file->open(Core::OpenMode::ReadOnly)) {
|
2021-10-30 13:04:12 +02:00
|
|
|
if (!suppress_errors)
|
|
|
|
warnln("Failed to open {}: {}", filename, file->error_string());
|
2021-07-10 13:20:44 +04:30
|
|
|
return false;
|
|
|
|
}
|
2020-11-28 18:10:56 +03:30
|
|
|
|
2021-11-05 00:35:31 +01:00
|
|
|
for (size_t line_number = 1; file->can_read_line(); ++line_number) {
|
2021-07-10 13:20:44 +04:30
|
|
|
auto line = file->read_line();
|
|
|
|
auto is_binary = memchr(line.characters(), 0, line.length()) != nullptr;
|
2020-11-27 20:44:25 +03:30
|
|
|
|
2021-11-05 00:35:31 +01:00
|
|
|
if (matches(line, filename, line_number, print_filename, is_binary) && is_binary && binary_mode == BinaryFileMode::Binary)
|
2021-07-10 13:20:44 +04:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2021-09-03 21:41:17 +02:00
|
|
|
auto add_directory = [&handle_file, user_has_specified_files](String base, Optional<String> recursive, auto handle_directory) -> void {
|
2021-07-10 13:20:44 +04:30
|
|
|
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)) {
|
2021-09-03 21:41:17 +02:00
|
|
|
auto key = user_has_specified_files ? path.view() : path.substring_view(base.length() + 1, path.length() - base.length() - 1);
|
2021-07-10 13:20:44 +04:30
|
|
|
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); };
|
2021-11-05 00:35:31 +01:00
|
|
|
size_t line_number = 0;
|
2021-07-10 13:20:44 +04:30
|
|
|
while ((nread = getline(&line, &line_len, stdin)) != -1) {
|
|
|
|
VERIFY(nread > 0);
|
|
|
|
if (line[nread - 1] == '\n')
|
|
|
|
--nread;
|
2021-11-05 00:35:31 +01:00
|
|
|
// Human-readable indexes start at 1, so it's fine to increment already.
|
|
|
|
line_number += 1;
|
2021-07-10 13:20:44 +04:30
|
|
|
StringView line_view(line, nread);
|
|
|
|
bool is_binary = line_view.contains(0);
|
|
|
|
|
|
|
|
if (is_binary && binary_mode == BinaryFileMode::Skip)
|
|
|
|
return 1;
|
2020-11-28 18:10:56 +03:30
|
|
|
|
2021-11-05 00:35:31 +01:00
|
|
|
auto matched = matches(line_view, "stdin", line_number, false, is_binary);
|
2021-07-10 13:20:44 +04:30
|
|
|
did_match_something = did_match_something || matched;
|
|
|
|
if (matched && is_binary && binary_mode == BinaryFileMode::Binary)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (recursive) {
|
2021-09-03 21:41:17 +02:00
|
|
|
if (user_has_specified_files) {
|
|
|
|
for (auto& filename : files) {
|
|
|
|
add_directory(filename, {}, add_directory);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
add_directory(".", {}, add_directory);
|
|
|
|
}
|
2020-11-27 20:44:25 +03:30
|
|
|
|
|
|
|
} else {
|
2021-07-10 13:20:44 +04:30
|
|
|
bool print_filename { files.size() > 1 };
|
|
|
|
for (auto& filename : files) {
|
|
|
|
if (!handle_file(filename, print_filename))
|
|
|
|
return 1;
|
|
|
|
}
|
2020-11-27 20:44:25 +03:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-10 13:20:44 +04:30
|
|
|
return did_match_something ? 0 : 1;
|
|
|
|
};
|
2020-11-27 20:44:25 +03:30
|
|
|
|
2021-07-10 13:20:44 +04:30
|
|
|
if (use_ere)
|
|
|
|
return grep_logic(Regex<PosixExtended>(pattern, options));
|
2020-11-27 20:44:25 +03:30
|
|
|
|
2021-07-10 13:20:44 +04:30
|
|
|
return grep_logic(Regex<PosixBasic>(pattern, options));
|
2020-11-27 20:44:25 +03:30
|
|
|
}
|