add putenv interface
This commit is contained in:
parent
6cba3b65c1
commit
b83513021d
@ -1,5 +1,5 @@
|
|||||||
#ifndef lint
|
#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
|
#endif
|
||||||
#ifndef TEST_EXECMD
|
#ifndef TEST_EXECMD
|
||||||
#include <unistd.h>
|
#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;
|
using namespace std;
|
||||||
#define MAX(A,B) (A>B?A:B)
|
#define MAX(A,B) (A>B?A:B)
|
||||||
|
|
||||||
int
|
void ExecCmd::putenv(const string &ea)
|
||||||
ExecCmd::doexec(const string &cmd, const list<string>& args,
|
{
|
||||||
|
env.push_back(ea);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ExecCmd::doexec(const string &cmd, const list<string>& args,
|
||||||
const string *input, string *output)
|
const string *input, string *output)
|
||||||
{
|
{
|
||||||
{
|
{ // Debug and logging
|
||||||
string command = cmd + " ";
|
string command = cmd + " ";
|
||||||
for (list<string>::const_iterator it = args.begin();it != args.end();
|
for (list<string>::const_iterator it = args.begin();it != args.end();
|
||||||
it++) {
|
it++) {
|
||||||
@ -186,6 +190,10 @@ ExecCmd::doexec(const string &cmd, const list<string>& args,
|
|||||||
while (argv[i]) cerr << argv[i++] << endl;}
|
while (argv[i]) cerr << argv[i++] << endl;}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
for (it = env.begin(); it != env.end(); it++) {
|
||||||
|
::putenv(it->c_str());
|
||||||
|
}
|
||||||
|
|
||||||
execvp(cmd.c_str(), (char *const*)argv);
|
execvp(cmd.c_str(), (char *const*)argv);
|
||||||
// Hu ho
|
// Hu ho
|
||||||
LOGERR(("ExecCmd::doexec: execvp(%s) failed. errno %d\n", cmd.c_str(),
|
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;
|
input = data;
|
||||||
string *ip = 0;
|
string *ip = 0;
|
||||||
//ip = &input;
|
//ip = &input;
|
||||||
|
mexec.putenv("TESTVARIABLE1=TESTVALUE1");
|
||||||
|
mexec.putenv("TESTVARIABLE2=TESTVALUE2");
|
||||||
|
mexec.putenv("TESTVARIABLE3=TESTVALUE3");
|
||||||
|
|
||||||
int status = mexec.doexec(cmd, l, ip, &output);
|
int status = mexec.doexec(cmd, l, ip, &output);
|
||||||
|
|
||||||
fprintf(stderr, "Status: 0x%x\n", status);
|
fprintf(stderr, "Status: 0x%x\n", status);
|
||||||
cout << "Output:" << output << endl;
|
cout << "Output:" << output << endl;
|
||||||
exit (status >> 8);
|
exit (status >> 8);
|
||||||
|
|||||||
@ -1,17 +1,40 @@
|
|||||||
#ifndef _EXECMD_H_INCLUDED_
|
#ifndef _EXECMD_H_INCLUDED_
|
||||||
#define _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 <string>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
|
||||||
|
/**
|
||||||
|
Execute command possibly taking both input and output (will do
|
||||||
|
asynchronous io as appropriate for things to work).
|
||||||
|
*/
|
||||||
class ExecCmd {
|
class ExecCmd {
|
||||||
public:
|
public:
|
||||||
// ExecCmd() : argv(0) {};
|
/**
|
||||||
// ~ExeCmd() {delete [] argv;}
|
* Execute command. Both input and output can be specified, and
|
||||||
int doexec(const std::string &cmd, const std::list<std::string>& a,
|
* 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,
|
const std::string *input = 0,
|
||||||
std::string *output = 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