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

stty: Port to LibMain

This commit is contained in:
alexmajor 2022-01-22 13:51:33 -05:00 committed by Brian Gianforcaro
parent e3dbe19018
commit 2f1717182e
2 changed files with 16 additions and 32 deletions

View file

@ -164,6 +164,7 @@ target_link_libraries(sql LibLine LibMain LibSQL LibIPC)
target_link_libraries(sort LibMain) target_link_libraries(sort LibMain)
target_link_libraries(stat LibMain) target_link_libraries(stat LibMain)
target_link_libraries(strace LibMain) target_link_libraries(strace LibMain)
target_link_libraries(stty LibMain)
target_link_libraries(su LibCrypt LibMain) target_link_libraries(su LibCrypt LibMain)
target_link_libraries(sync LibMain) target_link_libraries(sync LibMain)
target_link_libraries(syscall LibMain) target_link_libraries(syscall LibMain)

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2021, Daniel Bertalan <dani@danielbertalan.dev> * Copyright (c) 2021, Daniel Bertalan <dani@danielbertalan.dev>
* Copyright (c) 2022, Alex Major
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -13,6 +14,8 @@
#include <AK/String.h> #include <AK/String.h>
#include <AK/StringView.h> #include <AK/StringView.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <ctype.h> #include <ctype.h>
#include <fcntl.h> #include <fcntl.h>
#include <getopt.h> #include <getopt.h>
@ -527,28 +530,19 @@ Result<void, int> apply_modes(size_t parameter_count, char** raw_parameters, ter
return {}; return {};
} }
int main(int argc, char** argv) ErrorOr<int> serenity_main(Main::Arguments arguments)
{ {
if (pledge("stdio tty rpath", nullptr) < 0) { TRY(Core::System::pledge("stdio tty rpath"));
perror("pledge"); TRY(Core::System::unveil("/dev", "r"));
return 1; TRY(Core::System::unveil(nullptr, nullptr));
}
if (unveil("/dev", "r") < 0) {
perror("unveil");
return 1;
}
if (unveil(nullptr, nullptr) < 0) {
perror("unveil");
return 1;
}
String device_file; String device_file;
bool stty_readable = false; bool stty_readable = false;
bool all_settings = false; bool all_settings = false;
// Core::ArgsParser can't handle the weird syntax of stty, so we use getopt_long instead. // Core::ArgsParser can't handle the weird syntax of stty, so we use getopt_long instead.
int argc = arguments.argc;
char** argv = arguments.argv;
opterr = 0; // We handle unknown flags gracefully by starting to parse the arguments in `apply_modes`. opterr = 0; // We handle unknown flags gracefully by starting to parse the arguments in `apply_modes`.
int optc; int optc;
bool should_quit = false; bool should_quit = false;
@ -588,35 +582,24 @@ int main(int argc, char** argv)
ScopeGuard file_close_guard = [&] { close(terminal_fd); }; ScopeGuard file_close_guard = [&] { close(terminal_fd); };
termios initial_termios; termios initial_termios = TRY(Core::System::tcgetattr(terminal_fd));
if (tcgetattr(terminal_fd, &initial_termios) < 0) {
perror("tcgetattr");
exit(1);
}
winsize initial_winsize; winsize initial_winsize;
if (ioctl(terminal_fd, TIOCGWINSZ, &initial_winsize) < 0) { TRY(Core::System::ioctl(terminal_fd, TIOCGWINSZ, &initial_winsize));
perror("ioctl(TIOCGWINSZ)");
exit(1);
}
if (optind < argc) { if (optind < argc) {
if (stty_readable || all_settings) { if (stty_readable || all_settings) {
warnln("Modes cannot be set when printing settings"); warnln("Modes cannot be set when printing settings");
exit(1); exit(1);
} }
auto result = apply_modes(argc - optind, argv + optind, initial_termios, initial_winsize); auto result = apply_modes(argc - optind, argv + optind, initial_termios, initial_winsize);
if (result.is_error()) if (result.is_error())
return result.error(); return result.error();
if (tcsetattr(terminal_fd, TCSADRAIN, &initial_termios) < 0) { TRY(Core::System::tcsetattr(terminal_fd, TCSADRAIN, initial_termios));
perror("tcsetattr"); TRY(Core::System::ioctl(terminal_fd, TIOCSWINSZ, &initial_winsize));
exit(1);
}
if (ioctl(terminal_fd, TIOCSWINSZ, &initial_winsize) < 0) {
perror("ioctl(TIOCSWINSZ)");
exit(1);
}
} else if (stty_readable) { } else if (stty_readable) {
print_stty_readable(initial_termios); print_stty_readable(initial_termios);
} else { } else {