aspell checkpoint
This commit is contained in:
+34
-15
@@ -1,5 +1,5 @@
|
||||
#ifndef lint
|
||||
static char rcsid[] = "@(#$Id: execmd.cpp,v 1.17 2006-04-03 09:42:47 dockes Exp $ (C) 2004 J.F.Dockes";
|
||||
static char rcsid[] = "@(#$Id: execmd.cpp,v 1.18 2006-10-09 16:37:08 dockes Exp $ (C) 2004 J.F.Dockes";
|
||||
#endif
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
@@ -53,7 +53,9 @@ void ExecCmd::putenv(const string &ea)
|
||||
* raised in the callback */
|
||||
class ExecCmdRsrc {
|
||||
public:
|
||||
// Pipe for data going to the command
|
||||
int pipein[2];
|
||||
// Pipe for data coming out
|
||||
int pipeout[2];
|
||||
pid_t pid;
|
||||
ExecCmdRsrc() {
|
||||
@@ -124,26 +126,25 @@ int ExecCmd::doexec(const string &cmd, const list<string>& args,
|
||||
}
|
||||
|
||||
if (e.pid) {
|
||||
// Father process
|
||||
if (input) {
|
||||
close(e.pipein[0]);
|
||||
e.pipein[0] = -1;
|
||||
fcntl(e.pipein[1], F_SETFL, O_NONBLOCK);
|
||||
}
|
||||
if (output) {
|
||||
close(e.pipeout[1]);
|
||||
e.pipeout[1] = -1;
|
||||
fcntl(e.pipeout[0], F_SETFL, O_NONBLOCK);
|
||||
}
|
||||
fd_set readfds, writefds;
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 1;
|
||||
tv.tv_usec = 0;
|
||||
|
||||
if (input || output) {
|
||||
if (input)
|
||||
fcntl(e.pipein[1], F_SETFL, O_NONBLOCK);
|
||||
if (output)
|
||||
fcntl(e.pipeout[0], F_SETFL, O_NONBLOCK);
|
||||
unsigned int nwritten = 0;
|
||||
int nfds = MAX(e.pipein[1], e.pipeout[0]) + 1;
|
||||
fd_set readfds, writefds;
|
||||
struct timeval tv;
|
||||
tv.tv_sec = m_timeoutMs / 1000;
|
||||
tv.tv_usec = 1000 * (m_timeoutMs % 1000);
|
||||
for(; nfds > 0;) {
|
||||
if (m_cancelRequest)
|
||||
break;
|
||||
@@ -240,7 +241,22 @@ int ExecCmd::doexec(const string &cmd, const list<string>& args,
|
||||
e.pipeout[1] = -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Do we need to redirect stderr ?
|
||||
if (!m_stderrFile.empty()) {
|
||||
int fd = open(m_stderrFile.c_str(), O_WRONLY|O_CREAT
|
||||
#ifdef O_APPEND
|
||||
|O_APPEND
|
||||
#endif
|
||||
, 0600);
|
||||
if (fd < 0) {
|
||||
close(2);
|
||||
} else {
|
||||
if (fd != 2) {
|
||||
dup2(fd, 2);
|
||||
}
|
||||
lseek(2, 0, 2);
|
||||
}
|
||||
}
|
||||
e.reset();
|
||||
|
||||
// Allocate arg vector (2 more for arg0 + final 0)
|
||||
@@ -300,10 +316,10 @@ const char *data = "Une ligne de donnees\n";
|
||||
class MEAdv : public ExecCmdAdvise {
|
||||
public:
|
||||
ExecCmd *cmd;
|
||||
void newData(int) {
|
||||
cerr << "New Data!" << endl;
|
||||
CancelCheck::instance().setCancel();
|
||||
CancelCheck::instance().checkCancel();
|
||||
void newData(int cnt) {
|
||||
cerr << "newData(" << cnt << ")" << endl;
|
||||
// CancelCheck::instance().setCancel();
|
||||
// CancelCheck::instance().checkCancel();
|
||||
// cmd->setCancel();
|
||||
}
|
||||
};
|
||||
@@ -325,10 +341,13 @@ int main(int argc, const char **argv)
|
||||
MEAdv adv;
|
||||
adv.cmd = &mexec;
|
||||
mexec.setAdvise(&adv);
|
||||
mexec.setTimeout(500);
|
||||
mexec.setStderr("/tmp/trexecStderr");
|
||||
|
||||
string input, output;
|
||||
input = data;
|
||||
string *ip = 0;
|
||||
//ip = &input;
|
||||
ip = &input;
|
||||
mexec.putenv("TESTVARIABLE1=TESTVALUE1");
|
||||
mexec.putenv("TESTVARIABLE2=TESTVALUE2");
|
||||
mexec.putenv("TESTVARIABLE3=TESTVALUE3");
|
||||
|
||||
+45
-21
@@ -16,10 +16,14 @@
|
||||
*/
|
||||
#ifndef _EXECMD_H_INCLUDED_
|
||||
#define _EXECMD_H_INCLUDED_
|
||||
/* @(#$Id: execmd.h,v 1.8 2006-01-30 11:15:28 dockes Exp $ (C) 2004 J.F.Dockes */
|
||||
/* @(#$Id: execmd.h,v 1.9 2006-10-09 16:37:08 dockes Exp $ (C) 2004 J.F.Dockes */
|
||||
|
||||
#include <string>
|
||||
#include <list>
|
||||
#ifndef NO_NAMESPACES
|
||||
using std::list;
|
||||
using std::string;
|
||||
#endif
|
||||
|
||||
/** Callback function object to advise of new data arrival (or just heartbeat) * if cnt is 0 */
|
||||
class ExecCmdAdvise {
|
||||
@@ -37,46 +41,66 @@ class ExecCmd {
|
||||
/**
|
||||
* 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.
|
||||
* Both input and output can be specified, and asynchronous
|
||||
* io (select-based) is used to prevent blocking. This will not
|
||||
* work if input and output need to be synchronized (ie: Q/A), but
|
||||
* works ok for filtering.
|
||||
* The function is exception-safe. In case an exception occurs in the
|
||||
* advise callback, fds and pids will be cleaned-up properly.
|
||||
*
|
||||
* @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.
|
||||
* @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);
|
||||
int doexec(const string &cmd, const list<string>& args,
|
||||
const string *input = 0,
|
||||
string *output = 0);
|
||||
/**
|
||||
* Add/replace environment variable before executing command. This should
|
||||
* be called before doexec of course (possibly multiple times for several
|
||||
* variables).
|
||||
* Add/replace environment variable before executing command. This must
|
||||
* be called before doexec to have an effect (possibly multiple
|
||||
* times for several variables).
|
||||
* @param envassign an environment assignment string (name=value)
|
||||
*/
|
||||
void putenv(const std::string &envassign);
|
||||
void putenv(const string &envassign);
|
||||
|
||||
/** Set function object to call whenever new data is available */
|
||||
/**
|
||||
* Set function object to call whenever new data is available or on
|
||||
* select timeout.
|
||||
*/
|
||||
void setAdvise(ExecCmdAdvise *adv) {m_advise = adv;}
|
||||
|
||||
/** Cancel exec. This can be called from another thread or from the
|
||||
* advise callback, which could also raise an exception to accomplish
|
||||
* the same thing
|
||||
/**
|
||||
* Set select timeout in milliseconds. The default is 1 S.
|
||||
*/
|
||||
void setTimeout(int mS) {if (mS > 30) m_timeoutMs = mS;}
|
||||
|
||||
/**
|
||||
* Set destination for stderr data. The default is to let it alone (will
|
||||
* usually go to the terminal or to wherever the desktop messages go).
|
||||
* There is currently no option to put stderr data into a program variable
|
||||
* If the parameter can't be opened for writing, the command's
|
||||
* stderr will be closed.
|
||||
*/
|
||||
void setStderr(const string &stderrFile) {m_stderrFile = stderrFile;}
|
||||
|
||||
/**
|
||||
* Cancel/kill command. This can be called from another thread or
|
||||
* from the advise callback, which could also raise an exception to
|
||||
* accomplish the same thing
|
||||
*/
|
||||
void setCancel() {m_cancelRequest = true;}
|
||||
|
||||
ExecCmd() : m_advise(0), m_cancelRequest(false) {}
|
||||
ExecCmd() : m_advise(0), m_cancelRequest(false), m_timeoutMs(1000) {}
|
||||
|
||||
private:
|
||||
std::list<std::string> m_env;
|
||||
list<string> m_env;
|
||||
ExecCmdAdvise *m_advise;
|
||||
bool m_cancelRequest;
|
||||
bool m_cancelRequest;
|
||||
int m_timeoutMs;
|
||||
string m_stderrFile;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user