man.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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", nullptr));
  49. TRY(Core::System::unveil("/usr/share/man", "r"));
  50. TRY(Core::System::unveil("/bin", "x"));
  51. TRY(Core::System::unveil(nullptr, nullptr));
  52. const char* section = nullptr;
  53. const char* name = nullptr;
  54. const char* pager = nullptr;
  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](const char* section) {
  62. return String::formatted("/usr/share/man/man{}/{}.md", section, name);
  63. };
  64. if (!section) {
  65. const char* sections[] = {
  66. "1",
  67. "2",
  68. "3",
  69. "4",
  70. "5",
  71. "6",
  72. "7",
  73. "8"
  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. if (!section) {
  83. warnln("No man page for {}", name);
  84. exit(1);
  85. }
  86. }
  87. auto filename = make_path(section);
  88. String pager_command;
  89. if (pager)
  90. pager_command = pager;
  91. else
  92. pager_command = String::formatted("less -P 'Manual Page {}({}) line %l?e (END):.'", StringView(name).replace("'", "'\\''"), StringView(section).replace("'", "'\\''"));
  93. pid_t pager_pid = TRY(pipe_to_pager(pager_command));
  94. auto file = TRY(Core::File::open(filename, Core::OpenMode::ReadOnly));
  95. TRY(Core::System::pledge("stdio proc", nullptr));
  96. dbgln("Loading man page from {}", file->filename());
  97. auto buffer = file->read_all();
  98. auto source = String::copy(buffer);
  99. outln("{}({})\t\tSerenityOS manual", name, section);
  100. auto document = Markdown::Document::parse(source);
  101. VERIFY(document);
  102. String rendered = document->render_for_terminal(view_width);
  103. out("{}", rendered);
  104. // FIXME: Remove this wait, it shouldn't be necessary but Shell does not
  105. // resume properly without it. This wait also breaks <C-z> backgrounding
  106. fclose(stdout);
  107. int wstatus;
  108. waitpid(pager_pid, &wstatus, 0);
  109. return 0;
  110. }