man.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/String.h>
  9. #include <LibCore/ArgsParser.h>
  10. #include <LibCore/File.h>
  11. #include <LibCore/System.h>
  12. #include <LibMain/Main.h>
  13. #include <LibMarkdown/Document.h>
  14. #include <fcntl.h>
  15. #include <spawn.h>
  16. #include <stdio.h>
  17. #include <sys/ioctl.h>
  18. #include <sys/wait.h>
  19. #include <unistd.h>
  20. static ErrorOr<pid_t> pipe_to_pager(String const& command)
  21. {
  22. char const* argv[] = { "sh", "-c", command.characters(), nullptr };
  23. auto stdout_pipe = TRY(Core::System::pipe2(O_CLOEXEC));
  24. posix_spawn_file_actions_t action;
  25. posix_spawn_file_actions_init(&action);
  26. posix_spawn_file_actions_adddup2(&action, stdout_pipe[0], STDIN_FILENO);
  27. pid_t pid;
  28. if ((errno = posix_spawnp(&pid, argv[0], &action, nullptr, const_cast<char**>(argv), environ))) {
  29. perror("posix_spawn");
  30. exit(1);
  31. }
  32. posix_spawn_file_actions_destroy(&action);
  33. TRY(Core::System::dup2(stdout_pipe[1], STDOUT_FILENO));
  34. TRY(Core::System::close(stdout_pipe[1]));
  35. TRY(Core::System::close(stdout_pipe[0]));
  36. return pid;
  37. }
  38. ErrorOr<int> serenity_main(Main::Arguments arguments)
  39. {
  40. int view_width = 0;
  41. if (isatty(STDOUT_FILENO)) {
  42. struct winsize ws;
  43. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0)
  44. view_width = ws.ws_col;
  45. }
  46. if (view_width == 0)
  47. view_width = 80;
  48. TRY(Core::System::pledge("stdio rpath exec proc"));
  49. TRY(Core::System::unveil("/usr/share/man", "r"));
  50. TRY(Core::System::unveil("/bin", "x"));
  51. TRY(Core::System::unveil(nullptr, nullptr));
  52. StringView section {};
  53. StringView name {};
  54. StringView pager {};
  55. Core::ArgsParser args_parser;
  56. args_parser.set_general_help("Read manual pages. Try 'man man' to get started.");
  57. args_parser.add_positional_argument(section, "Section of the man page", "section", Core::ArgsParser::Required::No);
  58. args_parser.add_positional_argument(name, "Name of the man page", "name");
  59. args_parser.add_option(pager, "Pager to pipe the man page to", "pager", 'P', "pager");
  60. args_parser.parse(arguments);
  61. auto make_path = [name](StringView section) {
  62. return String::formatted("/usr/share/man/man{}/{}.md", section, name);
  63. };
  64. if (section.is_empty()) {
  65. constexpr StringView sections[] = {
  66. "1"sv,
  67. "2"sv,
  68. "3"sv,
  69. "4"sv,
  70. "5"sv,
  71. "6"sv,
  72. "7"sv,
  73. "8"sv
  74. };
  75. for (auto s : sections) {
  76. String path = make_path(s);
  77. if (access(path.characters(), R_OK) == 0) {
  78. section = s;
  79. break;
  80. }
  81. }
  82. }
  83. auto filename = make_path(section);
  84. if (section == nullptr) {
  85. warnln("No man page for {}", name);
  86. exit(1);
  87. } else if (access(filename.characters(), R_OK) != 0) {
  88. warnln("No man page for {} in section {}", name, section);
  89. exit(1);
  90. }
  91. String pager_command;
  92. if (!pager.is_empty())
  93. pager_command = pager;
  94. else
  95. pager_command = String::formatted("less -P 'Manual Page {}({}) line %l?e (END):.'", StringView(name).replace("'"sv, "'\\''"sv, ReplaceMode::FirstOnly), StringView(section).replace("'"sv, "'\\''"sv, ReplaceMode::FirstOnly));
  96. pid_t pager_pid = TRY(pipe_to_pager(pager_command));
  97. auto file = TRY(Core::File::open(filename, Core::OpenMode::ReadOnly));
  98. TRY(Core::System::pledge("stdio proc"));
  99. dbgln("Loading man page from {}", file->filename());
  100. auto buffer = file->read_all();
  101. auto source = String::copy(buffer);
  102. const String title("SerenityOS manual");
  103. int spaces = view_width / 2 - String(name).length() - String(section).length() - title.length() / 2 - 4;
  104. if (spaces < 0)
  105. spaces = 0;
  106. out("{}({})", name, section);
  107. while (spaces--)
  108. out(" ");
  109. outln(title);
  110. auto document = Markdown::Document::parse(source);
  111. VERIFY(document);
  112. String rendered = document->render_for_terminal(view_width);
  113. outln("{}", rendered);
  114. // FIXME: Remove this wait, it shouldn't be necessary but Shell does not
  115. // resume properly without it. This wait also breaks <C-z> backgrounding
  116. fclose(stdout);
  117. int wstatus;
  118. waitpid(pager_pid, &wstatus, 0);
  119. return 0;
  120. }