123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- /*
- * 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.
- */
- #include "Execution.h"
- #include "Shell.h"
- #include <LibCore/ArgsParser.h>
- #include <LibCore/Event.h>
- #include <LibCore/EventLoop.h>
- #include <LibCore/File.h>
- #include <signal.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/wait.h>
- RefPtr<Line::Editor> editor;
- Shell* s_shell;
- void FileDescriptionCollector::collect()
- {
- for (auto fd : m_fds)
- close(fd);
- m_fds.clear();
- }
- FileDescriptionCollector::~FileDescriptionCollector()
- {
- collect();
- }
- void FileDescriptionCollector::add(int fd)
- {
- m_fds.append(fd);
- }
- int main(int argc, char** argv)
- {
- Core::EventLoop loop;
- signal(SIGINT, [](int) {
- editor->interrupted();
- });
- signal(SIGWINCH, [](int) {
- editor->resized();
- });
- signal(SIGHUP, [](int) {
- s_shell->save_history();
- });
- signal(SIGCHLD, [](int) {
- auto& jobs = s_shell->jobs;
- Vector<u64> disowned_jobs;
- for (auto& job : jobs) {
- int wstatus = 0;
- auto child_pid = waitpid(job.value->pid(), &wstatus, WNOHANG);
- if (child_pid == job.value->pid()) {
- if (WIFEXITED(wstatus)) {
- job.value->set_has_exit(WEXITSTATUS(wstatus));
- } else if (WIFSIGNALED(wstatus) && !WIFSTOPPED(wstatus)) {
- job.value->set_has_exit(126);
- }
- }
- if (job.value->should_be_disowned())
- disowned_jobs.append(job.key);
- }
- for (auto key : disowned_jobs)
- jobs.remove(key);
- });
- // Ignore SIGTSTP as the shell should not be suspended with ^Z.
- signal(SIGTSTP, [](auto) {});
- if (pledge("stdio rpath wpath cpath proc exec tty accept", nullptr) < 0) {
- perror("pledge");
- return 1;
- }
- editor = Line::Editor::construct(Line::Configuration { Line::Configuration::UnescapedSpaces });
- auto shell = Shell::construct();
- s_shell = shell.ptr();
- editor->initialize();
- shell->termios = editor->termios();
- shell->default_termios = editor->default_termios();
- editor->on_display_refresh = [&](auto& editor) {
- editor.strip_styles();
- shell->highlight(editor);
- };
- editor->on_tab_complete = [&](const Line::Editor& editor) {
- return shell->complete(editor);
- };
- const char* command_to_run = nullptr;
- const char* file_to_read_from = nullptr;
- 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);
- parser.parse(argc, argv);
- if (command_to_run) {
- dbgprintf("sh -c '%s'\n", command_to_run);
- shell->run_command(command_to_run);
- return 0;
- }
- if (file_to_read_from && StringView { "-" } != file_to_read_from) {
- auto file = Core::File::construct(file_to_read_from);
- if (!file->open(Core::IODevice::ReadOnly)) {
- fprintf(stderr, "Failed to open %s: %s\n", file->filename().characters(), file->error_string());
- return 1;
- }
- for (;;) {
- auto line = file->read_line(4096);
- if (line.is_null())
- break;
- shell->run_command(String::copy(line, Chomp));
- }
- return 0;
- }
- editor->on_interrupt_handled = [&] {
- editor->finish();
- };
- shell->add_child(*editor);
- Core::EventLoop::current().post_event(*shell, make<Core::CustomEvent>(Shell::ShellEventType::ReadLine));
- return loop.exec();
- }
|