2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-05-16 19:05:13 +00:00
|
|
|
#include "Shell.h"
|
2020-06-17 14:43:34 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-05-16 19:05:13 +00:00
|
|
|
#include <LibCore/Event.h>
|
|
|
|
#include <LibCore/EventLoop.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/File.h>
|
2021-03-12 16:29:37 +00:00
|
|
|
#include <errno.h>
|
2018-11-06 09:46:40 +00:00
|
|
|
#include <signal.h>
|
2018-11-09 09:24:41 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2021-03-12 16:29:37 +00:00
|
|
|
#include <unistd.h>
|
2018-10-26 12:24:11 +00:00
|
|
|
|
2020-05-26 10:34:39 +00:00
|
|
|
RefPtr<Line::Editor> editor;
|
2020-10-01 14:43:01 +00:00
|
|
|
Shell::Shell* s_shell;
|
2019-04-25 14:10:16 +00:00
|
|
|
|
2020-09-07 18:15:31 +00:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
Core::EventLoop loop;
|
|
|
|
|
|
|
|
Core::EventLoop::register_signal(SIGINT, [](int) {
|
|
|
|
s_shell->kill_job(s_shell->current_job(), SIGINT);
|
|
|
|
});
|
|
|
|
|
|
|
|
Core::EventLoop::register_signal(SIGWINCH, [](int) {
|
|
|
|
s_shell->kill_job(s_shell->current_job(), SIGWINCH);
|
|
|
|
});
|
|
|
|
|
|
|
|
Core::EventLoop::register_signal(SIGTTIN, [](int) {});
|
|
|
|
Core::EventLoop::register_signal(SIGTTOU, [](int) {});
|
|
|
|
|
|
|
|
Core::EventLoop::register_signal(SIGHUP, [](int) {
|
|
|
|
for (auto& it : s_shell->jobs)
|
|
|
|
s_shell->kill_job(it.value.ptr(), SIGHUP);
|
|
|
|
|
2020-10-25 23:25:41 +00:00
|
|
|
s_shell->editor()->save_history(s_shell->get_history_path());
|
2020-09-07 18:15:31 +00:00
|
|
|
});
|
|
|
|
|
2020-06-25 12:45:23 +00:00
|
|
|
#ifdef __serenity__
|
2021-05-13 21:20:26 +00:00
|
|
|
if (pledge("stdio rpath wpath cpath proc exec tty sigaction unix fattr", nullptr) < 0) {
|
2020-05-26 10:52:42 +00:00
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
2020-06-25 12:45:23 +00:00
|
|
|
#endif
|
2020-05-26 10:52:42 +00:00
|
|
|
|
2021-03-07 06:12:24 +00:00
|
|
|
RefPtr<::Shell::Shell> shell;
|
|
|
|
bool attempt_interactive = false;
|
|
|
|
|
|
|
|
auto initialize = [&] {
|
|
|
|
editor = Line::Editor::construct();
|
|
|
|
editor->initialize();
|
|
|
|
|
|
|
|
shell = Shell::Shell::construct(*editor, attempt_interactive);
|
|
|
|
s_shell = shell.ptr();
|
|
|
|
|
|
|
|
s_shell->setup_signals();
|
|
|
|
|
|
|
|
#ifndef __serenity__
|
|
|
|
sigset_t blocked;
|
|
|
|
sigemptyset(&blocked);
|
|
|
|
sigaddset(&blocked, SIGTTOU);
|
|
|
|
sigaddset(&blocked, SIGTTIN);
|
|
|
|
pthread_sigmask(SIG_BLOCK, &blocked, nullptr);
|
|
|
|
#endif
|
|
|
|
shell->termios = editor->termios();
|
|
|
|
shell->default_termios = editor->default_termios();
|
|
|
|
|
|
|
|
editor->on_display_refresh = [&](auto& editor) {
|
|
|
|
editor.strip_styles();
|
|
|
|
if (shell->should_format_live()) {
|
|
|
|
auto line = editor.line();
|
|
|
|
ssize_t cursor = editor.cursor();
|
|
|
|
editor.clear_line();
|
|
|
|
editor.insert(shell->format(line, cursor));
|
|
|
|
if (cursor >= 0)
|
|
|
|
editor.set_cursor(cursor);
|
|
|
|
}
|
|
|
|
shell->highlight(editor);
|
|
|
|
};
|
|
|
|
editor->on_tab_complete = [&](const Line::Editor&) {
|
|
|
|
return shell->complete();
|
|
|
|
};
|
2020-05-26 10:52:42 +00:00
|
|
|
};
|
|
|
|
|
2020-06-17 14:43:34 +00:00
|
|
|
const char* command_to_run = nullptr;
|
|
|
|
const char* file_to_read_from = nullptr;
|
2020-08-04 04:57:25 +00:00
|
|
|
Vector<const char*> script_args;
|
2020-07-07 09:27:35 +00:00
|
|
|
bool skip_rc_files = false;
|
2020-09-16 00:43:04 +00:00
|
|
|
const char* format = nullptr;
|
2020-09-16 00:48:33 +00:00
|
|
|
bool should_format_live = false;
|
2021-07-04 08:59:40 +00:00
|
|
|
bool keep_open = false;
|
2020-06-17 14:43:34 +00:00
|
|
|
|
|
|
|
Core::ArgsParser parser;
|
|
|
|
parser.add_option(command_to_run, "String to read commands from", "command-string", 'c', "command-string");
|
2020-07-07 09:27:35 +00:00
|
|
|
parser.add_option(skip_rc_files, "Skip running shellrc files", "skip-shellrc", 0);
|
2020-10-02 16:23:47 +00:00
|
|
|
parser.add_option(format, "Format the given file into stdout and exit", "format", 0, "file");
|
2020-09-16 00:48:33 +00:00
|
|
|
parser.add_option(should_format_live, "Enable live formatting", "live-formatting", 'f');
|
2021-07-04 08:59:40 +00:00
|
|
|
parser.add_option(keep_open, "Keep the shell open after running the specified command or file", "keep-open", 0);
|
2020-08-04 04:57:25 +00:00
|
|
|
parser.add_positional_argument(file_to_read_from, "File to read commands from", "file", Core::ArgsParser::Required::No);
|
2021-01-22 16:42:36 +00:00
|
|
|
parser.add_positional_argument(script_args, "Extra arguments to pass to the script (via $* and co)", "argument", Core::ArgsParser::Required::No);
|
2020-06-17 14:43:34 +00:00
|
|
|
|
|
|
|
parser.parse(argc, argv);
|
|
|
|
|
2020-09-16 00:43:04 +00:00
|
|
|
if (format) {
|
2021-05-12 09:26:43 +00:00
|
|
|
auto file = Core::File::open(format, Core::OpenMode::ReadOnly);
|
2020-09-16 00:43:04 +00:00
|
|
|
if (file.is_error()) {
|
2021-05-02 06:57:53 +00:00
|
|
|
warnln("Error: {}", file.error());
|
2020-09-16 00:43:04 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-03-07 06:12:24 +00:00
|
|
|
initialize();
|
|
|
|
|
2020-09-16 00:43:04 +00:00
|
|
|
ssize_t cursor = -1;
|
|
|
|
puts(shell->format(file.value()->read_all(), cursor).characters());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-30 10:35:28 +00:00
|
|
|
auto pid = getpid();
|
|
|
|
if (auto sid = getsid(pid); sid == 0) {
|
2020-09-18 06:52:25 +00:00
|
|
|
if (setsid() < 0) {
|
|
|
|
perror("setsid");
|
|
|
|
// Let's just hope that it's ok.
|
|
|
|
}
|
2020-09-30 10:35:28 +00:00
|
|
|
} else if (sid != pid) {
|
|
|
|
if (getpgid(pid) != pid) {
|
|
|
|
if (setpgid(pid, sid) < 0) {
|
|
|
|
auto strerr = strerror(errno);
|
2020-10-04 13:35:43 +00:00
|
|
|
dbgln("couldn't setpgid: {}", strerr);
|
2020-09-30 10:35:28 +00:00
|
|
|
}
|
|
|
|
if (setsid() < 0) {
|
|
|
|
auto strerr = strerror(errno);
|
2020-10-04 13:35:43 +00:00
|
|
|
dbgln("couldn't setsid: {}", strerr);
|
2020-09-30 10:35:28 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-16 00:48:33 +00:00
|
|
|
}
|
2020-09-18 06:52:25 +00:00
|
|
|
|
2021-07-04 09:08:46 +00:00
|
|
|
auto execute_file = file_to_read_from && "-"sv != file_to_read_from;
|
2021-03-07 06:12:24 +00:00
|
|
|
attempt_interactive = !execute_file;
|
|
|
|
|
2021-07-04 08:59:40 +00:00
|
|
|
if (keep_open && !command_to_run && !execute_file) {
|
|
|
|
warnln("Option --keep-open can only be used in combination with -c or when specifying a file to execute.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-03-07 06:12:24 +00:00
|
|
|
initialize();
|
|
|
|
|
|
|
|
shell->set_live_formatting(should_format_live);
|
2020-09-13 09:29:34 +00:00
|
|
|
shell->current_script = argv[0];
|
|
|
|
|
2020-07-07 09:27:35 +00:00
|
|
|
if (!skip_rc_files) {
|
|
|
|
auto run_rc_file = [&](auto& name) {
|
|
|
|
String file_path = name;
|
|
|
|
if (file_path.starts_with('~'))
|
|
|
|
file_path = shell->expand_tilde(file_path);
|
|
|
|
if (Core::File::exists(file_path)) {
|
|
|
|
shell->run_file(file_path, false);
|
|
|
|
}
|
|
|
|
};
|
2020-10-01 14:43:01 +00:00
|
|
|
run_rc_file(Shell::Shell::global_init_file_path);
|
|
|
|
run_rc_file(Shell::Shell::local_init_file_path);
|
2020-06-17 15:07:44 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 04:57:25 +00:00
|
|
|
{
|
|
|
|
Vector<String> args;
|
|
|
|
for (auto* arg : script_args)
|
|
|
|
args.empend(arg);
|
2021-04-23 14:46:57 +00:00
|
|
|
shell->set_local_variable("ARGV", adopt_ref(*new Shell::AST::ListValue(move(args))));
|
2020-08-04 04:57:25 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 14:43:34 +00:00
|
|
|
if (command_to_run) {
|
2020-12-06 17:21:40 +00:00
|
|
|
dbgln("sh -c '{}'\n", command_to_run);
|
2021-07-04 08:59:40 +00:00
|
|
|
auto result = shell->run_command(command_to_run);
|
|
|
|
if (!keep_open)
|
|
|
|
return result;
|
2019-02-03 03:32:31 +00:00
|
|
|
}
|
|
|
|
|
2021-03-07 06:12:24 +00:00
|
|
|
if (execute_file) {
|
2021-07-04 08:59:40 +00:00
|
|
|
auto result = shell->run_file(file_to_read_from);
|
|
|
|
if (!keep_open) {
|
|
|
|
if (result)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
2019-09-14 19:20:13 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 10:34:39 +00:00
|
|
|
shell->add_child(*editor);
|
|
|
|
|
2020-10-01 14:43:01 +00:00
|
|
|
Core::EventLoop::current().post_event(*shell, make<Core::CustomEvent>(Shell::Shell::ShellEventType::ReadLine));
|
2019-05-06 23:39:10 +00:00
|
|
|
|
2020-05-16 19:05:13 +00:00
|
|
|
return loop.exec();
|
2018-10-23 08:12:50 +00:00
|
|
|
}
|