Skip to content

Commit

Permalink
reproc(++): Rename stop_actions variables/fields to stop.
Browse files Browse the repository at this point in the history
  • Loading branch information
DaanDeMeyer committed Jan 4, 2020
1 parent 7296756 commit 11e0303
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 39 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ BasedOnStyle: LLVM
---
Language: Cpp
AlwaysBreakTemplateDeclarations: Yes
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortFunctionsOnASingleLine: Empty
BinPackParameters: false
BreakBeforeBraces: Custom
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@

- Use `int` to represent timeout values.

- Renamed `stop_actions` field of `reproc_options` to `stop`.

### reproc++

- Remove `process::parse`, `process::exit_status` and `process::running`.
Expand Down Expand Up @@ -264,6 +266,8 @@

They still default to waiting indefinitely which matches their old behaviour.

- Renamed `stop_actions` field of `reproc::options` to `stop`.

### CMake

- Drop required CMake version to CMake 3.12.
Expand Down
7 changes: 3 additions & 4 deletions reproc/include/reproc/reproc.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ typedef struct reproc_options {
Stop actions that are passed to `reproc_stop` in `reproc_destroy` to stop the
child process.
When `stop_actions` is 3x `REPROC_STOP_NOOP`, `reproc_destroy` will default to
When `stop` is 3x `REPROC_STOP_NOOP`, `reproc_destroy` will default to
waiting indefinitely for the child process to exit.
*/
reproc_stop_actions stop_actions;
reproc_stop_actions stop;
} reproc_options;

/*! Allocate a new `reproc_t` instance on the heap. */
Expand Down Expand Up @@ -278,8 +278,7 @@ function, its exit status is returned.
Actionable errors:
- `REPROC_ETIMEDOUT`
*/
REPROC_EXPORT int reproc_stop(reproc_t *process,
reproc_stop_actions stop_actions);
REPROC_EXPORT int reproc_stop(reproc_t *process, reproc_stop_actions stop);

/*!
Release all resources associated with `process` including the memory allocated
Expand Down
21 changes: 10 additions & 11 deletions reproc/src/reproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct reproc_t {
handle handle;
struct stdio stdio;
int status;
reproc_stop_actions stop_actions;
reproc_stop_actions stop;
};

enum { STATUS_NOT_STARTED = -2, STATUS_IN_PROGRESS = -1 };
Expand Down Expand Up @@ -118,15 +118,15 @@ int reproc_start(reproc_t *process,
goto cleanup;
}

process->stop_actions = options.stop_actions;
process->stop = options.stop;

bool is_noop = process->stop_actions.first.action == REPROC_STOP_NOOP &&
process->stop_actions.second.action == REPROC_STOP_NOOP &&
process->stop_actions.third.action == REPROC_STOP_NOOP;
bool is_noop = process->stop.first.action == REPROC_STOP_NOOP &&
process->stop.second.action == REPROC_STOP_NOOP &&
process->stop.third.action == REPROC_STOP_NOOP;

if (is_noop) {
process->stop_actions.first.action = REPROC_STOP_WAIT;
process->stop_actions.first.timeout = REPROC_INFINITE;
process->stop.first.action = REPROC_STOP_WAIT;
process->stop.first.timeout = REPROC_INFINITE;
}

cleanup:
Expand Down Expand Up @@ -293,13 +293,12 @@ int reproc_kill(reproc_t *process)
return process_kill(process->handle);
}

int reproc_stop(reproc_t *process, reproc_stop_actions stop_actions)
int reproc_stop(reproc_t *process, reproc_stop_actions stop)
{
assert_return(process, REPROC_EINVAL);
assert_return(process->status != STATUS_NOT_STARTED, REPROC_EINVAL);

reproc_stop_action actions[3] = { stop_actions.first, stop_actions.second,
stop_actions.third };
reproc_stop_action actions[3] = { stop.first, stop.second, stop.third };
int r = -1;

for (size_t i = 0; i < ARRAY_SIZE(actions); i++) {
Expand Down Expand Up @@ -338,7 +337,7 @@ reproc_t *reproc_destroy(reproc_t *process)
assert_return(process, NULL);

if (process->status == STATUS_IN_PROGRESS) {
reproc_stop(process, process->stop_actions);
reproc_stop(process, process->stop);
}

process->handle = process_destroy(process->handle);
Expand Down
4 changes: 2 additions & 2 deletions reproc/src/windows/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ const char *error_string(int error)

static THREAD_LOCAL char string[ERROR_STRING_MAX_SIZE];

r = WideCharToMultiByte(CP_UTF8, 0, wstring, -1, string,
ARRAY_SIZE(string), NULL, NULL);
r = WideCharToMultiByte(CP_UTF8, 0, wstring, -1, string, ARRAY_SIZE(string),
NULL, NULL);

free(wstring);

Expand Down
2 changes: 1 addition & 1 deletion reproc/test/environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int main(void)
const char *argv[2] = { RESOURCE_DIRECTORY "/environment", NULL };
const char *envp[3] = { "IP=127.0.0.1", "PORT=8080", NULL };

r = reproc_start(process, argv, (reproc_options) { .environment = envp });
r = reproc_start(process, argv, (reproc_options){ .environment = envp });
assert(r == 0);

char *output = NULL;
Expand Down
2 changes: 1 addition & 1 deletion reproc/test/overflow.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ int main(void)

const char *argv[2] = { RESOURCE_DIRECTORY "/overflow", NULL };

r = reproc_start(process, argv, (reproc_options) { 0 });
r = reproc_start(process, argv, (reproc_options){ 0 });
assert(r >= 0);

char *output = NULL;
Expand Down
6 changes: 3 additions & 3 deletions reprocxx/examples/background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ int main(int argc, char *argv[])

reproc::process process;

reproc::stop_actions stop_actions = {
reproc::stop_actions stop = {
{ reproc::stop::terminate, reproc::milliseconds(5000) },
{ reproc::stop::kill, reproc::milliseconds(2000) },
{}
};

reproc::options options;
options.stop_actions = stop_actions;
options.stop = stop;

std::error_code ec = process.start(argv + 1, options);

Expand Down Expand Up @@ -80,7 +80,7 @@ int main(int argc, char *argv[])
}

int status = 0;
std::tie(status, ec) = process.stop(stop_actions);
std::tie(status, ec) = process.stop(options.stop);
if (ec) {
return fail(ec);
}
Expand Down
8 changes: 4 additions & 4 deletions reprocxx/examples/forward.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ int main(int argc, char *argv[])
//
// Note that C++14 has chrono literals which allows
// `reproc::milliseconds(5000)` to be replaced with `5000ms`.
reproc::stop_actions stop_actions = {
reproc::stop_actions stop = {
{ reproc::stop::noop, reproc::milliseconds(0) },
{ reproc::stop::terminate, reproc::milliseconds(5000) },
{ reproc::stop::kill, reproc::milliseconds(2000) }
};

reproc::options options;
options.stop_actions = stop_actions;
options.stop = stop;

// We have the child process inherit the parent's standard streams so the
// child process reads directly from the stdin and writes directly to the
Expand All @@ -72,10 +72,10 @@ int main(int argc, char *argv[])
// Call `process::stop` manually so we can access the exit status. We add
// `reproc::wait` with a timeout of ten seconds to give the process time to
// exit on its own before sending `SIGTERM`.
stop_actions.first = { reproc::stop::wait, reproc::milliseconds(10000) };
options.stop.first = { reproc::stop::wait, reproc::milliseconds(10000) };

int status = 0;
std::tie(status, ec) = process.stop(stop_actions);
std::tie(status, ec) = process.stop(options.stop);
if (ec) {
return fail(ec);
}
Expand Down
4 changes: 2 additions & 2 deletions reprocxx/include/reproc++/reproc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct options {
redirect err;
} redirect = {};

struct stop_actions stop_actions = {};
struct stop_actions stop = {};
};

enum class stream { in, out, err };
Expand Down Expand Up @@ -114,7 +114,7 @@ class process {

/*! `reproc_stop` but returns a pair of (status, error). */
REPROCXX_EXPORT std::pair<int, std::error_code>
stop(stop_actions stop_actions) noexcept;
stop(stop_actions stop) noexcept;

