summaryrefslogtreecommitdiff
path: root/Userland/Applications/Help/main.cpp
blob: 3ae157664b85521590606372ca48fbaf9204dd56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "MainWidget.h"
#include <AK/URL.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/System.h>
#include <LibGUI/Application.h>
#include <LibGUI/Icon.h>
#include <LibGUI/Window.h>
#include <LibMain/Main.h>

using namespace Help;

static DeprecatedString parse_input(StringView input)
{
    AK::URL url = URL::create_with_url_or_path(input);
    if (url.is_valid())
        return url.basename();

    return input;
}

ErrorOr<int> serenity_main(Main::Arguments arguments)
{
    TRY(Core::System::pledge("stdio recvfd sendfd rpath unix"));
    auto app = TRY(GUI::Application::try_create(arguments));

    TRY(Core::System::unveil("/sys/kernel/processes", "r"));
    TRY(Core::System::unveil("/res", "r"));
    // We specifically don't want to load this path from a library, as that can be hijacked with LD_PRELOAD.
    TRY(Core::System::unveil("/usr/share/man", "r"));
    TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
    TRY(Core::System::unveil("/tmp/session/%sid/portal/launch", "rw"));
    TRY(Core::System::unveil("/tmp/session/%sid/portal/webcontent", "rw"));
    TRY(Core::System::unveil(nullptr, nullptr));

    DeprecatedString start_page;
    u32 section = 0;

    Core::ArgsParser args_parser;
    // FIXME: These custom Args are a hack. What we want to do is have an optional int arg, then an optional string.
    // However, when only a string is provided, it gets forwarded to the int argument since that is first, and
    // parsing fails. This hack instead forwards it to the start_page in that case.
    args_parser.add_positional_argument(Core::ArgsParser::Arg {
        .help_string = "Section of the man page",
        .name = "section",
        .min_values = 0,
        .max_values = 1,
        .accept_value = [&](char const* input_ptr) {
            StringView input { input_ptr, strlen(input_ptr) };
            // If it's a number, use it as the section
            if (auto number = input.to_int(); number.has_value()) {
                section = number.value();
                return true;
            }

            // Otherwise, use it as the start_page
            start_page = parse_input(input);
            return true;
        } });
    args_parser.add_positional_argument(Core::ArgsParser::Arg {
        .help_string = "Help page to open. Either an absolute path to the markdown file, or a search query",
        .name = "page",
        .min_values = 0,
        .max_values = 1,
        .accept_value = [&](char const* input_ptr) {
            StringView input { input_ptr, strlen(input_ptr) };
            // If start_page was already set by our section arg, then it can't be set again
            if (!start_page.is_empty())
                return false;
            start_page = parse_input(input);
            return true;
        } });
    args_parser.parse(arguments);

    auto app_icon = GUI::Icon::default_icon("app-help"sv);

    auto window = TRY(GUI::Window::try_create());
    window->set_icon(app_icon.bitmap_for_size(16));
    window->set_title("Help");
    window->resize(570, 500);

    auto main_widget = TRY(window->try_set_main_widget<MainWidget>());
    TRY(main_widget->initialize_fallibles(window));
    TRY(main_widget->set_start_page(start_page, section));

    window->show();

    return app->exec();
}