Process.h
1/*
2 * Copyright (C) 2006-2008 by Marc Boris Duerner
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#ifndef PT_SYSTEM_PROCESS_H
30#define PT_SYSTEM_PROCESS_H
31
32#include <Pt/System/Api.h>
33#include <Pt/System/Path.h>
34#include <Pt/System/IODevice.h>
35#include <Pt/System/SystemError.h>
36#include <Pt/NonCopyable.h>
37#include <string>
38#include <vector>
39#include <cstddef>
40
41namespace Pt {
42
43namespace System {
44
49class PT_SYSTEM_API ProcessFailed : public SystemError
50{
51 public:
55
59 {}
60};
61
62
68{
69 public:
72 enum IOMode
73 {
74 Keep = 0,
75 Close = 1,
78 };
79
82 explicit ProcessInfo(const Path& command);
83
86 const Path& command() const
87 { return _command; }
88
91 ProcessInfo& addArg(const std::string& argument)
92 { _args.push_back(argument); return *this; }
93
96 std::size_t argCount() const
97 { return _args.size(); }
98
101 const std::string& arg(std::size_t idx) const
102 { return _args.at(idx); }
103
106 std::string toString() const;
107
110 bool isDetached() const
111 { return _detach; }
112
115 void setDetached(bool sw)
116 { _detach = sw; }
117
121 { _stdinMode = mode; }
122
125 bool isStdInputClosed() const
126 { return (_stdinMode & Close) == Close; }
127
131 { return (_stdinMode & Redirect) == Redirect; }
132
136 { _stdoutMode = mode; }
137
140 bool isStdOutputClosed() const
141 { return (_stdoutMode & Close) == Close; }
142
146 { return (_stdoutMode & Redirect) == Redirect; }
147
151 { _stderrMode = mode; }
152
155 bool isStdErrorClosed() const
156 { return (_stderrMode & Close) == Close; }
157
161 { return (_stderrMode & Redirect) == Redirect; }
162
166 { return (_stderrMode & ToStdOut) == ToStdOut; }
167
168 private:
169 Path _command;
170 std::vector<std::string> _args;
171 bool _detach;
172 IOMode _stdinMode;
173 IOMode _stdoutMode;
174 IOMode _stderrMode;
175};
176
191class PT_SYSTEM_API Process : private NonCopyable
192{
193 public:
196 enum State
197 {
198 Ready = 0,
202 };
203
204 public:
206 explicit Process(const ProcessInfo& procInfo);
207
210
212 const ProcessInfo& procInfo() const;
213
215 State state() const;
216
221 void start();
222
227 void kill();
228
233 int wait();
234
240 bool tryWait(int& status);
241
244
247
250
251 private:
252 class ProcessImpl *_impl;
253};
254
255
257: _command(command)
258, _detach(false)
259, _stdinMode(Close)
260, _stdoutMode(Close)
261, _stderrMode(Close)
262{
263}
264
265
266inline std::string ProcessInfo::toString() const
267{
268 std::string cmdline = _command.toLocal();
269
270 for( std::size_t i = 0; i < _args.size(); ++i )
271 cmdline += ' ' + _args[i];
272
273 return cmdline;
274}
275
276} // namespace System
277
278} // namespace Pt
279
280#endif // PT_SYSTEM_PROCESS_H
281
NonCopyable()
Default constructor.
Definition NonCopyable.h:63
Endpoint for I/O operations.
Definition IODevice.h:96
Represents a path in the file-system.
Definition Path.h:48
ProcessFailed()
Default Constructor.
~ProcessFailed()
Destructor.
Definition Process.h:58
Startup parameters for a Process.
Definition Process.h:68
std::size_t argCount() const
Number of command line arguments.
Definition Process.h:96
void setDetached(bool sw)
Process should detach.
Definition Process.h:115
void setStdError(IOMode mode)
Sets I/O flags for stderr.
Definition Process.h:150
ProcessInfo(const Path &command)
Construct with command to execute.
Definition Process.h:256
std::string toString() const
Returns full command line.
Definition Process.h:266
ProcessInfo & addArg(const std::string &argument)
Adds an argument to the list of arguments.
Definition Process.h:91
bool isStdErrorClosed() const
Returns true if stderr will be closed.
Definition Process.h:155
bool isStdInputClosed() const
Returns true if stdin will be closed.
Definition Process.h:125
IOMode
Flags for process I/O.
Definition Process.h:73
@ Redirect
Redirect I/O.
Definition Process.h:76
@ ToStdOut
Combine stderr with stdout, only valid for stderr.
Definition Process.h:77
@ Close
Close I/O.
Definition Process.h:75
@ Keep
Keep open.
Definition Process.h:74
bool isStdInputRedirected() const
Returns true if stdin will be redirected.
Definition Process.h:130
void setStdOutput(IOMode mode)
Sets I/O flags for stdout.
Definition Process.h:135
const std::string & arg(std::size_t idx) const
Returns a command line argument.
Definition Process.h:101
bool isStdOutputClosed() const
Returns true if stdout will be closed.
Definition Process.h:140
void setStdInput(IOMode mode)
Sets I/O flags for stdin.
Definition Process.h:120
const Path & command() const
Command to execute.
Definition Process.h:86
bool isStdErrorAsOutput() const
Returns true if stderr and atdout wil be combined.
Definition Process.h:165
bool isDetached() const
Returns true if process should detach.
Definition Process.h:110
bool isStdOutputRedirected() const
Returns true if stdout will be redirected.
Definition Process.h:145
bool isStdErrorRedirected() const
Returns true if stderr will be redirected.
Definition Process.h:160
Process(const ProcessInfo &procInfo)
Constructs with a process parameters.
bool tryWait(int &status)
Returns true if the process has finished.
State
State of the process.
Definition Process.h:197
@ Ready
Ready to run.
Definition Process.h:198
@ Failed
Execution has failed.
Definition Process.h:201
@ Running
Currently running.
Definition Process.h:199
@ Finished
Finished to run.
Definition Process.h:200
void start()
Start/Create the Process.
IODevice * stdError()
Returns an I/O device to stderr.
IODevice * stdOutput()
Returns an I/O device to stdout.
int wait()
Waits until the Process ends.
IODevice * stdInput()
Returns an I/O device to stdin.
void kill()
Kills the Process.
State state() const
Returns the current state.
const ProcessInfo & procInfo() const
Returns the process parameters.
~Process()
Destructor.
SystemError(const std::string &what)
Construct with error message.
System programming
Definition Connection.h:26
Core module.
Definition Allocator.h:33