main.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. Core::ArgsParser parser;
  81. parser.add_option(command_to_run, "String to read commands from", "command-string", 'c', "command-string");
  82. parser.add_option(skip_rc_files, "Skip running shellrc files", "skip-shellrc", 0);
  83. parser.add_option(format, "Format the given file into stdout and exit", "format", 0, "file");
  84. parser.add_option(should_format_live, "Enable live formatting", "live-formatting", 'f');
  85. parser.add_positional_argument(file_to_read_from, "File to read commands from", "file", Core::ArgsParser::Required::No);
  86. parser.add_positional_argument(script_args, "Extra arguments to pass to the script (via $* and co)", "argument", Core::ArgsParser::Required::No);
  87. parser.parse(argc, argv);
  88. if (format) {
  89. auto file = Core::File::open(format, Core::OpenMode::ReadOnly);
  90. if (file.is_error()) {
  91. warnln("Error: {}", file.error());
  92. return 1;
  93. }
  94. initialize();
  95. ssize_t cursor = -1;
  96. puts(shell->format(file.value()->read_all(), cursor).characters());
  97. return 0;
  98. }
  99. auto pid = getpid();
  100. if (auto sid = getsid(pid); sid == 0) {
  101. if (setsid() < 0) {
  102. perror("setsid");
  103. // Let's just hope that it's ok.
  104. }
  105. } else if (sid != pid) {
  106. if (getpgid(pid) != pid) {
  107. dbgln("We were already in a session with sid={} (we are {}), let's do some gymnastics", sid, pid);
  108. if (setpgid(pid, sid) < 0) {
  109. auto strerr = strerror(errno);
  110. dbgln("couldn't setpgid: {}", strerr);
  111. }
  112. if (setsid() < 0) {
  113. auto strerr = strerror(errno);
  114. dbgln("couldn't setsid: {}", strerr);
  115. }
  116. }
  117. }
  118. auto execute_file = file_to_read_from && StringView { "-" } != file_to_read_from;
  119. attempt_interactive = !execute_file;
  120. initialize();
  121. shell->set_live_formatting(should_format_live);
  122. shell->current_script = argv[0];
  123. if (!skip_rc_files) {
  124. auto run_rc_file = [&](auto& name) {
  125. String file_path = name;
  126. if (file_path.starts_with('~'))
  127. file_path = shell->expand_tilde(file_path);
  128. if (Core::File::exists(file_path)) {
  129. shell->run_file(file_path, false);
  130. }
  131. };
  132. run_rc_file(Shell::Shell::global_init_file_path);
  133. run_rc_file(Shell::Shell::local_init_file_path);
  134. }
  135. {
  136. Vector<String> args;
  137. for (auto* arg : script_args)
  138. args.empend(arg);
  139. shell->set_local_variable("ARGV", adopt_ref(*new Shell::AST::ListValue(move(args))));
  140. }
  141. if (command_to_run) {
  142. dbgln("sh -c '{}'\n", command_to_run);
  143. return shell->run_command(command_to_run);
  144. }
  145. if (execute_file) {
  146. if (shell->run_file(file_to_read_from))
  147. return 0;
  148. return 1;
  149. }
  150. shell->add_child(*editor);
  151. Core::EventLoop::current().post_event(*shell, make<Core::CustomEvent>(Shell::Shell::ShellEventType::ReadLine));
  152. return loop.exec();
  153. }