moved stuff around

This commit is contained in:
isanae
2020-12-03 17:44:52 -05:00
parent 5b67d037d6
commit b9d4c9e4ab
2 changed files with 62 additions and 58 deletions
+49 -49
View File
@@ -267,6 +267,31 @@ void process::run()
do_run(what);
}
void process::do_run(const std::string& what)
{
delete_external_log_file();
create_job();
io_.out.buffer = encoded_buffer(io_.out.encoding);
io_.err.buffer = encoded_buffer(io_.err.encoding);
STARTUPINFOW si = {};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES;
// these handles are given to STARTUPINFOW and must stay alive until the
// process is created in create(), they can be closed after that
handle_ptr stdout_handle = redirect_stdout(si);
handle_ptr stderr_handle = redirect_stderr(si);
handle_ptr stdin_handle = redirect_stdin(si);
const std::wstring cmd = utf8_to_utf16(this_env::get("COMSPEC"));
const std::wstring args = make_cmd_args(what);
const std::wstring cwd = exec_.cwd.native();
create(cmd, args, cwd, si);
}
void process::delete_external_log_file()
{
if (fs::exists(io_.error_log_file))
@@ -377,31 +402,6 @@ handle_ptr process::redirect_stdin(STARTUPINFOW& si)
return h;
}
void process::do_run(const std::string& what)
{
delete_external_log_file();
create_job();
io_.out.buffer = encoded_buffer(io_.out.encoding);
io_.err.buffer = encoded_buffer(io_.err.encoding);
STARTUPINFOW si = {};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES;
// these handles are given to STARTUPINFOW and must stay alive until the
// process is created in create(), they can be closed after that
handle_ptr stdout_handle = redirect_stdout(si);
handle_ptr stderr_handle = redirect_stderr(si);
handle_ptr stdin_handle = redirect_stdin(si);
const std::wstring cmd = utf8_to_utf16(this_env::get("COMSPEC"));
const std::wstring args = make_cmd_args(what);
const std::wstring cwd = exec_.cwd.native();
create(cmd, args, cwd, si);
}
void process::create(
std::wstring cmd, std::wstring args, std::wstring cwd, STARTUPINFOW si)
{
@@ -531,6 +531,15 @@ void process::join()
cx_->trace(context::cmd, "process interrupted and finished");
}
void process::on_timeout(bool& already_interrupted)
{
read_pipes(false);
feed_stdin();
if (!already_interrupted)
already_interrupted = check_interrupted();
}
void process::read_pipes(bool finish)
{
read_pipe(finish, io_.out, *impl_.stdout_pipe, context::std_out);
@@ -590,6 +599,21 @@ void process::read_pipe(
}
}
void process::feed_stdin()
{
if (io_.in && io_.in_offset < io_.in->size())
{
io_.in_offset += impl_.stdin_pipe->write({
io_.in->data() + io_.in_offset, io_.in->size() - io_.in_offset});
if (io_.in_offset >= io_.in->size())
{
impl_.stdin_pipe->close();
io_.in = {};
}
}
}
void process::on_completed()
{
// none of this stuff is needed if the process was interrupted, mob will
@@ -689,30 +713,6 @@ void process::on_process_failed()
}
}
void process::on_timeout(bool& already_interrupted)
{
read_pipes(false);
feed_stdin();
if (!already_interrupted)
already_interrupted = check_interrupted();
}
void process::feed_stdin()
{
if (io_.in && io_.in_offset < io_.in->size())
{
io_.in_offset += impl_.stdin_pipe->write({
io_.in->data() + io_.in_offset, io_.in->size() - io_.in_offset});
if (io_.in_offset >= io_.in->size())
{
impl_.stdin_pipe->close();
io_.in = {};
}
}
}
bool process::check_interrupted()
{
if (!impl_.interrupt)
+13 -9
View File
@@ -490,6 +490,12 @@ private:
void create(
std::wstring cmd, std::wstring args, std::wstring cwd, STARTUPINFOW si);
// called regularly in join(), checks for termination or interruption,
// handles pipes
//
void on_timeout(bool& already_interrupted);
// reads from stdin and stderr, `finish` must be true when the process has
// terminated
//
@@ -502,6 +508,11 @@ private:
bool finish, stream& s,
async_pipe_stdout& pipe, context::reason r);
// sends stuff to stdin, if any
//
void feed_stdin();
// called when the process has terminated; checks exit code, logs stuff and
// bails out on errors
//
@@ -515,15 +526,6 @@ private:
//
void on_process_failed();
// called regularly in join(), checks for termination or interruption,
// handles pipes
//
void on_timeout(bool& already_interrupted);
// sends stuff to stdin, if any
//
void feed_stdin();
// interrupts the process if needed, returns true if interrupted
//
bool check_interrupted();
@@ -532,6 +534,7 @@ private:
//
void terminate();
// if external_error_log() was called, dumps the contenf of the log file as
// errors
//
@@ -542,6 +545,7 @@ private:
//
void dump_stderr() noexcept;
// adds the given argument to the command line
//
// depending on the flags, the argument may be discarded; for example, if