diff --git a/README b/README index 35f427c..a2484ca 100644 --- a/README +++ b/README @@ -1,8 +1,8 @@ -Cppipe allows you to write and execute script like C++ programs. -It offers a shell like synthax for seamless command execution. +cppipe is a utility that allows you to use C++ programs as scripts. +it also offers a shell like syntax for seamless command execution In this way your scripts can benefit from increased control and speed, while -maintaining a lot of the potential for simple command callsynthax. +maintaining a lot of the potential for simple command call syntax. INSTALLATION: In terminal use: @@ -13,6 +13,6 @@ Just include HOW TO EXECUTE A CPPIPE PROGRAM: In terminal use: -cppipe my_cppipe_program.cpp +cppipe my_cppipe_program.cpp ${ANY_ARGUMENTS_FOR_YOUR_PROGRAM} + - diff --git a/childProcess.cpp b/childProcess.cpp index 1092833..5253404 100644 --- a/childProcess.cpp +++ b/childProcess.cpp @@ -17,6 +17,13 @@ RetProc::operator bool() return normal_exit && returned == 0; } +RetProc wait(Proc p) +{ + int status; + waitpid(p.pid, &status, 0); + return RetProc(p, status); +} + /* create a process taking input from pipe childIn */ static Proc createProc(const char* const argv[], const fd_t childIn[2]) { @@ -169,17 +176,6 @@ Proc createProcess(const char* const argv[], fd_t in, fd_t out, fd_t err) return proc; } -RetProc runProcess(const char* const argv[], fd_t in, fd_t out, fd_t err) -{ - Proc p = createProcess(argv, in, out, err); - - // Wait to end - int status; - waitpid(p.pid, &status, 0); - - return RetProc(p, status); -} - void exec_or_die(const char* const argv[]) { execvp(argv[0], (char* const *)argv); diff --git a/childProcess.hpp b/childProcess.hpp index 6ae42fc..585dba2 100644 --- a/childProcess.hpp +++ b/childProcess.hpp @@ -22,6 +22,9 @@ struct RetProc: public Proc /* returned process */ }; +// Wait for a running proccess to finish +RetProc wait(Proc); + enum Redirect: U32 { NOTHING =0, @@ -32,7 +35,8 @@ enum Redirect: U32 /* For all funcions argv is an array of command parameters - * argv[0] is the command itself, argv needs to end with nullptr */ + * argv[0] is the command itself, argv needs to end with nullptr + * non of the functions wait for the proccess to finish */ /* Create a proccess and redirect anyting flaged to a new pipe flags - INPUT, OUTPUT, ERR @@ -45,8 +49,5 @@ Proc createCapProcess(const char* const argv[], fd_t in=0, fd_t err=2); /* Can take FDs as arguments which will be used instead of std */ Proc createProcess(const char* const argv[], fd_t in=0, fd_t out=1, fd_t err=2); -/* Same but waits for the process to finish */ -RetProc runProcess(const char* const argv[], fd_t in=0, fd_t out=1, fd_t err=2); - /* execvp the command or exit */ void exec_or_die(const char* const argv[]); diff --git a/commands.cpp b/commands.cpp index 06ece75..e71977c 100644 --- a/commands.cpp +++ b/commands.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -26,7 +25,7 @@ static fd_t open_or_die(const char* file, I32 flags) RetProc Cmd::operator()(fd_t in, fd_t out, fd_t err) { - RetProc p = runProcess(argv.data(), in, out, err); + Proc p = createProcess(argv.data(), in, out, err); // Close files if such were used // if(in > 2) @@ -36,7 +35,7 @@ RetProc Cmd::operator()(fd_t in, fd_t out, fd_t err) // if(err > 2) // close(err); - return p; + return wait(p); } Proc Cmd::detach(fd_t in, fd_t out, fd_t err) @@ -99,10 +98,11 @@ RetProc PendingCmd::operator()() RetProc PendingCmd::runRedir() { assert(!execed_ && "executed command twice"); assert(out==1 && "already redirected"); + assert(out==1 && "capturing redirected proccess"); execed_ = true; - RetProc p = runProcess(cmd.argv.data(), in, err); - return p; + Proc p = createCapProcess(cmd.argv.data(), in, err); + return wait(p); } Proc PendingCmd::detach() diff --git a/test.cpp b/test.cpp index bd169ac..bc7b95b 100644 --- a/test.cpp +++ b/test.cpp @@ -8,7 +8,6 @@ using namespace std; int main() { - // f(var("ab")); Cmd ls("ls"); Cmd ll("ls", "-l"); Cmd grep("grep");