Shell: Port to LibMain

This commit is contained in:
Lucas CHOLLET 2022-01-03 01:59:39 +01:00 committed by Ali Mohammad Pur
parent 3fa5be655d
commit ed0f4bdfaf
Notes: sideshowbarker 2024-07-17 21:22:54 +09:00
3 changed files with 17 additions and 29 deletions

View file

@ -498,7 +498,7 @@ if (BUILD_LAGOM)
add_executable(shell_lagom ../../Userland/Shell/main.cpp)
set_target_properties(shell_lagom PROPERTIES OUTPUT_NAME shell)
target_link_libraries(shell_lagom LagomCore LagomShell)
target_link_libraries(shell_lagom LagomCore LagomShell LagomMain)
add_executable(wasm_lagom ../../Userland/Utilities/wasm.cpp)
set_target_properties(wasm_lagom PROPERTIES OUTPUT_NAME wasm)

View file

@ -24,7 +24,7 @@ set(SOURCES
)
serenity_bin(Shell)
target_link_libraries(Shell LibShell)
target_link_libraries(Shell LibShell LibMain)
install(DIRECTORY Tests/ DESTINATION usr/Tests/Shell
PATTERN "Tests/*"

View file

@ -9,17 +9,17 @@
#include <LibCore/Event.h>
#include <LibCore/EventLoop.h>
#include <LibCore/File.h>
#include <errno.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
RefPtr<Line::Editor> editor;
Shell::Shell* s_shell;
int main(int argc, char** argv)
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
Core::EventLoop loop;
@ -42,10 +42,7 @@ int main(int argc, char** argv)
});
#ifdef __serenity__
if (pledge("stdio rpath wpath cpath proc exec tty sigaction unix fattr", nullptr) < 0) {
perror("pledge");
return 1;
}
TRY(Core::System::pledge("stdio rpath wpath cpath proc exec tty sigaction unix fattr", nullptr));
#endif
RefPtr<::Shell::Shell> shell;
@ -104,38 +101,29 @@ int main(int argc, char** argv)
parser.add_positional_argument(file_to_read_from, "File to read commands from", "file", Core::ArgsParser::Required::No);
parser.add_positional_argument(script_args, "Extra arguments to pass to the script (via $* and co)", "argument", Core::ArgsParser::Required::No);
parser.parse(argc, argv);
parser.parse(arguments);
if (format) {
auto file = Core::File::open(format, Core::OpenMode::ReadOnly);
if (file.is_error()) {
warnln("Error: {}", file.error());
return 1;
}
auto file = TRY(Core::File::open(format, Core::OpenMode::ReadOnly));
initialize();
ssize_t cursor = -1;
puts(shell->format(file.value()->read_all(), cursor).characters());
puts(shell->format(file->read_all(), cursor).characters());
return 0;
}
auto pid = getpid();
if (auto sid = getsid(pid); sid == 0) {
if (setsid() < 0) {
perror("setsid");
// Let's just hope that it's ok.
}
if (auto res = Core::System::setsid(); res.is_error())
dbgln("{}", res.release_error());
} else if (sid != pid) {
if (getpgid(pid) != pid) {
if (setpgid(pid, sid) < 0) {
auto strerr = strerror(errno);
dbgln("couldn't setpgid: {}", strerr);
}
if (setsid() < 0) {
auto strerr = strerror(errno);
dbgln("couldn't setsid: {}", strerr);
}
if (auto res = Core::System::setpgid(pid, sid); res.is_error())
dbgln("{}", res.release_error());
if (auto res = Core::System::setsid(); res.is_error())
dbgln("{}", res.release_error());
}
}
@ -150,7 +138,7 @@ int main(int argc, char** argv)
initialize();
shell->set_live_formatting(should_format_live);
shell->current_script = argv[0];
shell->current_script = arguments.strings[0];
if (!skip_rc_files) {
auto run_rc_file = [&](auto& name) {