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 
41 namespace Pt {
42 
43 namespace System {
44 
50 class PT_SYSTEM_API ProcessFailed : public SystemError
51 {
52  public:
55  ProcessFailed();
56 
59  ~ProcessFailed() throw()
60  {}
61 };
62 
63 
66 {
67  public:
70  enum IOMode
71  {
72  Keep = 0,
73  Close = 1,
74  Redirect = 2,
75  ToStdOut = 3
76  };
77 
80  explicit ProcessInfo(const Path& command);
81 
84  const Path& command() const
85  { return _command; }
86 
89  ProcessInfo& addArg(const std::string& argument)
90  { _args.push_back(argument); return *this; }
91 
94  std::size_t argCount() const
95  { return _args.size(); }
96 
99  const std::string& arg(std::size_t idx) const
100  { return _args.at(idx); }
101 
104  bool isDetached() const
105  { return _detach; }
106 
109  void setDetached(bool sw)
110  { _detach = sw; }
111 
114  void setStdInput(IOMode mode)
115  { _stdinMode = mode; }
116 
119  bool isStdInputClosed() const
120  { return (_stdinMode & Close) == Close; }
121 
124  bool isStdInputRedirected() const
125  { return (_stdinMode & Redirect) == Redirect; }
126 
129  void setStdOutput(IOMode mode)
130  { _stdoutMode = mode; }
131 
134  bool isStdOutputClosed() const
135  { return (_stdoutMode & Close) == Close; }
136 
140  { return (_stdoutMode & Redirect) == Redirect; }
141 
144  void setStdError(IOMode mode)
145  { _stderrMode = mode; }
146 
149  bool isStdErrorClosed() const
150  { return (_stderrMode & Close) == Close; }
151 
154  bool isStdErrorRedirected() const
155  { return (_stderrMode & Redirect) == Redirect; }
156 
159  bool isStdErrorAsOutput() const
160  { return (_stderrMode & ToStdOut) == ToStdOut; }
161 
162  private:
163  Path _command;
164  std::vector<std::string> _args;
165  bool _detach;
166  IOMode _stdinMode;
167  IOMode _stdoutMode;
168  IOMode _stderrMode;
169 };
170 
172 class PT_SYSTEM_API Process : private NonCopyable
173 {
174  public:
177  enum State
178  {
179  Ready = 0,
180  Running = 1,
181  Finished = 2,
182  Failed = 3
183  };
184 
185  public:
187  explicit Process(const ProcessInfo& procInfo);
188 
190  ~Process();
191 
193  const ProcessInfo& procInfo() const;
194 
196  State state() const;
197 
202  void start();
203 
208  void kill();
209 
214  int wait();
215 
216  bool tryWait(int& status);
217 
219  IODevice* stdInput();
220 
222  IODevice* stdOutput();
223 
225  IODevice* stdError();
226 
227  private:
228  class ProcessImpl *_impl;
229 };
230 
231 
232 inline ProcessInfo::ProcessInfo(const Path& command)
233 : _command(command)
234 , _detach(false)
235 , _stdinMode(Close)
236 , _stdoutMode(Close)
237 , _stderrMode(Close)
238 {
239 }
240 
241 } // namespace System
242 
243 } // namespace Pt
244 
245 #endif // PT_SYSTEM_PROCESS_H
246 
void setDetached(bool sw)
Process should detach.
Definition: Process.h:109
Protects derived classes from being copied.
Definition: NonCopyable.h:54
ProcessInfo & addArg(const std::string &argument)
Adds an argument to the list of arguments.
Definition: Process.h:89
Keep open.
Definition: Process.h:72
bool isStdErrorAsOutput() const
Returns true if stderr and atdout wil be combined.
Definition: Process.h:159
Represents a path in the file-system.
Definition: Path.h:47
~ProcessFailed()
Destructor.
Definition: Process.h:59
void setStdError(IOMode mode)
Sets I/O flags for stderr.
Definition: Process.h:144
Close I/O.
Definition: Process.h:73
Combine stderr with stdout, only valid for stderr.
Definition: Process.h:75
bool isStdInputClosed() const
Returns true if stdin will be closed.
Definition: Process.h:119
Redirect I/O.
Definition: Process.h:74
bool isStdErrorClosed() const
Returns true if stderr will be closed.
Definition: Process.h:149
ProcessInfo(const Path &command)
Construct with command to execute.
Definition: Process.h:232
bool isStdErrorRedirected() const
Returns true if stderr will be redirected.
Definition: Process.h:154
const std::string & arg(std::size_t idx) const
Returns a command line argument.
Definition: Process.h:99
std::size_t argCount() const
Number of command line arguments.
Definition: Process.h:94
bool isStdOutputRedirected() const
Returns true if stdout will be redirected.
Definition: Process.h:139
bool isStdOutputClosed() const
Returns true if stdout will be closed.
Definition: Process.h:134
void setStdInput(IOMode mode)
Sets I/O flags for stdin.
Definition: Process.h:114
Endpoint for I/O operations.
Definition: IODevice.h:55
Exception class indication a system error.
Definition: SystemError.h:42
const Path & command() const
Command to execute.
Definition: Process.h:84
void setStdOutput(IOMode mode)
Sets I/O flags for stdout.
Definition: Process.h:129
State
State of the process.
Definition: Process.h:177
Executes shell commands.
Definition: Process.h:172
Process startup parameters
Definition: Process.h:65
bool isStdInputRedirected() const
Returns true if stdin will be redirected.
Definition: Process.h:124
Indicates process failure.
Definition: Process.h:50
bool isDetached() const
Returns true if process should detach.
Definition: Process.h:104
IOMode
Flags for process I/O.
Definition: Process.h:70