From 20a0572de8e502380c676a3c2dca958aa12cd68a Mon Sep 17 00:00:00 2001 From: Peter Elliott Date: Mon, 27 Dec 2021 19:43:45 -0700 Subject: [PATCH] Userland: Port less(1) to LibMain --- Userland/Utilities/CMakeLists.txt | 1 + Userland/Utilities/less.cpp | 42 +++++++++++++------------------ 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index 84e92f00e16..1ae985b01b0 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -105,6 +105,7 @@ target_link_libraries(ini LibMain) target_link_libraries(jp LibMain) target_link_libraries(js LibJS LibLine LibMain) target_link_libraries(keymap LibKeyboard LibMain) +target_link_libraries(less LibMain) target_link_libraries(logout LibMain) target_link_libraries(ls LibMain) target_link_libraries(lspci LibPCIDB LibMain) diff --git a/Userland/Utilities/less.cpp b/Userland/Utilities/less.cpp index 68e2a12f2ce..319712a59b7 100644 --- a/Userland/Utilities/less.cpp +++ b/Userland/Utilities/less.cpp @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include #include #include @@ -25,36 +27,34 @@ static struct termios g_save; // Flag set by a SIGWINCH signal handler to notify the main loop that the window has been resized. static Atomic g_resized { false }; -static void setup_tty(bool switch_buffer) +static ErrorOr setup_tty(bool switch_buffer) { // Save previous tty settings. - if (tcgetattr(STDOUT_FILENO, &g_save) == -1) { - perror("tcgetattr(3)"); - } + g_save = TRY(Core::System::tcgetattr(STDOUT_FILENO)); struct termios raw = g_save; raw.c_lflag &= ~(ECHO | ICANON); // Disable echo and line buffering - if (tcsetattr(STDOUT_FILENO, TCSAFLUSH, &raw) == -1) { - perror("tcsetattr(3)"); - } + TRY(Core::System::tcsetattr(STDOUT_FILENO, TCSAFLUSH, raw)); if (switch_buffer) { // Save cursor and switch to alternate buffer. out("\e[s\e[?1047h"); } + + return {}; } -static void teardown_tty(bool switch_buffer) +static ErrorOr teardown_tty(bool switch_buffer) { - if (tcsetattr(STDOUT_FILENO, TCSAFLUSH, &g_save) == -1) { - perror("tcsetattr(3)"); - } + TRY(Core::System::tcsetattr(STDOUT_FILENO, TCSAFLUSH, g_save)); if (switch_buffer) { out("\e[?1047l\e[u"); } + + return {}; } static Vector wrap_line(String const& string, size_t width) @@ -509,12 +509,9 @@ static void cat_file(FILE* file) } } -int main(int argc, char** argv) +ErrorOr serenity_main(Main::Arguments arguments) { - if (pledge("stdio rpath tty sigaction", nullptr) < 0) { - perror("pledge"); - return 1; - } + TRY(Core::System::pledge("stdio rpath tty sigaction", nullptr)); char const* filename = "-"; char const* prompt = "?f%f :.(line %l)?e (END):."; @@ -522,7 +519,7 @@ int main(int argc, char** argv) bool quit_at_eof = false; bool emulate_more = false; - if (LexicalPath::basename(argv[0]) == "more"sv) + if (LexicalPath::basename(arguments.strings[0]) == "more"sv) emulate_more = true; Core::ArgsParser args_parser; @@ -531,7 +528,7 @@ int main(int argc, char** argv) args_parser.add_option(dont_switch_buffer, "Don't use xterm alternate buffer", "no-init", 'X'); args_parser.add_option(quit_at_eof, "Exit when the end of the file is reached", "quit-at-eof", 'e'); args_parser.add_option(emulate_more, "Pretend that we are more(1)", "emulate-more", 'm'); - args_parser.parse(argc, argv); + args_parser.parse(arguments); FILE* file; if (String("-") == filename) { @@ -547,10 +544,7 @@ int main(int argc, char** argv) g_resized = true; }); - if (pledge("stdio tty", nullptr) < 0) { - perror("pledge"); - return 1; - } + TRY(Core::System::pledge("stdio tty", nullptr)); if (emulate_more) { // Configure options that match more's behavior @@ -564,7 +558,7 @@ int main(int argc, char** argv) return 0; } - setup_tty(!dont_switch_buffer); + TRY(setup_tty(!dont_switch_buffer)); Pager pager(filename, file, stdout, prompt); pager.init(); @@ -634,6 +628,6 @@ int main(int argc, char** argv) pager.clear_status(); - teardown_tty(!dont_switch_buffer); + TRY(teardown_tty(!dont_switch_buffer)); return 0; }