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:
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  std::string toString() const;
105 
108  bool isDetached() const
109  { return _detach; }
110 
113  void setDetached(bool sw)
114  { _detach = sw; }
115 
118  void setStdInput(IOMode mode)
119  { _stdinMode = mode; }
120 
123  bool isStdInputClosed() const
124  { return (_stdinMode & Close) == Close; }
125 
128  bool isStdInputRedirected() const
129  { return (_stdinMode & Redirect) == Redirect; }
130 
133  void setStdOutput(IOMode mode)
134  { _stdoutMode = mode; }
135 
138  bool isStdOutputClosed() const
139  { return (_stdoutMode & Close) == Close; }
140 
144  { return (_stdoutMode & Redirect) == Redirect; }
145 
148  void setStdError(IOMode mode)
149  { _stderrMode = mode; }
150 
153  bool isStdErrorClosed() const
154  { return (_stderrMode & Close) == Close; }
155 
158  bool isStdErrorRedirected() const
159  { return (_stderrMode & Redirect) == Redirect; }
160 
163  bool isStdErrorAsOutput() const
164  { return (_stderrMode & ToStdOut) == ToStdOut; }
165 
166  private:
167  Path _command;
168  std::vector<std::string> _args;
169  bool _detach;
170  IOMode _stdinMode;
171  IOMode _stdoutMode;
172  IOMode _stderrMode;
173 };
174 
176 class PT_SYSTEM_API Process : private NonCopyable
177 {
178  public:
181  enum State
182  {
183  Ready = 0,
184  Running = 1,
185  Finished = 2,
186  Failed = 3
187  };
188 
189  public:
191  explicit Process(const ProcessInfo& procInfo);
192 
195 
197  const ProcessInfo& procInfo() const;
198 
200  State state() const;
201 
206  void start();
207 
212  void kill();
213 
218  int wait();
219 
220  bool tryWait(int& status);
221 
224 
227 
230 
231  private:
232  class ProcessImpl *_impl;
233 };
234 
235 
236 inline ProcessInfo::ProcessInfo(const Path& command)
237 : _command(command)
238 , _detach(false)
239 , _stdinMode(Close)
240 , _stdoutMode(Close)
241 , _stderrMode(Close)
242 {
243 }
244 
245 
246 inline std::string ProcessInfo::toString() const
247 {
248  std::string cmdline = _command.toLocal();
249 
250  for( std::size_t i = 0; i < _args.size(); ++i )
251  cmdline += ' ' + _args[i];
252 
253  return cmdline;
254 }
255 
256 } // namespace System
257 
258 } // namespace Pt
259 
260 #endif // PT_SYSTEM_PROCESS_H
261 
Core module.
Definition: pt-gfx-images.dox:14
bool isStdInputRedirected() const
Returns true if stdin will be redirected.
Definition: Process.h:128
IOMode
Flags for process I/O.
Definition: Process.h:71
bool isStdOutputClosed() const
Returns true if stdout will be closed.
Definition: Process.h:138
bool isDetached() const
Returns true if process should detach.
Definition: Process.h:108
int wait()
Waits until the Process ends.
Executes shell commands.
Definition: Process.h:177
ProcessFailed()
Default Constructor.
IODevice * stdOutput()
Returns an I/O device to stdout.
Indicates process failure.
Definition: Process.h:51
void setStdInput(IOMode mode)
Sets I/O flags for stdin.
Definition: Process.h:118
void setDetached(bool sw)
Process should detach.
Definition: Process.h:113
bool isStdOutputRedirected() const
Returns true if stdout will be redirected.
Definition: Process.h:143
@ Redirect
Redirect I/O.
Definition: Process.h:74
~ProcessFailed()
Destructor.
Definition: Process.h:59
~Process()
Destructor.
bool isStdErrorAsOutput() const
Returns true if stderr and atdout wil be combined.
Definition: Process.h:163
Exception class indication a system error.
Definition: SystemError.h:43
const ProcessInfo & procInfo() const
Returns the process parameters.
bool isStdErrorRedirected() const
Returns true if stderr will be redirected.
Definition: Process.h:158
@ Keep
Keep open.
Definition: Process.h:72
bool isStdInputClosed() const
Returns true if stdin will be closed.
Definition: Process.h:123
void setStdOutput(IOMode mode)
Sets I/O flags for stdout.
Definition: Process.h:133
IODevice * stdInput()
Returns an I/O device to stdin.
Process(const ProcessInfo &procInfo)
Constructs with a process parameters.
void kill()
Kills the Process.
State
State of the process.
Definition: Process.h:182
std::string toString() const
Returns full command line.
Definition: Process.h:246
ProcessInfo & addArg(const std::string &argument)
Adds an argument to the list of arguments.
Definition: Process.h:89
IODevice * stdError()
Returns an I/O device to stderr.
Represents a path in the file-system.
Definition: Path.h:48
const std::string & arg(std::size_t idx) const
Returns a command line argument.
Definition: Process.h:99
Endpoint for I/O operations.
Definition: IODevice.h:56
@ Close
Close I/O.
Definition: Process.h:73
@ ToStdOut
Combine stderr with stdout, only valid for stderr.
Definition: Process.h:75
State state() const
Returns the current state.
void start()
Start/Create the Process.
std::size_t argCount() const
Number of command line arguments.
Definition: Process.h:94
Protects derived classes from being copied.
Definition: NonCopyable.h:54
const Path & command() const
Command to execute.
Definition: Process.h:84
bool isStdErrorClosed() const
Returns true if stderr will be closed.
Definition: Process.h:153
Process startup parameters
Definition: Process.h:66
ProcessInfo(const Path &command)
Construct with command to execute.
Definition: Process.h:236
void setStdError(IOMode mode)
Sets I/O flags for stderr.
Definition: Process.h:148
std::string toLocal() const
Returns the path name in local encoding.