Skip to content

Commit 018dcf5

Browse files
krkkawesomekling
authored andcommitted
grep: Return exit code '2' on error
1 parent 5a85449 commit 018dcf5

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

Userland/Utilities/grep.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ enum class BinaryFileMode {
2626
Skip,
2727
};
2828

29+
enum class ExitStatus {
30+
SomethingMatched = 0,
31+
NoLinesMatched = 1,
32+
ErrorOccurred = 2, // NOTE: `-q` flag also silences errors.
33+
};
34+
2935
template<typename... Ts>
3036
void fail(StringView format, Ts... args)
3137
{
@@ -172,7 +178,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
172178
for (auto& re : regular_expressions) {
173179
if (re.parser_result.error != regex::Error::NoError) {
174180
warnln("regex parse error: {}", regex::get_error_string(re.parser_result.error));
175-
return 1;
181+
return ExitStatus::ErrorOccurred;
176182
}
177183
}
178184

@@ -219,10 +225,10 @@ ErrorOr<int> serenity_main(Main::Arguments args)
219225
return false;
220226
};
221227

222-
bool did_match_something = false;
228+
auto exit_status = ExitStatus::NoLinesMatched;
223229

224230
auto handle_file = [&matches, binary_mode, count_lines, quiet_mode,
225-
user_specified_multiple_files, &matched_line_count, &did_match_something](StringView filename, bool print_filename) -> ErrorOr<void> {
231+
user_specified_multiple_files, &matched_line_count, &exit_status](StringView filename, bool print_filename) -> ErrorOr<void> {
226232
auto file = TRY(Core::File::open_file_or_standard_stream(filename, Core::File::OpenMode::Read));
227233
auto buffered_file = TRY(Core::InputBufferedFile::create(move(file)));
228234
if (filename == '-')
@@ -235,9 +241,12 @@ ErrorOr<int> serenity_main(Main::Arguments args)
235241
auto is_binary = line.contains('\0');
236242

237243
auto matched = matches(line, filename, line_number, print_filename, is_binary);
238-
did_match_something = did_match_something || matched;
239-
if (matched && is_binary && binary_mode == BinaryFileMode::Binary)
240-
break;
244+
if (matched) {
245+
if (exit_status == ExitStatus::NoLinesMatched)
246+
exit_status = ExitStatus::SomethingMatched;
247+
if (is_binary && binary_mode == BinaryFileMode::Binary)
248+
break;
249+
}
241250
}
242251

243252
if (count_lines && !quiet_mode) {
@@ -251,15 +260,17 @@ ErrorOr<int> serenity_main(Main::Arguments args)
251260
return {};
252261
};
253262

254-
auto add_directory = [&handle_file, user_has_specified_files, suppress_errors](DeprecatedString base, Optional<DeprecatedString> recursive, auto handle_directory) -> void {
263+
auto add_directory = [&handle_file, &exit_status, user_has_specified_files, suppress_errors](DeprecatedString base, Optional<DeprecatedString> recursive, auto handle_directory) -> void {
255264
Core::DirIterator it(recursive.value_or(base), Core::DirIterator::Flags::SkipDots);
256265
while (it.has_next()) {
257266
auto path = it.next_full_path();
258267
if (!FileSystem::is_directory(path)) {
259268
// Remove leading './' when `grep -r` was run without any specified paths.
260269
auto key = user_has_specified_files ? path.view() : path.substring_view(base.length() + 1);
261-
if (auto result = handle_file(key, true); result.is_error() && !suppress_errors)
270+
if (auto result = handle_file(key, true); result.is_error() && !suppress_errors) {
262271
warnln("Failed with file {}: {}", key, result.release_error());
272+
exit_status = ExitStatus::ErrorOccurred;
273+
}
263274

264275
} else {
265276
handle_directory(base, path, handle_directory);
@@ -283,11 +294,12 @@ ErrorOr<int> serenity_main(Main::Arguments args)
283294
auto result = handle_file(filename, print_filename);
284295
if (result.is_error() && !suppress_errors) {
285296
warnln("Failed with file {}: {}", filename, result.release_error());
297+
exit_status = ExitStatus::ErrorOccurred;
286298
}
287299
}
288300
}
289301

290-
return did_match_something ? 0 : 1;
302+
return exit_status;
291303
};
292304

293305
if (use_ere) {
@@ -296,13 +308,13 @@ ErrorOr<int> serenity_main(Main::Arguments args)
296308
auto escaped_pattern = (fixed_strings) ? escape_characters(pattern, ere_special_characters) : pattern;
297309
regular_expressions.append(Regex<PosixExtended>(escaped_pattern, options));
298310
}
299-
return grep_logic(regular_expressions);
311+
return to_underlying(grep_logic(regular_expressions));
300312
}
301313

302314
Vector<Regex<PosixBasic>> regular_expressions;
303315
for (auto pattern : patterns) {
304316
auto escaped_pattern = (fixed_strings) ? escape_characters(pattern, basic_special_characters) : pattern;
305317
regular_expressions.append(Regex<PosixBasic>(escaped_pattern, options));
306318
}
307-
return grep_logic(regular_expressions);
319+
return to_underlying(grep_logic(regular_expressions));
308320
}

0 commit comments

Comments
 (0)