2020-05-16 22:05:13 +03:00
|
|
|
/*
|
2021-04-28 23:46:44 +03:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-05-16 22:05:13 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-16 22:05:13 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Job.h"
|
|
|
|
#include "Parser.h"
|
|
|
|
#include <AK/CircularQueue.h>
|
|
|
|
#include <AK/HashMap.h>
|
2020-11-01 12:58:11 +03:00
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
2020-05-16 22:05:13 +03:00
|
|
|
#include <AK/String.h>
|
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <LibCore/Notifier.h>
|
|
|
|
#include <LibCore/Object.h>
|
|
|
|
#include <LibLine/Editor.h>
|
|
|
|
#include <termios.h>
|
|
|
|
|
|
|
|
#define ENUMERATE_SHELL_BUILTINS() \
|
2020-06-17 17:21:44 +03:00
|
|
|
__ENUMERATE_SHELL_BUILTIN(alias) \
|
2020-05-16 22:05:13 +03:00
|
|
|
__ENUMERATE_SHELL_BUILTIN(cd) \
|
|
|
|
__ENUMERATE_SHELL_BUILTIN(cdh) \
|
|
|
|
__ENUMERATE_SHELL_BUILTIN(pwd) \
|
2021-04-11 06:25:49 +03:00
|
|
|
__ENUMERATE_SHELL_BUILTIN(type) \
|
2020-12-29 22:42:34 +03:00
|
|
|
__ENUMERATE_SHELL_BUILTIN(exec) \
|
2020-05-16 22:05:13 +03:00
|
|
|
__ENUMERATE_SHELL_BUILTIN(exit) \
|
|
|
|
__ENUMERATE_SHELL_BUILTIN(export) \
|
2020-12-01 14:24:00 +03:00
|
|
|
__ENUMERATE_SHELL_BUILTIN(glob) \
|
2020-05-16 22:05:13 +03:00
|
|
|
__ENUMERATE_SHELL_BUILTIN(unset) \
|
|
|
|
__ENUMERATE_SHELL_BUILTIN(history) \
|
|
|
|
__ENUMERATE_SHELL_BUILTIN(umask) \
|
2021-01-18 09:35:00 +03:00
|
|
|
__ENUMERATE_SHELL_BUILTIN(not ) \
|
2020-05-16 22:05:13 +03:00
|
|
|
__ENUMERATE_SHELL_BUILTIN(dirs) \
|
|
|
|
__ENUMERATE_SHELL_BUILTIN(pushd) \
|
|
|
|
__ENUMERATE_SHELL_BUILTIN(popd) \
|
2020-06-29 04:52:58 +03:00
|
|
|
__ENUMERATE_SHELL_BUILTIN(setopt) \
|
2020-08-04 07:57:25 +03:00
|
|
|
__ENUMERATE_SHELL_BUILTIN(shift) \
|
2021-01-03 12:09:27 +03:00
|
|
|
__ENUMERATE_SHELL_BUILTIN(source) \
|
2020-05-16 22:05:13 +03:00
|
|
|
__ENUMERATE_SHELL_BUILTIN(time) \
|
|
|
|
__ENUMERATE_SHELL_BUILTIN(jobs) \
|
2020-05-24 19:31:39 +03:00
|
|
|
__ENUMERATE_SHELL_BUILTIN(disown) \
|
2020-05-16 22:05:13 +03:00
|
|
|
__ENUMERATE_SHELL_BUILTIN(fg) \
|
2020-10-26 20:00:14 +03:00
|
|
|
__ENUMERATE_SHELL_BUILTIN(bg) \
|
2021-01-18 04:04:16 +03:00
|
|
|
__ENUMERATE_SHELL_BUILTIN(wait) \
|
2021-04-23 17:39:13 +03:00
|
|
|
__ENUMERATE_SHELL_BUILTIN(dump) \
|
|
|
|
__ENUMERATE_SHELL_BUILTIN(kill)
|
2020-05-16 22:05:13 +03:00
|
|
|
|
2020-06-29 04:52:58 +03:00
|
|
|
#define ENUMERATE_SHELL_OPTIONS() \
|
|
|
|
__ENUMERATE_SHELL_OPTION(inline_exec_keep_empty_segments, false, "Keep empty segments in inline execute $(...)") \
|
|
|
|
__ENUMERATE_SHELL_OPTION(verbose, false, "Announce every command that is about to be executed")
|
|
|
|
|
2021-03-05 16:03:23 +03:00
|
|
|
#define ENUMERATE_SHELL_IMMEDIATE_FUNCTIONS() \
|
|
|
|
__ENUMERATE_SHELL_IMMEDIATE_FUNCTION(concat_lists) \
|
|
|
|
__ENUMERATE_SHELL_IMMEDIATE_FUNCTION(length) \
|
|
|
|
__ENUMERATE_SHELL_IMMEDIATE_FUNCTION(length_across) \
|
|
|
|
__ENUMERATE_SHELL_IMMEDIATE_FUNCTION(remove_suffix) \
|
|
|
|
__ENUMERATE_SHELL_IMMEDIATE_FUNCTION(remove_prefix) \
|
|
|
|
__ENUMERATE_SHELL_IMMEDIATE_FUNCTION(regex_replace) \
|
|
|
|
__ENUMERATE_SHELL_IMMEDIATE_FUNCTION(split)
|
|
|
|
|
2020-10-01 17:43:01 +03:00
|
|
|
namespace Shell {
|
|
|
|
|
2020-05-16 22:05:13 +03:00
|
|
|
class Shell;
|
|
|
|
|
|
|
|
class Shell : public Core::Object {
|
|
|
|
C_OBJECT(Shell);
|
|
|
|
|
|
|
|
public:
|
2020-07-07 12:27:35 +03:00
|
|
|
constexpr static auto local_init_file_path = "~/.shellrc";
|
|
|
|
constexpr static auto global_init_file_path = "/etc/shellrc";
|
2020-06-17 18:07:44 +03:00
|
|
|
|
2020-09-16 03:48:33 +03:00
|
|
|
bool should_format_live() const { return m_should_format_live; }
|
|
|
|
void set_live_formatting(bool value) { m_should_format_live = value; }
|
|
|
|
|
2020-09-07 21:15:31 +03:00
|
|
|
void setup_signals();
|
|
|
|
|
2021-01-03 12:08:20 +03:00
|
|
|
struct SourcePosition {
|
|
|
|
String source_file;
|
|
|
|
String literal_source_text;
|
|
|
|
Optional<AST::Position> position;
|
|
|
|
};
|
|
|
|
|
|
|
|
int run_command(const StringView&, Optional<SourcePosition> = {});
|
2020-08-03 03:04:27 +03:00
|
|
|
bool is_runnable(const StringView&);
|
2020-07-13 08:00:09 +03:00
|
|
|
RefPtr<Job> run_command(const AST::Command&);
|
2020-08-06 14:44:30 +03:00
|
|
|
NonnullRefPtrVector<Job> run_commands(Vector<AST::Command>&);
|
2020-07-05 17:57:37 +03:00
|
|
|
bool run_file(const String&, bool explicitly_invoked = true);
|
2020-08-14 21:00:48 +03:00
|
|
|
bool run_builtin(const AST::Command&, const NonnullRefPtrVector<AST::Rewiring>&, int& retval);
|
2020-06-29 04:56:06 +03:00
|
|
|
bool has_builtin(const StringView&) const;
|
2021-03-05 16:03:23 +03:00
|
|
|
RefPtr<AST::Node> run_immediate_function(StringView name, AST::ImmediateExpression& invoking_node, const NonnullRefPtrVector<AST::Node>&);
|
|
|
|
static bool has_immediate_function(const StringView&);
|
2020-06-17 16:35:06 +03:00
|
|
|
void block_on_job(RefPtr<Job>);
|
2020-10-28 17:15:17 +03:00
|
|
|
void block_on_pipeline(RefPtr<AST::Pipeline>);
|
2020-05-16 22:05:13 +03:00
|
|
|
String prompt() const;
|
|
|
|
|
|
|
|
static String expand_tilde(const String&);
|
2020-06-17 16:35:06 +03:00
|
|
|
static Vector<String> expand_globs(const StringView& path, StringView base);
|
|
|
|
static Vector<String> expand_globs(Vector<StringView> path_segments, const StringView& base);
|
2020-07-13 08:00:09 +03:00
|
|
|
Vector<AST::Command> expand_aliases(Vector<AST::Command>);
|
2020-06-17 16:35:06 +03:00
|
|
|
String resolve_path(String) const;
|
2020-06-17 17:21:44 +03:00
|
|
|
String resolve_alias(const String&) const;
|
2020-06-17 16:35:06 +03:00
|
|
|
|
2021-04-23 17:39:13 +03:00
|
|
|
static String find_in_path(const StringView& program_name);
|
|
|
|
|
2021-01-11 12:34:59 +03:00
|
|
|
static bool has_history_event(StringView);
|
|
|
|
|
2021-05-24 15:20:16 +03:00
|
|
|
RefPtr<AST::Value> get_argument(size_t) const;
|
|
|
|
RefPtr<AST::Value> lookup_local_variable(const String&) const;
|
|
|
|
String local_variable_or(const String&, const String&) const;
|
2020-11-01 12:59:25 +03:00
|
|
|
void set_local_variable(const String&, RefPtr<AST::Value>, bool only_in_current_frame = false);
|
|
|
|
void unset_local_variable(const String&, bool only_in_current_frame = false);
|
2020-05-16 22:05:13 +03:00
|
|
|
|
2020-09-13 14:24:33 +03:00
|
|
|
void define_function(String name, Vector<String> argnames, RefPtr<AST::Node> body);
|
|
|
|
bool has_function(const String&);
|
|
|
|
bool invoke_function(const AST::Command&, int& retval);
|
|
|
|
|
2020-09-16 03:43:04 +03:00
|
|
|
String format(const StringView&, ssize_t& cursor) const;
|
|
|
|
|
2020-10-26 02:25:41 +03:00
|
|
|
RefPtr<Line::Editor> editor() const { return m_editor; }
|
|
|
|
|
2020-07-12 00:12:46 +03:00
|
|
|
struct LocalFrame {
|
2021-03-05 15:44:53 +03:00
|
|
|
LocalFrame(String name, HashMap<String, RefPtr<AST::Value>> variables)
|
|
|
|
: name(move(name))
|
2020-11-01 12:58:11 +03:00
|
|
|
, local_variables(move(variables))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String name;
|
2020-07-12 00:12:46 +03:00
|
|
|
HashMap<String, RefPtr<AST::Value>> local_variables;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Frame {
|
2020-11-01 12:58:11 +03:00
|
|
|
Frame(NonnullOwnPtrVector<LocalFrame>& frames, const LocalFrame& frame)
|
2020-07-12 00:12:46 +03:00
|
|
|
: frames(frames)
|
|
|
|
, frame(frame)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
~Frame();
|
|
|
|
|
|
|
|
void leak_frame() { should_destroy_frame = false; }
|
|
|
|
|
|
|
|
private:
|
2020-11-01 12:58:11 +03:00
|
|
|
NonnullOwnPtrVector<LocalFrame>& frames;
|
2020-07-12 00:12:46 +03:00
|
|
|
const LocalFrame& frame;
|
|
|
|
bool should_destroy_frame { true };
|
|
|
|
};
|
|
|
|
|
2020-11-01 12:58:11 +03:00
|
|
|
[[nodiscard]] Frame push_frame(String name);
|
2020-07-12 00:12:46 +03:00
|
|
|
void pop_frame();
|
|
|
|
|
2021-01-11 12:34:59 +03:00
|
|
|
static String escape_token_for_double_quotes(const String& token);
|
2021-01-03 12:08:20 +03:00
|
|
|
static String escape_token_for_single_quotes(const String& token);
|
2020-05-16 22:05:13 +03:00
|
|
|
static String escape_token(const String& token);
|
|
|
|
static String unescape_token(const String& token);
|
2021-05-10 09:39:40 +03:00
|
|
|
enum class SpecialCharacterEscapeMode {
|
|
|
|
Untouched,
|
|
|
|
Escaped,
|
|
|
|
QuotedAsEscape,
|
|
|
|
QuotedAsHex,
|
|
|
|
};
|
|
|
|
static SpecialCharacterEscapeMode special_character_escape_mode(u32 c);
|
2020-05-16 22:05:13 +03:00
|
|
|
|
|
|
|
static bool is_glob(const StringView&);
|
|
|
|
static Vector<StringView> split_path(const StringView&);
|
|
|
|
|
2021-04-20 17:17:38 +03:00
|
|
|
enum class ExecutableOnly {
|
|
|
|
Yes,
|
|
|
|
No
|
|
|
|
};
|
|
|
|
|
2020-05-16 22:05:13 +03:00
|
|
|
void highlight(Line::Editor&) const;
|
2020-09-23 08:36:30 +03:00
|
|
|
Vector<Line::CompletionSuggestion> complete();
|
2021-04-20 17:17:38 +03:00
|
|
|
Vector<Line::CompletionSuggestion> complete_path(const String& base, const String&, size_t offset, ExecutableOnly executable_only);
|
2020-06-17 16:35:06 +03:00
|
|
|
Vector<Line::CompletionSuggestion> complete_program_name(const String&, size_t offset);
|
|
|
|
Vector<Line::CompletionSuggestion> complete_variable(const String&, size_t offset);
|
2020-06-23 17:40:41 +03:00
|
|
|
Vector<Line::CompletionSuggestion> complete_user(const String&, size_t offset);
|
2020-06-29 04:56:06 +03:00
|
|
|
Vector<Line::CompletionSuggestion> complete_option(const String&, const String&, size_t offset);
|
2021-03-05 16:03:23 +03:00
|
|
|
Vector<Line::CompletionSuggestion> complete_immediate_function_name(const String&, size_t offset);
|
2020-05-16 22:05:13 +03:00
|
|
|
|
2020-08-04 21:07:47 +03:00
|
|
|
void restore_ios();
|
2020-05-26 17:15:19 +03:00
|
|
|
|
2020-05-26 17:24:57 +03:00
|
|
|
u64 find_last_job_id() const;
|
2021-04-23 16:56:27 +03:00
|
|
|
const Job* find_job(u64 id, bool is_pid = false);
|
2020-07-12 17:21:47 +03:00
|
|
|
const Job* current_job() const { return m_current_job; }
|
|
|
|
void kill_job(const Job*, int sig);
|
2020-05-26 17:24:57 +03:00
|
|
|
|
2020-05-16 22:05:13 +03:00
|
|
|
String get_history_path();
|
|
|
|
void print_path(const String& path);
|
|
|
|
|
2020-05-25 15:27:07 +03:00
|
|
|
bool read_single_line();
|
2020-05-16 22:05:13 +03:00
|
|
|
|
2020-09-30 13:35:12 +03:00
|
|
|
void notify_child_event();
|
|
|
|
|
2020-05-16 22:05:13 +03:00
|
|
|
struct termios termios;
|
|
|
|
struct termios default_termios;
|
|
|
|
bool was_interrupted { false };
|
|
|
|
bool was_resized { false };
|
|
|
|
|
|
|
|
String cwd;
|
|
|
|
String username;
|
|
|
|
String home;
|
|
|
|
|
|
|
|
constexpr static auto TTYNameSize = 32;
|
|
|
|
constexpr static auto HostNameSize = 64;
|
|
|
|
|
|
|
|
char ttyname[TTYNameSize];
|
|
|
|
char hostname[HostNameSize];
|
|
|
|
|
|
|
|
uid_t uid;
|
|
|
|
int last_return_code { 0 };
|
|
|
|
Vector<String> directory_stack;
|
|
|
|
CircularQueue<String, 8> cd_history; // FIXME: have a configurable cd history length
|
2020-08-06 15:09:13 +03:00
|
|
|
HashMap<u64, NonnullRefPtr<Job>> jobs;
|
2020-05-16 22:05:13 +03:00
|
|
|
Vector<String, 256> cached_path;
|
|
|
|
|
2020-09-13 12:29:34 +03:00
|
|
|
String current_script;
|
|
|
|
|
2020-05-16 22:05:13 +03:00
|
|
|
enum ShellEventType {
|
|
|
|
ReadLine,
|
|
|
|
};
|
|
|
|
|
2020-12-10 17:55:13 +03:00
|
|
|
enum class ShellError {
|
|
|
|
None,
|
|
|
|
InternalControlFlowBreak,
|
|
|
|
InternalControlFlowContinue,
|
|
|
|
EvaluatedSyntaxError,
|
|
|
|
NonExhaustiveMatchRules,
|
|
|
|
InvalidGlobError,
|
2021-03-13 02:40:18 +03:00
|
|
|
InvalidSliceContentsError,
|
2021-01-03 12:08:20 +03:00
|
|
|
OpenFailure,
|
2020-12-10 17:55:13 +03:00
|
|
|
};
|
|
|
|
|
2021-01-03 12:08:20 +03:00
|
|
|
void raise_error(ShellError kind, String description, Optional<AST::Position> position = {})
|
2020-12-10 17:55:13 +03:00
|
|
|
{
|
|
|
|
m_error = kind;
|
|
|
|
m_error_description = move(description);
|
2021-01-03 12:08:20 +03:00
|
|
|
if (m_source_position.has_value() && position.has_value())
|
|
|
|
m_source_position.value().position = position.release_value();
|
2020-12-10 17:55:13 +03:00
|
|
|
}
|
|
|
|
bool has_error(ShellError err) const { return m_error == err; }
|
|
|
|
const String& error_description() const { return m_error_description; }
|
|
|
|
ShellError take_error()
|
|
|
|
{
|
|
|
|
auto err = m_error;
|
|
|
|
m_error = ShellError::None;
|
|
|
|
m_error_description = {};
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
void possibly_print_error() const;
|
2021-03-05 15:44:53 +03:00
|
|
|
static bool is_control_flow(ShellError error)
|
2021-01-03 12:08:20 +03:00
|
|
|
{
|
|
|
|
switch (error) {
|
|
|
|
case ShellError::InternalControlFlowBreak:
|
|
|
|
case ShellError::InternalControlFlowContinue:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-12-10 17:55:13 +03:00
|
|
|
|
2020-06-29 04:52:58 +03:00
|
|
|
#define __ENUMERATE_SHELL_OPTION(name, default_, description) \
|
|
|
|
bool name { default_ };
|
|
|
|
|
|
|
|
struct Options {
|
|
|
|
ENUMERATE_SHELL_OPTIONS();
|
|
|
|
} options;
|
|
|
|
|
|
|
|
#undef __ENUMERATE_SHELL_OPTION
|
|
|
|
|
2020-05-16 22:05:13 +03:00
|
|
|
private:
|
2021-03-07 09:12:24 +03:00
|
|
|
Shell(Line::Editor&, bool attempt_interactive);
|
2020-10-01 17:43:01 +03:00
|
|
|
Shell();
|
2020-05-16 22:05:13 +03:00
|
|
|
virtual ~Shell() override;
|
|
|
|
|
2021-05-11 10:12:36 +03:00
|
|
|
void timer_event(Core::TimerEvent&) override;
|
|
|
|
|
2021-05-24 15:20:16 +03:00
|
|
|
bool is_allowed_to_modify_termios(const AST::Command&) const;
|
|
|
|
|
2020-09-15 22:33:37 +03:00
|
|
|
// FIXME: Port to Core::Property
|
|
|
|
void save_to(JsonObject&);
|
2020-08-17 16:35:50 +03:00
|
|
|
void bring_cursor_to_beginning_of_a_line() const;
|
2020-05-26 14:11:03 +03:00
|
|
|
|
2021-04-23 16:56:27 +03:00
|
|
|
Optional<int> resolve_job_spec(const String&);
|
2020-05-16 22:05:13 +03:00
|
|
|
void cache_path();
|
2020-08-03 12:06:42 +03:00
|
|
|
void add_entry_to_cache(const String&);
|
2020-05-24 19:55:01 +03:00
|
|
|
void stop_all_jobs();
|
2020-07-12 17:21:47 +03:00
|
|
|
const Job* m_current_job { nullptr };
|
2020-07-12 00:12:46 +03:00
|
|
|
LocalFrame* find_frame_containing_local_variable(const String& name);
|
2021-05-24 15:20:16 +03:00
|
|
|
const LocalFrame* find_frame_containing_local_variable(const String& name) const
|
|
|
|
{
|
|
|
|
return const_cast<Shell*>(this)->find_frame_containing_local_variable(name);
|
|
|
|
}
|
2020-05-16 22:05:13 +03:00
|
|
|
|
2020-09-01 07:12:16 +03:00
|
|
|
void run_tail(RefPtr<Job>);
|
2020-10-26 02:01:29 +03:00
|
|
|
void run_tail(const AST::Command&, const AST::NodeWithAction&, int head_exit_code);
|
2020-09-01 07:12:16 +03:00
|
|
|
|
2020-12-29 22:42:34 +03:00
|
|
|
[[noreturn]] void execute_process(Vector<const char*>&& argv);
|
|
|
|
|
2020-05-16 22:05:13 +03:00
|
|
|
virtual void custom_event(Core::CustomEvent&) override;
|
|
|
|
|
2021-03-05 16:03:23 +03:00
|
|
|
#define __ENUMERATE_SHELL_IMMEDIATE_FUNCTION(name) \
|
|
|
|
RefPtr<AST::Node> immediate_##name(AST::ImmediateExpression& invoking_node, const NonnullRefPtrVector<AST::Node>&);
|
|
|
|
|
|
|
|
ENUMERATE_SHELL_IMMEDIATE_FUNCTIONS();
|
|
|
|
|
|
|
|
#undef __ENUMERATE_SHELL_IMMEDIATE_FUNCTION
|
|
|
|
|
|
|
|
RefPtr<AST::Node> immediate_length_impl(AST::ImmediateExpression& invoking_node, const NonnullRefPtrVector<AST::Node>&, bool across);
|
|
|
|
|
2020-05-16 22:05:13 +03:00
|
|
|
#define __ENUMERATE_SHELL_BUILTIN(builtin) \
|
|
|
|
int builtin_##builtin(int argc, const char** argv);
|
|
|
|
|
|
|
|
ENUMERATE_SHELL_BUILTINS();
|
|
|
|
|
|
|
|
#undef __ENUMERATE_SHELL_BUILTIN
|
|
|
|
|
2020-05-17 13:34:22 +03:00
|
|
|
constexpr static const char* builtin_names[] = {
|
|
|
|
#define __ENUMERATE_SHELL_BUILTIN(builtin) #builtin,
|
|
|
|
|
|
|
|
ENUMERATE_SHELL_BUILTINS()
|
|
|
|
|
|
|
|
#undef __ENUMERATE_SHELL_BUILTIN
|
|
|
|
};
|
|
|
|
|
2020-05-25 01:02:41 +03:00
|
|
|
bool m_should_ignore_jobs_on_next_exit { false };
|
2020-06-17 16:35:06 +03:00
|
|
|
pid_t m_pid { 0 };
|
|
|
|
|
2020-09-13 14:24:33 +03:00
|
|
|
struct ShellFunction {
|
|
|
|
String name;
|
|
|
|
Vector<String> arguments;
|
|
|
|
RefPtr<AST::Node> body;
|
|
|
|
};
|
|
|
|
|
|
|
|
HashMap<String, ShellFunction> m_functions;
|
2020-11-01 12:58:11 +03:00
|
|
|
NonnullOwnPtrVector<LocalFrame> m_local_frames;
|
2020-08-09 18:38:02 +03:00
|
|
|
NonnullRefPtrVector<AST::Redirection> m_global_redirections;
|
2020-07-12 00:12:46 +03:00
|
|
|
|
2020-06-17 17:21:44 +03:00
|
|
|
HashMap<String, String> m_aliases;
|
2020-08-05 08:30:01 +03:00
|
|
|
bool m_is_interactive { true };
|
2020-09-07 21:15:31 +03:00
|
|
|
bool m_is_subshell { false };
|
2020-12-07 19:07:04 +03:00
|
|
|
bool m_should_reinstall_signal_handlers { true };
|
2020-09-16 03:48:33 +03:00
|
|
|
|
2020-12-10 17:55:13 +03:00
|
|
|
ShellError m_error { ShellError::None };
|
|
|
|
String m_error_description;
|
2021-01-03 12:08:20 +03:00
|
|
|
Optional<SourcePosition> m_source_position;
|
2020-12-10 17:55:13 +03:00
|
|
|
|
2020-09-16 03:48:33 +03:00
|
|
|
bool m_should_format_live { false };
|
2020-09-23 08:36:30 +03:00
|
|
|
|
|
|
|
RefPtr<Line::Editor> m_editor;
|
2020-10-01 17:43:01 +03:00
|
|
|
|
|
|
|
bool m_default_constructed { false };
|
2020-12-01 12:25:14 +03:00
|
|
|
|
|
|
|
mutable bool m_last_continuation_state { false }; // false == not needed.
|
2021-05-11 10:12:36 +03:00
|
|
|
|
|
|
|
Optional<size_t> m_history_autosave_time;
|
2020-05-16 22:05:13 +03:00
|
|
|
};
|
|
|
|
|
2021-01-18 04:05:19 +03:00
|
|
|
[[maybe_unused]] static constexpr bool is_word_character(char c)
|
2020-05-16 22:05:13 +03:00
|
|
|
{
|
2021-01-18 04:05:19 +03:00
|
|
|
return c == '_' || (c <= 'Z' && c >= 'A') || (c <= 'z' && c >= 'a') || (c <= '9' && c >= '0');
|
2020-05-16 22:05:13 +03:00
|
|
|
}
|
2020-10-01 17:43:01 +03:00
|
|
|
|
2021-01-09 03:02:19 +03:00
|
|
|
inline size_t find_offset_into_node(const String& unescaped_text, size_t escaped_offset)
|
|
|
|
{
|
|
|
|
size_t unescaped_offset = 0;
|
|
|
|
size_t offset = 0;
|
2021-05-10 09:39:40 +03:00
|
|
|
auto do_find_offset = [&](auto& unescaped_text) {
|
|
|
|
for (auto c : unescaped_text) {
|
|
|
|
if (offset == escaped_offset)
|
|
|
|
return unescaped_offset;
|
|
|
|
|
|
|
|
switch (Shell::special_character_escape_mode(c)) {
|
|
|
|
case Shell::SpecialCharacterEscapeMode::Untouched:
|
|
|
|
break;
|
|
|
|
case Shell::SpecialCharacterEscapeMode::Escaped:
|
|
|
|
++offset; // X -> \X
|
|
|
|
break;
|
|
|
|
case Shell::SpecialCharacterEscapeMode::QuotedAsEscape:
|
|
|
|
offset += 3; // X -> "\Y"
|
|
|
|
break;
|
|
|
|
case Shell::SpecialCharacterEscapeMode::QuotedAsHex:
|
|
|
|
if (c > NumericLimits<u8>::max())
|
|
|
|
offset += 11; // X -> "\uhhhhhhhh"
|
|
|
|
else
|
|
|
|
offset += 5; // X -> "\xhh"
|
|
|
|
break;
|
|
|
|
}
|
2021-01-09 03:02:19 +03:00
|
|
|
++offset;
|
2021-05-10 09:39:40 +03:00
|
|
|
++unescaped_offset;
|
|
|
|
}
|
|
|
|
return unescaped_offset;
|
|
|
|
};
|
2021-01-09 03:02:19 +03:00
|
|
|
|
2021-05-10 09:39:40 +03:00
|
|
|
Utf8View view { unescaped_text };
|
|
|
|
if (view.validate())
|
|
|
|
return do_find_offset(view);
|
|
|
|
return do_find_offset(unescaped_text);
|
2021-01-09 03:02:19 +03:00
|
|
|
}
|
|
|
|
|
2020-10-01 17:43:01 +03:00
|
|
|
}
|