Serenity Operating System
1/*
2 * Copyright (c) 2021, the SerenityOS developers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibCore/ArgsParser.h>
8#include <LibMain/Main.h>
9#include <string.h>
10
11ErrorOr<int> serenity_main(Main::Arguments arguments)
12{
13 bool list = false;
14 bool search = false;
15 StringView keyword;
16
17 Core::ArgsParser args_parser;
18 args_parser.add_positional_argument(keyword, "Error number or string to search", "keyword", Core::ArgsParser::Required::No);
19 args_parser.add_option(list, "List all errno values", "list", 'l');
20 args_parser.add_option(search, "Search for error descriptions containing keyword", "search", 's');
21 args_parser.parse(arguments);
22
23 if (list) {
24 for (int i = 0; i < sys_nerr; i++) {
25 outln("{} {}", i, strerror(i));
26 }
27 return 0;
28 }
29
30 if (keyword.is_empty())
31 return 0;
32
33 if (search) {
34 for (int i = 0; i < sys_nerr; i++) {
35 auto error = DeprecatedString::formatted("{}", strerror(i));
36 if (error.contains(keyword, CaseSensitivity::CaseInsensitive)) {
37 outln("{} {}", i, error);
38 }
39 }
40 return 0;
41 }
42
43 auto maybe_errno = keyword.to_int();
44 if (!maybe_errno.has_value()) {
45 warnln("ERROR: Not understood: {}", keyword);
46 return 1;
47 }
48
49 auto error = DeprecatedString::formatted("{}", strerror(maybe_errno.value()));
50 if (error == "Unknown error"sv) {
51 warnln("ERROR: Unknown errno: {}", keyword);
52 return 1;
53 }
54 outln("{} {}", keyword, error);
55
56 return 0;
57}