main.cpp 6.5 KB

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