2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-05-16 23:35:13 +04:30
|
|
|
#include "Execution.h"
|
|
|
|
#include "Shell.h"
|
2020-06-17 19:13:34 +04:30
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-05-16 23:35:13 +04:30
|
|
|
#include <LibCore/Event.h>
|
|
|
|
#include <LibCore/EventLoop.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/File.h>
|
2018-11-06 10:46:40 +01:00
|
|
|
#include <signal.h>
|
2018-11-09 10:24:41 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2019-05-30 15:12:09 +02:00
|
|
|
#include <sys/wait.h>
|
2018-10-26 14:24:11 +02:00
|
|
|
|
2020-05-26 15:04:39 +04:30
|
|
|
RefPtr<Line::Editor> editor;
|
2020-05-16 23:35:13 +04:30
|
|
|
Shell* s_shell;
|
2019-04-25 16:10:16 +02:00
|
|
|
|
2020-05-16 23:35:13 +04:30
|
|
|
void FileDescriptionCollector::collect()
|
2020-04-30 08:19:47 +04:30
|
|
|
{
|
2020-05-16 23:35:13 +04:30
|
|
|
for (auto fd : m_fds)
|
|
|
|
close(fd);
|
|
|
|
m_fds.clear();
|
2020-04-30 08:19:47 +04:30
|
|
|
}
|
|
|
|
|
2020-05-16 23:35:13 +04:30
|
|
|
FileDescriptionCollector::~FileDescriptionCollector()
|
2020-03-30 21:31:41 +04:30
|
|
|
{
|
2020-05-16 23:35:13 +04:30
|
|
|
collect();
|
2020-03-30 21:31:41 +04:30
|
|
|
}
|
|
|
|
|
2020-05-16 23:35:13 +04:30
|
|
|
void FileDescriptionCollector::add(int fd)
|
2020-05-10 12:35:59 +04:30
|
|
|
{
|
2020-05-16 23:35:13 +04:30
|
|
|
m_fds.append(fd);
|
2020-05-10 12:35:59 +04:30
|
|
|
}
|
|
|
|
|
2019-02-03 04:32:31 +01:00
|
|
|
int main(int argc, char** argv)
|
2018-10-23 10:12:50 +02:00
|
|
|
{
|
2020-05-16 23:35:13 +04:30
|
|
|
Core::EventLoop loop;
|
2020-05-26 18:45:19 +04:30
|
|
|
|
2019-07-08 19:04:13 +02:00
|
|
|
signal(SIGINT, [](int) {
|
2020-05-26 15:04:39 +04:30
|
|
|
editor->interrupted();
|
2019-07-08 19:04:13 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
signal(SIGWINCH, [](int) {
|
2020-05-26 15:04:39 +04:30
|
|
|
editor->resized();
|
2020-03-30 21:55:09 +04:30
|
|
|
});
|
|
|
|
|
|
|
|
signal(SIGHUP, [](int) {
|
2020-05-16 23:35:13 +04:30
|
|
|
s_shell->save_history();
|
2019-07-08 19:04:13 +02:00
|
|
|
});
|
2019-06-19 10:08:51 +01:00
|
|
|
|
2020-05-16 03:03:50 +04:30
|
|
|
signal(SIGCHLD, [](int) {
|
2020-05-16 23:35:13 +04:30
|
|
|
auto& jobs = s_shell->jobs;
|
2020-06-17 18:05:06 +04:30
|
|
|
Vector<u64> disowned_jobs;
|
2020-05-16 23:35:13 +04:30
|
|
|
for (auto& job : jobs) {
|
|
|
|
int wstatus = 0;
|
|
|
|
auto child_pid = waitpid(job.value->pid(), &wstatus, WNOHANG);
|
|
|
|
if (child_pid == job.value->pid()) {
|
2020-06-17 18:05:06 +04:30
|
|
|
if (WIFEXITED(wstatus)) {
|
|
|
|
job.value->set_has_exit(WEXITSTATUS(wstatus));
|
|
|
|
} else if (WIFSIGNALED(wstatus) && !WIFSTOPPED(wstatus)) {
|
|
|
|
job.value->set_has_exit(126);
|
2020-05-16 03:03:50 +04:30
|
|
|
}
|
|
|
|
}
|
2020-06-17 18:05:06 +04:30
|
|
|
if (job.value->should_be_disowned())
|
|
|
|
disowned_jobs.append(job.key);
|
2020-05-16 03:03:50 +04:30
|
|
|
}
|
2020-06-17 18:05:06 +04:30
|
|
|
for (auto key : disowned_jobs)
|
|
|
|
jobs.remove(key);
|
2020-05-16 03:03:50 +04:30
|
|
|
});
|
|
|
|
|
2020-05-25 23:43:43 +04:30
|
|
|
// Ignore SIGTSTP as the shell should not be suspended with ^Z.
|
|
|
|
signal(SIGTSTP, [](auto) {});
|
|
|
|
|
2020-05-26 13:52:42 +03:00
|
|
|
if (pledge("stdio rpath wpath cpath proc exec tty accept", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-05-26 15:04:39 +04:30
|
|
|
editor = Line::Editor::construct(Line::Configuration { Line::Configuration::UnescapedSpaces });
|
|
|
|
|
2020-05-26 13:52:42 +03:00
|
|
|
auto shell = Shell::construct();
|
|
|
|
s_shell = shell.ptr();
|
|
|
|
|
2020-05-26 15:04:39 +04:30
|
|
|
editor->initialize();
|
|
|
|
shell->termios = editor->termios();
|
|
|
|
shell->default_termios = editor->default_termios();
|
2020-05-26 13:52:42 +03:00
|
|
|
|
2020-05-26 15:04:39 +04:30
|
|
|
editor->on_display_refresh = [&](auto& editor) {
|
2020-05-26 13:52:42 +03:00
|
|
|
editor.strip_styles();
|
|
|
|
shell->highlight(editor);
|
|
|
|
};
|
2020-05-26 15:04:39 +04:30
|
|
|
editor->on_tab_complete = [&](const Line::Editor& editor) {
|
2020-05-26 13:52:42 +03:00
|
|
|
return shell->complete(editor);
|
|
|
|
};
|
|
|
|
|
2020-06-17 19:13:34 +04:30
|
|
|
const char* command_to_run = nullptr;
|
|
|
|
const char* file_to_read_from = nullptr;
|
2020-06-17 19:37:44 +04:30
|
|
|
bool skip_init_file = false;
|
2020-06-17 19:13:34 +04:30
|
|
|
|
|
|
|
Core::ArgsParser parser;
|
|
|
|
parser.add_option(command_to_run, "String to read commands from", "command-string", 'c', "command-string");
|
|
|
|
parser.add_positional_argument(file_to_read_from, "File to read commands from", "file", Core::ArgsParser::Required::No);
|
2020-06-17 19:37:44 +04:30
|
|
|
parser.add_option(skip_init_file, "Skip running ~/shell-init.sh", "skip-init", 0);
|
2020-06-17 19:13:34 +04:30
|
|
|
|
|
|
|
parser.parse(argc, argv);
|
|
|
|
|
2020-06-17 19:37:44 +04:30
|
|
|
if (!skip_init_file) {
|
|
|
|
String file_path = Shell::init_file_path;
|
|
|
|
if (file_path.starts_with('~'))
|
|
|
|
file_path = shell->expand_tilde(file_path);
|
|
|
|
if (!shell->run_file(file_path)) {
|
|
|
|
fprintf(stderr, "Shell: Failed to execute init file '%s'\n", Shell::init_file_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 19:13:34 +04:30
|
|
|
if (command_to_run) {
|
|
|
|
dbgprintf("sh -c '%s'\n", command_to_run);
|
|
|
|
shell->run_command(command_to_run);
|
2019-05-13 04:52:55 +02:00
|
|
|
return 0;
|
2019-02-03 04:32:31 +01:00
|
|
|
}
|
|
|
|
|
2020-06-17 19:13:34 +04:30
|
|
|
if (file_to_read_from && StringView { "-" } != file_to_read_from) {
|
2020-06-17 19:37:44 +04:30
|
|
|
if (shell->run_file(file_to_read_from))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
2019-09-14 21:20:13 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 15:04:39 +04:30
|
|
|
editor->on_interrupt_handled = [&] {
|
2020-06-17 18:05:06 +04:30
|
|
|
editor->finish();
|
2020-05-13 14:24:11 +04:30
|
|
|
};
|
|
|
|
|
2020-05-26 15:04:39 +04:30
|
|
|
shell->add_child(*editor);
|
|
|
|
|
2020-05-16 23:35:13 +04:30
|
|
|
Core::EventLoop::current().post_event(*shell, make<Core::CustomEvent>(Shell::ShellEventType::ReadLine));
|
2019-05-07 01:39:10 +02:00
|
|
|
|
2020-05-16 23:35:13 +04:30
|
|
|
return loop.exec();
|
2018-10-23 10:12:50 +02:00
|
|
|
}
|