fish-shell/src/redirection.h
ridiculousfish 88dc484858 Introduce dup2_list_t
This represents a "resolved" io_chain_t, where all of the different io_data_t
types have been reduced to a sequence of dup2() and close(). This will
eliminate a lot of the logic duplication around posix_spawn vs fork, and pave
the way for in-process redirections.
2019-02-02 14:21:46 -08:00

63 lines
1.7 KiB
C++

#ifndef FISH_REDIRECTION_H
#define FISH_REDIRECTION_H
#include "common.h"
#include "maybe.h"
#include "io.h"
#include <vector>
/// This file supports "applying" redirections.
/// A class representing a sequence of basic redirections.
class dup2_list_t {
/// A type that represents the action dup2(src, target).
/// If target is negative, this represents close(src).
/// Note none of the fds here are considered 'owned'.
struct action_t {
int src;
int target;
};
/// The list of actions.
std::vector<action_t> actions_;
/// The list of fds that we opened, and are responsible for closing.
std::vector<autoclose_fd_t> opened_fds_;
/// Append a dup2 action.
void add_dup2(int src, int target) {
assert(src >= 0 && target >= 0 && "Invalid fd in add_dup2");
if (src != target) {
actions_.push_back(action_t{src, target});
}
}
/// Append a close action.
void add_close(int fd) {
assert(fd >= 0 && "Invalid fd in add_close");
actions_.push_back(action_t{fd, -1});
}
dup2_list_t() = default;
public:
~dup2_list_t();
/// Disable copying because we own our fds.
dup2_list_t(const dup2_list_t &) = delete;
void operator=(const dup2_list_t &) = delete;
dup2_list_t(dup2_list_t &&) = default;
dup2_list_t &operator=(dup2_list_t &&) = default;
/// \return the list of dup2 actions.
const std::vector<action_t> &get_actions() const { return actions_; }
/// Produce a dup_fd_list_t from an io_chain. This may not be called before fork().
/// The result contains the list of fd actions (dup2 and close), as well as the list of fds opened.
static maybe_t<dup2_list_t> resolve_chain(const io_chain_t &);
};
#endif