This commit is contained in:
vrd
2023-10-13 14:04:13 +03:00
parent 275dc6ddae
commit 11b2cef4f0
5 changed files with 22 additions and 26 deletions
+5 -5
View File
@@ -1,8 +1,8 @@
Cppipe allows you to write and execute script like C++ programs. cppipe is a utility that allows you to use C++ programs as scripts.
It offers a shell like synthax for seamless command execution. it also offers a shell like syntax for seamless command execution
In this way your scripts can benefit from increased control and speed, while 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: INSTALLATION:
In terminal use: In terminal use:
@@ -13,6 +13,6 @@ Just include <cppipe/commands.hpp>
HOW TO EXECUTE A CPPIPE PROGRAM: HOW TO EXECUTE A CPPIPE PROGRAM:
In terminal use: In terminal use:
cppipe my_cppipe_program.cpp cppipe my_cppipe_program.cpp ${ANY_ARGUMENTS_FOR_YOUR_PROGRAM}
+7 -11
View File
@@ -17,6 +17,13 @@ RetProc::operator bool()
return normal_exit && returned == 0; 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 */ /* create a process taking input from pipe childIn */
static Proc createProc(const char* const argv[], const fd_t childIn[2]) 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; 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[]) void exec_or_die(const char* const argv[])
{ {
execvp(argv[0], (char* const *)argv); execvp(argv[0], (char* const *)argv);
+5 -4
View File
@@ -22,6 +22,9 @@ struct RetProc: public Proc /* returned process */
}; };
// Wait for a running proccess to finish
RetProc wait(Proc);
enum Redirect: U32 enum Redirect: U32
{ {
NOTHING =0, NOTHING =0,
@@ -32,7 +35,8 @@ enum Redirect: U32
/* For all funcions argv is an array of command parameters /* 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 /* Create a proccess and redirect anyting flaged to a new pipe
flags - INPUT, OUTPUT, ERR 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 */ /* 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); 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 */ /* execvp the command or exit */
void exec_or_die(const char* const argv[]); void exec_or_die(const char* const argv[]);
+5 -5
View File
@@ -1,7 +1,6 @@
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <assert.h> #include <assert.h>
#include <sys/wait.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
@@ -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 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 // Close files if such were used
// if(in > 2) // if(in > 2)
@@ -36,7 +35,7 @@ RetProc Cmd::operator()(fd_t in, fd_t out, fd_t err)
// if(err > 2) // if(err > 2)
// close(err); // close(err);
return p; return wait(p);
} }
Proc Cmd::detach(fd_t in, fd_t out, fd_t err) Proc Cmd::detach(fd_t in, fd_t out, fd_t err)
@@ -99,10 +98,11 @@ RetProc PendingCmd::operator()()
RetProc PendingCmd::runRedir() RetProc PendingCmd::runRedir()
{ {
assert(!execed_ && "executed command twice"); assert(out==1 && "already redirected"); assert(!execed_ && "executed command twice"); assert(out==1 && "already redirected");
assert(out==1 && "capturing redirected proccess");
execed_ = true; execed_ = true;
RetProc p = runProcess(cmd.argv.data(), in, err); Proc p = createCapProcess(cmd.argv.data(), in, err);
return p; return wait(p);
} }
Proc PendingCmd::detach() Proc PendingCmd::detach()
-1
View File
@@ -8,7 +8,6 @@
using namespace std; using namespace std;
int main() int main()
{ {
// f(var("ab"));
Cmd ls("ls"); Cmd ls("ls");
Cmd ll("ls", "-l"); Cmd ll("ls", "-l");
Cmd grep("grep"); Cmd grep("grep");