main.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. signal(SIGINT, [](int) {
  57. editor->interrupted();
  58. });
  59. signal(SIGWINCH, [](int) {
  60. editor->resized();
  61. });
  62. signal(SIGHUP, [](int) {
  63. s_shell->save_history();
  64. });
  65. signal(SIGCHLD, [](int) {
  66. auto& jobs = s_shell->jobs;
  67. Vector<u64> disowned_jobs;
  68. for (auto& job : jobs) {
  69. int wstatus = 0;
  70. auto child_pid = waitpid(job.value->pid(), &wstatus, WNOHANG);
  71. if (child_pid == job.value->pid()) {
  72. if (WIFEXITED(wstatus)) {
  73. job.value->set_has_exit(WEXITSTATUS(wstatus));
  74. } else if (WIFSIGNALED(wstatus) && !WIFSTOPPED(wstatus)) {
  75. job.value->set_has_exit(126);
  76. }
  77. }
  78. if (job.value->should_be_disowned())
  79. disowned_jobs.append(job.key);
  80. }
  81. for (auto key : disowned_jobs)
  82. jobs.remove(key);
  83. });
  84. // Ignore SIGTSTP as the shell should not be suspended with ^Z.
  85. signal(SIGTSTP, [](auto) {});
  86. if (pledge("stdio rpath wpath cpath proc exec tty accept", nullptr) < 0) {
  87. perror("pledge");
  88. return 1;
  89. }
  90. editor = Line::Editor::construct(Line::Configuration { Line::Configuration::UnescapedSpaces });
  91. auto shell = Shell::construct();
  92. s_shell = shell.ptr();
  93. editor->initialize();
  94. shell->termios = editor->termios();
  95. shell->default_termios = editor->default_termios();
  96. editor->on_display_refresh = [&](auto& editor) {
  97. editor.strip_styles();
  98. shell->highlight(editor);
  99. };
  100. editor->on_tab_complete = [&](const Line::Editor& editor) {
  101. return shell->complete(editor);
  102. };
  103. const char* command_to_run = nullptr;
  104. const char* file_to_read_from = nullptr;
  105. Core::ArgsParser parser;
  106. parser.add_option(command_to_run, "String to read commands from", "command-string", 'c', "command-string");
  107. parser.add_positional_argument(file_to_read_from, "File to read commands from", "file", Core::ArgsParser::Required::No);
  108. parser.parse(argc, argv);
  109. if (command_to_run) {
  110. dbgprintf("sh -c '%s'\n", command_to_run);
  111. shell->run_command(command_to_run);
  112. return 0;
  113. }
  114. if (file_to_read_from && StringView { "-" } != file_to_read_from) {
  115. auto file = Core::File::construct(file_to_read_from);
  116. if (!file->open(Core::IODevice::ReadOnly)) {
  117. fprintf(stderr, "Failed to open %s: %s\n", file->filename().characters(), file->error_string());
  118. return 1;
  119. }
  120. for (;;) {
  121. auto line = file->read_line(4096);
  122. if (line.is_null())
  123. break;
  124. shell->run_command(String::copy(line, Chomp));
  125. }
  126. return 0;
  127. }
  128. editor->on_interrupt_handled = [&] {
  129. editor->finish();
  130. };
  131. shell->add_child(*editor);
  132. Core::EventLoop::current().post_event(*shell, make<Core::CustomEvent>(Shell::ShellEventType::ReadLine));
  133. return loop.exec();
  134. }