main.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/Event.h>
  29. #include <LibCore/EventLoop.h>
  30. #include <LibCore/File.h>
  31. #include <signal.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <sys/wait.h>
  36. Line::Editor editor { Line::Configuration { Line::Configuration::UnescapedSpaces } };
  37. Shell* s_shell;
  38. void FileDescriptionCollector::collect()
  39. {
  40. for (auto fd : m_fds)
  41. close(fd);
  42. m_fds.clear();
  43. }
  44. FileDescriptionCollector::~FileDescriptionCollector()
  45. {
  46. collect();
  47. }
  48. void FileDescriptionCollector::add(int fd)
  49. {
  50. m_fds.append(fd);
  51. }
  52. int main(int argc, char** argv)
  53. {
  54. Core::EventLoop loop;
  55. if (pledge("stdio rpath wpath cpath proc exec tty accept", nullptr) < 0) {
  56. perror("pledge");
  57. return 1;
  58. }
  59. auto shell = Shell::construct();
  60. s_shell = shell.ptr();
  61. editor.initialize();
  62. shell->termios = editor.termios();
  63. shell->default_termios = editor.default_termios();
  64. editor.on_display_refresh = [&](auto& editor) {
  65. editor.strip_styles();
  66. shell->highlight(editor);
  67. };
  68. editor.on_tab_complete = [&](const Line::Editor& editor) {
  69. return shell->complete(editor);
  70. };
  71. signal(SIGINT, [](int) {
  72. editor.interrupted();
  73. });
  74. signal(SIGWINCH, [](int) {
  75. editor.resized();
  76. });
  77. signal(SIGHUP, [](int) {
  78. s_shell->save_history();
  79. });
  80. signal(SIGCHLD, [](int) {
  81. auto& jobs = s_shell->jobs;
  82. for (auto& job : jobs) {
  83. int wstatus = 0;
  84. auto child_pid = waitpid(job.value->pid(), &wstatus, WNOHANG);
  85. if (child_pid == job.value->pid()) {
  86. if (WIFEXITED(wstatus) || WIFSIGNALED(wstatus)) {
  87. Core::EventLoop::current().post_event(*s_shell, make<Core::CustomEvent>(Shell::ShellEventType::ChildExited, job.value));
  88. }
  89. }
  90. }
  91. });
  92. if (argc > 2 && !strcmp(argv[1], "-c")) {
  93. dbgprintf("sh -c '%s'\n", argv[2]);
  94. shell->run_command(argv[2]);
  95. return 0;
  96. }
  97. if (argc == 2 && argv[1][0] != '-') {
  98. auto file = Core::File::construct(argv[1]);
  99. if (!file->open(Core::IODevice::ReadOnly)) {
  100. fprintf(stderr, "Failed to open %s: %s\n", file->filename().characters(), file->error_string());
  101. return 1;
  102. }
  103. for (;;) {
  104. auto line = file->read_line(4096);
  105. if (line.is_null())
  106. break;
  107. shell->run_command(String::copy(line, Chomp));
  108. }
  109. return 0;
  110. }
  111. editor.on_interrupt_handled = [&] {
  112. if (!shell->should_read_more()) {
  113. shell->finish_command();
  114. editor.finish();
  115. }
  116. };
  117. Core::EventLoop::current().post_event(*shell, make<Core::CustomEvent>(Shell::ShellEventType::ReadLine));
  118. return loop.exec();
  119. }