diff --git a/Shell/AST.cpp b/Shell/AST.cpp index 8639bdc90be..5943f33663b 100644 --- a/Shell/AST.cpp +++ b/Shell/AST.cpp @@ -546,7 +546,7 @@ void CloseFdRedirection::dump(int level) const RefPtr CloseFdRedirection::run(RefPtr) { Command command; - command.redirections.append(*new CloseRedirection(m_fd)); + command.redirections.append(adopt(*new CloseRedirection(m_fd))); return create(move(command)); } @@ -1122,8 +1122,8 @@ RefPtr Pipe::run(RefPtr 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 ReadRedirection::run(RefPtr 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(move(command)); } @@ -1265,7 +1265,7 @@ RefPtr ReadWriteRedirection::run(RefPtr 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(move(command)); } @@ -1758,7 +1758,7 @@ RefPtr WriteAppendRedirection::run(RefPtr 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(move(command)); } @@ -1785,7 +1785,7 @@ RefPtr WriteRedirection::run(RefPtr 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(move(command)); } diff --git a/Shell/AST.h b/Shell/AST.h index 531e560bf27..d70ba1218b1 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 { 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(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, String> apply() const override { return static_cast>(this); } +struct FdRedirection : public Redirection { + virtual Result, String> apply() const override + { + return RefPtr(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 a1130947204..c3184eefa4e 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(argv_.ptr())->values(); if ((size_t)count > values.size()) { diff --git a/Shell/Shell.cpp b/Shell/Shell.cpp index 25ee4b5fd2d..cf6c66a9f70 100644 --- a/Shell/Shell.cpp +++ b/Shell/Shell.cpp @@ -287,8 +287,8 @@ Vector Shell::expand_aliases(Vector initial_commands auto* ast = static_cast(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 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> Shell::run_commands(Vector& 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 d191474d911..be79d761d5c 100644 --- a/Shell/main.cpp +++ b/Shell/main.cpp @@ -187,7 +187,7 @@ int main(int argc, char** argv) Vector 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) {