add Run::Go(...) methods with pt::TextStream arguments
This commit is contained in:
2 files changed
+233
-52
No files matched your search
+197
-35
@@ -147,33 +147,60 @@ int Run::LastReturn()
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void Run::Write(const char * in, size_t inlen)
|
bool Run::Write(const char * in, size_t inlen, int descriptor)
|
||||||
{
|
{
|
||||||
ssize_t len;
|
ssize_t len;
|
||||||
|
|
||||||
while( inlen > 0 )
|
while( inlen > 0 )
|
||||||
{
|
{
|
||||||
len = write(desout[1], in, inlen);
|
len = write(descriptor, in, inlen);
|
||||||
|
|
||||||
if( len < 0 || len > (ssize_t)inlen )
|
if( len < 0 || len > (ssize_t)inlen )
|
||||||
{
|
{
|
||||||
// something wrong
|
// something wrong
|
||||||
last_status = 3;
|
last_status = 3;
|
||||||
break;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
in += len;
|
in += len;
|
||||||
inlen -= len;
|
inlen -= len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool Run::Write(const pt::TextStream & in, int descriptor)
|
||||||
|
{
|
||||||
|
pt::TextStream::const_iterator i = in.begin();
|
||||||
|
pt::TextStream::const_iterator i_end = in.end();
|
||||||
|
char buf[1024];
|
||||||
|
size_t buf_max_len = sizeof(buf) / sizeof(char);
|
||||||
|
|
||||||
|
while( i != i_end )
|
||||||
|
{
|
||||||
|
size_t buf_len=0;
|
||||||
|
|
||||||
|
for( ; i != i_end && buf_len < buf_max_len ; ++i, ++buf_len)
|
||||||
|
{
|
||||||
|
buf[buf_len] = *i;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( !Write(buf, buf_len, descriptor) )
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void Run::Read(std::string & out, int descriptor)
|
void Run::Read(std::string & out, int descriptor)
|
||||||
{
|
{
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
size_t buflen = sizeof(buf) / sizeof(char);
|
size_t buflen = sizeof(buf) / sizeof(char);
|
||||||
ssize_t len;
|
ssize_t len;
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
@@ -193,6 +220,29 @@ ssize_t len;
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Run::Read(pt::TextStream & out, int descriptor)
|
||||||
|
{
|
||||||
|
char buf[1024];
|
||||||
|
size_t buflen = sizeof(buf) / sizeof(char);
|
||||||
|
ssize_t len;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
len = read(descriptor, buf, buflen);
|
||||||
|
|
||||||
|
if( len < 0 || len > (ssize_t)buflen )
|
||||||
|
{
|
||||||
|
// something wrong
|
||||||
|
last_status = 4;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( len > 0 )
|
||||||
|
out.write(buf, len);
|
||||||
|
}
|
||||||
|
while( len > 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void Run::ChildThrow()
|
void Run::ChildThrow()
|
||||||
{
|
{
|
||||||
@@ -297,13 +347,28 @@ return true;
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void Run::WriteRead(const char * in, size_t inlen, std::string * out, std::string * err_out)
|
void Run::WriteRead(const char * in, size_t inlen, pt::TextStream * out, pt::TextStream * err_out)
|
||||||
{
|
{
|
||||||
if( in && inlen>0 )
|
if( in && inlen > 0 )
|
||||||
Write(in, inlen);
|
Write(in, inlen, desout[1]);
|
||||||
|
|
||||||
close(desout[1]);
|
close(desout[1]);
|
||||||
|
WriteReadFinish(out, err_out);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Run::WriteRead(const pt::TextStream * in, pt::TextStream * out, pt::TextStream * err_out)
|
||||||
|
{
|
||||||
|
if( in && !in->empty() )
|
||||||
|
Write(*in, desout[1]);
|
||||||
|
|
||||||
|
close(desout[1]);
|
||||||
|
WriteReadFinish(out, err_out);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Run::WriteReadFinish(pt::TextStream * out, pt::TextStream * err_out)
|
||||||
|
{
|
||||||
if( out )
|
if( out )
|
||||||
Read(*out, desin[0]);
|
Read(*out, desin[0]);
|
||||||
|
|
||||||
@@ -315,7 +380,6 @@ void Run::WriteRead(const char * in, size_t inlen, std::string * out, std::strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void Run::CheckStatus()
|
void Run::CheckStatus()
|
||||||
{
|
{
|
||||||
int status;
|
int status;
|
||||||
@@ -350,8 +414,38 @@ int status;
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int Run::GoStringStream(const char * in, size_t inlen, pt::TextStream * out, pt::TextStream * err_out)
|
||||||
|
{
|
||||||
|
if( GoPrepare(out, err_out) )
|
||||||
|
{
|
||||||
|
WriteRead(in, inlen, out, err_out);
|
||||||
|
GoFinish();
|
||||||
|
}
|
||||||
|
|
||||||
int Run::Go(const char * in, size_t inlen, std::string * out, std::string * err_out)
|
return last_return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int Run::GoStringString(const char * in, size_t inlen, std::string * out, std::string * err_out)
|
||||||
|
{
|
||||||
|
pt::TextStream out_str;
|
||||||
|
pt::TextStream err_out_str;
|
||||||
|
pt::TextStream * pout_str = (out) ? &out_str : nullptr;
|
||||||
|
pt::TextStream * perr_out_str = (err_out) ? & err_out_str : nullptr;
|
||||||
|
|
||||||
|
int res = GoStringStream(in, inlen, pout_str, perr_out_str);
|
||||||
|
|
||||||
|
if( out )
|
||||||
|
out_str.to_str(*out);
|
||||||
|
|
||||||
|
if( err_out )
|
||||||
|
err_out_str.to_str(*err_out);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool Run::GoPrepare(pt::TextStream * out, pt::TextStream * err_out)
|
||||||
{
|
{
|
||||||
last_status = 0;
|
last_status = 0;
|
||||||
last_return = 255;
|
last_return = 255;
|
||||||
@@ -359,89 +453,157 @@ int Run::Go(const char * in, size_t inlen, std::string * out, std::string * err_
|
|||||||
if( out )
|
if( out )
|
||||||
out->clear();
|
out->clear();
|
||||||
|
|
||||||
|
if( err_out )
|
||||||
|
err_out->clear();
|
||||||
|
|
||||||
if( !command )
|
if( !command )
|
||||||
{
|
{
|
||||||
last_status = 7;
|
last_status = 7;
|
||||||
return last_return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( !CreatePipes() )
|
if( !CreatePipes() )
|
||||||
return last_return;
|
return false;
|
||||||
|
|
||||||
if( !Fork() )
|
if( !Fork() )
|
||||||
return last_return;
|
return false;
|
||||||
|
|
||||||
// here goes the parent
|
// here goes the parent
|
||||||
close(desin[1]);
|
close(desin[1]);
|
||||||
close(desin_err[1]);
|
close(desin_err[1]);
|
||||||
close(desout[0]);
|
close(desout[0]);
|
||||||
WriteRead(in, inlen, out, err_out);
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Run::GoFinish()
|
||||||
|
{
|
||||||
CheckStatus();
|
CheckStatus();
|
||||||
|
|
||||||
if( last_status != 0 )
|
if( last_status != 0 )
|
||||||
log << log1 << "Run: a problem with the command, last_status: " << last_status << logend;
|
log << log1 << "Run: a problem with the command, last_status: " << last_status << logend;
|
||||||
|
|
||||||
return last_return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int Run::Go(const char * in, size_t inlen, std::string & out)
|
int Run::Go(const pt::TextStream * in, pt::TextStream * out, pt::TextStream * err_out)
|
||||||
{
|
{
|
||||||
return Go(in, inlen, &out, nullptr);
|
if( GoPrepare(out, err_out) )
|
||||||
|
{
|
||||||
|
WriteRead(in, out, err_out);
|
||||||
|
GoFinish();
|
||||||
|
}
|
||||||
|
|
||||||
|
return last_return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int Run::Go(const char * in, std::string & out)
|
int Run::Go(const pt::TextStream & in, pt::TextStream & out, pt::TextStream & err_out)
|
||||||
{
|
{
|
||||||
size_t len = strlen(in);
|
return Go(&in, &out, &err_out);
|
||||||
return Go(in, len, &out, nullptr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int Run::Go(const char * in, std::string & out, std::string & err_out)
|
int Run::Go(const pt::TextStream & in, pt::TextStream & out)
|
||||||
{
|
{
|
||||||
size_t len = strlen(in);
|
return Go(&in, &out, nullptr);
|
||||||
return Go(in, len, &out, &err_out);
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int Run::Go(const pt::TextStream & in)
|
||||||
|
{
|
||||||
|
return Go(&in, nullptr, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int Run::Go()
|
||||||
|
{
|
||||||
|
return Go(nullptr, nullptr, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int Run::Go(const char * in, size_t inlen, pt::TextStream & out, pt::TextStream & err_out)
|
||||||
|
{
|
||||||
|
return GoStringStream(in, inlen, &out, &err_out);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int Run::Go(const char * in, size_t inlen, pt::TextStream & out)
|
||||||
|
{
|
||||||
|
return GoStringStream(in, inlen, &out, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int Run::Go(const char * in, size_t inlen)
|
int Run::Go(const char * in, size_t inlen)
|
||||||
{
|
{
|
||||||
return Go(in, inlen, nullptr, nullptr);
|
return GoStringStream(in, inlen, nullptr, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int Run::Go(const char * in, pt::TextStream & out, pt::TextStream & err_out)
|
||||||
|
{
|
||||||
|
size_t len = strlen(in);
|
||||||
|
return GoStringStream(in, len, &out, &err_out);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int Run::Go(const char * in, pt::TextStream & out)
|
||||||
|
{
|
||||||
|
size_t len = strlen(in);
|
||||||
|
return GoStringStream(in, len, &out, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int Run::Go(const char * in)
|
int Run::Go(const char * in)
|
||||||
{
|
{
|
||||||
size_t len = strlen(in);
|
size_t len = strlen(in);
|
||||||
return Go(in, len, nullptr, nullptr);
|
return GoStringStream(in, len, nullptr, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int Run::Go()
|
int Run::Go(const char * in, size_t inlen, std::string & out, std::string & err_out)
|
||||||
{
|
{
|
||||||
return Go(nullptr, 0, nullptr, nullptr);
|
return GoStringString(in, inlen, &out, &err_out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int Run::Go(const std::string in, std::string & out)
|
int Run::Go(const char * in, size_t inlen, std::string & out)
|
||||||
{
|
{
|
||||||
return Go(in.c_str(), in.size(), &out, nullptr);
|
return GoStringString(in, inlen, &out, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int Run::Go(const std::string in)
|
int Run::Go(const char * in, std::string & out, std::string & err_out)
|
||||||
{
|
{
|
||||||
return Go(in.c_str(), in.size(), nullptr, nullptr);
|
size_t len = strlen(in);
|
||||||
|
return GoStringString(in, len, &out, &err_out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int Run::Go(std::string & out)
|
int Run::Go(const char * in, std::string & out)
|
||||||
{
|
{
|
||||||
return Go(nullptr, 0, &out, nullptr);
|
size_t len = strlen(in);
|
||||||
|
return GoStringString(in, len, &out, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int Run::Go(const std::string & in, std::string & out, std::string & err_out)
|
||||||
|
{
|
||||||
|
return GoStringString(in.c_str(), in.size(), &out, &err_out);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int Run::Go(const std::string & in, std::string & out)
|
||||||
|
{
|
||||||
|
return GoStringString(in.c_str(), in.size(), &out, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int Run::Go(const std::string & in)
|
||||||
|
{
|
||||||
|
return GoStringStream(in.c_str(), in.size(), nullptr, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace Winix
|
} // namespace Winix
|
||||||
|
|
||||||
+36
-17
@@ -120,22 +120,32 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
executing the command
|
* executing the command
|
||||||
you should call Par(), Env() and Cmd() beforehand
|
* you should call Par(), Env() and Cmd() beforehand
|
||||||
*/
|
*
|
||||||
int Go(const char * in, size_t inlen, std::string & out);
|
* in the first method in, out and err_out can be a null
|
||||||
int Go(const char * in, std::string & out);
|
*
|
||||||
int Go(const char * in, size_t inlen);
|
*/
|
||||||
int Go(const char * in);
|
int Go(const pt::TextStream * in, pt::TextStream * out, pt::TextStream * err_out);
|
||||||
int Go(const std::string in, std::string & out);
|
int Go(const pt::TextStream & in, pt::TextStream & out, pt::TextStream & err_out);
|
||||||
int Go(const std::string in);
|
int Go(const pt::TextStream & in, pt::TextStream & out);
|
||||||
int Go(std::string & out);
|
int Go(const pt::TextStream & in);
|
||||||
int Go();
|
int Go();
|
||||||
|
|
||||||
/*
|
int Go(const char * in, size_t inlen, pt::TextStream & out, pt::TextStream & err_out);
|
||||||
* with standard error
|
int Go(const char * in, size_t inlen, pt::TextStream & out);
|
||||||
*/
|
int Go(const char * in, size_t inlen);
|
||||||
|
int Go(const char * in, pt::TextStream & out, pt::TextStream & err_out);
|
||||||
|
int Go(const char * in, pt::TextStream & out);
|
||||||
|
int Go(const char * in);
|
||||||
|
|
||||||
|
int Go(const char * in, size_t inlen, std::string & out, std::string & err_out);
|
||||||
|
int Go(const char * in, size_t inlen, std::string & out);
|
||||||
int Go(const char * in, std::string & out, std::string & err_out);
|
int Go(const char * in, std::string & out, std::string & err_out);
|
||||||
|
int Go(const char * in, std::string & out);
|
||||||
|
int Go(const std::string & in, std::string & out, std::string & err_out);
|
||||||
|
int Go(const std::string & in, std::string & out);
|
||||||
|
int Go(const std::string & in);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -163,16 +173,25 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
int Go(const char * in, size_t inlen, std::string * out, std::string * err_out);
|
int GoStringStream(const char * in, size_t inlen, pt::TextStream * out, pt::TextStream * err_out);
|
||||||
|
int GoStringString(const char * in, size_t inlen, std::string * out, std::string * err_out);
|
||||||
|
bool GoPrepare(pt::TextStream * out, pt::TextStream * err_out);
|
||||||
|
void GoFinish();
|
||||||
|
|
||||||
|
|
||||||
void SetName();
|
void SetName();
|
||||||
void CheckStatus();
|
void CheckStatus();
|
||||||
void WriteRead(const char * in, size_t inlen, std::string * out, std::string * err_out);
|
void WriteRead(const char * in, size_t inlen, pt::TextStream * out, pt::TextStream * err_out);
|
||||||
|
void WriteRead(const pt::TextStream * in, pt::TextStream * out, pt::TextStream * err_out);
|
||||||
|
void WriteReadFinish(pt::TextStream * out, pt::TextStream * err_out);
|
||||||
bool CreatePipes();
|
bool CreatePipes();
|
||||||
bool Fork();
|
bool Fork();
|
||||||
void ChildThrow();
|
void ChildThrow();
|
||||||
void Child();
|
void Child();
|
||||||
void Write(const char * in, size_t inlen);
|
bool Write(const char * in, size_t inlen, int descriptor);
|
||||||
|
bool Write(const pt::TextStream & in, int descriptor);
|
||||||
void Read(std::string & out, int descriptor);
|
void Read(std::string & out, int descriptor);
|
||||||
|
void Read(pt::TextStream & out, int descriptor);
|
||||||
|
|
||||||
|
|
||||||
int last_status;
|
int last_status;
|
||||||
@@ -187,7 +206,7 @@ private:
|
|||||||
|
|
||||||
char * par[WINIX_RUN_MAX_PARAMETERS + 2];
|
char * par[WINIX_RUN_MAX_PARAMETERS + 2];
|
||||||
char * env[WINIX_RUN_MAX_PARAMETERS + 1];
|
char * env[WINIX_RUN_MAX_PARAMETERS + 1];
|
||||||
const char * command;
|
const char * command; // a pointer to the command being run
|
||||||
|
|
||||||
size_t parlen;
|
size_t parlen;
|
||||||
size_t envlen;
|
size_t envlen;
|
||||||
|
|||||||
Reference in new issue
Block a user