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