mirror of
https://github.com/arun11299/cpp-subprocess.git
synced 2025-08-05 12:56:23 -04:00
Replace tabs with spaces
Coding style uses indent with spaces, but there were a few lost tabs that had to be replaced.
This commit is contained in:
parent
c8577e3d83
commit
78651742fb
203
subprocess.hpp
203
subprocess.hpp
@ -141,9 +141,9 @@ namespace util
|
||||
* Function: split
|
||||
* Parameters:
|
||||
* [in] str : Input string which needs to be split based upon the
|
||||
* delimiters provided.
|
||||
* delimiters provided.
|
||||
* [in] deleims : Delimiter characters based upon which the string needs
|
||||
* to be split. Default constructed to ' '(space) and '\t'(tab)
|
||||
* to be split. Default constructed to ' '(space) and '\t'(tab)
|
||||
* [out] vector<string> : Vector of strings split at deleimiter.
|
||||
*/
|
||||
static inline std::vector<std::string>
|
||||
@ -155,8 +155,8 @@ namespace util
|
||||
while (true) {
|
||||
auto pos = str.find_first_of(delims, init);
|
||||
if (pos == std::string::npos) {
|
||||
res.emplace_back(str.substr(init, str.length()));
|
||||
break;
|
||||
res.emplace_back(str.substr(init, str.length()));
|
||||
break;
|
||||
}
|
||||
res.emplace_back(str.substr(init, pos - init));
|
||||
pos++;
|
||||
@ -171,15 +171,14 @@ namespace util
|
||||
* Function: join
|
||||
* Parameters:
|
||||
* [in] vec : Vector of strings which needs to be joined to form
|
||||
* a single string with words seperated by
|
||||
* a seperator char.
|
||||
* a single string with words seperated by a seperator char.
|
||||
* [in] sep : String used to seperate 2 words in the joined string.
|
||||
* Default constructed to ' ' (space).
|
||||
* Default constructed to ' ' (space).
|
||||
* [out] string: Joined string.
|
||||
*/
|
||||
static
|
||||
std::string join(const std::vector<std::string>& vec,
|
||||
const std::string& sep = " ")
|
||||
const std::string& sep = " ")
|
||||
{
|
||||
std::string res;
|
||||
for (auto& elem : vec) res.append(elem + sep);
|
||||
@ -195,7 +194,7 @@ namespace util
|
||||
* Parameters:
|
||||
* [in] fd : The descriptor on which FD_CLOEXEC needs to be set/reset.
|
||||
* [in] set : If 'true', set FD_CLOEXEC.
|
||||
* If 'false' unset FD_CLOEXEC.
|
||||
* If 'false' unset FD_CLOEXEC.
|
||||
*/
|
||||
static
|
||||
void set_clo_on_exec(int fd, bool set = true)
|
||||
@ -214,8 +213,8 @@ namespace util
|
||||
* read and write descriptors of the pipe.
|
||||
* Parameters:
|
||||
* [out] : A pair of file descriptors.
|
||||
* First element of pair is the read descriptor of pipe.
|
||||
* Second element is the write descriptor of pipe.
|
||||
* First element of pair is the read descriptor of pipe.
|
||||
* Second element is the write descriptor of pipe.
|
||||
*/
|
||||
static
|
||||
std::pair<int, int> pipe_cloexec() throw (OSError)
|
||||
@ -241,7 +240,7 @@ namespace util
|
||||
* [in] fd : The file descriptotr to write to.
|
||||
* [in] buf: Buffer from which data needs to be written to fd.
|
||||
* [in] length: The number of bytes that needs to be written from
|
||||
* `buf` to `fd`.
|
||||
* `buf` to `fd`.
|
||||
* [out] int : Number of bytes written or -1 in case of failure.
|
||||
*/
|
||||
static
|
||||
@ -266,7 +265,7 @@ namespace util
|
||||
* [in] buf : The buffer into which it needs to write the data.
|
||||
* [in] read_upto: Max number of bytes which must be read from `fd`.
|
||||
* [out] int : Number of bytes written to `buf` or read from `fd`
|
||||
* OR -1 in case of error.
|
||||
* OR -1 in case of error.
|
||||
* NOTE: In case of EINTR while reading from socket, this API
|
||||
* will retry to read from `fd`, but only till the EINTR counter
|
||||
* reaches 50 after which it will return with whatever data it read.
|
||||
@ -280,12 +279,12 @@ namespace util
|
||||
while (1) {
|
||||
int read_bytes = read(fd, buf, read_upto);
|
||||
if (read_bytes == -1) {
|
||||
if (errno == EINTR) {
|
||||
if (eintr_cnter >= 50) return -1;
|
||||
eintr_cnter++;
|
||||
continue;
|
||||
}
|
||||
return -1;
|
||||
if (errno == EINTR) {
|
||||
if (eintr_cnter >= 50) return -1;
|
||||
eintr_cnter++;
|
||||
continue;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
if (read_bytes == 0) return rbytes;
|
||||
|
||||
@ -302,7 +301,7 @@ namespace util
|
||||
* Parameters:
|
||||
* [in] fd : The file descriptor from which to read from.
|
||||
* [in] buf : The buffer of type `class Buffer` into which
|
||||
* the read data is written to.
|
||||
* the read data is written to.
|
||||
* [out] int: Number of bytes read OR -1 in case of failure.
|
||||
*
|
||||
* NOTE: `class Buffer` is a exposed public class. See below.
|
||||
@ -319,18 +318,18 @@ namespace util
|
||||
while (1) {
|
||||
int rd_bytes = read_atmost_n(fd, buffer, buf.size());
|
||||
if (rd_bytes == increment) {
|
||||
// Resize the buffer to accomodate more
|
||||
orig_size = orig_size * 1.5;
|
||||
increment = orig_size - buf.size();
|
||||
buf.resize(orig_size);
|
||||
buffer += rd_bytes;
|
||||
total_bytes_read += rd_bytes;
|
||||
} else if (rd_bytes != -1){
|
||||
total_bytes_read += rd_bytes;
|
||||
break;
|
||||
// Resize the buffer to accomodate more
|
||||
orig_size = orig_size * 1.5;
|
||||
increment = orig_size - buf.size();
|
||||
buf.resize(orig_size);
|
||||
buffer += rd_bytes;
|
||||
total_bytes_read += rd_bytes;
|
||||
} else if (rd_bytes != -1) {
|
||||
total_bytes_read += rd_bytes;
|
||||
break;
|
||||
} else {
|
||||
if (total_bytes_read == 0) return -1;
|
||||
break;
|
||||
if (total_bytes_read == 0) return -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return total_bytes_read;
|
||||
@ -344,10 +343,10 @@ namespace util
|
||||
* Parameters:
|
||||
* [in] pid : The pid of the process.
|
||||
* [out] pair<int, int>:
|
||||
* pair.first : Return code of the waitpid call.
|
||||
* pair.second : Exit status of the process.
|
||||
* pair.first : Return code of the waitpid call.
|
||||
* pair.second : Exit status of the process.
|
||||
*
|
||||
* NOTE: This is a blocking call as in, it will loop
|
||||
* NOTE: This is a blocking call as in, it will loop
|
||||
* till the child is exited.
|
||||
*/
|
||||
static
|
||||
@ -919,13 +918,13 @@ private:
|
||||
* 7. kill(sig_num) - Kill the child. SIGTERM used by default.
|
||||
* 8. send(...) - Send input to the input channel of the child.
|
||||
* 9. communicate(...) - Get the output/error from the child and close the channels
|
||||
* from the parent side.
|
||||
* from the parent side.
|
||||
*10. input() - Get the input channel/File pointer. Can be used for
|
||||
* cutomizing the way of sending input to child.
|
||||
* cutomizing the way of sending input to child.
|
||||
*11. output() - Get the output channel/File pointer. Usually used
|
||||
in case of redirection. See piping examples.
|
||||
in case of redirection. See piping examples.
|
||||
*12. error() - Get the error channel/File poiner. Usually used
|
||||
in case of redirection.
|
||||
in case of redirection.
|
||||
*13. start_process() - Start the child process. Only to be used when
|
||||
* `defer_spawn` option was provided in Popen constructor.
|
||||
*/
|
||||
@ -1187,19 +1186,19 @@ void Popen::execute_process() throw (CalledProcessError, OSError)
|
||||
char err_buf[SP_MAX_ERR_BUF_SIZ] = {0,};
|
||||
|
||||
int read_bytes = util::read_atmost_n(
|
||||
err_rd_pipe,
|
||||
err_buf,
|
||||
SP_MAX_ERR_BUF_SIZ);
|
||||
err_rd_pipe,
|
||||
err_buf,
|
||||
SP_MAX_ERR_BUF_SIZ);
|
||||
close(err_rd_pipe);
|
||||
|
||||
if (read_bytes || strlen(err_buf)) {
|
||||
// Call waitpid to reap the child process
|
||||
// waitpid suspends the calling process until the
|
||||
// child terminates.
|
||||
wait();
|
||||
// Call waitpid to reap the child process
|
||||
// waitpid suspends the calling process until the
|
||||
// child terminates.
|
||||
wait();
|
||||
|
||||
// Throw whatever information we have about child failure
|
||||
throw CalledProcessError(err_buf);
|
||||
// Throw whatever information we have about child failure
|
||||
throw CalledProcessError(err_buf);
|
||||
}
|
||||
} catch (std::exception& exp) {
|
||||
stream_.cleanup_fds();
|
||||
@ -1277,10 +1276,10 @@ namespace detail {
|
||||
|
||||
try {
|
||||
if (stream.write_to_parent_ == 0)
|
||||
stream.write_to_parent_ = dup(stream.write_to_parent_);
|
||||
stream.write_to_parent_ = dup(stream.write_to_parent_);
|
||||
|
||||
if (stream.err_write_ == 0 || stream.err_write_ == 1)
|
||||
stream.err_write_ = dup(stream.err_write_);
|
||||
stream.err_write_ = dup(stream.err_write_);
|
||||
|
||||
// Make the child owned descriptors as the
|
||||
// stdin, stdout and stderr for the child process
|
||||
@ -1307,13 +1306,13 @@ namespace detail {
|
||||
|
||||
// Close the duped descriptors
|
||||
if (stream.read_from_parent_ != -1 && stream.read_from_parent_ > 2)
|
||||
close(stream.read_from_parent_);
|
||||
close(stream.read_from_parent_);
|
||||
|
||||
if (stream.write_to_parent_ != -1 && stream.write_to_parent_ > 2)
|
||||
close(stream.write_to_parent_);
|
||||
close(stream.write_to_parent_);
|
||||
|
||||
if (stream.err_write_ != -1 && stream.err_write_ > 2)
|
||||
close(stream.err_write_);
|
||||
close(stream.err_write_);
|
||||
|
||||
// Close all the inherited fd's except the error write pipe
|
||||
if (parent_->close_fds_) {
|
||||
@ -1333,19 +1332,19 @@ namespace detail {
|
||||
}
|
||||
|
||||
if (parent_->has_preexec_fn_) {
|
||||
parent_->preexec_fn_();
|
||||
parent_->preexec_fn_();
|
||||
}
|
||||
|
||||
if (parent_->session_leader_) {
|
||||
sys_ret = setsid();
|
||||
if (sys_ret == -1) throw OSError("setsid failed", errno);
|
||||
sys_ret = setsid();
|
||||
if (sys_ret == -1) throw OSError("setsid failed", errno);
|
||||
}
|
||||
|
||||
// Replace the current image with the executable
|
||||
if (parent_->env_.size()) {
|
||||
for (auto& kv : parent_->env_) {
|
||||
setenv(kv.first.c_str(), kv.second.c_str(), 1);
|
||||
}
|
||||
}
|
||||
sys_ret = execvp(parent_->exe_name_.c_str(), parent_->cargv_.data());
|
||||
} else {
|
||||
sys_ret = execvp(parent_->exe_name_.c_str(), parent_->cargv_.data());
|
||||
@ -1380,13 +1379,13 @@ namespace detail {
|
||||
if (h == nullptr) continue;
|
||||
switch (bufsiz_) {
|
||||
case 0:
|
||||
setvbuf(h, nullptr, _IONBF, BUFSIZ);
|
||||
break;
|
||||
setvbuf(h, nullptr, _IONBF, BUFSIZ);
|
||||
break;
|
||||
case 1:
|
||||
setvbuf(h, nullptr, _IONBF, BUFSIZ);
|
||||
break;
|
||||
setvbuf(h, nullptr, _IONBF, BUFSIZ);
|
||||
break;
|
||||
default:
|
||||
setvbuf(h, nullptr, _IOFBF, bufsiz_);
|
||||
setvbuf(h, nullptr, _IOFBF, bufsiz_);
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -1415,50 +1414,50 @@ namespace detail {
|
||||
OutBuffer obuf;
|
||||
ErrBuffer ebuf;
|
||||
if (stream_->input()) {
|
||||
if (msg) {
|
||||
int wbytes = std::fwrite(msg, sizeof(char), length, stream_->input());
|
||||
if (wbytes < length) {
|
||||
if (errno != EPIPE && errno != EINVAL) {
|
||||
throw OSError("fwrite error", errno);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Close the input stream
|
||||
stream_->input_.reset();
|
||||
if (msg) {
|
||||
int wbytes = std::fwrite(msg, sizeof(char), length, stream_->input());
|
||||
if (wbytes < length) {
|
||||
if (errno != EPIPE && errno != EINVAL) {
|
||||
throw OSError("fwrite error", errno);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Close the input stream
|
||||
stream_->input_.reset();
|
||||
} else if (stream_->output()) {
|
||||
// Read till EOF
|
||||
// ATTN: This could be blocking, if the process
|
||||
// at the other end screws up, we get screwed up as well
|
||||
obuf.add_cap(out_buf_cap_);
|
||||
// Read till EOF
|
||||
// ATTN: This could be blocking, if the process
|
||||
// at the other end screws up, we get screwed up as well
|
||||
obuf.add_cap(out_buf_cap_);
|
||||
|
||||
int rbytes = util::read_all(
|
||||
fileno(stream_->output()),
|
||||
obuf.buf);
|
||||
int rbytes = util::read_all(
|
||||
fileno(stream_->output()),
|
||||
obuf.buf);
|
||||
|
||||
if (rbytes == -1) {
|
||||
throw OSError("read to obuf failed", errno);
|
||||
}
|
||||
if (rbytes == -1) {
|
||||
throw OSError("read to obuf failed", errno);
|
||||
}
|
||||
|
||||
obuf.length = rbytes;
|
||||
// Close the output stream
|
||||
stream_->output_.reset();
|
||||
obuf.length = rbytes;
|
||||
// Close the output stream
|
||||
stream_->output_.reset();
|
||||
|
||||
} else if (stream_->error()) {
|
||||
// Same screwness applies here as well
|
||||
ebuf.add_cap(err_buf_cap_);
|
||||
// Same screwness applies here as well
|
||||
ebuf.add_cap(err_buf_cap_);
|
||||
|
||||
int rbytes = util::read_atmost_n(
|
||||
fileno(stream_->error()),
|
||||
ebuf.buf.data(),
|
||||
ebuf.buf.size());
|
||||
int rbytes = util::read_atmost_n(
|
||||
fileno(stream_->error()),
|
||||
ebuf.buf.data(),
|
||||
ebuf.buf.size());
|
||||
|
||||
if (rbytes == -1) {
|
||||
throw OSError("read to ebuf failed", errno);
|
||||
}
|
||||
if (rbytes == -1) {
|
||||
throw OSError("read to ebuf failed", errno);
|
||||
}
|
||||
|
||||
ebuf.length = rbytes;
|
||||
// Close the error stream
|
||||
stream_->error_.reset();
|
||||
ebuf.length = rbytes;
|
||||
// Close the error stream
|
||||
stream_->error_.reset();
|
||||
}
|
||||
return std::make_pair(std::move(obuf), std::move(ebuf));
|
||||
}
|
||||
@ -1492,12 +1491,12 @@ namespace detail {
|
||||
}
|
||||
if (stream_->input()) {
|
||||
if (msg) {
|
||||
int wbytes = std::fwrite(msg, sizeof(char), length, stream_->input());
|
||||
if (wbytes < length) {
|
||||
if (errno != EPIPE && errno != EINVAL) {
|
||||
throw OSError("fwrite error", errno);
|
||||
}
|
||||
}
|
||||
int wbytes = std::fwrite(msg, sizeof(char), length, stream_->input());
|
||||
if (wbytes < length) {
|
||||
if (errno != EPIPE && errno != EINVAL) {
|
||||
throw OSError("fwrite error", errno);
|
||||
}
|
||||
}
|
||||
}
|
||||
stream_->input_.reset();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user