mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-06-06 08:51:14 -03:00
io_data_t to store the source_fd directly
Now that all io_data_ts know their source fd, just store it directly in the base class. This will simplify some uses of io_data_t.
This commit is contained in:
@@ -392,8 +392,8 @@ static bool exec_internal_builtin_proc(parser_t &parser, const std::shared_ptr<j
|
|||||||
// which is internal to fish. We still respect this redirection in
|
// which is internal to fish. We still respect this redirection in
|
||||||
// that we pass it on as a block IO to the code that source runs,
|
// that we pass it on as a block IO to the code that source runs,
|
||||||
// and therefore this is not an error.
|
// and therefore this is not an error.
|
||||||
if (in_fd->old_fd >= 0 && in_fd->old_fd < 3) {
|
if (in_fd->source_fd >= 0 && in_fd->source_fd < 3) {
|
||||||
local_builtin_stdin = in_fd->old_fd;
|
local_builtin_stdin = in_fd->source_fd;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,13 +30,13 @@
|
|||||||
io_data_t::~io_data_t() = default;
|
io_data_t::~io_data_t() = default;
|
||||||
|
|
||||||
io_file_t::io_file_t(int f, autoclose_fd_t file)
|
io_file_t::io_file_t(int f, autoclose_fd_t file)
|
||||||
: io_data_t(io_mode_t::file, f), file_fd_(std::move(file)) {
|
: io_data_t(io_mode_t::file, f, file_fd_.fd()), file_fd_(std::move(file)) {
|
||||||
assert(file_fd_.valid() && "File is not valid");
|
assert(file_fd_.valid() && "File is not valid");
|
||||||
}
|
}
|
||||||
|
|
||||||
void io_close_t::print() const { std::fwprintf(stderr, L"close %d\n", fd); }
|
void io_close_t::print() const { std::fwprintf(stderr, L"close %d\n", fd); }
|
||||||
|
|
||||||
void io_fd_t::print() const { std::fwprintf(stderr, L"FD map %d -> %d\n", old_fd, fd); }
|
void io_fd_t::print() const { std::fwprintf(stderr, L"FD map %d -> %d\n", source_fd, fd); }
|
||||||
|
|
||||||
void io_file_t::print() const { std::fwprintf(stderr, L"file (%d)\n", file_fd_.fd()); }
|
void io_file_t::print() const { std::fwprintf(stderr, L"file (%d)\n", file_fd_.fd()); }
|
||||||
|
|
||||||
|
|||||||
27
src/io.h
27
src/io.h
@@ -169,27 +169,31 @@ enum class io_mode_t { file, pipe, fd, close, bufferfill };
|
|||||||
|
|
||||||
/// Represents an FD redirection.
|
/// Represents an FD redirection.
|
||||||
class io_data_t {
|
class io_data_t {
|
||||||
private:
|
|
||||||
// No assignment or copying allowed.
|
// No assignment or copying allowed.
|
||||||
io_data_t(const io_data_t &rhs);
|
io_data_t(const io_data_t &rhs) = delete;
|
||||||
void operator=(const io_data_t &rhs);
|
void operator=(const io_data_t &rhs) = delete;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
io_data_t(io_mode_t m, int f) : io_mode(m), fd(f) {}
|
io_data_t(io_mode_t m, int fd, int source_fd) : io_mode(m), fd(fd), source_fd(source_fd) {}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/// Type of redirect.
|
/// Type of redirect.
|
||||||
const io_mode_t io_mode;
|
const io_mode_t io_mode;
|
||||||
|
|
||||||
/// FD to redirect.
|
/// FD to redirect.
|
||||||
const int fd;
|
const int fd;
|
||||||
|
|
||||||
|
/// Source fd. This is dup2'd to fd, or if it is -1, then fd is closed.
|
||||||
|
/// That is, we call dup2(source_fd, fd).
|
||||||
|
const int source_fd;
|
||||||
|
|
||||||
virtual void print() const = 0;
|
virtual void print() const = 0;
|
||||||
virtual ~io_data_t() = 0;
|
virtual ~io_data_t() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class io_close_t : public io_data_t {
|
class io_close_t : public io_data_t {
|
||||||
public:
|
public:
|
||||||
explicit io_close_t(int f) : io_data_t(io_mode_t::close, f) {}
|
explicit io_close_t(int f) : io_data_t(io_mode_t::close, f, -1) {}
|
||||||
|
|
||||||
void print() const override;
|
void print() const override;
|
||||||
~io_close_t() override;
|
~io_close_t() override;
|
||||||
@@ -197,14 +201,13 @@ class io_close_t : public io_data_t {
|
|||||||
|
|
||||||
class io_fd_t : public io_data_t {
|
class io_fd_t : public io_data_t {
|
||||||
public:
|
public:
|
||||||
/// fd to redirect specified fd to. For example, in 2>&1, old_fd is 1, and io_data_t::fd is 2.
|
|
||||||
const int old_fd;
|
|
||||||
|
|
||||||
void print() const override;
|
void print() const override;
|
||||||
|
|
||||||
~io_fd_t() override;
|
~io_fd_t() override;
|
||||||
|
|
||||||
io_fd_t(int f, int old) : io_data_t(io_mode_t::fd, f), old_fd(old) {}
|
/// fd to redirect specified fd to. For example, in 2>&1, source_fd is 1, and io_data_t::fd
|
||||||
|
/// is 2.
|
||||||
|
io_fd_t(int f, int source_fd) : io_data_t(io_mode_t::fd, f, source_fd) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Represents a redirection to or from an opened file.
|
/// Represents a redirection to or from an opened file.
|
||||||
@@ -235,7 +238,9 @@ class io_pipe_t : public io_data_t {
|
|||||||
void print() const override;
|
void print() const override;
|
||||||
|
|
||||||
io_pipe_t(int fd, bool is_input, autoclose_fd_t pipe_fd)
|
io_pipe_t(int fd, bool is_input, autoclose_fd_t pipe_fd)
|
||||||
: io_data_t(io_mode_t::pipe, fd), pipe_fd_(std::move(pipe_fd)), is_input_(is_input) {}
|
: io_data_t(io_mode_t::pipe, fd, pipe_fd.fd()),
|
||||||
|
pipe_fd_(std::move(pipe_fd)),
|
||||||
|
is_input_(is_input) {}
|
||||||
|
|
||||||
~io_pipe_t() override;
|
~io_pipe_t() override;
|
||||||
|
|
||||||
@@ -260,7 +265,7 @@ class io_bufferfill_t : public io_data_t {
|
|||||||
// The ctor is public to support make_shared() in the static create function below.
|
// The ctor is public to support make_shared() in the static create function below.
|
||||||
// Do not invoke this directly.
|
// Do not invoke this directly.
|
||||||
io_bufferfill_t(autoclose_fd_t write_fd, std::shared_ptr<io_buffer_t> buffer)
|
io_bufferfill_t(autoclose_fd_t write_fd, std::shared_ptr<io_buffer_t> buffer)
|
||||||
: io_data_t(io_mode_t::bufferfill, STDOUT_FILENO),
|
: io_data_t(io_mode_t::bufferfill, STDOUT_FILENO, write_fd.fd()),
|
||||||
write_fd_(std::move(write_fd)),
|
write_fd_(std::move(write_fd)),
|
||||||
buffer_(std::move(buffer)) {}
|
buffer_(std::move(buffer)) {}
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ dup2_list_t dup2_list_t::resolve_chain(const io_chain_t &io_chain) {
|
|||||||
|
|
||||||
case io_mode_t::fd: {
|
case io_mode_t::fd: {
|
||||||
const io_fd_t *io = static_cast<const io_fd_t *>(io_ref.get());
|
const io_fd_t *io = static_cast<const io_fd_t *>(io_ref.get());
|
||||||
result.add_dup2(io->old_fd, io->fd);
|
result.add_dup2(io->source_fd, io->fd);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user