2019-01-28 21:26:22 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
|
|
|
#include "redirection.h"
|
|
|
|
|
2022-08-21 06:14:48 +00:00
|
|
|
#include <errno.h>
|
2019-01-28 21:26:22 +00:00
|
|
|
#include <fcntl.h>
|
2022-08-21 21:51:33 +00:00
|
|
|
|
2022-08-21 06:14:48 +00:00
|
|
|
#include <memory>
|
2019-01-28 21:26:22 +00:00
|
|
|
|
2019-12-13 00:44:24 +00:00
|
|
|
#include "io.h"
|
2019-10-13 22:50:48 +00:00
|
|
|
#include "wutil.h"
|
|
|
|
|
2019-01-28 21:26:22 +00:00
|
|
|
dup2_list_t::~dup2_list_t() = default;
|
|
|
|
|
2019-12-13 00:44:24 +00:00
|
|
|
maybe_t<int> redirection_spec_t::get_target_as_fd() const {
|
|
|
|
errno = 0;
|
|
|
|
int result = fish_wcstoi(target.c_str());
|
|
|
|
if (errno || result < 0) return none();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int redirection_spec_t::oflags() const {
|
|
|
|
switch (mode) {
|
|
|
|
case redirection_mode_t::append:
|
|
|
|
return O_CREAT | O_APPEND | O_WRONLY;
|
|
|
|
case redirection_mode_t::overwrite:
|
|
|
|
return O_CREAT | O_WRONLY | O_TRUNC;
|
|
|
|
case redirection_mode_t::noclob:
|
|
|
|
return O_CREAT | O_EXCL | O_WRONLY;
|
|
|
|
case redirection_mode_t::input:
|
|
|
|
return O_RDONLY;
|
|
|
|
case redirection_mode_t::fd:
|
2019-12-14 02:42:08 +00:00
|
|
|
default:
|
2019-12-13 00:44:24 +00:00
|
|
|
DIE("Not a file redirection");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 22:49:05 +00:00
|
|
|
dup2_list_t dup2_list_t::resolve_chain(const io_chain_t &io_chain) {
|
2019-01-28 21:26:22 +00:00
|
|
|
ASSERT_IS_NOT_FORKED_CHILD();
|
|
|
|
dup2_list_t result;
|
2019-12-29 23:51:22 +00:00
|
|
|
for (const auto &io : io_chain) {
|
|
|
|
if (io->source_fd < 0) {
|
|
|
|
result.add_close(io->fd);
|
|
|
|
} else {
|
|
|
|
result.add_dup2(io->source_fd, io->fd);
|
2019-01-28 21:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-29 22:49:05 +00:00
|
|
|
return result;
|
2019-01-28 21:26:22 +00:00
|
|
|
}
|
2019-02-02 20:52:51 +00:00
|
|
|
|
|
|
|
int dup2_list_t::fd_for_target_fd(int target) const {
|
|
|
|
// Paranoia.
|
|
|
|
if (target < 0) {
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
// Note we can simply walk our action list backwards, looking for src -> target dups.
|
|
|
|
int cursor = target;
|
|
|
|
for (auto iter = actions_.rbegin(); iter != actions_.rend(); ++iter) {
|
|
|
|
if (iter->target == cursor) {
|
|
|
|
// cursor is replaced by iter->src
|
|
|
|
cursor = iter->src;
|
|
|
|
} else if (iter->src == cursor && iter->target < 0) {
|
|
|
|
// cursor is closed.
|
|
|
|
cursor = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cursor;
|
|
|
|
}
|