Serenity Operating System
1/*
2 * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
3 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
4 * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
5 * Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
6 *
7 * SPDX-License-Identifier: BSD-2-Clause
8 */
9
10#include "MainWidget.h"
11#include <AK/URL.h>
12#include <LibCore/ArgsParser.h>
13#include <LibCore/System.h>
14#include <LibGUI/Application.h>
15#include <LibGUI/Icon.h>
16#include <LibGUI/Window.h>
17#include <LibMain/Main.h>
18
19using namespace Help;
20
21ErrorOr<int> serenity_main(Main::Arguments arguments)
22{
23 TRY(Core::System::pledge("stdio recvfd sendfd rpath unix"));
24 auto app = TRY(GUI::Application::try_create(arguments));
25
26 TRY(Core::System::unveil("/res", "r"));
27 // We specifically don't want to load this path from a library, as that can be hijacked with LD_PRELOAD.
28 TRY(Core::System::unveil("/usr/share/man", "r"));
29 TRY(Core::System::unveil("/tmp/session/%sid/portal/filesystemaccess", "rw"));
30 TRY(Core::System::unveil("/tmp/session/%sid/portal/launch", "rw"));
31 TRY(Core::System::unveil("/tmp/session/%sid/portal/webcontent", "rw"));
32 TRY(Core::System::unveil(nullptr, nullptr));
33
34 DeprecatedString first_query_parameter;
35 DeprecatedString second_query_parameter;
36
37 Core::ArgsParser args_parser;
38 // The actual "page query" parsing happens when we set the main widget's start page.
39 args_parser.add_positional_argument(
40 first_query_parameter,
41 "Section of the man page",
42 "section",
43 Core::ArgsParser::Required::No);
44 args_parser.add_positional_argument(
45 second_query_parameter,
46 "Help page to open. Either an absolute path to the markdown file, or a search query",
47 "page",
48 Core::ArgsParser::Required::No);
49 args_parser.parse(arguments);
50 Vector<StringView, 2> query_parameters;
51 if (!first_query_parameter.is_empty())
52 query_parameters.append(first_query_parameter);
53 if (!second_query_parameter.is_empty())
54 query_parameters.append(second_query_parameter);
55
56 auto app_icon = GUI::Icon::default_icon("app-help"sv);
57
58 auto window = TRY(GUI::Window::try_create());
59 window->set_icon(app_icon.bitmap_for_size(16));
60 window->set_title("Help");
61 window->resize(570, 500);
62
63 auto main_widget = TRY(window->set_main_widget<MainWidget>());
64 TRY(main_widget->initialize_fallibles(window));
65 TRY(main_widget->set_start_page(query_parameters));
66
67 window->show();
68
69 return app->exec();
70}