|
a/src/utils/execmd.h |
|
b/src/utils/execmd.h |
|
... |
|
... |
14 |
* Free Software Foundation, Inc.,
|
14 |
* Free Software Foundation, Inc.,
|
15 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
15 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
16 |
*/
|
16 |
*/
|
17 |
#ifndef _EXECMD_H_INCLUDED_
|
17 |
#ifndef _EXECMD_H_INCLUDED_
|
18 |
#define _EXECMD_H_INCLUDED_
|
18 |
#define _EXECMD_H_INCLUDED_
|
19 |
/* @(#$Id: execmd.h,v 1.8 2006-01-30 11:15:28 dockes Exp $ (C) 2004 J.F.Dockes */
|
19 |
/* @(#$Id: execmd.h,v 1.9 2006-10-09 16:37:08 dockes Exp $ (C) 2004 J.F.Dockes */
|
20 |
|
20 |
|
21 |
#include <string>
|
21 |
#include <string>
|
22 |
#include <list>
|
22 |
#include <list>
|
|
|
23 |
#ifndef NO_NAMESPACES
|
|
|
24 |
using std::list;
|
|
|
25 |
using std::string;
|
|
|
26 |
#endif
|
23 |
|
27 |
|
24 |
/** Callback function object to advise of new data arrival (or just heartbeat) * if cnt is 0 */
|
28 |
/** Callback function object to advise of new data arrival (or just heartbeat) * if cnt is 0 */
|
25 |
class ExecCmdAdvise {
|
29 |
class ExecCmdAdvise {
|
26 |
public:
|
30 |
public:
|
27 |
virtual ~ExecCmdAdvise() {}
|
31 |
virtual ~ExecCmdAdvise() {}
|
|
... |
|
... |
35 |
class ExecCmd {
|
39 |
class ExecCmd {
|
36 |
public:
|
40 |
public:
|
37 |
/**
|
41 |
/**
|
38 |
* Execute command.
|
42 |
* Execute command.
|
39 |
*
|
43 |
*
|
40 |
* Both input and output can be specified, and
|
44 |
* Both input and output can be specified, and asynchronous
|
41 |
* asynchronous io is used to prevent blocking. This wont work if
|
45 |
* io (select-based) is used to prevent blocking. This will not
|
42 |
* input and output need to be synchronized (ie: Q/A), but ok for
|
46 |
* work if input and output need to be synchronized (ie: Q/A), but
|
43 |
* filtering.
|
47 |
* works ok for filtering.
|
44 |
* The function is exception-safe. In case an exception occurs in the
|
48 |
* The function is exception-safe. In case an exception occurs in the
|
45 |
* advise callback, fds and pids will be cleaned-up properly.
|
49 |
* advise callback, fds and pids will be cleaned-up properly.
|
46 |
*
|
50 |
*
|
47 |
* @param cmd the program to execute. This must be an absolute file name
|
51 |
* @param cmd the program to execute. This must be an absolute file name
|
48 |
* or exist in the PATH.
|
52 |
* or exist in the PATH.
|
49 |
* @param args the argument list (NOT including argv[0]).
|
53 |
* @param args the argument list (NOT including argv[0]).
|
50 |
* @param input Input to send to the command.
|
54 |
* @param input Input to send TO the command.
|
51 |
* @param output Output from the command.
|
55 |
* @param output Output FROM the command.
|
52 |
* @return the exec ouput status (0 if ok).
|
56 |
* @return the exec ouput status (0 if ok).
|
53 |
*/
|
57 |
*/
|
54 |
int doexec(const std::string &cmd, const std::list<std::string>& args,
|
58 |
int doexec(const string &cmd, const list<string>& args,
|
55 |
const std::string *input = 0,
|
59 |
const string *input = 0,
|
56 |
std::string *output = 0);
|
60 |
string *output = 0);
|
57 |
/**
|
61 |
/**
|
58 |
* Add/replace environment variable before executing command. This should
|
62 |
* Add/replace environment variable before executing command. This must
|
59 |
* be called before doexec of course (possibly multiple times for several
|
63 |
* be called before doexec to have an effect (possibly multiple
|
60 |
* variables).
|
64 |
* times for several variables).
|
61 |
* @param envassign an environment assignment string (name=value)
|
65 |
* @param envassign an environment assignment string (name=value)
|
62 |
*/
|
66 |
*/
|
63 |
void putenv(const std::string &envassign);
|
67 |
void putenv(const string &envassign);
|
64 |
|
68 |
|
|
|
69 |
/**
|
65 |
/** Set function object to call whenever new data is available */
|
70 |
* Set function object to call whenever new data is available or on
|
|
|
71 |
* select timeout.
|
|
|
72 |
*/
|
66 |
void setAdvise(ExecCmdAdvise *adv) {m_advise = adv;}
|
73 |
void setAdvise(ExecCmdAdvise *adv) {m_advise = adv;}
|
67 |
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Set select timeout in milliseconds. The default is 1 S.
|
|
|
77 |
*/
|
|
|
78 |
void setTimeout(int mS) {if (mS > 30) m_timeoutMs = mS;}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Set destination for stderr data. The default is to let it alone (will
|
|
|
82 |
* usually go to the terminal or to wherever the desktop messages go).
|
|
|
83 |
* There is currently no option to put stderr data into a program variable
|
|
|
84 |
* If the parameter can't be opened for writing, the command's
|
|
|
85 |
* stderr will be closed.
|
|
|
86 |
*/
|
|
|
87 |
void setStderr(const string &stderrFile) {m_stderrFile = stderrFile;}
|
|
|
88 |
|
|
|
89 |
/**
|
68 |
/** Cancel exec. This can be called from another thread or from the
|
90 |
* Cancel/kill command. This can be called from another thread or
|
69 |
* advise callback, which could also raise an exception to accomplish
|
91 |
* from the advise callback, which could also raise an exception to
|
70 |
* the same thing
|
92 |
* accomplish the same thing
|
71 |
*/
|
93 |
*/
|
72 |
void setCancel() {m_cancelRequest = true;}
|
94 |
void setCancel() {m_cancelRequest = true;}
|
73 |
|
95 |
|
74 |
ExecCmd() : m_advise(0), m_cancelRequest(false) {}
|
96 |
ExecCmd() : m_advise(0), m_cancelRequest(false), m_timeoutMs(1000) {}
|
75 |
|
97 |
|
76 |
private:
|
98 |
private:
|
77 |
std::list<std::string> m_env;
|
99 |
list<string> m_env;
|
78 |
ExecCmdAdvise *m_advise;
|
100 |
ExecCmdAdvise *m_advise;
|
79 |
bool m_cancelRequest;
|
101 |
bool m_cancelRequest;
|
|
|
102 |
int m_timeoutMs;
|
|
|
103 |
string m_stderrFile;
|
80 |
};
|
104 |
};
|
81 |
|
105 |
|
82 |
|
106 |
|
83 |
#endif /* _EXECMD_H_INCLUDED_ */
|
107 |
#endif /* _EXECMD_H_INCLUDED_ */
|