mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-10 01:51:03 +09:00
man: Refactor with LibManual and Core::Stream
This commit is contained in:
parent
146bce53dd
commit
64ca546a06
Notes:
sideshowbarker
2024-07-17 07:08:37 +09:00
Author: https://github.com/kleinesfilmroellchen
Commit: 64ca546a06
Pull-request: https://github.com/SerenityOS/serenity/pull/14393
Reviewed-by: https://github.com/ADKaster ✅
Reviewed-by: https://github.com/sin-ack
2 changed files with 29 additions and 40 deletions
|
@ -98,7 +98,7 @@ target_link_libraries(keymap PRIVATE LibKeyboard)
|
||||||
target_link_libraries(less PRIVATE LibLine)
|
target_link_libraries(less PRIVATE LibLine)
|
||||||
target_link_libraries(lspci PRIVATE LibPCIDB)
|
target_link_libraries(lspci PRIVATE LibPCIDB)
|
||||||
target_link_libraries(lsusb PRIVATE LibUSBDB)
|
target_link_libraries(lsusb PRIVATE LibUSBDB)
|
||||||
target_link_libraries(man PRIVATE LibMarkdown)
|
target_link_libraries(man PRIVATE LibMarkdown LibManual)
|
||||||
target_link_libraries(markdown-check PRIVATE LibMarkdown)
|
target_link_libraries(markdown-check PRIVATE LibMarkdown)
|
||||||
target_link_libraries(matroska PRIVATE LibVideo)
|
target_link_libraries(matroska PRIVATE LibVideo)
|
||||||
target_link_libraries(md PRIVATE LibMarkdown)
|
target_link_libraries(md PRIVATE LibMarkdown)
|
||||||
|
|
|
@ -8,13 +8,14 @@
|
||||||
#include <AK/ByteBuffer.h>
|
#include <AK/ByteBuffer.h>
|
||||||
#include <AK/DeprecatedString.h>
|
#include <AK/DeprecatedString.h>
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
|
#include <LibCore/File.h>
|
||||||
#include <LibCore/Stream.h>
|
#include <LibCore/Stream.h>
|
||||||
#include <LibCore/System.h>
|
#include <LibCore/System.h>
|
||||||
#include <LibMain/Main.h>
|
#include <LibMain/Main.h>
|
||||||
|
#include <LibManual/PageNode.h>
|
||||||
|
#include <LibManual/SectionNode.h>
|
||||||
#include <LibMarkdown/Document.h>
|
#include <LibMarkdown/Document.h>
|
||||||
#include <fcntl.h>
|
|
||||||
#include <spawn.h>
|
#include <spawn.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
@ -29,11 +30,7 @@ static ErrorOr<pid_t> pipe_to_pager(DeprecatedString const& command)
|
||||||
posix_spawn_file_actions_init(&action);
|
posix_spawn_file_actions_init(&action);
|
||||||
posix_spawn_file_actions_adddup2(&action, stdout_pipe[0], STDIN_FILENO);
|
posix_spawn_file_actions_adddup2(&action, stdout_pipe[0], STDIN_FILENO);
|
||||||
|
|
||||||
pid_t pid;
|
pid_t pid = TRY(Core::System::posix_spawnp("sh"sv, &action, nullptr, const_cast<char**>(argv), environ));
|
||||||
if ((errno = posix_spawnp(&pid, argv[0], &action, nullptr, const_cast<char**>(argv), environ))) {
|
|
||||||
perror("posix_spawn");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
posix_spawn_file_actions_destroy(&action);
|
posix_spawn_file_actions_destroy(&action);
|
||||||
|
|
||||||
TRY(Core::System::dup2(stdout_pipe[1], STDOUT_FILENO));
|
TRY(Core::System::dup2(stdout_pipe[1], STDOUT_FILENO));
|
||||||
|
@ -45,7 +42,7 @@ static ErrorOr<pid_t> pipe_to_pager(DeprecatedString const& command)
|
||||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
{
|
{
|
||||||
int view_width = 0;
|
int view_width = 0;
|
||||||
if (isatty(STDOUT_FILENO)) {
|
if (isatty(STDOUT_FILENO) != 0) {
|
||||||
struct winsize ws;
|
struct winsize ws;
|
||||||
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0)
|
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0)
|
||||||
view_width = ws.ws_col;
|
view_width = ws.ws_col;
|
||||||
|
@ -59,9 +56,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
TRY(Core::System::unveil("/bin", "x"));
|
TRY(Core::System::unveil("/bin", "x"));
|
||||||
TRY(Core::System::unveil(nullptr, nullptr));
|
TRY(Core::System::unveil(nullptr, nullptr));
|
||||||
|
|
||||||
StringView section {};
|
DeprecatedString section;
|
||||||
StringView name {};
|
DeprecatedString name;
|
||||||
StringView pager {};
|
DeprecatedString pager;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
args_parser.set_general_help("Read manual pages. Try 'man man' to get started.");
|
args_parser.set_general_help("Read manual pages. Try 'man man' to get started.");
|
||||||
|
@ -70,49 +67,41 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
args_parser.add_option(pager, "Pager to pipe the man page to", "pager", 'P', "pager");
|
args_parser.add_option(pager, "Pager to pipe the man page to", "pager", 'P', "pager");
|
||||||
args_parser.parse(arguments);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
auto make_path = [name](StringView section) {
|
Optional<NonnullRefPtr<Manual::PageNode>> page;
|
||||||
return DeprecatedString::formatted("/usr/share/man/man{}/{}.md", section, name);
|
|
||||||
};
|
|
||||||
if (section.is_empty()) {
|
if (section.is_empty()) {
|
||||||
constexpr StringView sections[] = {
|
for (auto const& s : Manual::sections) {
|
||||||
"1"sv,
|
auto const maybe_page = make_ref_counted<Manual::PageNode>(s, TRY(String::from_utf8(name)));
|
||||||
"2"sv,
|
if (Core::File::exists(TRY(maybe_page->path()).to_deprecated_string())) {
|
||||||
"3"sv,
|
page = maybe_page;
|
||||||
"4"sv,
|
section = s->section_name().to_deprecated_string();
|
||||||
"5"sv,
|
|
||||||
"6"sv,
|
|
||||||
"7"sv,
|
|
||||||
"8"sv
|
|
||||||
};
|
|
||||||
for (auto s : sections) {
|
|
||||||
DeprecatedString path = make_path(s);
|
|
||||||
if (access(path.characters(), R_OK) == 0) {
|
|
||||||
section = s;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
auto number_section = section.to_uint();
|
||||||
|
if (number_section.has_value())
|
||||||
|
page = make_ref_counted<Manual::PageNode>(Manual::sections[number_section.value() - 1], TRY(String::from_utf8(name)));
|
||||||
|
else
|
||||||
|
warnln("Section name '{}' invalid", section);
|
||||||
}
|
}
|
||||||
auto filename = make_path(section);
|
|
||||||
if (section == nullptr) {
|
if (!page.has_value()) {
|
||||||
warnln("No man page for {}", name);
|
warnln("No man page for {}", name);
|
||||||
exit(1);
|
exit(1);
|
||||||
} else if (access(filename.characters(), R_OK) != 0) {
|
} else if (!Core::File::exists(TRY((*page)->path()))) {
|
||||||
warnln("No man page for {} in section {}", name, section);
|
warnln("No man page for {} in section {}", name, section);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
DeprecatedString pager_command;
|
if (pager.is_empty())
|
||||||
if (!pager.is_empty())
|
pager = TRY(String::formatted("less -P 'Manual Page {}({}) line %l?e (END):.'", StringView(name).replace("'"sv, "'\\''"sv, ReplaceMode::FirstOnly), StringView(section).replace("'"sv, "'\\''"sv, ReplaceMode::FirstOnly))).to_deprecated_string();
|
||||||
pager_command = pager;
|
pid_t pager_pid = TRY(pipe_to_pager(pager));
|
||||||
else
|
|
||||||
pager_command = DeprecatedString::formatted("less -P 'Manual Page {}({}) line %l?e (END):.'", StringView(name).replace("'"sv, "'\\''"sv, ReplaceMode::FirstOnly), StringView(section).replace("'"sv, "'\\''"sv, ReplaceMode::FirstOnly));
|
|
||||||
pid_t pager_pid = TRY(pipe_to_pager(pager_command));
|
|
||||||
|
|
||||||
auto file = TRY(Core::Stream::File::open(filename, Core::Stream::OpenMode::Read));
|
auto file = TRY(Core::Stream::File::open(TRY((*page)->path()), Core::Stream::OpenMode::Read));
|
||||||
|
|
||||||
TRY(Core::System::pledge("stdio proc"));
|
TRY(Core::System::pledge("stdio proc"));
|
||||||
|
|
||||||
dbgln("Loading man page from {}", filename);
|
dbgln("Loading man page from {}", (*page)->path());
|
||||||
auto buffer = TRY(file->read_all());
|
auto buffer = TRY(file->read_all());
|
||||||
auto source = DeprecatedString::copy(buffer);
|
auto source = DeprecatedString::copy(buffer);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue