1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 08:07:34 +00:00

man: Port to LibMain :^)

This commit is contained in:
Andreas Kling 2021-11-24 22:13:53 +01:00
parent f69741fa70
commit 1682cd10be
2 changed files with 13 additions and 30 deletions

View file

@ -79,7 +79,7 @@ target_link_libraries(keymap LibKeyboard LibMain)
target_link_libraries(logout LibMain) target_link_libraries(logout LibMain)
target_link_libraries(lspci LibPCIDB) target_link_libraries(lspci LibPCIDB)
target_link_libraries(lsusb LibUSBDB LibMain) target_link_libraries(lsusb LibUSBDB LibMain)
target_link_libraries(man LibMarkdown) target_link_libraries(man LibMarkdown LibMain)
target_link_libraries(markdown-check LibMarkdown) target_link_libraries(markdown-check LibMarkdown)
target_link_libraries(matroska LibVideo) target_link_libraries(matroska LibVideo)
target_link_libraries(md LibMarkdown) target_link_libraries(md LibMarkdown)

View file

@ -9,6 +9,8 @@
#include <AK/String.h> #include <AK/String.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/File.h> #include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <LibMarkdown/Document.h> #include <LibMarkdown/Document.h>
#include <fcntl.h> #include <fcntl.h>
#include <spawn.h> #include <spawn.h>
@ -43,7 +45,7 @@ static pid_t pipe_to_pager(String const& command)
return pid; return pid;
} }
int main(int argc, char* argv[]) ErrorOr<int> serenity_main(Main::Arguments arguments)
{ {
int view_width = 0; int view_width = 0;
if (isatty(STDOUT_FILENO)) { if (isatty(STDOUT_FILENO)) {
@ -55,22 +57,10 @@ int main(int argc, char* argv[])
if (view_width == 0) if (view_width == 0)
view_width = 80; view_width = 80;
if (pledge("stdio rpath exec proc", nullptr) < 0) { TRY(Core::System::pledge("stdio rpath exec proc", nullptr));
perror("pledge"); TRY(Core::System::unveil("/usr/share/man", "r"));
return 1; TRY(Core::System::unveil("/bin", "x"));
} TRY(Core::System::unveil(nullptr, nullptr));
if (unveil("/usr/share/man", "r") < 0) {
perror("unveil");
return 1;
}
if (unveil("/bin", "x") < 0) {
perror("unveil");
return 1;
}
unveil(nullptr, nullptr);
const char* section = nullptr; const char* section = nullptr;
const char* name = nullptr; const char* name = nullptr;
@ -81,8 +71,7 @@ int main(int argc, char* argv[])
args_parser.add_positional_argument(section, "Section of the man page", "section", Core::ArgsParser::Required::No); args_parser.add_positional_argument(section, "Section of the man page", "section", Core::ArgsParser::Required::No);
args_parser.add_positional_argument(name, "Name of the man page", "name"); args_parser.add_positional_argument(name, "Name of the man page", "name");
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(argc, argv);
auto make_path = [name](const char* section) { auto make_path = [name](const char* section) {
return String::formatted("/usr/share/man/man{}/{}.md", section, name); return String::formatted("/usr/share/man/man{}/{}.md", section, name);
@ -111,8 +100,7 @@ int main(int argc, char* argv[])
} }
} }
auto file = Core::File::construct(); auto filename = make_path(section);
file->set_filename(make_path(section));
String pager_command; String pager_command;
if (pager) if (pager)
@ -121,15 +109,9 @@ int main(int argc, char* argv[])
pager_command = String::formatted("less -P 'Manual Page {}({}) line %l?e (END):.'", StringView(name).replace("'", "'\\''"), StringView(section).replace("'", "'\\''")); pager_command = String::formatted("less -P 'Manual Page {}({}) line %l?e (END):.'", StringView(name).replace("'", "'\\''"), StringView(section).replace("'", "'\\''"));
pid_t pager_pid = pipe_to_pager(pager_command); pid_t pager_pid = pipe_to_pager(pager_command);
if (!file->open(Core::OpenMode::ReadOnly)) { auto file = TRY(Core::File::open(filename, Core::OpenMode::ReadOnly));
perror("Failed to open man page file");
exit(1);
}
if (pledge("stdio proc", nullptr) < 0) { TRY(Core::System::pledge("stdio proc", nullptr));
perror("pledge");
return 1;
}
dbgln("Loading man page from {}", file->filename()); dbgln("Loading man page from {}", file->filename());
auto buffer = file->read_all(); auto buffer = file->read_all();
@ -148,4 +130,5 @@ int main(int argc, char* argv[])
fclose(stdout); fclose(stdout);
int wstatus; int wstatus;
waitpid(pager_pid, &wstatus, 0); waitpid(pager_pid, &wstatus, 0);
return 0;
} }