main.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. RefPtr<Line::Editor> editor;
  37. Shell::Shell* s_shell;
  38. int main(int argc, char** argv)
  39. {
  40. Core::EventLoop loop;
  41. Core::EventLoop::register_signal(SIGINT, [](int) {
  42. s_shell->kill_job(s_shell->current_job(), SIGINT);
  43. });
  44. Core::EventLoop::register_signal(SIGWINCH, [](int) {
  45. s_shell->kill_job(s_shell->current_job(), SIGWINCH);
  46. });
  47. Core::EventLoop::register_signal(SIGTTIN, [](int) {});
  48. Core::EventLoop::register_signal(SIGTTOU, [](int) {});
  49. Core::EventLoop::register_signal(SIGHUP, [](int) {
  50. for (auto& it : s_shell->jobs)
  51. s_shell->kill_job(it.value.ptr(), SIGHUP);
  52. s_shell->editor()->save_history(s_shell->get_history_path());
  53. });
  54. #ifdef __serenity__
  55. if (pledge("stdio rpath wpath cpath proc exec tty accept sigaction unix fattr", nullptr) < 0) {
  56. perror("pledge");
  57. return 1;
  58. }
  59. #endif
  60. RefPtr<::Shell::Shell> shell;
  61. bool attempt_interactive = false;
  62. auto initialize = [&] {
  63. editor = Line::Editor::construct();
  64. editor->initialize();
  65. shell = Shell::Shell::construct(*editor, attempt_interactive);
  66. s_shell = shell.ptr();
  67. s_shell->setup_signals();
  68. #ifndef __serenity__
  69. sigset_t blocked;
  70. sigemptyset(&blocked);
  71. sigaddset(&blocked, SIGTTOU);
  72. sigaddset(&blocked, SIGTTIN);
  73. pthread_sigmask(SIG_BLOCK, &blocked, nullptr);
  74. #endif
  75. shell->termios = editor->termios();
  76. shell->default_termios = editor->default_termios();
  77. editor->on_display_refresh = [&](auto& editor) {
  78. editor.strip_styles();
  79. if (shell->should_format_live()) {
  80. auto line = editor.line();
  81. ssize_t cursor = editor.cursor();
  82. editor.clear_line();
  83. editor.insert(shell->format(line, cursor));
  84. if (cursor >= 0)
  85. editor.set_cursor(cursor);
  86. }
  87. shell->highlight(editor);
  88. };
  89. editor->on_tab_complete = [&](const Line::Editor&) {
  90. return shell->complete();
  91. };
  92. };
  93. const char* command_to_run = nullptr;
  94. const char* file_to_read_from = nullptr;
  95. Vector<const char*> script_args;
  96. bool skip_rc_files = false;
  97. const char* format = nullptr;
  98. bool should_format_live = false;
  99. Core::ArgsParser parser;
  100. parser.add_option(command_to_run, "String to read commands from", "command-string", 'c', "command-string");
  101. parser.add_option(skip_rc_files, "Skip running shellrc files", "skip-shellrc", 0);
  102. parser.add_option(format, "Format the given file into stdout and exit", "format", 0, "file");
  103. parser.add_option(should_format_live, "Enable live formatting", "live-formatting", 'f');
  104. parser.add_positional_argument(file_to_read_from, "File to read commands from", "file", Core::ArgsParser::Required::No);
  105. parser.add_positional_argument(script_args, "Extra arguments to pass to the script (via $* and co)", "argument", Core::ArgsParser::Required::No);
  106. parser.parse(argc, argv);
  107. if (format) {
  108. auto file = Core::File::open(format, Core::IODevice::ReadOnly);
  109. if (file.is_error()) {
  110. fprintf(stderr, "Error: %s", file.error().characters());
  111. return 1;
  112. }
  113. initialize();
  114. ssize_t cursor = -1;
  115. puts(shell->format(file.value()->read_all(), cursor).characters());
  116. return 0;
  117. }
  118. auto pid = getpid();
  119. if (auto sid = getsid(pid); sid == 0) {
  120. if (setsid() < 0) {
  121. perror("setsid");
  122. // Let's just hope that it's ok.
  123. }
  124. } else if (sid != pid) {
  125. if (getpgid(pid) != pid) {
  126. dbgln("We were already in a session with sid={} (we are {}), let's do some gymnastics", sid, pid);
  127. if (setpgid(pid, sid) < 0) {
  128. auto strerr = strerror(errno);
  129. dbgln("couldn't setpgid: {}", strerr);
  130. }
  131. if (setsid() < 0) {
  132. auto strerr = strerror(errno);
  133. dbgln("couldn't setsid: {}", strerr);
  134. }
  135. }
  136. }
  137. auto execute_file = file_to_read_from && StringView { "-" } != file_to_read_from;
  138. attempt_interactive = !execute_file;
  139. initialize();
  140. shell->set_live_formatting(should_format_live);
  141. shell->current_script = argv[0];
  142. if (!skip_rc_files) {
  143. auto run_rc_file = [&](auto& name) {
  144. String file_path = name;
  145. if (file_path.starts_with('~'))
  146. file_path = shell->expand_tilde(file_path);
  147. if (Core::File::exists(file_path)) {
  148. shell->run_file(file_path, false);
  149. }
  150. };
  151. run_rc_file(Shell::Shell::global_init_file_path);
  152. run_rc_file(Shell::Shell::local_init_file_path);
  153. }
  154. {
  155. Vector<String> args;
  156. for (auto* arg : script_args)
  157. args.empend(arg);
  158. shell->set_local_variable("ARGV", adopt(*new Shell::AST::ListValue(move(args))));
  159. }
  160. if (command_to_run) {
  161. dbgln("sh -c '{}'\n", command_to_run);
  162. return shell->run_command(command_to_run);
  163. }
  164. if (execute_file) {
  165. if (shell->run_file(file_to_read_from))
  166. return 0;
  167. return 1;
  168. }
  169. shell->add_child(*editor);
  170. Core::EventLoop::current().post_event(*shell, make<Core::CustomEvent>(Shell::Shell::ShellEventType::ReadLine));
  171. return loop.exec();
  172. }