rename process_pipe
This commit is contained in:
parent
61fcfeee2e
commit
ce1b94624a
|
@ -80,23 +80,23 @@ struct process_error: std::system_error {
|
||||||
using std::system_error::system_error;
|
using std::system_error::system_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class process_pipe {
|
enum class process_stream {
|
||||||
DEFAULT = 0,
|
DEFAULT = 0,
|
||||||
PIPE,
|
PIPE,
|
||||||
STDOUT
|
STDOUT
|
||||||
};
|
};
|
||||||
|
|
||||||
struct OSTD_EXPORT process_info {
|
struct OSTD_EXPORT process_info {
|
||||||
process_pipe use_in = process_pipe::DEFAULT;
|
process_stream use_in = process_stream::DEFAULT;
|
||||||
process_pipe use_out = process_pipe::DEFAULT;
|
process_stream use_out = process_stream::DEFAULT;
|
||||||
process_pipe use_err = process_pipe::DEFAULT;
|
process_stream use_err = process_stream::DEFAULT;
|
||||||
file_stream in = file_stream{};
|
file_stream in = file_stream{};
|
||||||
file_stream out = file_stream{};
|
file_stream out = file_stream{};
|
||||||
file_stream err = file_stream{};
|
file_stream err = file_stream{};
|
||||||
|
|
||||||
process_info() {}
|
process_info() {}
|
||||||
process_info(
|
process_info(
|
||||||
process_pipe in_use, process_pipe out_use, process_pipe err_use
|
process_stream in_use, process_stream out_use, process_stream err_use
|
||||||
):
|
):
|
||||||
use_in(in_use), use_out(out_use), use_err(err_use)
|
use_in(in_use), use_out(out_use), use_err(err_use)
|
||||||
{}
|
{}
|
||||||
|
|
|
@ -135,7 +135,7 @@ OSTD_EXPORT void process_info::open_impl(
|
||||||
std::string const &cmd, std::vector<std::string> const &args,
|
std::string const &cmd, std::vector<std::string> const &args,
|
||||||
bool use_path
|
bool use_path
|
||||||
) {
|
) {
|
||||||
if (use_in == process_pipe::STDOUT) {
|
if (use_in == process_stream::STDOUT) {
|
||||||
throw process_error{EINVAL, std::generic_category()};
|
throw process_error{EINVAL, std::generic_category()};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,9 +153,9 @@ OSTD_EXPORT void process_info::open_impl(
|
||||||
|
|
||||||
if (
|
if (
|
||||||
(pipe(fd_errno) < 0) ||
|
(pipe(fd_errno) < 0) ||
|
||||||
((use_in == process_pipe::PIPE) && (pipe(fd_stdin) < 0)) ||
|
((use_in == process_stream::PIPE) && (pipe(fd_stdin) < 0)) ||
|
||||||
((use_out == process_pipe::PIPE) && (pipe(fd_stdout) < 0)) ||
|
((use_out == process_stream::PIPE) && (pipe(fd_stdout) < 0)) ||
|
||||||
((use_err == process_pipe::PIPE) && (pipe(fd_stderr) < 0))
|
((use_err == process_stream::PIPE) && (pipe(fd_stderr) < 0))
|
||||||
) {
|
) {
|
||||||
throw process_error{errno, std::generic_category()};
|
throw process_error{errno, std::generic_category()};
|
||||||
}
|
}
|
||||||
|
@ -172,7 +172,7 @@ OSTD_EXPORT void process_info::open_impl(
|
||||||
std::exit(1);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
/* prepare standard streams */
|
/* prepare standard streams */
|
||||||
if (use_in == process_pipe::PIPE) {
|
if (use_in == process_stream::PIPE) {
|
||||||
/* close writing end */
|
/* close writing end */
|
||||||
::close(fd_stdin[1]);
|
::close(fd_stdin[1]);
|
||||||
if (dup2(fd_stdin[0], STDIN_FILENO) < 0) {
|
if (dup2(fd_stdin[0], STDIN_FILENO) < 0) {
|
||||||
|
@ -180,7 +180,7 @@ OSTD_EXPORT void process_info::open_impl(
|
||||||
std::exit(1);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (use_out == process_pipe::PIPE) {
|
if (use_out == process_stream::PIPE) {
|
||||||
/* close reading end */
|
/* close reading end */
|
||||||
::close(fd_stdout[0]);
|
::close(fd_stdout[0]);
|
||||||
if (dup2(fd_stdout[1], STDOUT_FILENO) < 0) {
|
if (dup2(fd_stdout[1], STDOUT_FILENO) < 0) {
|
||||||
|
@ -188,14 +188,14 @@ OSTD_EXPORT void process_info::open_impl(
|
||||||
std::exit(1);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (use_err == process_pipe::PIPE) {
|
if (use_err == process_stream::PIPE) {
|
||||||
/* close reading end */
|
/* close reading end */
|
||||||
::close(fd_stderr[0]);
|
::close(fd_stderr[0]);
|
||||||
if (dup2(fd_stderr[1], STDERR_FILENO) < 0) {
|
if (dup2(fd_stderr[1], STDERR_FILENO) < 0) {
|
||||||
write(fd_errno[1], int(errno), sizeof(int));
|
write(fd_errno[1], int(errno), sizeof(int));
|
||||||
std::exit(1);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
} else if (use_err == process_pipe::STDOUT) {
|
} else if (use_err == process_stream::STDOUT) {
|
||||||
if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) {
|
if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) {
|
||||||
write(fd_errno[1], int(errno), sizeof(int));
|
write(fd_errno[1], int(errno), sizeof(int));
|
||||||
std::exit(1);
|
std::exit(1);
|
||||||
|
@ -211,17 +211,17 @@ OSTD_EXPORT void process_info::open_impl(
|
||||||
} else {
|
} else {
|
||||||
/* parent process */
|
/* parent process */
|
||||||
::close(fd_errno[1]);
|
::close(fd_errno[1]);
|
||||||
if (use_in == process_pipe::PIPE) {
|
if (use_in == process_stream::PIPE) {
|
||||||
/* close reading end */
|
/* close reading end */
|
||||||
::close(fd_stdin[0]);
|
::close(fd_stdin[0]);
|
||||||
in.open(fdopen(fd_stdin[1], "w"), stdstream_close);
|
in.open(fdopen(fd_stdin[1], "w"), stdstream_close);
|
||||||
}
|
}
|
||||||
if (use_out == process_pipe::PIPE) {
|
if (use_out == process_stream::PIPE) {
|
||||||
/* close writing end */
|
/* close writing end */
|
||||||
::close(fd_stdout[1]);
|
::close(fd_stdout[1]);
|
||||||
out.open(fdopen(fd_stdout[0], "r"), stdstream_close);
|
out.open(fdopen(fd_stdout[0], "r"), stdstream_close);
|
||||||
}
|
}
|
||||||
if (use_err == process_pipe::PIPE) {
|
if (use_err == process_stream::PIPE) {
|
||||||
/* close writing end */
|
/* close writing end */
|
||||||
::close(fd_stderr[1]);
|
::close(fd_stderr[1]);
|
||||||
err.open(fdopen(fd_stderr[0], "r"), stdstream_close);
|
err.open(fdopen(fd_stderr[0], "r"), stdstream_close);
|
||||||
|
|
Loading…
Reference in a new issue