2020-01-18 08:38:21 +00: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 19:05:13 +00:00
|
|
|
#include "Execution.h"
|
|
|
|
#include "Shell.h"
|
|
|
|
#include <LibCore/Event.h>
|
|
|
|
#include <LibCore/EventLoop.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/File.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>
|
2019-05-30 13:12:09 +00:00
|
|
|
#include <sys/wait.h>
|
2018-10-26 12:24:11 +00:00
|
|
|
|
2020-05-26 10:34:39 +00:00
|
|
|
RefPtr<Line::Editor> editor;
|
2020-05-16 19:05:13 +00:00
|
|
|
Shell* s_shell;
|
2019-04-25 14:10:16 +00:00
|
|
|
|
2020-05-16 19:05:13 +00:00
|
|
|
void FileDescriptionCollector::collect()
|
2020-04-30 03:49:47 +00:00
|
|
|
{
|
2020-05-16 19:05:13 +00:00
|
|
|
for (auto fd : m_fds)
|
|
|
|
close(fd);
|
|
|
|
m_fds.clear();
|
2020-04-30 03:49:47 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 19:05:13 +00:00
|
|
|
FileDescriptionCollector::~FileDescriptionCollector()
|
2020-03-30 17:01:41 +00:00
|
|
|
{
|
2020-05-16 19:05:13 +00:00
|
|
|
collect();
|
2020-03-30 17:01:41 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 19:05:13 +00:00
|
|
|
void FileDescriptionCollector::add(int fd)
|
2020-05-10 08:05:59 +00:00
|
|
|
{
|
2020-05-16 19:05:13 +00:00
|
|
|
m_fds.append(fd);
|
2020-05-10 08:05:59 +00:00
|
|
|
}
|
|
|
|
|
2019-02-03 03:32:31 +00:00
|
|
|
int main(int argc, char** argv)
|
2018-10-23 08:12:50 +00:00
|
|
|
{
|
2020-05-16 19:05:13 +00:00
|
|
|
Core::EventLoop loop;
|
2020-05-26 14:15:19 +00:00
|
|
|
|
2019-07-08 17:04:13 +00:00
|
|
|
signal(SIGINT, [](int) {
|
2020-05-26 10:34:39 +00:00
|
|
|
editor->interrupted();
|
2019-07-08 17:04:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
signal(SIGWINCH, [](int) {
|
2020-05-26 10:34:39 +00:00
|
|
|
editor->resized();
|
2020-03-30 17:25:09 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
signal(SIGHUP, [](int) {
|
2020-05-16 19:05:13 +00:00
|
|
|
s_shell->save_history();
|
2019-07-08 17:04:13 +00:00
|
|
|
});
|
2019-06-19 09:08:51 +00:00
|
|
|
|
2020-05-15 22:33:50 +00:00
|
|
|
signal(SIGCHLD, [](int) {
|
2020-05-16 19:05:13 +00:00
|
|
|
auto& jobs = s_shell->jobs;
|
2020-06-17 13:35:06 +00:00
|
|
|
Vector<u64> disowned_jobs;
|
2020-05-16 19:05:13 +00:00
|
|
|
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 13:35:06 +00:00
|
|
|
if (WIFEXITED(wstatus)) {
|
|
|
|
job.value->set_has_exit(WEXITSTATUS(wstatus));
|
|
|
|
} else if (WIFSIGNALED(wstatus) && !WIFSTOPPED(wstatus)) {
|
|
|
|
job.value->set_has_exit(126);
|
2020-05-15 22:33:50 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-17 13:35:06 +00:00
|
|
|
if (job.value->should_be_disowned())
|
|
|
|
disowned_jobs.append(job.key);
|
2020-05-15 22:33:50 +00:00
|
|
|
}
|
2020-06-17 13:35:06 +00:00
|
|
|
for (auto key : disowned_jobs)
|
|
|
|
jobs.remove(key);
|
2020-05-15 22:33:50 +00:00
|
|
|
});
|
|
|
|
|
2020-05-25 19:13:43 +00:00
|
|
|
// Ignore SIGTSTP as the shell should not be suspended with ^Z.
|
|
|
|
signal(SIGTSTP, [](auto) {});
|
|
|
|
|
2020-05-26 10:52:42 +00:00
|
|
|
if (pledge("stdio rpath wpath cpath proc exec tty accept", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-05-26 10:34:39 +00:00
|
|
|
editor = Line::Editor::construct(Line::Configuration { Line::Configuration::UnescapedSpaces });
|
|
|
|
|
2020-05-26 10:52:42 +00:00
|
|
|
auto shell = Shell::construct();
|
|
|
|
s_shell = shell.ptr();
|
|
|
|
|
2020-05-26 10:34:39 +00:00
|
|
|
editor->initialize();
|
|
|
|
shell->termios = editor->termios();
|
|
|
|
shell->default_termios = editor->default_termios();
|
2020-05-26 10:52:42 +00:00
|
|
|
|
2020-05-26 10:34:39 +00:00
|
|
|
editor->on_display_refresh = [&](auto& editor) {
|
2020-05-26 10:52:42 +00:00
|
|
|
editor.strip_styles();
|
|
|
|
shell->highlight(editor);
|
|
|
|
};
|
2020-05-26 10:34:39 +00:00
|
|
|
editor->on_tab_complete = [&](const Line::Editor& editor) {
|
2020-05-26 10:52:42 +00:00
|
|
|
return shell->complete(editor);
|
|
|
|
};
|
|
|
|
|
2019-05-13 02:52:55 +00:00
|
|
|
if (argc > 2 && !strcmp(argv[1], "-c")) {
|
|
|
|
dbgprintf("sh -c '%s'\n", argv[2]);
|
2020-05-16 19:05:13 +00:00
|
|
|
shell->run_command(argv[2]);
|
2019-05-13 02:52:55 +00:00
|
|
|
return 0;
|
2019-02-03 03:32:31 +00:00
|
|
|
}
|
|
|
|
|
2019-09-14 19:20:13 +00:00
|
|
|
if (argc == 2 && argv[1][0] != '-') {
|
2020-02-02 11:34:39 +00:00
|
|
|
auto file = Core::File::construct(argv[1]);
|
|
|
|
if (!file->open(Core::IODevice::ReadOnly)) {
|
2019-09-21 18:50:06 +00:00
|
|
|
fprintf(stderr, "Failed to open %s: %s\n", file->filename().characters(), file->error_string());
|
2019-09-14 19:20:13 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
for (;;) {
|
2019-09-21 18:50:06 +00:00
|
|
|
auto line = file->read_line(4096);
|
2019-09-14 19:20:13 +00:00
|
|
|
if (line.is_null())
|
|
|
|
break;
|
2020-05-16 19:05:13 +00:00
|
|
|
shell->run_command(String::copy(line, Chomp));
|
2019-09-14 19:20:13 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-26 10:34:39 +00:00
|
|
|
editor->on_interrupt_handled = [&] {
|
2020-06-17 13:35:06 +00:00
|
|
|
editor->finish();
|
2020-05-13 09:54:11 +00:00
|
|
|
};
|
|
|
|
|
2020-05-26 10:34:39 +00:00
|
|
|
shell->add_child(*editor);
|
|
|
|
|
2020-05-16 19:05:13 +00:00
|
|
|
Core::EventLoop::current().post_event(*shell, make<Core::CustomEvent>(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
|
|
|
}
|