diff --git a/exec.cpp b/exec.cpp index 9e63c3d11..d064cdd22 100644 --- a/exec.cpp +++ b/exec.cpp @@ -571,20 +571,24 @@ void exec(parser_t &parser, job_t *j) { shared_ptr &io = j->io.at(idx); - if ((io->io_mode == IO_BUFFER) && io->is_input) + if ((io->io_mode == IO_BUFFER)) { - /* - Input redirection - create a new gobetween process to take - care of buffering, save the redirection in input_redirect - */ - process_t *fake = new process_t(); - fake->type = INTERNAL_BUFFER; - fake->pipe_write_fd = 1; - j->first_process->pipe_read_fd = io->fd; - fake->next = j->first_process; - j->first_process = fake; - input_redirect = static_cast(io.get()); - break; + CAST_INIT(io_buffer_t *, io_buffer, io.get()); + if (io_buffer->is_input) + { + /* + Input redirection - create a new gobetween process to take + care of buffering, save the redirection in input_redirect + */ + process_t *fake = new process_t(); + fake->type = INTERNAL_BUFFER; + fake->pipe_write_fd = 1; + j->first_process->pipe_read_fd = io->fd; + fake->next = j->first_process; + j->first_process = fake; + input_redirect = io_buffer; + break; + } } } @@ -615,12 +619,10 @@ void exec(parser_t &parser, job_t *j) } - shared_ptr pipe_read(new io_pipe_t(0)); - pipe_read->is_input = 1; + shared_ptr pipe_read(new io_pipe_t(0, true)); pipe_read->pipe_fd[0] = pipe_read->pipe_fd[1] = -1; - shared_ptr pipe_write(new io_pipe_t(1)); - pipe_write->is_input = 0; + shared_ptr pipe_write(new io_pipe_t(1, false)); pipe_write->pipe_fd[0] = pipe_write->pipe_fd[1] = -1; j->io.push_back(pipe_write); diff --git a/io.cpp b/io.cpp index 6c42b6a33..241f89d91 100644 --- a/io.cpp +++ b/io.cpp @@ -72,12 +72,14 @@ void io_file_t::print() const void io_pipe_t::print() const { - fprintf(stderr, "pipe {%d, %d}\n", pipe_fd[0], pipe_fd[1]); + fprintf(stderr, "pipe {%d, %d} (input: %s)\n", pipe_fd[0], pipe_fd[1], + is_input ? "yes" : "no"); } void io_buffer_t::print() const { - fprintf(stderr, "buffer %p (size %lu)\n", out_buffer_ptr(), out_buffer_size()); + fprintf(stderr, "buffer %p (input: %s, size %lu)\n", out_buffer_ptr(), + is_input ? "yes" : "no", out_buffer_size()); } void io_buffer_t::read() @@ -132,8 +134,7 @@ void io_buffer_t::read() io_buffer_t *io_buffer_t::create(bool is_input) { bool success = true; - io_buffer_t *buffer_redirect = new io_buffer_t(is_input ? 0 : 1); - buffer_redirect->is_input = is_input ? true : false; + io_buffer_t *buffer_redirect = new io_buffer_t(is_input ? 0 : 1, is_input); if (exec_pipe(buffer_redirect->pipe_fd) == -1) { @@ -221,7 +222,7 @@ void io_print(const io_chain_t &chain) for (size_t i=0; i < chain.size(); i++) { const shared_ptr &io = chain.at(i); - fprintf(stderr, "\t%lu: fd:%d, input:%s, ", (unsigned long)i, io->fd, io->is_input ? "yes" : "no"); + fprintf(stderr, "\t%lu: fd:%d, ", (unsigned long)i, io->fd); io->print(); } } diff --git a/io.h b/io.h index 084202629..c9bcd36a3 100644 --- a/io.h +++ b/io.h @@ -24,8 +24,7 @@ class io_data_t protected: io_data_t(io_mode_t m, int f) : io_mode(m), - fd(f), - is_input(0) + fd(f) { } @@ -36,10 +35,6 @@ class io_data_t int fd; virtual void print() const = 0; - - /** Set to true if this is an input io redirection */ - bool is_input; - virtual ~io_data_t() = 0; }; @@ -106,12 +101,14 @@ class io_pipe_t : public io_data_t { public: int pipe_fd[2]; + bool is_input; virtual void print() const; - io_pipe_t(int f): + io_pipe_t(int f, bool i): io_data_t(IO_PIPE, f), - pipe_fd() + pipe_fd(), + is_input(i) { } }; @@ -122,8 +119,8 @@ class io_buffer_t : public io_pipe_t /** buffer to save output in */ std::vector *out_buffer; - io_buffer_t(int f): - io_pipe_t(f), + io_buffer_t(int f, bool i): + io_pipe_t(f, i), out_buffer(new std::vector) { io_mode = IO_BUFFER; diff --git a/postfork.cpp b/postfork.cpp index 558805ab4..3fa3e8c47 100644 --- a/postfork.cpp +++ b/postfork.cpp @@ -248,7 +248,7 @@ static int handle_child_io(io_chain_t &io_chain) { CAST_INIT(io_pipe_t *, io_pipe, io); /* If write_pipe_idx is 0, it means we're connecting to the read end (first pipe fd). If it's 1, we're connecting to the write end (second pipe fd). */ - unsigned int write_pipe_idx = (io->is_input ? 0 : 1); + unsigned int write_pipe_idx = (io_pipe->is_input ? 0 : 1); /* debug( 0, L"%ls %ls on fd %d (%d %d)", @@ -488,7 +488,7 @@ bool fork_actions_make_spawn_properties(posix_spawnattr_t *attr, posix_spawn_fil case IO_PIPE: { CAST_INIT(const io_pipe_t *, io_pipe, io.get()); - unsigned int write_pipe_idx = (io->is_input ? 0 : 1); + unsigned int write_pipe_idx = (io_pipe->is_input ? 0 : 1); int from_fd = io_pipe->pipe_fd[write_pipe_idx]; int to_fd = io->fd; if (! err)