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 
49 class PT_SYSTEM_API ProcessFailed : public SystemError
50 {
51  public:
55 
58  ~ProcessFailed() throw()
59  {}
60 };
61 
62 
68 {
69  public:
72  enum IOMode
73  {
74  Keep = 0,
75  Close = 1,
76  Redirect = 2,
77  ToStdOut = 3
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 
120  void setStdInput(IOMode mode)
121  { _stdinMode = mode; }
122 
125  bool isStdInputClosed() const
126  { return (_stdinMode & Close) == Close; }
127 
130  bool isStdInputRedirected() const
131  { return (_stdinMode & Redirect) == Redirect; }
132 
135  void setStdOutput(IOMode mode)
136  { _stdoutMode = mode; }
137 
140  bool isStdOutputClosed() const
141  { return (_stdoutMode & Close) == Close; }
142 
146  { return (_stdoutMode & Redirect) == Redirect; }
147 
150  void setStdError(IOMode mode)
151  { _stderrMode = mode; }
152 
155  bool isStdErrorClosed() const
156  { return (_stderrMode & Close) == Close; }
157 
160  bool isStdErrorRedirected() const
161  { return (_stderrMode & Redirect) == Redirect; }
162 
165  bool isStdErrorAsOutput() const
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 
191 class PT_SYSTEM_API Process : private NonCopyable
192 {
193  public:
196  enum State
197  {
198  Ready = 0,
199  Running = 1,
200  Finished = 2,
201  Failed = 3
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 
256 inline ProcessInfo::ProcessInfo(const Path& command)
257 : _command(command)
258 , _detach(false)
259 , _stdinMode(Close)
260 , _stdoutMode(Close)
261 , _stderrMode(Close)
262 {
263 }
264 
265 
266 inline 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 
Core module.
Definition: Allocator.h:33
bool isStdInputRedirected() const
Returns true if stdin will be redirected.
Definition: Process.h:130
IOMode
Flags for process I/O.
Definition: Process.h:73
bool isStdOutputClosed() const
Returns true if stdout will be closed.
Definition: Process.h:140
bool isDetached() const
Returns true if process should detach.
Definition: Process.h:110
int wait()
Waits until the Process ends.
Starts another program as a child process.
Definition: Process.h:192
ProcessFailed()
Default Constructor.
IODevice * stdOutput()
Returns an I/O device to stdout.
Thrown when a process does not terminate normally.
Definition: Process.h:50
void setStdInput(IOMode mode)
Sets I/O flags for stdin.
Definition: Process.h:120
void setDetached(bool sw)
Process should detach.
Definition: Process.h:115
bool isStdOutputRedirected() const
Returns true if stdout will be redirected.
Definition: Process.h:145
@ Redirect
Redirect I/O.
Definition: Process.h:76
~ProcessFailed()
Destructor.
Definition: Process.h:58
~Process()
Destructor.
bool isStdErrorAsOutput() const
Returns true if stderr and atdout wil be combined.
Definition: Process.h:165
Exception for a failed system operation.
Definition: SystemError.h:45
const ProcessInfo & procInfo() const
Returns the process parameters.
bool isStdErrorRedirected() const
Returns true if stderr will be redirected.
Definition: Process.h:160
@ Keep
Keep open.
Definition: Process.h:74
bool isStdInputClosed() const
Returns true if stdin will be closed.
Definition: Process.h:125
void setStdOutput(IOMode mode)
Sets I/O flags for stdout.
Definition: Process.h:135
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:197
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
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:101
Endpoint for I/O operations.
Definition: IODevice.h:96
@ Close
Close I/O.
Definition: Process.h:75
@ ToStdOut
Combine stderr with stdout, only valid for stderr.
Definition: Process.h:77
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:96
Base class that disables copy construction and assignment.
Definition: NonCopyable.h:59
const Path & command() const
Command to execute.
Definition: Process.h:86
bool isStdErrorClosed() const
Returns true if stderr will be closed.
Definition: Process.h:155
bool tryWait(int &status)
Returns true if the process has finished.
Startup parameters for a Process.
Definition: Process.h:68
ProcessInfo(const Path &command)
Construct with command to execute.
Definition: Process.h:256
void setStdError(IOMode mode)
Sets I/O flags for stderr.
Definition: Process.h:150
std::string toLocal() const
Returns the path name in local encoding.