grep.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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/ScopeGuard.h>
  8. #include <AK/String.h>
  9. #include <AK/Vector.h>
  10. #include <LibCore/ArgsParser.h>
  11. #include <LibCore/DirIterator.h>
  12. #include <LibCore/File.h>
  13. #include <LibCore/System.h>
  14. #include <LibMain/Main.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. ErrorOr<int> serenity_main(Main::Arguments args)
  32. {
  33. TRY(Core::System::pledge("stdio rpath", nullptr));
  34. Vector<const char*> files;
  35. bool recursive { false };
  36. bool use_ere { false };
  37. Vector<const char*> patterns;
  38. BinaryFileMode binary_mode { BinaryFileMode::Binary };
  39. bool case_insensitive = false;
  40. bool line_numbers = false;
  41. bool invert_match = false;
  42. bool quiet_mode = false;
  43. bool suppress_errors = false;
  44. bool colored_output = isatty(STDOUT_FILENO);
  45. bool count_lines = false;
  46. size_t matched_line_count = 0;
  47. Core::ArgsParser args_parser;
  48. args_parser.add_option(recursive, "Recursively scan files", "recursive", 'r');
  49. args_parser.add_option(use_ere, "Extended regular expressions", "extended-regexp", 'E');
  50. args_parser.add_option(Core::ArgsParser::Option {
  51. .requires_argument = true,
  52. .help_string = "Pattern",
  53. .long_name = "regexp",
  54. .short_name = 'e',
  55. .value_name = "Pattern",
  56. .accept_value = [&](auto* str) {
  57. patterns.append(str);
  58. return true;
  59. },
  60. });
  61. args_parser.add_option(case_insensitive, "Make matches case-insensitive", nullptr, 'i');
  62. args_parser.add_option(line_numbers, "Output line-numbers", "line-numbers", 'n');
  63. args_parser.add_option(invert_match, "Select non-matching lines", "invert-match", 'v');
  64. args_parser.add_option(quiet_mode, "Do not write anything to standard output", "quiet", 'q');
  65. args_parser.add_option(suppress_errors, "Suppress error messages for nonexistent or unreadable files", "no-messages", 's');
  66. args_parser.add_option(Core::ArgsParser::Option {
  67. .requires_argument = true,
  68. .help_string = "Action to take for binary files ([binary], text, skip)",
  69. .long_name = "binary-mode",
  70. .accept_value = [&](auto* str) {
  71. if ("text"sv == str)
  72. binary_mode = BinaryFileMode::Text;
  73. else if ("binary"sv == str)
  74. binary_mode = BinaryFileMode::Binary;
  75. else if ("skip"sv == str)
  76. binary_mode = BinaryFileMode::Skip;
  77. else
  78. return false;
  79. return true;
  80. },
  81. });
  82. args_parser.add_option(Core::ArgsParser::Option {
  83. .requires_argument = false,
  84. .help_string = "Treat binary files as text (same as --binary-mode text)",
  85. .long_name = "text",
  86. .short_name = 'a',
  87. .accept_value = [&](auto) {
  88. binary_mode = BinaryFileMode::Text;
  89. return true;
  90. },
  91. });
  92. args_parser.add_option(Core::ArgsParser::Option {
  93. .requires_argument = false,
  94. .help_string = "Ignore binary files (same as --binary-mode skip)",
  95. .long_name = nullptr,
  96. .short_name = 'I',
  97. .accept_value = [&](auto) {
  98. binary_mode = BinaryFileMode::Skip;
  99. return true;
  100. },
  101. });
  102. args_parser.add_option(Core::ArgsParser::Option {
  103. .requires_argument = true,
  104. .help_string = "When to use colored output for the matching text ([auto], never, always)",
  105. .long_name = "color",
  106. .short_name = 0,
  107. .value_name = "WHEN",
  108. .accept_value = [&](auto* str) {
  109. if ("never"sv == str)
  110. colored_output = false;
  111. else if ("always"sv == str)
  112. colored_output = true;
  113. else if ("auto"sv != str)
  114. return false;
  115. return true;
  116. },
  117. });
  118. args_parser.add_option(count_lines, "Output line count instead of line contents", "count", 'c');
  119. args_parser.add_positional_argument(files, "File(s) to process", "file", Core::ArgsParser::Required::No);
  120. args_parser.parse(args);
  121. // mock grep behavior: if -e is omitted, use first positional argument as pattern
  122. if (patterns.size() == 0 && files.size())
  123. patterns.append(files.take_first());
  124. auto user_has_specified_files = !files.is_empty();
  125. auto user_specified_multiple_files = files.size() >= 2;
  126. PosixOptions options {};
  127. if (case_insensitive)
  128. options |= PosixFlags::Insensitive;
  129. auto grep_logic = [&](auto&& regular_expressions) {
  130. for (auto& re : regular_expressions) {
  131. if (re.parser_result.error != regex::Error::NoError) {
  132. return 1;
  133. }
  134. }
  135. auto matches = [&](StringView str, StringView filename, size_t line_number, bool print_filename, bool is_binary) {
  136. size_t last_printed_char_pos { 0 };
  137. if (is_binary && binary_mode == BinaryFileMode::Skip)
  138. return false;
  139. for (auto& re : regular_expressions) {
  140. auto result = re.match(str, PosixFlags::Global);
  141. if (!(result.success ^ invert_match))
  142. continue;
  143. if (quiet_mode)
  144. return true;
  145. if (count_lines) {
  146. matched_line_count++;
  147. return true;
  148. }
  149. if (is_binary && binary_mode == BinaryFileMode::Binary) {
  150. outln(colored_output ? "binary file \x1B[34m{}\x1B[0m matches" : "binary file {} matches", filename);
  151. } else {
  152. if ((result.matches.size() || invert_match) && print_filename)
  153. out(colored_output ? "\x1B[34m{}:\x1B[0m" : "{}:", filename);
  154. if ((result.matches.size() || invert_match) && line_numbers)
  155. out(colored_output ? "\x1B[35m{}:\x1B[0m" : "{}:", line_number);
  156. for (auto& match : result.matches) {
  157. out(colored_output ? "{}\x1B[32m{}\x1B[0m" : "{}{}",
  158. StringView(&str[last_printed_char_pos], match.global_offset - last_printed_char_pos),
  159. match.view.to_string());
  160. last_printed_char_pos = match.global_offset + match.view.length();
  161. }
  162. outln("{}", StringView(&str[last_printed_char_pos], str.length() - last_printed_char_pos));
  163. }
  164. return true;
  165. }
  166. return false;
  167. };
  168. bool did_match_something = false;
  169. auto handle_file = [&matches, binary_mode, suppress_errors, count_lines, quiet_mode,
  170. user_specified_multiple_files, &matched_line_count, &did_match_something](StringView filename, bool print_filename) -> bool {
  171. auto file = Core::File::construct(filename);
  172. if (!file->open(Core::OpenMode::ReadOnly)) {
  173. if (!suppress_errors)
  174. warnln("Failed to open {}: {}", filename, file->error_string());
  175. return false;
  176. }
  177. auto file_size_or_error = Core::File::size(filename);
  178. if (file_size_or_error.is_error()) {
  179. if (!suppress_errors)
  180. warnln("Failed to retrieve size of {}: {}", filename, strerror(file_size_or_error.error().code()));
  181. return false;
  182. }
  183. auto file_size = file_size_or_error.release_value();
  184. for (size_t line_number = 1; file->can_read_line(); ++line_number) {
  185. auto line = file->read_line(file_size);
  186. auto is_binary = memchr(line.characters(), 0, line.length()) != nullptr;
  187. auto matched = matches(line, filename, line_number, print_filename, is_binary);
  188. did_match_something = did_match_something || matched;
  189. if (matched && is_binary && binary_mode == BinaryFileMode::Binary)
  190. break;
  191. }
  192. if (count_lines && !quiet_mode) {
  193. if (user_specified_multiple_files)
  194. outln("{}:{}", filename, matched_line_count);
  195. else
  196. outln("{}", matched_line_count);
  197. matched_line_count = 0;
  198. }
  199. return true;
  200. };
  201. auto add_directory = [&handle_file, user_has_specified_files](String base, Optional<String> recursive, auto handle_directory) -> void {
  202. Core::DirIterator it(recursive.value_or(base), Core::DirIterator::Flags::SkipDots);
  203. while (it.has_next()) {
  204. auto path = it.next_full_path();
  205. if (!Core::File::is_directory(path)) {
  206. auto key = user_has_specified_files ? path.view() : path.substring_view(base.length() + 1, path.length() - base.length() - 1);
  207. handle_file(key, true);
  208. } else {
  209. handle_directory(base, path, handle_directory);
  210. }
  211. }
  212. };
  213. if (!files.size() && !recursive) {
  214. char* line = nullptr;
  215. size_t line_len = 0;
  216. ssize_t nread = 0;
  217. ScopeGuard free_line = [line] { free(line); };
  218. size_t line_number = 0;
  219. while ((nread = getline(&line, &line_len, stdin)) != -1) {
  220. VERIFY(nread > 0);
  221. if (line[nread - 1] == '\n')
  222. --nread;
  223. // Human-readable indexes start at 1, so it's fine to increment already.
  224. line_number += 1;
  225. StringView line_view(line, nread);
  226. bool is_binary = line_view.contains(0);
  227. if (is_binary && binary_mode == BinaryFileMode::Skip)
  228. return 1;
  229. auto matched = matches(line_view, "stdin", line_number, false, is_binary);
  230. did_match_something = did_match_something || matched;
  231. if (matched && is_binary && binary_mode == BinaryFileMode::Binary)
  232. break;
  233. }
  234. if (count_lines && !quiet_mode)
  235. outln("{}", matched_line_count);
  236. } else {
  237. if (recursive) {
  238. if (user_has_specified_files) {
  239. for (auto& filename : files) {
  240. add_directory(filename, {}, add_directory);
  241. }
  242. } else {
  243. add_directory(".", {}, add_directory);
  244. }
  245. } else {
  246. bool print_filename { files.size() > 1 };
  247. for (auto& filename : files) {
  248. if (!handle_file(filename, print_filename))
  249. return 1;
  250. }
  251. }
  252. }
  253. return did_match_something ? 0 : 1;
  254. };
  255. if (use_ere) {
  256. Vector<Regex<PosixExtended>> regular_expressions;
  257. for (auto pattern : patterns) {
  258. regular_expressions.append(Regex<PosixExtended>(pattern, options));
  259. }
  260. return grep_logic(regular_expressions);
  261. }
  262. Vector<Regex<PosixBasic>> regular_expressions;
  263. for (auto pattern : patterns) {
  264. regular_expressions.append(Regex<PosixBasic>(pattern, options));
  265. }
  266. return grep_logic(regular_expressions);
  267. }