private:
std::unique_ptr<reproc_t, void (*)(reproc_t *)> process_;
Expand Down
21 changes: 10 additions & 11 deletions reprocxx/src/reproc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ static std::error_code error_code_from(int r)
return { -r, std::system_category() };
}

static reproc_stop_actions reproc_stop_actions_from(stop_actions stop_actions)
static reproc_stop_actions reproc_stop_actions_from(stop_actions stop)
{
return { { static_cast<REPROC_STOP>(stop_actions.first.action),
stop_actions.first.timeout.count() },
{ static_cast<REPROC_STOP>(stop_actions.second.action),
stop_actions.second.timeout.count() },
{ static_cast<REPROC_STOP>(stop_actions.third.action),
stop_actions.third.timeout.count() } };
return {
{ static_cast<REPROC_STOP>(stop.first.action), stop.first.timeout.count() },
{ static_cast<REPROC_STOP>(stop.second.action),
stop.second.timeout.count() },
{ static_cast<REPROC_STOP>(stop.third.action), stop.third.timeout.count() }
};
}

auto deleter = [](reproc_t *process) { reproc_destroy(process); };
Expand All @@ -55,7 +55,7 @@ std::error_code process::start(const arguments &arguments,
{ static_cast<REPROC_REDIRECT>(options.redirect.in),
static_cast<REPROC_REDIRECT>(options.redirect.out),
static_cast<REPROC_REDIRECT>(options.redirect.err) },
reproc_stop_actions_from(options.stop_actions)
reproc_stop_actions_from(options.stop)
};

int r = reproc_start(process_.get(), arguments.data(), reproc_options);
Expand Down Expand Up @@ -105,10 +105,9 @@ std::error_code process::kill() noexcept
return error_code_from(r);
}

std::pair<int, std::error_code>
process::stop(stop_actions stop_actions) noexcept
std::pair<int, std::error_code> process::stop(stop_actions stop) noexcept
{
int r = reproc_stop(process_.get(), reproc_stop_actions_from(stop_actions));
int r = reproc_stop(process_.get(), reproc_stop_actions_from(stop));
return { r, error_code_from(r) };
}

Expand Down

0 comments on commit 11e0303

Please sign in to comment.