mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:07:36 +00:00
Userland: Port less(1) to LibMain
This commit is contained in:
parent
6312742257
commit
20a0572de8
2 changed files with 19 additions and 24 deletions
|
@ -105,6 +105,7 @@ target_link_libraries(ini LibMain)
|
||||||
target_link_libraries(jp LibMain)
|
target_link_libraries(jp LibMain)
|
||||||
target_link_libraries(js LibJS LibLine LibMain)
|
target_link_libraries(js LibJS LibLine LibMain)
|
||||||
target_link_libraries(keymap LibKeyboard LibMain)
|
target_link_libraries(keymap LibKeyboard LibMain)
|
||||||
|
target_link_libraries(less LibMain)
|
||||||
target_link_libraries(logout LibMain)
|
target_link_libraries(logout LibMain)
|
||||||
target_link_libraries(ls LibMain)
|
target_link_libraries(ls LibMain)
|
||||||
target_link_libraries(lspci LibPCIDB LibMain)
|
target_link_libraries(lspci LibPCIDB LibMain)
|
||||||
|
|
|
@ -12,6 +12,8 @@
|
||||||
#include <AK/Utf8View.h>
|
#include <AK/Utf8View.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
|
#include <LibCore/System.h>
|
||||||
|
#include <LibMain/Main.h>
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
@ -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.
|
// Flag set by a SIGWINCH signal handler to notify the main loop that the window has been resized.
|
||||||
static Atomic<bool> g_resized { false };
|
static Atomic<bool> g_resized { false };
|
||||||
|
|
||||||
static void setup_tty(bool switch_buffer)
|
static ErrorOr<void> setup_tty(bool switch_buffer)
|
||||||
{
|
{
|
||||||
// Save previous tty settings.
|
// Save previous tty settings.
|
||||||
if (tcgetattr(STDOUT_FILENO, &g_save) == -1) {
|
g_save = TRY(Core::System::tcgetattr(STDOUT_FILENO));
|
||||||
perror("tcgetattr(3)");
|
|
||||||
}
|
|
||||||
|
|
||||||
struct termios raw = g_save;
|
struct termios raw = g_save;
|
||||||
raw.c_lflag &= ~(ECHO | ICANON);
|
raw.c_lflag &= ~(ECHO | ICANON);
|
||||||
|
|
||||||
// Disable echo and line buffering
|
// Disable echo and line buffering
|
||||||
if (tcsetattr(STDOUT_FILENO, TCSAFLUSH, &raw) == -1) {
|
TRY(Core::System::tcsetattr(STDOUT_FILENO, TCSAFLUSH, raw));
|
||||||
perror("tcsetattr(3)");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (switch_buffer) {
|
if (switch_buffer) {
|
||||||
// Save cursor and switch to alternate buffer.
|
// Save cursor and switch to alternate buffer.
|
||||||
out("\e[s\e[?1047h");
|
out("\e[s\e[?1047h");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
static void teardown_tty(bool switch_buffer)
|
static ErrorOr<void> teardown_tty(bool switch_buffer)
|
||||||
{
|
{
|
||||||
if (tcsetattr(STDOUT_FILENO, TCSAFLUSH, &g_save) == -1) {
|
TRY(Core::System::tcsetattr(STDOUT_FILENO, TCSAFLUSH, g_save));
|
||||||
perror("tcsetattr(3)");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (switch_buffer) {
|
if (switch_buffer) {
|
||||||
out("\e[?1047l\e[u");
|
out("\e[?1047l\e[u");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
static Vector<StringView> wrap_line(String const& string, size_t width)
|
static Vector<StringView> 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<int> serenity_main(Main::Arguments arguments)
|
||||||
{
|
{
|
||||||
if (pledge("stdio rpath tty sigaction", nullptr) < 0) {
|
TRY(Core::System::pledge("stdio rpath tty sigaction", nullptr));
|
||||||
perror("pledge");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
char const* filename = "-";
|
char const* filename = "-";
|
||||||
char const* prompt = "?f%f :.(line %l)?e (END):.";
|
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 quit_at_eof = false;
|
||||||
bool emulate_more = false;
|
bool emulate_more = false;
|
||||||
|
|
||||||
if (LexicalPath::basename(argv[0]) == "more"sv)
|
if (LexicalPath::basename(arguments.strings[0]) == "more"sv)
|
||||||
emulate_more = true;
|
emulate_more = true;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
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(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(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.add_option(emulate_more, "Pretend that we are more(1)", "emulate-more", 'm');
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
FILE* file;
|
FILE* file;
|
||||||
if (String("-") == filename) {
|
if (String("-") == filename) {
|
||||||
|
@ -547,10 +544,7 @@ int main(int argc, char** argv)
|
||||||
g_resized = true;
|
g_resized = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (pledge("stdio tty", nullptr) < 0) {
|
TRY(Core::System::pledge("stdio tty", nullptr));
|
||||||
perror("pledge");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (emulate_more) {
|
if (emulate_more) {
|
||||||
// Configure options that match more's behavior
|
// Configure options that match more's behavior
|
||||||
|
@ -564,7 +558,7 @@ int main(int argc, char** argv)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
setup_tty(!dont_switch_buffer);
|
TRY(setup_tty(!dont_switch_buffer));
|
||||||
|
|
||||||
Pager pager(filename, file, stdout, prompt);
|
Pager pager(filename, file, stdout, prompt);
|
||||||
pager.init();
|
pager.init();
|
||||||
|
@ -634,6 +628,6 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
pager.clear_status();
|
pager.clear_status();
|
||||||
|
|
||||||
teardown_tty(!dont_switch_buffer);
|
TRY(teardown_tty(!dont_switch_buffer));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue