1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        process.h
3 // Purpose:     wxProcess class
4 // Author:      Guilhem Lavaux
5 // Modified by: Vadim Zeitlin to check error codes, added Detach() method
6 // Created:     24/06/98
7 // RCS-ID:      $Id: process.h,v 1.1 2006/12/02 15:58:26 scara Exp $
8 // Copyright:   (c) 1998 Guilhem Lavaux
9 // Licence:     wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11 
12 #ifndef _WX_PROCESSH__
13 #define _WX_PROCESSH__
14 
15 #if defined(__GNUG__) && !defined(__APPLE__)
16     #pragma interface "process.h"
17 #endif
18 
19 #include "wx/event.h"
20 
21 #if wxUSE_STREAMS
22     #include "wx/stream.h"
23 #endif
24 
25 #include "wx/utils.h"       // for wxSignal
26 
27 // the wxProcess creation flags
28 enum
29 {
30     // no redirection
31     wxPROCESS_DEFAULT = 0,
32 
33     // redirect the IO of the child process
34     wxPROCESS_REDIRECT = 1
35 };
36 
37 // ----------------------------------------------------------------------------
38 // A wxProcess object should be passed to wxExecute - than its OnTerminate()
39 // function will be called when the process terminates.
40 // ----------------------------------------------------------------------------
41 
42 class WXDLLEXPORT wxProcess : public wxEvtHandler
43 {
44 public:
45     // kill the process with the given PID
46     static wxKillError Kill(int pid, wxSignal sig = wxSIGTERM);
47 
48     // test if the given process exists
49     static bool Exists(int pid);
50 
51     // this function replaces the standard popen() one: it launches a process
52     // asynchronously and allows the caller to get the streams connected to its
53     // std{in|out|err}
54     //
55     // on error NULL is returned, in any case the process object will be
56     // deleted automatically when the process terminates and should *not* be
57     // deleted by the caller
58     static wxProcess *Open(const wxString& cmd, int flags = wxEXEC_ASYNC);
59 
60 
61     // ctors
62     wxProcess(wxEvtHandler *parent = (wxEvtHandler *) NULL, int id = -1)
63         { Init(parent, id, wxPROCESS_DEFAULT); }
64 
wxProcess(int flags)65     wxProcess(int flags) { Init(NULL, -1, flags); }
66 
67     virtual ~wxProcess();
68 
69     // may be overridden to be notified about process termination
70     virtual void OnTerminate(int pid, int status);
71 
72     // call this before passing the object to wxExecute() to redirect the
73     // launched process stdin/stdout, then use GetInputStream() and
74     // GetOutputStream() to get access to them
Redirect()75     void Redirect() { m_redirect = TRUE; }
IsRedirected()76     bool IsRedirected() const { return m_redirect; }
77 
78     // detach from the parent - should be called by the parent if it's deleted
79     // before the process it started terminates
80     void Detach();
81 
82 #if wxUSE_STREAMS
83     // Pipe handling
GetInputStream()84     wxInputStream *GetInputStream() const { return m_inputStream; }
GetErrorStream()85     wxInputStream *GetErrorStream() const { return m_errorStream; }
GetOutputStream()86     wxOutputStream *GetOutputStream() const { return m_outputStream; }
87 
88     // close the output stream indicating that nothing more will be written
CloseOutput()89     void CloseOutput() { delete m_outputStream; m_outputStream = NULL; }
90 
91     // return TRUE if the child process stdout is not closed
92     bool IsInputOpened() const;
93 
94     // return TRUE if any input is available on the child process stdout/err
95     bool IsInputAvailable() const;
96     bool IsErrorAvailable() const;
97 
98     // implementation only (for wxExecute)
99     //
100     // NB: the streams passed here should correspond to the child process
101     //     stdout, stdin and stderr and here the normal naming convention is
102     //     used unlike elsewhere in this class
103     void SetPipeStreams(wxInputStream *outStream,
104                         wxOutputStream *inStream,
105                         wxInputStream *errStream);
106 #endif // wxUSE_STREAMS
107 
108     // for backwards compatibility only, don't use
109 #if WXWIN_COMPATIBILITY_2_2
wxProcess(wxEvtHandler * parent,bool redirect)110     wxProcess(wxEvtHandler *parent, bool redirect)
111         { Init(parent, -1, redirect ? wxPROCESS_REDIRECT : wxPROCESS_DEFAULT); }
112 #endif // WXWIN_COMPATIBILITY_2_2
113 
114 protected:
115     void Init(wxEvtHandler *parent, int id, int flags);
116 
117     int m_id;
118 
119 #if wxUSE_STREAMS
120     // these streams are connected to stdout, stderr and stdin of the child
121     // process respectively (yes, m_inputStream corresponds to stdout -- very
122     // confusing but too late to change now)
123     wxInputStream  *m_inputStream,
124                    *m_errorStream;
125     wxOutputStream *m_outputStream;
126 #endif // wxUSE_STREAMS
127 
128     bool m_redirect;
129 
130     DECLARE_DYNAMIC_CLASS(wxProcess)
131 };
132 
133 // ----------------------------------------------------------------------------
134 // wxProcess events
135 // ----------------------------------------------------------------------------
136 
137 BEGIN_DECLARE_EVENT_TYPES()
138     DECLARE_EVENT_TYPE(wxEVT_END_PROCESS, 440)
END_DECLARE_EVENT_TYPES()139 END_DECLARE_EVENT_TYPES()
140 
141 class WXDLLEXPORT wxProcessEvent : public wxEvent
142 {
143 public:
144     wxProcessEvent(int id = 0, int pid = 0, int exitcode = 0) : wxEvent(id)
145     {
146         m_eventType = wxEVT_END_PROCESS;
147         m_pid = pid;
148         m_exitcode = exitcode;
149     }
150 
151     // accessors
152         // PID of process which terminated
153     int GetPid() { return m_pid; }
154 
155         // the exit code
156     int GetExitCode() { return m_exitcode; }
157 
158     // implement the base class pure virtual
159     virtual wxEvent *Clone() const { return new wxProcessEvent(*this); }
160 
161 public:
162     int m_pid,
163         m_exitcode;
164 
165     DECLARE_DYNAMIC_CLASS(wxProcessEvent)
166 };
167 
168 typedef void (wxEvtHandler::*wxProcessEventFunction)(wxProcessEvent&);
169 
170 #define EVT_END_PROCESS(id, func) \
171    DECLARE_EVENT_TABLE_ENTRY( \
172            wxEVT_END_PROCESS, id, -1, \
173            (wxObjectEventFunction) \
174            (wxEventFunction) \
175            (wxProcessEventFunction) & func, NULL),
176 
177 #endif
178     // _WX_PROCESSH__
179