grep.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (c) 2020, Emanuel Sprung <emanuel.sprung@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/ScopeGuard.h>
  9. #include <AK/String.h>
  10. #include <AK/Utf8View.h>
  11. #include <AK/Vector.h>
  12. #include <LibCore/ArgsParser.h>
  13. #include <LibCore/DirIterator.h>
  14. #include <LibCore/File.h>
  15. #include <LibRegex/Regex.h>
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. enum class BinaryFileMode {
  19. Binary,
  20. Text,
  21. Skip,
  22. };
  23. template<typename... Ts>
  24. void fail(StringView format, Ts... args)
  25. {
  26. warn("\x1b[31m");
  27. warnln(format, forward<Ts>(args)...);
  28. warn("\x1b[0m");
  29. abort();
  30. }
  31. int main(int argc, char** argv)
  32. {
  33. if (pledge("stdio rpath", nullptr) < 0) {
  34. perror("pledge");
  35. return 1;
  36. }
  37. Vector<const char*> files;
  38. bool recursive { false };
  39. bool use_ere { false };
  40. const char* pattern = nullptr;
  41. BinaryFileMode binary_mode { BinaryFileMode::Binary };
  42. bool case_insensitive = false;
  43. bool line_numbers = false;
  44. bool invert_match = false;
  45. bool quiet_mode = false;
  46. bool suppress_errors = false;
  47. bool colored_output = isatty(STDOUT_FILENO);
  48. Core::ArgsParser args_parser;
  49. args_parser.add_option(recursive, "Recursively scan files", "recursive", 'r');
  50. args_parser.add_option(use_ere, "Extended regular expressions", "extended-regexp", 'E');
  51. args_parser.add_option(pattern, "Pattern", "regexp", 'e', "Pattern");
  52. args_parser.add_option(case_insensitive, "Make matches case-insensitive", nullptr, 'i');
  53. args_parser.add_option(line_numbers, "Output line-numbers", "line-numbers", 'n');
  54. args_parser.add_option(invert_match, "Select non-matching lines", "invert-match", 'v');
  55. args_parser.add_option(quiet_mode, "Do not write anything to standard output", "quiet", 'q');
  56. args_parser.add_option(suppress_errors, "Suppress error messages for nonexistent or unreadable files", "no-messages", 's');
  57. args_parser.add_option(Core::ArgsParser::Option {
  58. .requires_argument = true,
  59. .help_string = "Action to take for binary files ([binary], text, skip)",
  60. .long_name = "binary-mode",
  61. .accept_value = [&](auto* str) {
  62. if ("text"sv == str)
  63. binary_mode = BinaryFileMode::Text;
  64. else if ("binary"sv == str)
  65. binary_mode = BinaryFileMode::Binary;
  66. else if ("skip"sv == str)
  67. binary_mode = BinaryFileMode::Skip;
  68. else
  69. return false;
  70. return true;
  71. },
  72. });
  73. args_parser.add_option(Core::ArgsParser::Option {
  74. .requires_argument = false,
  75. .help_string = "Treat binary files as text (same as --binary-mode text)",
  76. .long_name = "text",
  77. .short_name = 'a',
  78. .accept_value = [&](auto) {
  79. binary_mode = BinaryFileMode::Text;
  80. return true;
  81. },
  82. });
  83. args_parser.add_option(Core::ArgsParser::Option {
  84. .requires_argument = false,
  85. .help_string = "Ignore binary files (same as --binary-mode skip)",
  86. .long_name = nullptr,
  87. .short_name = 'I',
  88. .accept_value = [&](auto) {
  89. binary_mode = BinaryFileMode::Skip;
  90. return true;
  91. },
  92. });
  93. args_parser.add_option(Core::ArgsParser::Option {
  94. .requires_argument = true,
  95. .help_string = "When to use colored output for the matching text ([auto], never, always)",
  96. .long_name = "color",
  97. .short_name = 0,
  98. .value_name = "WHEN",
  99. .accept_value = [&](auto* str) {
  100. if ("never"sv == str)
  101. colored_output = false;
  102. else if ("always"sv == str)
  103. colored_output = true;
  104. else if ("auto"sv != str)
  105. return false;
  106. return true;
  107. },
  108. });
  109. args_parser.add_positional_argument(files, "File(s) to process", "file", Core::ArgsParser::Required::No);
  110. args_parser.parse(argc, argv);
  111. // mock grep behavior: if -e is omitted, use first positional argument as pattern
  112. if (pattern == nullptr && files.size())
  113. pattern = files.take_first();
  114. auto user_has_specified_files = !files.is_empty();
  115. PosixOptions options {};
  116. if (case_insensitive)
  117. options |= PosixFlags::Insensitive;
  118. auto grep_logic = [&](auto&& re) {
  119. if (re.parser_result.error != regex::Error::NoError) {
  120. return 1;
  121. }
  122. auto matches = [&](StringView str, StringView filename, size_t line_number, bool print_filename, bool is_binary) {
  123. size_t last_printed_char_pos { 0 };
  124. if (is_binary && binary_mode == BinaryFileMode::Skip)
  125. return false;
  126. auto result = re.match(str, PosixFlags::Global);
  127. if (result.success ^ invert_match) {
  128. if (quiet_mode)
  129. return true;
  130. if (is_binary && binary_mode == BinaryFileMode::Binary) {
  131. outln(colored_output ? "binary file \x1B[34m{}\x1B[0m matches" : "binary file {} matches", filename);
  132. } else {
  133. if ((result.matches.size() || invert_match) && print_filename)
  134. out(colored_output ? "\x1B[34m{}:\x1B[0m" : "{}:", filename);
  135. if ((result.matches.size() || invert_match) && line_numbers)
  136. out(colored_output ? "\x1B[35m{}:\x1B[0m" : "{}:", line_number);
  137. for (auto& match : result.matches) {
  138. out(colored_output ? "{}\x1B[32m{}\x1B[0m" : "{}{}",
  139. StringView(&str[last_printed_char_pos], match.global_offset - last_printed_char_pos),
  140. match.view.to_string());
  141. last_printed_char_pos = match.global_offset + match.view.length();
  142. }
  143. outln("{}", StringView(&str[last_printed_char_pos], str.length() - last_printed_char_pos));
  144. }
  145. return true;
  146. }
  147. return false;
  148. };
  149. auto handle_file = [&matches, binary_mode, suppress_errors](StringView filename, bool print_filename) -> bool {
  150. auto file = Core::File::construct(filename);
  151. if (!file->open(Core::OpenMode::ReadOnly)) {
  152. if (!suppress_errors)
  153. warnln("Failed to open {}: {}", filename, file->error_string());
  154. return false;
  155. }
  156. for (size_t line_number = 1; file->can_read_line(); ++line_number) {
  157. auto line = file->read_line();
  158. auto is_binary = memchr(line.characters(), 0, line.length()) != nullptr;
  159. if (matches(line, filename, line_number, print_filename, is_binary) && is_binary && binary_mode == BinaryFileMode::Binary)
  160. return true;
  161. }
  162. return true;
  163. };
  164. auto add_directory = [&handle_file, user_has_specified_files](String base, Optional<String> recursive, auto handle_directory) -> void {
  165. Core::DirIterator it(recursive.value_or(base), Core::DirIterator::Flags::SkipDots);
  166. while (it.has_next()) {
  167. auto path = it.next_full_path();
  168. if (!Core::File::is_directory(path)) {
  169. auto key = user_has_specified_files ? path.view() : path.substring_view(base.length() + 1, path.length() - base.length() - 1);
  170. handle_file(key, true);
  171. } else {
  172. handle_directory(base, path, handle_directory);
  173. }
  174. }
  175. };
  176. bool did_match_something = false;
  177. if (!files.size() && !recursive) {
  178. char* line = nullptr;
  179. size_t line_len = 0;
  180. ssize_t nread = 0;
  181. ScopeGuard free_line = [line] { free(line); };
  182. size_t line_number = 0;
  183. while ((nread = getline(&line, &line_len, stdin)) != -1) {
  184. VERIFY(nread > 0);
  185. if (line[nread - 1] == '\n')
  186. --nread;
  187. // Human-readable indexes start at 1, so it's fine to increment already.
  188. line_number += 1;
  189. StringView line_view(line, nread);
  190. bool is_binary = line_view.contains(0);
  191. if (is_binary && binary_mode == BinaryFileMode::Skip)
  192. return 1;
  193. auto matched = matches(line_view, "stdin", line_number, false, is_binary);
  194. did_match_something = did_match_something || matched;
  195. if (matched && is_binary && binary_mode == BinaryFileMode::Binary)
  196. return 0;
  197. }
  198. } else {
  199. if (recursive) {
  200. if (user_has_specified_files) {
  201. for (auto& filename : files) {
  202. add_directory(filename, {}, add_directory);
  203. }
  204. } else {
  205. add_directory(".", {}, add_directory);
  206. }
  207. } else {
  208. bool print_filename { files.size() > 1 };
  209. for (auto& filename : files) {
  210. if (!handle_file(filename, print_filename))
  211. return 1;
  212. }
  213. }
  214. }
  215. return did_match_something ? 0 : 1;
  216. };
  217. if (use_ere)
  218. return grep_logic(Regex<PosixExtended>(pattern, options));
  219. return grep_logic(Regex<PosixBasic>(pattern, options));
  220. }