Shell.h 9.2 KB

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