Shell.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. int run_command(const StringView&);
  69. bool is_runnable(const StringView&);
  70. RefPtr<Job> run_command(const AST::Command&);
  71. Vector<RefPtr<Job>> run_commands(Vector<AST::Command>&);
  72. bool run_file(const String&, bool explicitly_invoked = true);
  73. bool run_builtin(int argc, const char** argv, int& retval);
  74. bool has_builtin(const StringView&) const;
  75. void block_on_job(RefPtr<Job>);
  76. String prompt() const;
  77. static String expand_tilde(const String&);
  78. static Vector<String> expand_globs(const StringView& path, StringView base);
  79. static Vector<String> expand_globs(Vector<StringView> path_segments, const StringView& base);
  80. Vector<AST::Command> expand_aliases(Vector<AST::Command>);
  81. String resolve_path(String) const;
  82. String resolve_alias(const String&) const;
  83. RefPtr<AST::Value> lookup_local_variable(const String&);
  84. String local_variable_or(const String&, const String&);
  85. void set_local_variable(const String&, RefPtr<AST::Value>);
  86. void unset_local_variable(const String&);
  87. struct LocalFrame {
  88. HashMap<String, RefPtr<AST::Value>> local_variables;
  89. };
  90. struct Frame {
  91. Frame(Vector<LocalFrame>& frames, const LocalFrame& frame)
  92. : frames(frames)
  93. , frame(frame)
  94. {
  95. }
  96. ~Frame();
  97. void leak_frame() { should_destroy_frame = false; }
  98. private:
  99. Vector<LocalFrame>& frames;
  100. const LocalFrame& frame;
  101. bool should_destroy_frame { true };
  102. };
  103. [[nodiscard]] Frame push_frame();
  104. void pop_frame();
  105. static String escape_token(const String& token);
  106. static String unescape_token(const String& token);
  107. static bool is_glob(const StringView&);
  108. static Vector<StringView> split_path(const StringView&);
  109. void highlight(Line::Editor&) const;
  110. Vector<Line::CompletionSuggestion> complete(const Line::Editor&);
  111. Vector<Line::CompletionSuggestion> complete_path(const String& base, const String&, size_t offset);
  112. Vector<Line::CompletionSuggestion> complete_program_name(const String&, size_t offset);
  113. Vector<Line::CompletionSuggestion> complete_variable(const String&, size_t offset);
  114. Vector<Line::CompletionSuggestion> complete_user(const String&, size_t offset);
  115. Vector<Line::CompletionSuggestion> complete_option(const String&, const String&, size_t offset);
  116. void restore_stdin();
  117. u64 find_last_job_id() const;
  118. const Job* find_job(u64 id);
  119. const Job* current_job() const { return m_current_job; }
  120. void kill_job(const Job*, int sig);
  121. String get_history_path();
  122. void load_history();
  123. void save_history();
  124. void print_path(const String& path);
  125. bool read_single_line();
  126. struct termios termios;
  127. struct termios default_termios;
  128. bool was_interrupted { false };
  129. bool was_resized { false };
  130. String cwd;
  131. String username;
  132. String home;
  133. constexpr static auto TTYNameSize = 32;
  134. constexpr static auto HostNameSize = 64;
  135. char ttyname[TTYNameSize];
  136. char hostname[HostNameSize];
  137. uid_t uid;
  138. int last_return_code { 0 };
  139. Vector<String> directory_stack;
  140. CircularQueue<String, 8> cd_history; // FIXME: have a configurable cd history length
  141. HashMap<u64, RefPtr<Job>> jobs;
  142. Vector<String, 256> cached_path;
  143. enum ShellEventType {
  144. ReadLine,
  145. };
  146. #define __ENUMERATE_SHELL_OPTION(name, default_, description) \
  147. bool name { default_ };
  148. struct Options {
  149. ENUMERATE_SHELL_OPTIONS();
  150. } options;
  151. #undef __ENUMERATE_SHELL_OPTION
  152. private:
  153. Shell();
  154. virtual ~Shell() override;
  155. // ^Core::Object
  156. virtual void save_to(JsonObject&) override;
  157. void cache_path();
  158. void add_entry_to_cache(const String&);
  159. void stop_all_jobs();
  160. const Job* m_current_job { nullptr };
  161. LocalFrame* find_frame_containing_local_variable(const String& name);
  162. virtual void custom_event(Core::CustomEvent&) override;
  163. #define __ENUMERATE_SHELL_BUILTIN(builtin) \
  164. int builtin_##builtin(int argc, const char** argv);
  165. ENUMERATE_SHELL_BUILTINS();
  166. #undef __ENUMERATE_SHELL_BUILTIN
  167. constexpr static const char* builtin_names[] = {
  168. #define __ENUMERATE_SHELL_BUILTIN(builtin) #builtin,
  169. ENUMERATE_SHELL_BUILTINS()
  170. #undef __ENUMERATE_SHELL_BUILTIN
  171. };
  172. StringBuilder m_complete_line_builder;
  173. bool m_should_ignore_jobs_on_next_exit { false };
  174. pid_t m_pid { 0 };
  175. Vector<LocalFrame> m_local_frames;
  176. HashMap<String, String> m_aliases;
  177. };
  178. static constexpr bool is_word_character(char c)
  179. {
  180. return c == '_' || (c <= 'Z' && c >= 'A') || (c <= 'z' && c >= 'a');
  181. }