Switch to unified view

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
19
#include <signal.h>
20
#include <signal.h>
21
20
#include <string>
22
#include <string>
21
#include <vector>
23
#include <vector>
22
#include <stack>
24
#include <stack>
23
#ifndef NO_NAMESPACES
24
using std::string;
25
using std::vector;
26
using std::stack;
27
#endif
28
25
29
#include "netcon.h"
26
#include "netcon.h"
30
27
31
/** 
28
/** 
32
 * Callback function object to advise of new data arrival, or just periodic 
29
 * Callback function object to advise of new data arrival, or just periodic 
...
...
83
     * Add/replace environment variable before executing command. This must
80
     * Add/replace environment variable before executing command. This must
84
     * be called before doexec() to have an effect (possibly multiple
81
     * be called before doexec() to have an effect (possibly multiple
85
     * times for several variables).
82
     * times for several variables).
86
     * @param envassign an environment assignment string ("name=value")
83
     * @param envassign an environment assignment string ("name=value")
87
     */
84
     */
88
    void putenv(const string &envassign);
85
    void putenv(const std::string &envassign);
89
    void putenv(const string &name, const string& value);
86
    void putenv(const std::string &name, const std::string& value);
90
87
91
    /** 
88
    /** 
92
     * Set function objects to call whenever new data is available or on
89
     * Set function objects to call whenever new data is available or on
93
     * select timeout / whenever new data is needed to send. Must be called
90
     * select timeout / whenever new data is needed to send. Must be called
94
     * before doexec()
91
     * before doexec()
...
...
108
     * usually go to the terminal or to wherever the desktop messages go).
105
     * usually go to the terminal or to wherever the desktop messages go).
109
     * There is currently no option to put stderr data into a program variable
106
     * There is currently no option to put stderr data into a program variable
110
     * If the parameter can't be opened for writing, the command's
107
     * If the parameter can't be opened for writing, the command's
111
     * stderr will be closed.
108
     * stderr will be closed.
112
     */
109
     */
113
    void setStderr(const string &stderrFile) {m_stderrFile = stderrFile;}
110
    void setStderr(const std::string &stderrFile) {m_stderrFile = stderrFile;}
114
111
115
    /**
112
    /**
116
     * Execute command. 
113
     * Execute command. 
117
     *
114
     *
118
     * Both input and output can be specified, and asynchronous 
115
     * Both input and output can be specified, and asynchronous 
...
...
127
     * @param args the argument vector (NOT including argv[0]).
124
     * @param args the argument vector (NOT including argv[0]).
128
     * @param input Input to send TO the command.
125
     * @param input Input to send TO the command.
129
     * @param output Output FROM the command.
126
     * @param output Output FROM the command.
130
     * @return the exec ouput status (0 if ok), or -1
127
     * @return the exec ouput status (0 if ok), or -1
131
     */
128
     */
132
    int doexec(const string &cmd, const vector<string>& args, 
129
    int doexec(const std::string &cmd, const std::vector<std::string>& args, 
133
           const string *input = 0, 
130
           const std::string *input = 0, 
134
           string *output = 0);
131
           std::string *output = 0);
135
132
136
    /*
133
    /*
137
     * The next four methods can be used when a Q/A dialog needs to be 
134
     * The next four methods can be used when a Q/A dialog needs to be 
138
     * performed with the command
135
     * performed with the command
139
     */
136
     */
140
    int startExec(const string &cmd, const vector<string>& args, 
137
    int startExec(const std::string &cmd, const std::vector<std::string>& args, 
141
          bool has_input, bool has_output);
138
          bool has_input, bool has_output);
142
    int send(const string& data);
139
    int send(const std::string& data);
143
    int receive(string& data, int cnt = -1);
140
    int receive(std::string& data, int cnt = -1);
144
    int getline(string& data);
141
    int getline(std::string& data);
145
    int wait();
142
    int wait();
146
    /** Wait with WNOHANG set. 
143
    /** Wait with WNOHANG set. 
147
    @return true if process exited, false else.
144
    @return true if process exited, false else.
148
    @param O: status, the wait(2) call's status value */
145
    @param O: status, the wait(2) call's status value */
149
    bool maybereap(int *status);
146
    bool maybereap(int *status);
...
...
175
     * @param cmd command name
172
     * @param cmd command name
176
     * @param exe on return, executable path name if found
173
     * @param exe on return, executable path name if found
