1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
#include <utils.h>
#include <exceptions.h>
#include <cstring>
extern "C"
{
#include <unistd.h>
#include <spawn.h>
}
TermSize::TermSize()
{
term_size.ws_col = 0;
term_size.ws_row = 0;
term_size.ws_xpixel = 0;
term_size.ws_ypixel = 0;
}
bool TermSize::probe_terminal_size()
{
valid = (ioctl(1, TIOCGWINSZ, &term_size) == 0);
return valid;
}
bool TermSize::is_valid() const
{
return valid;
}
uint16_t TermSize::get_size_x() const
{
return static_cast<uint16_t> (term_size.ws_col);
}
uint16_t TermSize::get_size_y() const
{
return static_cast<uint16_t> (term_size.ws_row);
}
namespace posix
{
const size_t PIPE_READ_SIDE = 0;
const size_t PIPE_WRITE_SIDE = 1;
void close_fd(int& fd) noexcept
{
if (fd != -1)
{
int rc = 0;
do
{
errno = 0;
rc = close(fd);
}
while (rc != 0 && errno == EINTR);
fd = -1;
}
}
// @throws std::bad_alloc, EventsSourceException
SpawnFileActions::SpawnFileActions()
{
actions_mgr = std::unique_ptr<posix_spawn_file_actions_t>(new posix_spawn_file_actions_t);
actions = actions_mgr.get();
if (posix_spawn_file_actions_init(actions) != 0)
{
if (errno == ENOMEM)
{
throw std::bad_alloc();
}
throw EventsSourceException();
}
}
SpawnFileActions::~SpawnFileActions() noexcept
{
posix_spawn_file_actions_destroy(actions);
actions = nullptr;
}
// @throws std::bad_alloc, EventsSourceException
SpawnAttr::SpawnAttr()
{
attr_mgr = std::unique_ptr<posix_spawnattr_t>(new posix_spawnattr_t);
attr = attr_mgr.get();
if (posix_spawnattr_init(attr) != 0)
{
if (errno == ENOMEM)
{
throw std::bad_alloc();
}
throw EventsSourceException();
}
}
SpawnAttr::~SpawnAttr() noexcept
{
posix_spawnattr_destroy(attr);
attr = nullptr;
}
// @throws std::bad_alloc
SpawnArgs::SpawnArgs(const char* const arg_list[])
{
while (arg_list[args_size] != nullptr)
{
++args_size;
}
++args_size;
try
{
args = new char*[args_size];
for (size_t idx = 0; idx < args_size - 1; ++idx)
{
// If the allocation fails, make sure there is a null pointer in this slot
args[idx] = nullptr;
size_t arg_length = std::strlen(arg_list[idx]) + 1;
args[idx] = new char[arg_length];
std::memcpy(args[idx], arg_list[idx], arg_length);
}
args[args_size - 1] = nullptr;
}
catch (std::bad_alloc&)
{
if (args != nullptr)
{
destroy();
}
throw;
}
}
SpawnArgs::~SpawnArgs() noexcept
{
destroy();
}
void SpawnArgs::destroy() noexcept
{
for (size_t idx = 0; idx < args_size && args[idx] != nullptr; ++idx)
{
delete[] args[idx];
args[idx] = nullptr;
}
delete[] args;
args = nullptr;
}
// @throws EventsSourceException
Pipe::Pipe(int (*pipe_fd)[2])
{
managed_pipe = pipe_fd;
if (pipe2(*pipe_fd, 0) != 0)
{
throw EventsSourceException();
}
}
Pipe::~Pipe() noexcept
{
if (managed_pipe != nullptr)
{
posix::close_fd((*managed_pipe)[PIPE_READ_SIDE]);
posix::close_fd((*managed_pipe)[PIPE_WRITE_SIDE]);
}
}
void Pipe::release() noexcept
{
managed_pipe = nullptr;
}
void Pipe::close_read_side() noexcept
{
if (managed_pipe != nullptr)
{
posix::close_fd((*managed_pipe)[PIPE_READ_SIDE]);
}
}
void Pipe::close_write_side() noexcept
{
if (managed_pipe != nullptr)
{
posix::close_fd((*managed_pipe)[PIPE_WRITE_SIDE]);
}
}
}
|