Shell.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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(time) \
  54. __ENUMERATE_SHELL_BUILTIN(jobs) \
  55. __ENUMERATE_SHELL_BUILTIN(disown) \
  56. __ENUMERATE_SHELL_BUILTIN(fg) \
  57. __ENUMERATE_SHELL_BUILTIN(bg)
  58. #define ENUMERATE_SHELL_OPTIONS() \
  59. __ENUMERATE_SHELL_OPTION(inline_exec_keep_empty_segments, false, "Keep empty segments in inline execute $(...)") \
  60. __ENUMERATE_SHELL_OPTION(verbose, false, "Announce every command that is about to be executed")
  61. class Shell;
  62. class Shell : public Core::Object {
  63. C_OBJECT(Shell);
  64. public:
  65. constexpr static auto local_init_file_path = "~/.shellrc";
  66. constexpr static auto global_init_file_path = "/etc/shellrc";
  67. int run_command(const StringView&);
  68. RefPtr<Job> run_command(const AST::Command&);
  69. Vector<RefPtr<Job>> run_commands(Vector<AST::Command>&);
  70. bool run_file(const String&, bool explicitly_invoked = true);
  71. bool run_builtin(int argc, const char** argv, int& retval);
  72. bool has_builtin(const StringView&) const;
  73. void block_on_job(RefPtr<Job>);
  74. String prompt() const;
  75. static String expand_tilde(const String&);
  76. static Vector<String> expand_globs(const StringView& path, StringView base);
  77. static Vector<String> expand_globs(Vector<StringView> path_segments, const StringView& base);
  78. Vector<AST::Command> expand_aliases(Vector<AST::Command>);
  79. String resolve_path(String) const;
  80. String resolve_alias(const String&) const;
  81. RefPtr<AST::Value> lookup_local_variable(const String&);
  82. String local_variable_or(const String&, const String&);
  83. void set_local_variable(const String&, RefPtr<AST::Value>);
  84. void unset_local_variable(const String&);
  85. static String escape_token(const String& token);
  86. static String unescape_token(const String& token);
  87. static bool is_glob(const StringView&);
  88. static Vector<StringView> split_path(const StringView&);
  89. void highlight(Line::Editor&) const;
  90. Vector<Line::CompletionSuggestion> complete(const Line::Editor&);
  91. Vector<Line::CompletionSuggestion> complete_path(const String& base, const String&, size_t offset);
  92. Vector<Line::CompletionSuggestion> complete_program_name(const String&, size_t offset);
  93. Vector<Line::CompletionSuggestion> complete_variable(const String&, size_t offset);
  94. Vector<Line::CompletionSuggestion> complete_user(const String&, size_t offset);
  95. Vector<Line::CompletionSuggestion> complete_option(const String&, const String&, size_t offset);
  96. void restore_stdin();
  97. u64 find_last_job_id() const;
  98. const Job* find_job(u64 id);
  99. const Job* current_job() const { return m_current_job; }
  100. void kill_job(const Job*, int sig);
  101. String get_history_path();
  102. void load_history();
  103. void save_history();
  104. void print_path(const String& path);
  105. bool read_single_line();
  106. struct termios termios;
  107. struct termios default_termios;
  108. bool was_interrupted { false };
  109. bool was_resized { false };
  110. String cwd;
  111. String username;
  112. String home;
  113. constexpr static auto TTYNameSize = 32;
  114. constexpr static auto HostNameSize = 64;
  115. char ttyname[TTYNameSize];
  116. char hostname[HostNameSize];
  117. uid_t uid;
  118. int last_return_code { 0 };
  119. Vector<String> directory_stack;
  120. CircularQueue<String, 8> cd_history; // FIXME: have a configurable cd history length
  121. HashMap<u64, RefPtr<Job>> jobs;
  122. Vector<String, 256> cached_path;
  123. enum ShellEventType {
  124. ReadLine,
  125. };
  126. #define __ENUMERATE_SHELL_OPTION(name, default_, description) \
  127. bool name { default_ };
  128. struct Options {
  129. ENUMERATE_SHELL_OPTIONS();
  130. } options;
  131. #undef __ENUMERATE_SHELL_OPTION
  132. private:
  133. Shell();
  134. virtual ~Shell() override;
  135. // ^Core::Object
  136. virtual void save_to(JsonObject&) override;
  137. void cache_path();
  138. void stop_all_jobs();
  139. const Job* m_current_job { nullptr };
  140. virtual void custom_event(Core::CustomEvent&) override;
  141. #define __ENUMERATE_SHELL_BUILTIN(builtin) \
  142. int builtin_##builtin(int argc, const char** argv);
  143. ENUMERATE_SHELL_BUILTINS();
  144. #undef __ENUMERATE_SHELL_BUILTIN
  145. constexpr static const char* builtin_names[] = {
  146. #define __ENUMERATE_SHELL_BUILTIN(builtin) #builtin,
  147. ENUMERATE_SHELL_BUILTINS()
  148. #undef __ENUMERATE_SHELL_BUILTIN
  149. };
  150. StringBuilder m_complete_line_builder;
  151. bool m_should_ignore_jobs_on_next_exit { false };
  152. pid_t m_pid { 0 };
  153. HashMap<String, RefPtr<AST::Value>> m_local_variables;
  154. HashMap<String, String> m_aliases;
  155. };
  156. static constexpr bool is_word_character(char c)
  157. {
  158. return c == '_' || (c <= 'Z' && c >= 'A') || (c <= 'z' && c >= 'a');
  159. }