main.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Shell.h"
  7. #include <LibCore/ArgsParser.h>
  8. #include <LibCore/Event.h>
  9. #include <LibCore/EventLoop.h>
  10. #include <LibCore/File.h>
  11. #include <errno.h>
  12. #include <signal.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. RefPtr<Line::Editor> editor;
  18. Shell::Shell* s_shell;
  19. int main(int argc, char** argv)
  20. {
  21. Core::EventLoop loop;
  22. Core::EventLoop::register_signal(SIGINT, [](int) {
  23. s_shell->kill_job(s_shell->current_job(), SIGINT);
  24. });
  25. Core::EventLoop::register_signal(SIGWINCH, [](int) {
  26. s_shell->kill_job(s_shell->current_job(), SIGWINCH);
  27. });
  28. Core::EventLoop::register_signal(SIGTTIN, [](int) {});
  29. Core::EventLoop::register_signal(SIGTTOU, [](int) {});
  30. Core::EventLoop::register_signal(SIGHUP, [](int) {
  31. for (auto& it : s_shell->jobs)
  32. s_shell->kill_job(it.value.ptr(), SIGHUP);
  33. s_shell->editor()->save_history(s_shell->get_history_path());
  34. });
  35. #ifdef __serenity__
  36. if (pledge("stdio rpath wpath cpath proc exec tty sigaction unix fattr", nullptr) < 0) {
  37. perror("pledge");
  38. return 1;
  39. }
  40. #endif
  41. RefPtr<::Shell::Shell> shell;
  42. bool attempt_interactive = false;
  43. auto initialize = [&] {
  44. editor = Line::Editor::construct();
  45. editor->initialize();
  46. shell = Shell::Shell::construct(*editor, attempt_interactive);
  47. s_shell = shell.ptr();
  48. s_shell->setup_signals();
  49. #ifndef __serenity__
  50. sigset_t blocked;
  51. sigemptyset(&blocked);
  52. sigaddset(&blocked, SIGTTOU);
  53. sigaddset(&blocked, SIGTTIN);
  54. pthread_sigmask(SIG_BLOCK, &blocked, nullptr);
  55. #endif
  56. shell->termios = editor->termios();
  57. shell->default_termios = editor->default_termios();
  58. editor->on_display_refresh = [&](auto& editor) {
  59. editor.strip_styles();
  60. if (shell->should_format_live()) {
  61. auto line = editor.line();
  62. ssize_t cursor = editor.cursor();
  63. editor.clear_line();
  64. editor.insert(shell->format(line, cursor));
  65. if (cursor >= 0)
  66. editor.set_cursor(cursor);
  67. }
  68. shell->highlight(editor);
  69. };
  70. editor->on_tab_complete = [&](const Line::Editor&) {
  71. return shell->complete();
  72. };
  73. };
  74. const char* command_to_run = nullptr;
  75. const char* file_to_read_from = nullptr;
  76. Vector<const char*> script_args;
  77. bool skip_rc_files = false;
  78. const char* format = nullptr;
  79. bool should_format_live = false;
  80. bool keep_open = false;
  81. Core::ArgsParser parser;
  82. parser.add_option(command_to_run, "String to read commands from", "command-string", 'c', "command-string");
  83. parser.add_option(skip_rc_files, "Skip running shellrc files", "skip-shellrc", 0);
  84. parser.add_option(format, "Format the given file into stdout and exit", "format", 0, "file");
  85. parser.add_option(should_format_live, "Enable live formatting", "live-formatting", 'f');
  86. parser.add_option(keep_open, "Keep the shell open after running the specified command or file", "keep-open", 0);
  87. parser.add_positional_argument(file_to_read_from, "File to read commands from", "file", Core::ArgsParser::Required::No);
  88. parser.add_positional_argument(script_args, "Extra arguments to pass to the script (via $* and co)", "argument", Core::ArgsParser::Required::No);
  89. parser.parse(argc, argv);
  90. if (format) {
  91. auto file = Core::File::open(format, Core::OpenMode::ReadOnly);
  92. if (file.is_error()) {
  93. warnln("Error: {}", file.error());
  94. return 1;
  95. }
  96. initialize();
  97. ssize_t cursor = -1;
  98. puts(shell->format(file.value()->read_all(), cursor).characters());
  99. return 0;
  100. }
  101. auto pid = getpid();
  102. if (auto sid = getsid(pid); sid == 0) {
  103. if (setsid() < 0) {
  104. perror("setsid");
  105. // Let's just hope that it's ok.
  106. }
  107. } else if (sid != pid) {
  108. if (getpgid(pid) != pid) {
  109. if (setpgid(pid, sid) < 0) {
  110. auto strerr = strerror(errno);
  111. dbgln("couldn't setpgid: {}", strerr);
  112. }
  113. if (setsid() < 0) {
  114. auto strerr = strerror(errno);
  115. dbgln("couldn't setsid: {}", strerr);
  116. }
  117. }
  118. }
  119. auto execute_file = file_to_read_from && "-"sv != file_to_read_from;
  120. attempt_interactive = !execute_file;
  121. if (keep_open && !command_to_run && !execute_file) {
  122. warnln("Option --keep-open can only be used in combination with -c or when specifying a file to execute.");
  123. return 1;
  124. }
  125. initialize();
  126. shell->set_live_formatting(should_format_live);
  127. shell->current_script = argv[0];
  128. if (!skip_rc_files) {
  129. auto run_rc_file = [&](auto& name) {
  130. String file_path = name;
  131. if (file_path.starts_with('~'))
  132. file_path = shell->expand_tilde(file_path);
  133. if (Core::File::exists(file_path)) {
  134. shell->run_file(file_path, false);
  135. }
  136. };
  137. run_rc_file(Shell::Shell::global_init_file_path);
  138. run_rc_file(Shell::Shell::local_init_file_path);
  139. }
  140. {
  141. Vector<String> args;
  142. for (auto* arg : script_args)
  143. args.empend(arg);
  144. shell->set_local_variable("ARGV", adopt_ref(*new Shell::AST::ListValue(move(args))));
  145. }
  146. if (command_to_run) {
  147. dbgln("sh -c '{}'\n", command_to_run);
  148. auto result = shell->run_command(command_to_run);
  149. if (!keep_open)
  150. return result;
  151. }
  152. if (execute_file) {
  153. auto result = shell->run_file(file_to_read_from);
  154. if (!keep_open) {
  155. if (result)
  156. return 0;
  157. return 1;
  158. }
  159. }
  160. shell->add_child(*editor);
  161. Core::EventLoop::current().post_event(*shell, make<Core::CustomEvent>(Shell::Shell::ShellEventType::ReadLine));
  162. return loop.exec();
  163. }