ladybird/Userland/Utilities/errno.cpp

79 lines
2.4 KiB
C++
Raw Normal View History

2021-05-16 01:00:09 +03:00
/*
* Copyright (c) 2021, the SerenityOS developers.
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
2021-05-16 01:00:09 +03:00
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/ArgsParser.h>
2021-12-21 21:08:23 +03:00
#include <LibMain/Main.h>
#include <string.h>
2021-05-16 01:00:09 +03:00
Array<StringView, EMAXERRNO + 1> s_errno_names = {
#define __ENUMERATE_ERRNO_CODE(c, s) #c##sv,
ENUMERATE_ERRNO_CODES(__ENUMERATE_ERRNO_CODE)
#undef __ENUMERATE_ERRNO_CODE
};
2021-12-21 21:08:23 +03:00
ErrorOr<int> serenity_main(Main::Arguments arguments)
2021-05-16 01:00:09 +03:00
{
bool list = false;
bool search = false;
2021-12-21 21:08:23 +03:00
StringView keyword;
2021-05-16 01:00:09 +03:00
Core::ArgsParser args_parser;
args_parser.add_positional_argument(keyword, "Error number or name to look up", "keyword", Core::ArgsParser::Required::No);
2021-05-16 01:00:09 +03:00
args_parser.add_option(list, "List all errno values", "list", 'l');
args_parser.add_option(search, "Search for error descriptions containing keyword", "search", 's');
2021-12-21 21:08:23 +03:00
args_parser.parse(arguments);
2021-05-16 01:00:09 +03:00
auto max_name_length = 0;
for (int i = 0; i < sys_nerr; i++) {
max_name_length = max(max_name_length, s_errno_names[i].length());
}
auto output_errno_description = [max_name_length](int errno_code, auto error_string) {
outln("{:{}} {:>2} {}", s_errno_names[errno_code], max_name_length, errno_code, error_string);
};
2021-05-16 01:00:09 +03:00
if (list) {
for (int i = 0; i < sys_nerr; i++)
output_errno_description(i, strerror(i));
2021-05-16 01:00:09 +03:00
return 0;
}
2021-12-21 21:08:23 +03:00
if (keyword.is_empty())
2021-05-16 01:00:09 +03:00
return 0;
if (search) {
for (int i = 0; i < sys_nerr; i++) {
auto error_string = strerror(i);
StringView error { error_string, strlen(error_string) };
if (error.contains(keyword, CaseSensitivity::CaseInsensitive))
output_errno_description(i, error);
2021-05-16 01:00:09 +03:00
}
return 0;
}
if (auto maybe_error_code = keyword.to_int(); maybe_error_code.has_value()) {
auto error_code = maybe_error_code.value();
auto error = strerror(error_code);
if (error == "Unknown error"sv) {
warnln("ERROR: Unknown errno: {}", keyword);
return 1;
}
output_errno_description(error_code, error);
return 0;
2021-05-16 01:00:09 +03:00
}
for (int i = 0; i < sys_nerr; i++) {
if (keyword.equals_ignoring_ascii_case(s_errno_names[i])) {
output_errno_description(i, strerror(i));
return 0;
}
2021-05-16 01:00:09 +03:00
}
warnln("ERROR: Not understood: {}", keyword);
return 1;
2021-05-16 01:00:09 +03:00
}