add support for the standard error stream in the Run class

This commit is contained in:
tomasz.sowa committed 2026-11-23 20:37:50 +01:00
1 parent 7a16230a5c
commit c9fd6c1a32
2 files changed
+62 -28

No files matched your search

+49 -21
View File
@@ -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);
}