add putenv interface
This commit is contained in:
parent
6cba3b65c1
commit
b83513021d
@ -1,5 +1,5 @@
|
||||
#ifndef lint
|
||||
static char rcsid[] = "@(#$Id: execmd.cpp,v 1.6 2005-03-17 14:02:05 dockes Exp $ (C) 2004 J.F.Dockes";
|
||||
static char rcsid[] = "@(#$Id: execmd.cpp,v 1.7 2005-11-18 13:52:48 dockes Exp $ (C) 2004 J.F.Dockes";
|
||||
#endif
|
||||
#ifndef TEST_EXECMD
|
||||
#include <unistd.h>
|
||||
@ -21,11 +21,15 @@ static char rcsid[] = "@(#$Id: execmd.cpp,v 1.6 2005-03-17 14:02:05 dockes Exp $
|
||||
using namespace std;
|
||||
#define MAX(A,B) (A>B?A:B)
|
||||
|
||||
int
|
||||
ExecCmd::doexec(const string &cmd, const list<string>& args,
|
||||
void ExecCmd::putenv(const string &ea)
|
||||
{
|
||||
env.push_back(ea);
|
||||
}
|
||||
|
||||
int ExecCmd::doexec(const string &cmd, const list<string>& args,
|
||||
const string *input, string *output)
|
||||
{
|
||||
{
|
||||
{ // Debug and logging
|
||||
string command = cmd + " ";
|
||||
for (list<string>::const_iterator it = args.begin();it != args.end();
|
||||
it++) {
|
||||
@ -186,6 +190,10 @@ ExecCmd::doexec(const string &cmd, const list<string>& args,
|
||||
while (argv[i]) cerr << argv[i++] << endl;}
|
||||
#endif
|
||||
|
||||
for (it = env.begin(); it != env.end(); it++) {
|
||||
::putenv(it->c_str());
|
||||
}
|
||||
|
||||
execvp(cmd.c_str(), (char *const*)argv);
|
||||
// Hu ho
|
||||
LOGERR(("ExecCmd::doexec: execvp(%s) failed. errno %d\n", cmd.c_str(),
|
||||
@ -224,7 +232,12 @@ int main(int argc, const char **argv)
|
||||
input = data;
|
||||
string *ip = 0;
|
||||
//ip = &input;
|
||||
mexec.putenv("TESTVARIABLE1=TESTVALUE1");
|
||||
mexec.putenv("TESTVARIABLE2=TESTVALUE2");
|
||||
mexec.putenv("TESTVARIABLE3=TESTVALUE3");
|
||||
|
||||
int status = mexec.doexec(cmd, l, ip, &output);
|
||||
|
||||
fprintf(stderr, "Status: 0x%x\n", status);
|
||||
cout << "Output:" << output << endl;
|
||||
exit (status >> 8);
|
||||
|
||||
@ -1,17 +1,40 @@
|
||||
#ifndef _EXECMD_H_INCLUDED_
|
||||
#define _EXECMD_H_INCLUDED_
|
||||
/* @(#$Id: execmd.h,v 1.2 2005-03-17 14:02:06 dockes Exp $ (C) 2004 J.F.Dockes */
|
||||
/* @(#$Id: execmd.h,v 1.3 2005-11-18 13:52:48 dockes Exp $ (C) 2004 J.F.Dockes */
|
||||
|
||||
#include <string>
|
||||
#include <list>
|
||||
|
||||
/**
|
||||
Execute command possibly taking both input and output (will do
|
||||
asynchronous io as appropriate for things to work).
|
||||
*/
|
||||
class ExecCmd {
|
||||
public:
|
||||
// ExecCmd() : argv(0) {};
|
||||
// ~ExeCmd() {delete [] argv;}
|
||||
int doexec(const std::string &cmd, const std::list<std::string>& a,
|
||||
/**
|
||||
* Execute command. Both input and output can be specified, and
|
||||
* asynchronous io is used to prevent blocking. This wont work if
|
||||
* input and output need to be synchronized (ie: Q/A), but ok for
|
||||
* filtering.
|
||||
* @param cmd the program to execute. This must be an absolute file name
|
||||
* or exist in the PATH.
|
||||
* @param args the argument list (NOT including argv[0]).
|
||||
* @param input Input to send to the command.
|
||||
* @param output Output from the command.
|
||||
* @return the exec ouput status (0 if ok).
|
||||
*/
|
||||
int doexec(const std::string &cmd, const std::list<std::string>& args,
|
||||
const std::string *input = 0,
|
||||
std::string *output = 0);
|
||||
/**
|
||||
* Add/replace environment variable before executing command. This should
|
||||
* be called before doexec of course.
|
||||
* @param envassign an environment assignment string (name=value)
|
||||
*/
|
||||
void putenv(const std::string &envassign);
|
||||
|
||||
private:
|
||||
std::list<std::string> env;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user