diff --git a/.clang-format b/.clang-format index 4cbdf3934..0c0abe6f0 100644 --- a/.clang-format +++ b/.clang-format @@ -3,6 +3,7 @@ BasedOnStyle: LLVM --- Language: Cpp AlwaysBreakTemplateDeclarations: Yes +AllowAllParametersOfDeclarationOnNextLine: false AllowShortFunctionsOnASingleLine: Empty BinPackParameters: false BreakBeforeBraces: Custom diff --git a/CHANGELOG.md b/CHANGELOG.md index 71597251c..54a59cb82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. @@ -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. diff --git a/reproc/include/reproc/reproc.h b/reproc/include/reproc/reproc.h index 5249ca17d..a9578d383 100644 --- a/reproc/include/reproc/reproc.h +++ b/reproc/include/reproc/reproc.h @@ -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. */ @@ -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 diff --git a/reproc/src/reproc.c b/reproc/src/reproc.c index 4e102297b..1b5699d80 100644 --- a/reproc/src/reproc.c +++ b/reproc/src/reproc.c @@ -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 }; @@ -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: @@ -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++) { @@ -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); diff --git a/reproc/src/windows/error.c b/reproc/src/windows/error.c index 112d0375c..8f79b310f 100644 --- a/reproc/src/windows/error.c +++ b/reproc/src/windows/error.c @@ -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); diff --git a/reproc/test/environment.c b/reproc/test/environment.c index 82e9b00e5..f79ad18ce 100644 --- a/reproc/test/environment.c +++ b/reproc/test/environment.c @@ -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; diff --git a/reproc/test/overflow.c b/reproc/test/overflow.c index c9b1af89a..f2c7001d4 100644 --- a/reproc/test/overflow.c +++ b/reproc/test/overflow.c @@ -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; diff --git a/reprocxx/examples/background.cpp b/reprocxx/examples/background.cpp index 617888aff..9b00c0e4c 100644 --- a/reprocxx/examples/background.cpp +++ b/reprocxx/examples/background.cpp @@ -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); @@ -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); } diff --git a/reprocxx/examples/forward.cpp b/reprocxx/examples/forward.cpp index 9926b374f..cf77effe7 100644 --- a/reprocxx/examples/forward.cpp +++ b/reprocxx/examples/forward.cpp @@ -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 @@ -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); } diff --git a/reprocxx/include/reproc++/reproc.hpp b/reprocxx/include/reproc++/reproc.hpp index 2de6e5736..8759b3cc6 100644 --- a/reprocxx/include/reproc++/reproc.hpp +++ b/reprocxx/include/reproc++/reproc.hpp @@ -64,7 +64,7 @@ struct options { redirect err; } redirect = {}; - struct stop_actions stop_actions = {}; + struct stop_actions stop = {}; }; enum class stream { in, out, err }; @@ -114,7 +114,7 @@ class process { /*! `reproc_stop` but returns a pair of (status, error). */ REPROCXX_EXPORT std::pair - stop(stop_actions stop_actions) noexcept; + stop(stop_actions stop) noexcept; private: std::unique_ptr process_; diff --git a/reprocxx/src/reproc.cpp b/reprocxx/src/reproc.cpp index c060e69de..3323b8ca3 100644 --- a/reprocxx/src/reproc.cpp +++ b/reprocxx/src/reproc.cpp @@ -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(stop_actions.first.action), - stop_actions.first.timeout.count() }, - { static_cast(stop_actions.second.action), - stop_actions.second.timeout.count() }, - { static_cast(stop_actions.third.action), - stop_actions.third.timeout.count() } }; + return { + { static_cast(stop.first.action), stop.first.timeout.count() }, + { static_cast(stop.second.action), + stop.second.timeout.count() }, + { static_cast(stop.third.action), stop.third.timeout.count() } + }; } auto deleter = [](reproc_t *process) { reproc_destroy(process); }; @@ -55,7 +55,7 @@ std::error_code process::start(const arguments &arguments, { static_cast(options.redirect.in), static_cast(options.redirect.out), static_cast(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); @@ -105,10 +105,9 @@ std::error_code process::kill() noexcept return error_code_from(r); } -std::pair -process::stop(stop_actions stop_actions) noexcept +std::pair 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) }; }