main.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "MainWidget.h"
  9. #include <AK/URL.h>
  10. #include <LibCore/ArgsParser.h>
  11. #include <LibCore/System.h>
  12. #include <LibGUI/Application.h>
  13. #include <LibGUI/Icon.h>
  14. #include <LibGUI/Window.h>
  15. #include <LibMain/Main.h>
  16. using namespace Help;
  17. static String parse_input(StringView input)
  18. {
  19. AK::URL url(input);
  20. if (url.is_valid())
  21. return url.basename();
  22. return input;
  23. }
  24. ErrorOr<int> serenity_main(Main::Arguments arguments)
  25. {
  26. TRY(Core::System::pledge("stdio recvfd sendfd rpath unix"));
  27. auto app = TRY(GUI::Application::try_create(arguments));
  28. TRY(Core::System::unveil("/res", "r"));
  29. TRY(Core::System::unveil("/usr/share/man", "r"));
  30. TRY(Core::System::unveil("/tmp/portal/filesystemaccess", "rw"));
  31. TRY(Core::System::unveil("/tmp/user/%uid/portal/launch", "rw"));
  32. TRY(Core::System::unveil("/tmp/user/%uid/portal/webcontent", "rw"));
  33. TRY(Core::System::unveil(nullptr, nullptr));
  34. String start_page;
  35. u32 section = 0;
  36. Core::ArgsParser args_parser;
  37. // FIXME: These custom Args are a hack. What we want to do is have an optional int arg, then an optional string.
  38. // However, when only a string is provided, it gets forwarded to the int argument since that is first, and
  39. // parsing fails. This hack instead forwards it to the start_page in that case.
  40. args_parser.add_positional_argument(Core::ArgsParser::Arg {
  41. .help_string = "Section of the man page",
  42. .name = "section",
  43. .min_values = 0,
  44. .max_values = 1,
  45. .accept_value = [&](char const* input_ptr) {
  46. StringView input { input_ptr, strlen(input_ptr) };
  47. // If it's a number, use it as the section
  48. if (auto number = input.to_int(); number.has_value()) {
  49. section = number.value();
  50. return true;
  51. }
  52. // Otherwise, use it as the start_page
  53. start_page = parse_input(input);
  54. return true;
  55. } });
  56. args_parser.add_positional_argument(Core::ArgsParser::Arg {
  57. .help_string = "Help page to open. Either an absolute path to the markdown file, or a search query",
  58. .name = "page",
  59. .min_values = 0,
  60. .max_values = 1,
  61. .accept_value = [&](char const* input_ptr) {
  62. StringView input { input_ptr, strlen(input_ptr) };
  63. // If start_page was already set by our section arg, then it can't be set again
  64. if (start_page.is_empty())
  65. return false;
  66. start_page = parse_input(input);
  67. return true;
  68. } });
  69. args_parser.parse(arguments);
  70. auto app_icon = GUI::Icon::default_icon("app-help"sv);
  71. auto window = TRY(GUI::Window::try_create());
  72. window->set_icon(app_icon.bitmap_for_size(16));
  73. window->set_title("Help");
  74. window->resize(570, 500);
  75. auto main_widget = TRY(window->try_set_main_widget<MainWidget>());
  76. TRY(main_widget->initialize_fallibles(window));
  77. main_widget->set_start_page(start_page, section);
  78. window->show();
  79. return app->exec();
  80. }