1998-07-01 13:26:46 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: process.h
|
|
|
|
// Purpose: wxProcess class
|
|
|
|
// Author: Guilhem Lavaux
|
1999-02-17 12:56:59 -05:00
|
|
|
// Modified by: Vadim Zeitlin to check error codes, added Detach() method
|
1998-07-01 13:26:46 -04:00
|
|
|
// Created: 24/06/98
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 1998 Guilhem Lavaux
|
2004-05-23 16:53:33 -04:00
|
|
|
// Licence: wxWindows licence
|
1998-07-01 13:26:46 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1998-08-14 20:23:28 -04:00
|
|
|
#ifndef _WX_PROCESSH__
|
|
|
|
#define _WX_PROCESSH__
|
1998-07-01 13:26:46 -04:00
|
|
|
|
2003-08-09 08:38:21 -04:00
|
|
|
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
|
1999-02-07 16:26:54 -05:00
|
|
|
#pragma interface "process.h"
|
1998-07-01 13:26:46 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/event.h"
|
2000-07-15 15:51:35 -04:00
|
|
|
|
|
|
|
#if wxUSE_STREAMS
|
2001-01-31 12:21:59 -05:00
|
|
|
#include "wx/stream.h"
|
2000-07-15 15:51:35 -04:00
|
|
|
#endif
|
1998-07-01 13:26:46 -04:00
|
|
|
|
2001-06-15 20:59:07 -04:00
|
|
|
#include "wx/utils.h" // for wxSignal
|
|
|
|
|
2002-07-12 14:15:49 -04:00
|
|
|
// the wxProcess creation flags
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
// no redirection
|
|
|
|
wxPROCESS_DEFAULT = 0,
|
|
|
|
|
|
|
|
// redirect the IO of the child process
|
|
|
|
wxPROCESS_REDIRECT = 1
|
|
|
|
};
|
|
|
|
|
2001-01-31 12:16:40 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-02-07 16:26:54 -05:00
|
|
|
// A wxProcess object should be passed to wxExecute - than its OnTerminate()
|
|
|
|
// function will be called when the process terminates.
|
2001-01-31 12:16:40 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2003-07-01 21:59:24 -04:00
|
|
|
class WXDLLIMPEXP_BASE wxProcess : public wxEvtHandler
|
1998-07-01 13:26:46 -04:00
|
|
|
{
|
1999-02-07 16:26:54 -05:00
|
|
|
public:
|
2002-07-12 14:15:49 -04:00
|
|
|
// kill the process with the given PID
|
|
|
|
static wxKillError Kill(int pid, wxSignal sig = wxSIGTERM);
|
|
|
|
|
|
|
|
// test if the given process exists
|
|
|
|
static bool Exists(int pid);
|
|
|
|
|
|
|
|
// this function replaces the standard popen() one: it launches a process
|
|
|
|
// asynchronously and allows the caller to get the streams connected to its
|
|
|
|
// std{in|out|err}
|
|
|
|
//
|
|
|
|
// on error NULL is returned, in any case the process object will be
|
|
|
|
// deleted automatically when the process terminates and should *not* be
|
|
|
|
// deleted by the caller
|
2002-07-12 19:25:14 -04:00
|
|
|
static wxProcess *Open(const wxString& cmd, int flags = wxEXEC_ASYNC);
|
2002-07-12 14:15:49 -04:00
|
|
|
|
|
|
|
|
|
|
|
// ctors
|
2000-03-02 14:06:13 -05:00
|
|
|
wxProcess(wxEvtHandler *parent = (wxEvtHandler *) NULL, int id = -1)
|
2002-07-12 14:15:49 -04:00
|
|
|
{ Init(parent, id, wxPROCESS_DEFAULT); }
|
|
|
|
|
|
|
|
wxProcess(int flags) { Init(NULL, -1, flags); }
|
1998-07-01 13:26:46 -04:00
|
|
|
|
2000-03-02 14:06:13 -05:00
|
|
|
virtual ~wxProcess();
|
|
|
|
|
|
|
|
// may be overridden to be notified about process termination
|
1999-02-07 16:26:54 -05:00
|
|
|
virtual void OnTerminate(int pid, int status);
|
1998-07-01 13:26:46 -04:00
|
|
|
|
2000-03-02 14:06:13 -05:00
|
|
|
// call this before passing the object to wxExecute() to redirect the
|
|
|
|
// launched process stdin/stdout, then use GetInputStream() and
|
|
|
|
// GetOutputStream() to get access to them
|
|
|
|
void Redirect() { m_redirect = TRUE; }
|
|
|
|
bool IsRedirected() const { return m_redirect; }
|
|
|
|
|
1999-02-17 12:56:59 -05:00
|
|
|
// detach from the parent - should be called by the parent if it's deleted
|
|
|
|
// before the process it started terminates
|
|
|
|
void Detach();
|
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
#if wxUSE_STREAMS
|
2000-02-27 05:44:49 -05:00
|
|
|
// Pipe handling
|
2000-03-02 14:06:13 -05:00
|
|
|
wxInputStream *GetInputStream() const { return m_inputStream; }
|
2000-07-15 15:51:35 -04:00
|
|
|
wxInputStream *GetErrorStream() const { return m_errorStream; }
|
2000-03-02 14:06:13 -05:00
|
|
|
wxOutputStream *GetOutputStream() const { return m_outputStream; }
|
2000-02-27 05:44:49 -05:00
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
// close the output stream indicating that nothing more will be written
|
|
|
|
void CloseOutput() { delete m_outputStream; m_outputStream = NULL; }
|
|
|
|
|
2002-08-20 18:33:20 -04:00
|
|
|
// return TRUE if the child process stdout is not closed
|
|
|
|
bool IsInputOpened() const;
|
|
|
|
|
|
|
|
// return TRUE if any input is available on the child process stdout/err
|
|
|
|
bool IsInputAvailable() const;
|
|
|
|
bool IsErrorAvailable() const;
|
|
|
|
|
2000-03-02 14:06:13 -05:00
|
|
|
// implementation only (for wxExecute)
|
2002-07-12 14:15:49 -04:00
|
|
|
//
|
|
|
|
// NB: the streams passed here should correspond to the child process
|
|
|
|
// stdout, stdin and stderr and here the normal naming convention is
|
|
|
|
// used unlike elsewhere in this class
|
|
|
|
void SetPipeStreams(wxInputStream *outStream,
|
|
|
|
wxOutputStream *inStream,
|
2000-07-15 15:51:35 -04:00
|
|
|
wxInputStream *errStream);
|
|
|
|
#endif // wxUSE_STREAMS
|
2000-02-27 05:44:49 -05:00
|
|
|
|
2002-07-12 14:15:49 -04:00
|
|
|
// for backwards compatibility only, don't use
|
|
|
|
#if WXWIN_COMPATIBILITY_2_2
|
|
|
|
wxProcess(wxEvtHandler *parent, bool redirect)
|
|
|
|
{ Init(parent, -1, redirect ? wxPROCESS_REDIRECT : wxPROCESS_DEFAULT); }
|
|
|
|
#endif // WXWIN_COMPATIBILITY_2_2
|
2001-06-15 20:59:07 -04:00
|
|
|
|
1999-02-07 16:26:54 -05:00
|
|
|
protected:
|
2002-07-12 14:15:49 -04:00
|
|
|
void Init(wxEvtHandler *parent, int id, int flags);
|
2000-03-02 14:06:13 -05:00
|
|
|
|
1999-02-07 16:26:54 -05:00
|
|
|
int m_id;
|
2000-03-02 14:06:13 -05:00
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
#if wxUSE_STREAMS
|
2002-07-12 14:15:49 -04:00
|
|
|
// these streams are connected to stdout, stderr and stdin of the child
|
|
|
|
// process respectively (yes, m_inputStream corresponds to stdout -- very
|
|
|
|
// confusing but too late to change now)
|
2000-07-15 15:51:35 -04:00
|
|
|
wxInputStream *m_inputStream,
|
|
|
|
*m_errorStream;
|
2000-03-02 14:06:13 -05:00
|
|
|
wxOutputStream *m_outputStream;
|
2000-07-15 15:51:35 -04:00
|
|
|
#endif // wxUSE_STREAMS
|
2000-03-02 14:06:13 -05:00
|
|
|
|
|
|
|
bool m_redirect;
|
2001-06-15 20:59:07 -04:00
|
|
|
|
|
|
|
DECLARE_DYNAMIC_CLASS(wxProcess)
|
2003-01-02 18:38:11 -05:00
|
|
|
DECLARE_NO_COPY_CLASS(wxProcess)
|
1998-07-01 13:26:46 -04:00
|
|
|
};
|
|
|
|
|
2001-01-31 12:16:40 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxProcess events
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2001-01-31 12:25:56 -05:00
|
|
|
BEGIN_DECLARE_EVENT_TYPES()
|
2003-07-19 11:05:08 -04:00
|
|
|
DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_BASE, wxEVT_END_PROCESS, 440)
|
2001-01-31 12:25:56 -05:00
|
|
|
END_DECLARE_EVENT_TYPES()
|
|
|
|
|
2003-07-01 21:59:24 -04:00
|
|
|
class WXDLLIMPEXP_BASE wxProcessEvent : public wxEvent
|
2001-01-31 12:21:59 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxProcessEvent(int id = 0, int pid = 0, int exitcode = 0) : wxEvent(id)
|
|
|
|
{
|
|
|
|
m_eventType = wxEVT_END_PROCESS;
|
|
|
|
m_pid = pid;
|
|
|
|
m_exitcode = exitcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// accessors
|
|
|
|
// PID of process which terminated
|
|
|
|
int GetPid() { return m_pid; }
|
|
|
|
|
|
|
|
// the exit code
|
|
|
|
int GetExitCode() { return m_exitcode; }
|
|
|
|
|
2001-11-21 16:44:52 -05:00
|
|
|
// implement the base class pure virtual
|
|
|
|
virtual wxEvent *Clone() const { return new wxProcessEvent(*this); }
|
|
|
|
|
2001-01-31 12:21:59 -05:00
|
|
|
public:
|
2001-11-21 16:44:52 -05:00
|
|
|
int m_pid,
|
|
|
|
m_exitcode;
|
2001-01-31 12:21:59 -05:00
|
|
|
|
2003-07-21 20:24:07 -04:00
|
|
|
DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxProcessEvent)
|
2001-01-31 12:21:59 -05:00
|
|
|
};
|
|
|
|
|
2001-10-10 18:34:55 -04:00
|
|
|
typedef void (wxEvtHandler::*wxProcessEventFunction)(wxProcessEvent&);
|
1998-07-01 13:26:46 -04:00
|
|
|
|
2001-01-26 12:35:36 -05:00
|
|
|
#define EVT_END_PROCESS(id, func) \
|
2001-10-10 18:34:55 -04:00
|
|
|
DECLARE_EVENT_TABLE_ENTRY( \
|
|
|
|
wxEVT_END_PROCESS, id, -1, \
|
2004-02-10 17:15:19 -05:00
|
|
|
(wxObjectEventFunction) (wxEventFunction) wxStaticCastEvent( wxProcessEventFunction, & func ), NULL),
|
1998-07-01 13:26:46 -04:00
|
|
|
|
|
|
|
#endif
|
1998-08-14 20:23:28 -04:00
|
|
|
// _WX_PROCESSH__
|