add support for the standard error stream in the Run class
This commit is contained in:
2 files changed
+62
-28
No files matched your search
+49
-21
@@ -4,8 +4,8 @@
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2011-2014, Tomasz Sowa
|
||||
/*
|
||||
* Copyright (c) 2011-2026, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -169,7 +169,7 @@ ssize_t len;
|
||||
|
||||
|
||||
|
||||
void Run::Read(std::string & out)
|
||||
void Run::Read(std::string & out, int descriptor)
|
||||
{
|
||||
char buf[1024];
|
||||
size_t buflen = sizeof(buf) / sizeof(char);
|
||||
@@ -177,7 +177,7 @@ ssize_t len;
|
||||
|
||||
do
|
||||
{
|
||||
len = read(desin[0], buf, buflen);
|
||||
len = read(descriptor, buf, buflen);
|
||||
|
||||
if( len < 0 || len > (ssize_t)buflen )
|
||||
{
|
||||
@@ -202,16 +202,17 @@ void Run::ChildThrow()
|
||||
if( dup2(desin[1], 1) == -1 )
|
||||
return;
|
||||
|
||||
close(2);
|
||||
close(desin[0]);
|
||||
if( dup2(desin_err[1], 2) == -1 )
|
||||
return;
|
||||
|
||||
close(desout[1]);
|
||||
close(desin[0]);
|
||||
close(desin_err[0]);
|
||||
|
||||
par[parlen+1] = 0;
|
||||
env[envlen] = 0;
|
||||
|
||||
execve(command, par, env);
|
||||
// if we are here then there is something wrong (execve failed)
|
||||
// !! IMPROVE ME may add exit() ?
|
||||
}
|
||||
|
||||
|
||||
@@ -242,6 +243,17 @@ bool Run::CreatePipes()
|
||||
return false;
|
||||
}
|
||||
|
||||
res = pipe(desin_err);
|
||||
|
||||
if( res != 0 )
|
||||
{
|
||||
log << log1 << "Run: pipe failed (desin_err)" << logend;
|
||||
last_status = 1;
|
||||
close(desin[0]);
|
||||
close(desin[1]);
|
||||
return false;
|
||||
}
|
||||
|
||||
res = pipe(desout);
|
||||
|
||||
if( res != 0 )
|
||||
@@ -250,10 +262,12 @@ bool Run::CreatePipes()
|
||||
last_status = 1;
|
||||
close(desin[0]);
|
||||
close(desin[1]);
|
||||
close(desin_err[0]);
|
||||
close(desin_err[1]);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -268,6 +282,8 @@ bool Run::Fork()
|
||||
last_status = 2;
|
||||
close(desin[0]);
|
||||
close(desin[1]);
|
||||
close(desin_err[0]);
|
||||
close(desin_err[1]);
|
||||
close(desout[0]);
|
||||
close(desout[1]);
|
||||
return false;
|
||||
@@ -281,7 +297,7 @@ return true;
|
||||
|
||||
|
||||
|
||||
void Run::WriteRead(const char * in, size_t inlen, std::string * out)
|
||||
void Run::WriteRead(const char * in, size_t inlen, std::string * out, std::string * err_out)
|
||||
{
|
||||
if( in && inlen>0 )
|
||||
Write(in, inlen);
|
||||
@@ -289,9 +305,13 @@ void Run::WriteRead(const char * in, size_t inlen, std::string * out)
|
||||
close(desout[1]);
|
||||
|
||||
if( out )
|
||||
Read(*out);
|
||||
Read(*out, desin[0]);
|
||||
|
||||
if( err_out )
|
||||
Read(*err_out, desin_err[0]);
|
||||
|
||||
close(desin[0]);
|
||||
close(desin_err[0]);
|
||||
}
|
||||
|
||||
|
||||
@@ -331,7 +351,7 @@ int status;
|
||||
|
||||
|
||||
|
||||
int Run::Go(const char * in, size_t inlen, std::string * out)
|
||||
int Run::Go(const char * in, size_t inlen, std::string * out, std::string * err_out)
|
||||
{
|
||||
last_status = 0;
|
||||
last_return = 255;
|
||||
@@ -353,8 +373,9 @@ int Run::Go(const char * in, size_t inlen, std::string * out)
|
||||
|
||||
// here goes the parent
|
||||
close(desin[1]);
|
||||
close(desin_err[1]);
|
||||
close(desout[0]);
|
||||
WriteRead(in, inlen, out);
|
||||
WriteRead(in, inlen, out, err_out);
|
||||
CheckStatus();
|
||||
|
||||
if( last_status != 0 )
|
||||
@@ -366,51 +387,58 @@ return last_return;
|
||||
|
||||
int Run::Go(const char * in, size_t inlen, std::string & out)
|
||||
{
|
||||
return Go(in, inlen, &out);
|
||||
return Go(in, inlen, &out, nullptr);
|
||||
}
|
||||
|
||||
|
||||
int Run::Go(const char * in, std::string & out)
|
||||
{
|
||||
size_t len = strlen(in);
|
||||
return Go(in, len, &out);
|
||||
return Go(in, len, &out, nullptr);
|
||||
}
|
||||
|
||||
|
||||
int Run::Go(const char * in, std::string & out, std::string & err_out)
|
||||
{
|
||||
size_t len = strlen(in);
|
||||
return Go(in, len, &out, &err_out);
|
||||
}
|
||||
|
||||
|
||||
int Run::Go(const char * in, size_t inlen)
|
||||
{
|
||||
return Go(in, inlen, 0);
|
||||
return Go(in, inlen, nullptr, nullptr);
|
||||
}
|
||||
|
||||
|
||||
int Run::Go(const char * in)
|
||||
{
|
||||
size_t len = strlen(in);
|
||||
return Go(in, len, 0);
|
||||
return Go(in, len, nullptr, nullptr);
|
||||
}
|
||||
|
||||
|
||||
int Run::Go()
|
||||
{
|
||||
return Go(0, 0, 0);
|
||||
return Go(nullptr, 0, nullptr, nullptr);
|
||||
}
|
||||
|
||||
|
||||
int Run::Go(const std::string in, std::string & out)
|
||||
{
|
||||
return Go(in.c_str(), in.size(), &out);
|
||||
return Go(in.c_str(), in.size(), &out, nullptr);
|
||||
}
|
||||
|
||||
|
||||
int Run::Go(const std::string in)
|
||||
{
|
||||
return Go(in.c_str(), in.size(), 0);
|
||||
return Go(in.c_str(), in.size(), nullptr, nullptr);
|
||||
}
|
||||
|
||||
|
||||
int Run::Go(std::string & out)
|
||||
{
|
||||
return Go(0, 0, &out);
|
||||
return Go(nullptr, 0, &out, nullptr);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+13
-7
@@ -4,8 +4,8 @@
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2011-2018, Tomasz Sowa
|
||||
/*
|
||||
* Copyright (c) 2011-2026, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -132,6 +132,11 @@ public:
|
||||
int Go(std::string & out);
|
||||
int Go();
|
||||
|
||||
/*
|
||||
* with standard error
|
||||
*/
|
||||
int Go(const char * in, std::string & out, std::string & err_out);
|
||||
|
||||
|
||||
/*
|
||||
last status:
|
||||
@@ -158,16 +163,16 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
int Go(const char * in, size_t inlen, std::string * out);
|
||||
int Go(const char * in, size_t inlen, std::string * out, std::string * err_out);
|
||||
void SetName();
|
||||
void CheckStatus();
|
||||
void WriteRead(const char * in, size_t inlen, std::string * out);
|
||||
void WriteRead(const char * in, size_t inlen, std::string * out, std::string * err_out);
|
||||
bool CreatePipes();
|
||||
bool Fork();
|
||||
void ChildThrow();
|
||||
void Child();
|
||||
void Write(const char * in, size_t inlen);
|
||||
void Read(std::string & out);
|
||||
void Read(std::string & out, int descriptor);
|
||||
|
||||
|
||||
int last_status;
|
||||
@@ -176,8 +181,9 @@ private:
|
||||
// if last_status!=0 then last_return is 255
|
||||
int last_return;
|
||||
|
||||
int desin[2];
|
||||
int desout[2];
|
||||
int desin[2]; // standard otput: the child writes to desin[1], the parent reads from desin[0]
|
||||
int desin_err[2]; // standard error output: the child writes to desin_err[1], the parent reads from desin_err[0]
|
||||
int desout[2]; // standard input: the parent writes to desout[1], the child reads from desout[0]
|
||||
|
||||
char * par[WINIX_RUN_MAX_PARAMETERS + 2];
|
||||
char * env[WINIX_RUN_MAX_PARAMETERS + 1];
|
||||
|
||||
Reference in new issue
Block a user