From 0a5af1f757091d375f3004b080aac23628b9b6f1 Mon Sep 17 00:00:00 2001 From: Venelin Date: Fri, 27 Dec 2024 18:53:04 +0200 Subject: [PATCH] Cmd implicit constructor --- run_tests.sh | 7 ++++--- src/commands.hpp | 19 ++++++++++--------- src/commands.inl | 23 ++++++++++++++++++++--- test/functions_test.cppipe | 32 +++++++++++++++++--------------- todo.txt | 1 + 5 files changed, 52 insertions(+), 30 deletions(-) diff --git a/run_tests.sh b/run_tests.sh index a988731..cdd4363 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -2,10 +2,11 @@ set -e # Test cppipe functions -OKs=$(test/functions_test.cppipe 2>/dev/null | grep OK | wc -l) -if ! [ $OKs = 13 ] +OKs=$(test/functions_test.cppipe | grep OK | wc -l) +EXPECTED=14 +if ! [ $OKs = $EXPECTED ] then - echo "Functions test failed: EXPECTED 13 OKs, got $OKs" + echo "Functions test failed: EXPECTED $EXPECTED OKs, got $OKs" exit 1 fi echo Functions test OK! diff --git a/src/commands.hpp b/src/commands.hpp index 64c4a3c..b1888f4 100644 --- a/src/commands.hpp +++ b/src/commands.hpp @@ -18,11 +18,11 @@ class Cmd public: /* all args must be const char* */ template - explicit Cmd(Args... args) - : argv({args...}) - { - argv.push_back(nullptr); - } + explicit Cmd(Args...); + + /* Implicitly convert from { "command", "param" } + * e.g: exec({ "cmd", "arg" }); */ + Cmd(std::initializer_list args); /* Execute the command, if arguments are not given use stdin,out,err else use the given file desciptors. this can also be used @@ -45,8 +45,9 @@ public: class PendingCmd { public: + PendingCmd(std::initializer_list cmd_args); /* Can be implicitly created from a command */ - PendingCmd(const Cmd&, fd_t in=0, fd_t out=1, fd_t err=2); + PendingCmd(Cmd, fd_t in=0, fd_t out=1, fd_t err=2); /* Execute the command on destruction */ ~PendingCmd(); @@ -60,10 +61,10 @@ public: /* Prevent a pending command from being executed on destruction */ void cancel(); - const Cmd& cmd; - fd_t in, out, err; + Cmd cmd; + fd_t in=0, out=1, err=2; private: - bool execed_; + bool execed_ = false; friend Proc detach(const PendingCmd&); friend Proc detachRedirIn(const PendingCmd&); diff --git a/src/commands.inl b/src/commands.inl index d32f96e..e4ea498 100644 --- a/src/commands.inl +++ b/src/commands.inl @@ -28,6 +28,18 @@ namespace _cppipe } } +template +inline Cmd::Cmd(Args... args) + : Cmd({ args... }) +{ +} + +inline Cmd::Cmd(std::initializer_list args) + : argv(args) +{ + argv.push_back(nullptr); +} + inline DeadProc Cmd::operator()(fd_t in, fd_t out, fd_t err) const { Proc p = createProcess(argv.data(), in, out, err); @@ -58,13 +70,18 @@ inline Cmd& Cmd::operator+=(const char* arg) return *this; } +inline PendingCmd::PendingCmd(std::initializer_list cmd_args) + : cmd(cmd_args) + , in(0) + , out(1) + , err(2) +{} -inline PendingCmd::PendingCmd(const Cmd& origin, fd_t in, fd_t out, fd_t err) - : cmd(origin) +inline PendingCmd::PendingCmd(Cmd origin, fd_t in, fd_t out, fd_t err) + : cmd(std::move(origin)) , in(in) , out(out) , err(err) - , execed_(false) {} inline PendingCmd::~PendingCmd() diff --git a/test/functions_test.cppipe b/test/functions_test.cppipe index a932eaa..88ffe93 100755 --- a/test/functions_test.cppipe +++ b/test/functions_test.cppipe @@ -25,7 +25,7 @@ int main(int argc, char* argv[]) string out1 = $(ll + "src" | grep + "inl" | grep + "child"); if(out1.find("childProcess.inl") != string::npos) - cout << "OK 0/12" << endl; + cout << "OK 0/13" << endl; string out2 = $(echo + "abc" + "def"); // "abc def" = 7 chars, trailing newlines are stripped by $() @@ -35,8 +35,8 @@ int main(int argc, char* argv[]) exit(1); } - Cmd success("echo", "OK 1/12"); - Cmd fail("mkdir", "."); + Cmd success("echo", "OK 1/13"); + Cmd fail("false"); Cmd unexpected("echo", "FAILURE"); success && fail && @@ -47,34 +47,36 @@ int main(int argc, char* argv[]) // unexpected && // unexpected; - Cmd write_file("echo", "Existing ", " ", "file. OK 2/12"); + Cmd write_file("echo", "Existing ", " ", "file. OK 2/13"); write_file > "file.txt"; grep + "Existing" < "file.txt"; - echo + "Appended to file OK 3/12" >> "file.txt"; + echo + "Appended to file OK 3/13" >> "file.txt"; grep + "Appended" < "file.txt" && rm + "file.txt" && fail || - Cmd("echo", "OK 4/12"); + Cmd("echo", "OK 4/13"); - echo + "OK 5/12" && - echo + "OK 6/12", - Cmd("echo", "OK 7/12"); + echo + "OK 5/13" && + echo + "OK 6/13", + Cmd("echo", "OK 7/13"); - echo + "OK 8/12" & - echo + "OK 9/12" && - echo + "OK 10/12"; + echo + "OK 8/13" & + echo + "OK 9/13" && + echo + "OK 10/13"; // wait for all detached while(wait(nullptr) != -1); Cmd run_OK = echo; - run_OK.append_args({ "OK 11/12" }); + run_OK.append_args({ "OK 11/13" }); run( run_OK ); - exec( echo + "OK 12/12" ); + run({ "echo", "OK 12/13" }); + + exec({ "echo", "OK 13/13" }); // todo - // exec( echo + $(echo + "OK 11/12") ); + // exec( echo + $(echo + "OK 11/13") ); } diff --git a/todo.txt b/todo.txt index 64fa16b..5c6ec23 100644 --- a/todo.txt +++ b/todo.txt @@ -1,3 +1,4 @@ +fix debug example #!/usr/bin/cppipe description - advantages over manual compile man