Shell.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c) 2020, The SerenityOS developers.
  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. #pragma once
  27. #include "Job.h"
  28. #include "Parser.h"
  29. #include <AK/CircularQueue.h>
  30. #include <AK/HashMap.h>
  31. #include <AK/String.h>
  32. #include <AK/StringBuilder.h>
  33. #include <AK/Types.h>
  34. #include <AK/Vector.h>
  35. #include <LibCore/Notifier.h>
  36. #include <LibCore/Object.h>
  37. #include <LibLine/Editor.h>
  38. #include <termios.h>
  39. #define ENUMERATE_SHELL_BUILTINS() \
  40. __ENUMERATE_SHELL_BUILTIN(alias) \
  41. __ENUMERATE_SHELL_BUILTIN(cd) \
  42. __ENUMERATE_SHELL_BUILTIN(cdh) \
  43. __ENUMERATE_SHELL_BUILTIN(pwd) \
  44. __ENUMERATE_SHELL_BUILTIN(exit) \
  45. __ENUMERATE_SHELL_BUILTIN(export) \
  46. __ENUMERATE_SHELL_BUILTIN(unset) \
  47. __ENUMERATE_SHELL_BUILTIN(history) \
  48. __ENUMERATE_SHELL_BUILTIN(umask) \
  49. __ENUMERATE_SHELL_BUILTIN(dirs) \
  50. __ENUMERATE_SHELL_BUILTIN(pushd) \
  51. __ENUMERATE_SHELL_BUILTIN(popd) \
  52. __ENUMERATE_SHELL_BUILTIN(setopt) \
  53. __ENUMERATE_SHELL_BUILTIN(shift) \
  54. __ENUMERATE_SHELL_BUILTIN(time) \
  55. __ENUMERATE_SHELL_BUILTIN(jobs) \
  56. __ENUMERATE_SHELL_BUILTIN(disown) \
  57. __ENUMERATE_SHELL_BUILTIN(fg) \
  58. __ENUMERATE_SHELL_BUILTIN(bg)
  59. #define ENUMERATE_SHELL_OPTIONS() \
  60. __ENUMERATE_SHELL_OPTION(inline_exec_keep_empty_segments, false, "Keep empty segments in inline execute $(...)") \
  61. __ENUMERATE_SHELL_OPTION(verbose, false, "Announce every command that is about to be executed")
  62. class Shell;
  63. class Shell : public Core::Object {
  64. C_OBJECT(Shell);
  65. public:
  66. constexpr static auto local_init_file_path = "~/.shellrc";
  67. constexpr static auto global_init_file_path = "/etc/shellrc";
  68. bool should_format_live() const { return m_should_format_live; }
  69. void set_live_formatting(bool value) { m_should_format_live = value; }
  70. void setup_signals();
  71. int run_command(const StringView&);
  72. bool is_runnable(const StringView&);
  73. RefPtr<Job> run_command(const AST::Command&);
  74. NonnullRefPtrVector<Job> run_commands(Vector<AST::Command>&);
  75. bool run_file(const String&, bool explicitly_invoked = true);
  76. bool run_builtin(const AST::Command&, const NonnullRefPtrVector<AST::Rewiring>&, int& retval);
  77. bool has_builtin(const StringView&) const;
  78. void block_on_job(RefPtr<Job>);
  79. String prompt() const;
  80. static String expand_tilde(const String&);
  81. static Vector<String> expand_globs(const StringView& path, StringView base);
  82. static Vector<String> expand_globs(Vector<StringView> path_segments, const StringView& base);
  83. Vector<AST::Command> expand_aliases(Vector<AST::Command>);
  84. String resolve_path(String) const;
  85. String resolve_alias(const String&) const;
  86. RefPtr<AST::Value> get_argument(size_t);
  87. RefPtr<AST::Value> lookup_local_variable(const String&);
  88. String local_variable_or(const String&, const String&);
  89. void set_local_variable(const String&, RefPtr<AST::Value>);
  90. void unset_local_variable(const String&);
  91. void define_function(String name, Vector<String> argnames, RefPtr<AST::Node> body);
  92. bool has_function(const String&);
  93. bool invoke_function(const AST::Command&, int& retval);
  94. String format(const StringView&, ssize_t& cursor) const;
  95. struct LocalFrame {
  96. HashMap<String, RefPtr<AST::Value>> local_variables;
  97. };
  98. struct Frame {
  99. Frame(Vector<LocalFrame>& frames, const LocalFrame& frame)
  100. : frames(frames)
  101. , frame(frame)
  102. {
  103. }
  104. ~Frame();
  105. void leak_frame() { should_destroy_frame = false; }
  106. private:
  107. Vector<LocalFrame>& frames;
  108. const LocalFrame& frame;
  109. bool should_destroy_frame { true };
  110. };
  111. [[nodiscard]] Frame push_frame();
  112. void pop_frame();
  113. static String escape_token(const String& token);
  114. static String unescape_token(const String& token);
  115. static bool is_glob(const StringView&);
  116. static Vector<StringView> split_path(const StringView&);
  117. void highlight(Line::Editor&) const;
  118. Vector<Line::CompletionSuggestion> complete();
  119. Vector<Line::CompletionSuggestion> complete_path(const String& base, const String&, size_t offset);
  120. Vector<Line::CompletionSuggestion> complete_program_name(const String&, size_t offset);
  121. Vector<Line::CompletionSuggestion> complete_variable(const String&, size_t offset);
  122. Vector<Line::CompletionSuggestion> complete_user(const String&, size_t offset);
  123. Vector<Line::CompletionSuggestion> complete_option(const String&, const String&, size_t offset);
  124. void restore_ios();
  125. u64 find_last_job_id() const;
  126. const Job* find_job(u64 id);
  127. const Job* current_job() const { return m_current_job; }
  128. void kill_job(const Job*, int sig);
  129. String get_history_path();
  130. void load_history();
  131. void save_history();
  132. void print_path(const String& path);
  133. bool read_single_line();
  134. void notify_child_event();
  135. struct termios termios;
  136. struct termios default_termios;
  137. bool was_interrupted { false };
  138. bool was_resized { false };
  139. String cwd;
  140. String username;
  141. String home;
  142. constexpr static auto TTYNameSize = 32;
  143. constexpr static auto HostNameSize = 64;
  144. char ttyname[TTYNameSize];
  145. char hostname[HostNameSize];
  146. uid_t uid;
  147. int last_return_code { 0 };
  148. Vector<String> directory_stack;
  149. CircularQueue<String, 8> cd_history; // FIXME: have a configurable cd history length
  150. HashMap<u64, NonnullRefPtr<Job>> jobs;
  151. Vector<String, 256> cached_path;
  152. String current_script;
  153. enum ShellEventType {
  154. ReadLine,
  155. };
  156. #define __ENUMERATE_SHELL_OPTION(name, default_, description) \
  157. bool name { default_ };
  158. struct Options {
  159. ENUMERATE_SHELL_OPTIONS();
  160. } options;
  161. #undef __ENUMERATE_SHELL_OPTION
  162. private:
  163. Shell(Line::Editor&);
  164. virtual ~Shell() override;
  165. // FIXME: Port to Core::Property
  166. void save_to(JsonObject&);
  167. void bring_cursor_to_beginning_of_a_line() const;
  168. void cache_path();
  169. void add_entry_to_cache(const String&);
  170. void stop_all_jobs();
  171. const Job* m_current_job { nullptr };
  172. LocalFrame* find_frame_containing_local_variable(const String& name);
  173. void run_tail(RefPtr<Job>);
  174. void run_tail(const AST::NodeWithAction&, int head_exit_code);
  175. virtual void custom_event(Core::CustomEvent&) override;
  176. #define __ENUMERATE_SHELL_BUILTIN(builtin) \
  177. int builtin_##builtin(int argc, const char** argv);
  178. ENUMERATE_SHELL_BUILTINS();
  179. #undef __ENUMERATE_SHELL_BUILTIN
  180. constexpr static const char* builtin_names[] = {
  181. #define __ENUMERATE_SHELL_BUILTIN(builtin) #builtin,
  182. ENUMERATE_SHELL_BUILTINS()
  183. #undef __ENUMERATE_SHELL_BUILTIN
  184. };
  185. StringBuilder m_complete_line_builder;
  186. bool m_should_ignore_jobs_on_next_exit { false };
  187. pid_t m_pid { 0 };
  188. struct ShellFunction {
  189. String name;
  190. Vector<String> arguments;
  191. RefPtr<AST::Node> body;
  192. };
  193. HashMap<String, ShellFunction> m_functions;
  194. Vector<LocalFrame> m_local_frames;
  195. NonnullRefPtrVector<AST::Redirection> m_global_redirections;
  196. HashMap<String, String> m_aliases;
  197. bool m_is_interactive { true };
  198. bool m_is_subshell { false };
  199. bool m_should_format_live { false };
  200. RefPtr<Line::Editor> m_editor;
  201. };
  202. static constexpr bool is_word_character(char c)
  203. {
  204. return c == '_' || (c <= 'Z' && c >= 'A') || (c <= 'z' && c >= 'a');
  205. }