main.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Execution.h"
  27. #include "Shell.h"
  28. #include <LibCore/ArgsParser.h>
  29. #include <LibCore/Event.h>
  30. #include <LibCore/EventLoop.h>
  31. #include <LibCore/File.h>
  32. #include <signal.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <sys/wait.h>
  37. RefPtr<Line::Editor> editor;
  38. Shell* s_shell;
  39. void FileDescriptionCollector::collect()
  40. {
  41. for (auto fd : m_fds)
  42. close(fd);
  43. m_fds.clear();
  44. }
  45. FileDescriptionCollector::~FileDescriptionCollector()
  46. {
  47. collect();
  48. }
  49. void FileDescriptionCollector::add(int fd)
  50. {
  51. m_fds.append(fd);
  52. }
  53. int main(int argc, char** argv)
  54. {
  55. Core::EventLoop loop;
  56. Core::EventLoop::register_signal(SIGINT, [](int) {
  57. editor->interrupted();
  58. s_shell->kill_job(s_shell->current_job(), SIGINT);
  59. });
  60. Core::EventLoop::register_signal(SIGWINCH, [](int) {
  61. editor->resized();
  62. s_shell->kill_job(s_shell->current_job(), SIGWINCH);
  63. });
  64. Core::EventLoop::register_signal(SIGTTIN, [](int) {});
  65. Core::EventLoop::register_signal(SIGTTOU, [](int) {});
  66. Core::EventLoop::register_signal(SIGHUP, [](int) {
  67. for (auto& it : s_shell->jobs)
  68. s_shell->kill_job(it.value.ptr(), SIGHUP);
  69. s_shell->save_history();
  70. });
  71. Core::EventLoop::register_signal(SIGCHLD, [](int) {
  72. auto& jobs = s_shell->jobs;
  73. Vector<u64> disowned_jobs;
  74. for (auto& job : jobs) {
  75. int wstatus = 0;
  76. auto child_pid = waitpid(job.value->pid(), &wstatus, WNOHANG);
  77. if (child_pid < 0) {
  78. if (errno == ECHILD) {
  79. // The child process went away before we could process its death, just assume it exited all ok.
  80. // FIXME: This should never happen, the child should stay around until we do the waitpid above.
  81. dbg() << "Child process gone, cannot get exit code for " << job.key;
  82. child_pid = job.value->pid();
  83. } else {
  84. ASSERT_NOT_REACHED();
  85. }
  86. }
  87. #ifndef __serenity__
  88. if (child_pid == 0) {
  89. // Linux: if child didn't "change state", but existed.
  90. continue;
  91. }
  92. #endif
  93. if (child_pid == job.value->pid()) {
  94. if (WIFEXITED(wstatus)) {
  95. job.value->set_has_exit(WEXITSTATUS(wstatus));
  96. } else if (WIFSIGNALED(wstatus) && !WIFSTOPPED(wstatus)) {
  97. job.value->set_has_exit(126);
  98. } else if (WIFSTOPPED(wstatus)) {
  99. job.value->unblock();
  100. }
  101. }
  102. if (job.value->should_be_disowned())
  103. disowned_jobs.append(job.key);
  104. }
  105. for (auto key : disowned_jobs)
  106. jobs.remove(key);
  107. });
  108. Core::EventLoop::register_signal(SIGTSTP, [](auto) {
  109. auto job = s_shell->current_job();
  110. s_shell->kill_job(job, SIGTSTP);
  111. if (job) {
  112. job->set_is_suspended(true);
  113. job->unblock();
  114. }
  115. });
  116. #ifndef __serenity__
  117. sigset_t blocked;
  118. sigemptyset(&blocked);
  119. sigaddset(&blocked, SIGTTOU);
  120. pthread_sigmask(SIG_BLOCK, &blocked, NULL);
  121. #endif
  122. #ifdef __serenity__
  123. if (pledge("stdio rpath wpath cpath proc exec tty accept sigaction", nullptr) < 0) {
  124. perror("pledge");
  125. return 1;
  126. }
  127. #endif
  128. editor = Line::Editor::construct(Line::Configuration { Line::Configuration::UnescapedSpaces });
  129. auto shell = Shell::construct();
  130. s_shell = shell.ptr();
  131. editor->initialize();
  132. shell->termios = editor->termios();
  133. shell->default_termios = editor->default_termios();
  134. editor->on_display_refresh = [&](auto& editor) {
  135. editor.strip_styles();
  136. shell->highlight(editor);
  137. };
  138. editor->on_tab_complete = [&](const Line::Editor& editor) {
  139. return shell->complete(editor);
  140. };
  141. const char* command_to_run = nullptr;
  142. const char* file_to_read_from = nullptr;
  143. Vector<const char*> script_args;
  144. bool skip_rc_files = false;
  145. Core::ArgsParser parser;
  146. parser.add_option(command_to_run, "String to read commands from", "command-string", 'c', "command-string");
  147. parser.add_option(skip_rc_files, "Skip running shellrc files", "skip-shellrc", 0);
  148. parser.add_positional_argument(file_to_read_from, "File to read commands from", "file", Core::ArgsParser::Required::No);
  149. parser.add_positional_argument(script_args, "Extra argumets to pass to the script (via $* and co)", "argument", Core::ArgsParser::Required::No);
  150. parser.parse(argc, argv);
  151. if (!skip_rc_files) {
  152. auto run_rc_file = [&](auto& name) {
  153. String file_path = name;
  154. if (file_path.starts_with('~'))
  155. file_path = shell->expand_tilde(file_path);
  156. if (Core::File::exists(file_path)) {
  157. shell->run_file(file_path, false);
  158. }
  159. };
  160. run_rc_file(Shell::global_init_file_path);
  161. run_rc_file(Shell::local_init_file_path);
  162. }
  163. {
  164. Vector<String> args;
  165. for (auto* arg : script_args)
  166. args.empend(arg);
  167. shell->set_local_variable("ARGV", *new AST::ListValue(move(args)));
  168. }
  169. if (command_to_run) {
  170. dbgprintf("sh -c '%s'\n", command_to_run);
  171. shell->run_command(command_to_run);
  172. return 0;
  173. }
  174. if (file_to_read_from && StringView { "-" } != file_to_read_from) {
  175. if (shell->run_file(file_to_read_from))
  176. return 0;
  177. return 1;
  178. }
  179. editor->on_interrupt_handled = [&] {
  180. editor->finish();
  181. };
  182. shell->add_child(*editor);
  183. Core::EventLoop::current().post_event(*shell, make<Core::CustomEvent>(Shell::ShellEventType::ReadLine));
  184. return loop.exec();
  185. }