diff options
-rw-r--r-- | Shell/AST.cpp | 14 | ||||
-rw-r--r-- | Shell/AST.h | 29 | ||||
-rw-r--r-- | Shell/Builtin.cpp | 2 | ||||
-rw-r--r-- | Shell/Shell.cpp | 7 | ||||
-rw-r--r-- | Shell/main.cpp | 2 |
5 files changed, 32 insertions, 22 deletions
diff --git a/Shell/AST.cpp b/Shell/AST.cpp index 8639bdc90b..5943f33663 100644 --- a/Shell/AST.cpp +++ b/Shell/AST.cpp @@ -546,7 +546,7 @@ void CloseFdRedirection::dump(int level) const RefPtr<Value> CloseFdRedirection::run(RefPtr<Shell>) { Command command; - command.redirections.append(*new CloseRedirection(m_fd)); + command.redirections.append(adopt(*new CloseRedirection(m_fd))); return create<CommandValue>(move(command)); } @@ -1122,8 +1122,8 @@ RefPtr<Value> Pipe::run(RefPtr<Shell> shell) auto pipe_write_end = new FdRedirection(STDIN_FILENO, -1, Rewiring::Close::Destination); auto pipe_read_end = new FdRedirection(STDOUT_FILENO, -1, pipe_write_end, Rewiring::Close::RefreshDestination); - first_in_right.redirections.append(*pipe_write_end); - last_in_left.redirections.append(*pipe_read_end); + first_in_right.redirections.append(adopt(*pipe_write_end)); + last_in_left.redirections.append(adopt(*pipe_read_end)); last_in_left.should_wait = false; last_in_left.is_pipe_source = true; @@ -1238,7 +1238,7 @@ RefPtr<Value> ReadRedirection::run(RefPtr<Shell> shell) StringBuilder builder; builder.join(" ", path_segments); - command.redirections.append(*new PathRedirection(builder.to_string(), m_fd, PathRedirection::Read)); + command.redirections.append(adopt(*new PathRedirection(builder.to_string(), m_fd, PathRedirection::Read))); return create<CommandValue>(move(command)); } @@ -1265,7 +1265,7 @@ RefPtr<Value> ReadWriteRedirection::run(RefPtr<Shell> shell) StringBuilder builder; builder.join(" ", path_segments); - command.redirections.append(*new PathRedirection(builder.to_string(), m_fd, PathRedirection::ReadWrite)); + command.redirections.append(adopt(*new PathRedirection(builder.to_string(), m_fd, PathRedirection::ReadWrite))); return create<CommandValue>(move(command)); } @@ -1758,7 +1758,7 @@ RefPtr<Value> WriteAppendRedirection::run(RefPtr<Shell> shell) StringBuilder builder; builder.join(" ", path_segments); - command.redirections.append(*new PathRedirection(builder.to_string(), m_fd, PathRedirection::WriteAppend)); + command.redirections.append(adopt(*new PathRedirection(builder.to_string(), m_fd, PathRedirection::WriteAppend))); return create<CommandValue>(move(command)); } @@ -1785,7 +1785,7 @@ RefPtr<Value> WriteRedirection::run(RefPtr<Shell> shell) StringBuilder builder; builder.join(" ", path_segments); - command.redirections.append(*new PathRedirection(builder.to_string(), m_fd, PathRedirection::Write)); + command.redirections.append(adopt(*new PathRedirection(builder.to_string(), m_fd, PathRedirection::Write))); return create<CommandValue>(move(command)); } diff --git a/Shell/AST.h b/Shell/AST.h index 531e560bf2..d70ba1218b 100644 --- a/Shell/AST.h +++ b/Shell/AST.h @@ -48,10 +48,11 @@ struct Position { bool contains(size_t offset) const { return start_offset <= offset && offset <= end_offset; } }; +struct FdRedirection; struct Rewiring : public RefCounted<Rewiring> { int source_fd { -1 }; int dest_fd { -1 }; - Rewiring* other_pipe_end { nullptr }; + FdRedirection* other_pipe_end { nullptr }; enum class Close { None, Source, @@ -67,7 +68,7 @@ struct Rewiring : public RefCounted<Rewiring> { { } - Rewiring(int source, int dest, Rewiring* other_end, Close close) + Rewiring(int source, int dest, FdRedirection* other_end, Close close) : source_fd(source) , dest_fd(dest) , other_pipe_end(other_end) @@ -121,20 +122,30 @@ private: virtual bool is_path_redirection() const override { return true; } }; -struct FdRedirection : public Redirection - , public Rewiring { - - virtual Result<RefPtr<Rewiring>, String> apply() const override { return static_cast<RefPtr<Rewiring>>(this); } +struct FdRedirection : public Redirection { + virtual Result<RefPtr<Rewiring>, String> apply() const override + { + return RefPtr<Rewiring>(adopt(*new Rewiring(source_fd, dest_fd, other_pipe_end, action))); + } virtual ~FdRedirection(); FdRedirection(int source, int dest, Rewiring::Close close) - : Rewiring(source, dest, close) + : FdRedirection(source, dest, nullptr, close) { } - FdRedirection(int source, int dest, Rewiring* pipe_end, Rewiring::Close close) - : Rewiring(source, dest, pipe_end, close) + + FdRedirection(int source, int dest, FdRedirection* pipe_end, Rewiring::Close close) + : source_fd(source) + , dest_fd(dest) + , other_pipe_end(pipe_end) + , action(close) { } + int source_fd { -1 }; + int dest_fd { -1 }; + FdRedirection* other_pipe_end { nullptr }; + Rewiring::Close action { Rewiring::Close::None }; + private: virtual bool is_fd_redirection() const override { return true; } }; diff --git a/Shell/Builtin.cpp b/Shell/Builtin.cpp index a113094720..c3184eefa4 100644 --- a/Shell/Builtin.cpp +++ b/Shell/Builtin.cpp @@ -698,7 +698,7 @@ int Shell::builtin_shift(int argc, const char** argv) } if (!argv_->is_list()) - argv_ = *new AST::ListValue({ argv_ }); + argv_ = adopt(*new AST::ListValue({ argv_ })); auto& values = static_cast<AST::ListValue*>(argv_.ptr())->values(); if ((size_t)count > values.size()) { diff --git a/Shell/Shell.cpp b/Shell/Shell.cpp index 25ee4b5fd2..cf6c66a9f7 100644 --- a/Shell/Shell.cpp +++ b/Shell/Shell.cpp @@ -287,8 +287,8 @@ Vector<AST::Command> Shell::expand_aliases(Vector<AST::Command> initial_commands auto* ast = static_cast<AST::Execute*>(subcommand_ast.ptr()); subcommand_ast = ast->command(); } - AST::Node& substitute = *new AST::Join(subcommand_ast->position(), subcommand_ast, *new AST::CommandLiteral(subcommand_ast->position(), command)); - for (auto& subst_command : substitute.run(*this)->resolve_as_commands(*this)) { + RefPtr<AST::Node> substitute = adopt(*new AST::Join(subcommand_ast->position(), subcommand_ast, adopt(*new AST::CommandLiteral(subcommand_ast->position(), command)))); + for (auto& subst_command : substitute->run(*this)->resolve_as_commands(*this)) { if (!subst_command.argv.is_empty() && subst_command.argv.first() == argv0) // Disallow an alias resolving to itself. commands.append(subst_command); else @@ -596,8 +596,7 @@ Vector<RefPtr<Job>> Shell::run_commands(Vector<AST::Command>& commands) auto path_redir = (const AST::PathRedirection*)redir.ptr(); dbg() << "redir path " << (int)path_redir->direction << " " << path_redir->path << " <-> " << path_redir->fd; } else if (redir->is_fd_redirection()) { - auto fd_redir = (const AST::FdRedirection*)redir.ptr(); - dbg() << "redir fd " << fd_redir->source_fd << " -> " << fd_redir->dest_fd; + dbg() << "redir fd " << redir->source_fd << " -> " << redir->dest_fd; } else if (redir->is_close_redirection()) { auto close_redir = (const AST::CloseRedirection*)redir.ptr(); dbg() << "close fd " << close_redir->fd; diff --git a/Shell/main.cpp b/Shell/main.cpp index d191474d91..be79d761d5 100644 --- a/Shell/main.cpp +++ b/Shell/main.cpp @@ -187,7 +187,7 @@ int main(int argc, char** argv) Vector<String> args; for (auto* arg : script_args) args.empend(arg); - shell->set_local_variable("ARGV", *new AST::ListValue(move(args))); + shell->set_local_variable("ARGV", adopt(*new AST::ListValue(move(args)))); } if (command_to_run) { |