177
     * @param path exec seach path to use instead of getenv(PATH)
174
     * @param path exec seach path to use instead of getenv(PATH)
178
     * @return true if found
175
     * @return true if found
179
     */
176
     */
180
    static bool which(const string& cmd, string& exe, const char* path = 0);
177
    static bool which(const std::string& cmd, std::string& exe, const char* path = 0);
181
178
182
    /**
179
    /**
183
     * Execute command and return stdout output in a string
180
     * Execute command and return stdout output in a string
184
     * @param cmd input: command and args
181
     * @param cmd input: command and args
185
     * @param out output: what the command printed
182
     * @param out output: what the command printed
...
...
189
186
190
    friend class ExecCmdRsrc;
187
    friend class ExecCmdRsrc;
191
 private:
188
 private:
192
    static bool      o_useVfork;
189
    static bool      o_useVfork;
193
190
194
    vector<string>   m_env;
191
    std::vector<std::string>   m_env;
195
    ExecCmdAdvise   *m_advise;
192
    ExecCmdAdvise   *m_advise;
196
    ExecCmdProvide  *m_provide;
193
    ExecCmdProvide  *m_provide;
197
    bool             m_killRequest;
194
    bool             m_killRequest;
198
    int              m_timeoutMs;
195
    int              m_timeoutMs;
199
    string           m_stderrFile;
196
    std::string           m_stderrFile;
200
    // Pipe for data going to the command
197
    // Pipe for data going to the command
201
    int              m_pipein[2];
198
    int              m_pipein[2];
202
    NetconP          m_tocmd;
199
    NetconP          m_tocmd;
203
    // Pipe for data coming out
200
    // Pipe for data coming out
204
    int              m_pipeout[2];
201
    int              m_pipeout[2];
...
...
215
    m_pipein[0] = m_pipein[1] = m_pipeout[0] = m_pipeout[1] = -1;
212
    m_pipein[0] = m_pipein[1] = m_pipeout[0] = m_pipeout[1] = -1;
216
    m_pid = -1;
213
    m_pid = -1;
217
    sigemptyset(&m_blkcld);
214
    sigemptyset(&m_blkcld);
218
    }
215
    }
219
    // Child process code
216
    // Child process code
220
    inline void dochild(const string &cmd, const char **argv, 
217
    inline void dochild(const std::string &cmd, const char **argv, 
221
            const char **envv, bool has_input, bool has_output);
218
            const char **envv, bool has_input, bool has_output);
222
    /* Copyconst and assignment private and forbidden */
219
    /* Copyconst and assignment private and forbidden */
223
    ExecCmd(const ExecCmd &) {}
220
    ExecCmd(const ExecCmd &) {}
224
    ExecCmd& operator=(const ExecCmd &) {return *this;};
221
    ExecCmd& operator=(const ExecCmd &) {return *this;};
225
};
222
};
...
...
259
    {
256
    {
260
    m_atexitfuncs.push(function);
257
    m_atexitfuncs.push(function);
261
    return 0;
258
    return 0;
262
    }
259
    }
263
    void reexec();
260
    void reexec();
264
    const string& getreason() {return m_reason;}
261
    const std::string& getreason() {return m_reason;}
265
    // Insert new args into the initial argv. idx designates the place
262
    // Insert new args into the initial argv. idx designates the place
266
    // before which the new args are inserted (the default of 1
263
    // before which the new args are inserted (the default of 1
267
    // inserts after argv[0] which would probably be an appropriate
264
    // inserts after argv[0] which would probably be an appropriate
268
    // place for additional options)
265
    // place for additional options)
269
    void insertArgs(const vector<string>& args, int idx = 1);
266
    void insertArgs(const std::vector<std::string>& args, int idx = 1);
270
    void removeArg(const string& arg);
267
    void removeArg(const std::string& arg);
271
private:
268
private:
272
    vector<string> m_argv;
269
    std::vector<std::string> m_argv;
273
    string m_curdir;
270
    std::string m_curdir;
274
    int    m_cfd;
271
    int    m_cfd;
275
    string m_reason;
272
    std::string m_reason;
276
    stack<void (*)(void)> m_atexitfuncs;
273
    std::stack<void (*)(void)> m_atexitfuncs;
277
};
274
};
278
275
279
#endif /* _EXECMD_H_INCLUDED_ */
276
#endif /* _EXECMD_H_INCLUDED_ */