summaryrefslogtreecommitdiffstats
path: root/Mac/Python
diff options
context:
space:
mode:
Diffstat (limited to 'Mac/Python')
-rw-r--r--Mac/Python/pyGUSISIOUX.cp117
1 files changed, 78 insertions, 39 deletions
diff --git a/Mac/Python/pyGUSISIOUX.cp b/Mac/Python/pyGUSISIOUX.cp
index f26216b..40670d8 100644
--- a/Mac/Python/pyGUSISIOUX.cp
+++ b/Mac/Python/pyGUSISIOUX.cp
@@ -30,6 +30,12 @@
#include "macglue.h"
extern Boolean SIOUXUseWaitNextEvent;
+static PyReadHandler sInConsole = 0L;
+static PyWriteHandler sOutConsole = 0L;
+static PyWriteHandler sErrConsole = 0L;
+
+inline bool hasCustomConsole(void) { return sInConsole != 0L; }
+
class GUSISIOUXSocket : public GUSISocket {
public:
~GUSISIOUXSocket();
@@ -42,14 +48,12 @@ virtual int fstat(struct stat * buf);
virtual int isatty();
bool select(bool * canRead, bool * canWrite, bool *);
- static GUSISIOUXSocket * Instance();
-private:
- static GUSISIOUXSocket * sInstance;
-
- GUSISIOUXSocket();
- bool initialized;
- void Initialize();
- bool fDelayConsole;
+ static GUSISIOUXSocket * Instance(int fd);
+private:
+ GUSISIOUXSocket(int fd);
+ static bool initialized;
+ static void Initialize();
+ int fFd;
};
class GUSISIOUXDevice : public GUSIDevice {
public:
@@ -63,28 +67,19 @@ private:
static GUSISIOUXDevice * sInstance;
};
-GUSISIOUXSocket * GUSISIOUXSocket::sInstance;
-GUSISIOUXSocket * GUSISIOUXSocket::Instance()
+GUSISIOUXSocket * GUSISIOUXSocket::Instance(int fd)
{
- if (!sInstance)
- if (sInstance = new GUSISIOUXSocket)
- sInstance->AddReference();
-
- return sInstance;
+ return new GUSISIOUXSocket(fd);
}
// This declaration lies about the return type
extern "C" void SIOUXHandleOneEvent(EventRecord *userevent);
-GUSISIOUXSocket::GUSISIOUXSocket()
+bool GUSISIOUXSocket::initialized = false;
+
+GUSISIOUXSocket::GUSISIOUXSocket(int fd) : fFd(fd)
{
- if (PyMac_GetDelayConsoleFlag())
- fDelayConsole = true;
- else
- fDelayConsole = false;
- if ( fDelayConsole )
- initialized = 0;
- else
+ if (!PyMac_GetDelayConsoleFlag() && !hasCustomConsole() && !initialized)
Initialize();
/* Tell the upper layers there's no unseen output */
PyMac_OutputSeen();
@@ -93,24 +88,39 @@ GUSISIOUXSocket::GUSISIOUXSocket()
void
GUSISIOUXSocket::Initialize()
{
- initialized = 1;
- InstallConsole(0);
- GUSISetHook(GUSI_EventHook+nullEvent, (GUSIHook)SIOUXHandleOneEvent);
- GUSISetHook(GUSI_EventHook+mouseDown, (GUSIHook)SIOUXHandleOneEvent);
- GUSISetHook(GUSI_EventHook+mouseUp, (GUSIHook)SIOUXHandleOneEvent);
- GUSISetHook(GUSI_EventHook+updateEvt, (GUSIHook)SIOUXHandleOneEvent);
- GUSISetHook(GUSI_EventHook+diskEvt, (GUSIHook)SIOUXHandleOneEvent);
- GUSISetHook(GUSI_EventHook+activateEvt, (GUSIHook)SIOUXHandleOneEvent);
- GUSISetHook(GUSI_EventHook+osEvt, (GUSIHook)SIOUXHandleOneEvent);
- PyMac_InitMenuBar();
+ if(!initialized && !hasCustomConsole())
+ {
+ initialized = true;
+ InstallConsole(0);
+ GUSISetHook(GUSI_EventHook+nullEvent, (GUSIHook)SIOUXHandleOneEvent);
+ GUSISetHook(GUSI_EventHook+mouseDown, (GUSIHook)SIOUXHandleOneEvent);
+ GUSISetHook(GUSI_EventHook+mouseUp, (GUSIHook)SIOUXHandleOneEvent);
+ GUSISetHook(GUSI_EventHook+updateEvt, (GUSIHook)SIOUXHandleOneEvent);
+ GUSISetHook(GUSI_EventHook+diskEvt, (GUSIHook)SIOUXHandleOneEvent);
+ GUSISetHook(GUSI_EventHook+activateEvt, (GUSIHook)SIOUXHandleOneEvent);
+ GUSISetHook(GUSI_EventHook+osEvt, (GUSIHook)SIOUXHandleOneEvent);
+ PyMac_InitMenuBar();
+ }
}
GUSISIOUXSocket::~GUSISIOUXSocket()
{
- if ( !initialized ) return;
+ if ( !initialized || hasCustomConsole() )
+ return;
+
+ initialized = false;
RemoveConsole();
}
ssize_t GUSISIOUXSocket::read(const GUSIScatterer & buffer)
{
+ if(hasCustomConsole())
+ {
+ if(fFd == 0)
+ return buffer.SetLength(
+ sInConsole((char *) buffer.Buffer(), (int)buffer.Length()));
+
+ return 0;
+ }
+
if ( !initialized ) Initialize();
GUSIStdioFlush();
PyMac_OutputSeen();
@@ -121,6 +131,16 @@ ssize_t GUSISIOUXSocket::read(const GUSIScatterer & buffer)
}
ssize_t GUSISIOUXSocket::write(const GUSIGatherer & buffer)
{
+ if(hasCustomConsole())
+ {
+ if(fFd == 1)
+ return sOutConsole((char *) buffer.Buffer(), (int)buffer.Length());
+ else if(fFd == 2)
+ return sErrConsole((char *) buffer.Buffer(), (int)buffer.Length());
+
+ return 0;
+ }
+
ssize_t rv;
if ( !initialized ) Initialize();
@@ -198,14 +218,33 @@ bool GUSISIOUXDevice::Want(GUSIFileToken & file)
}
GUSISocket * GUSISIOUXDevice::open(GUSIFileToken &, int)
{
- return GUSISIOUXSocket::Instance();
+ return GUSISIOUXSocket::Instance(1);
}
void GUSISetupConsoleDescriptors()
{
GUSIDescriptorTable * table = GUSIDescriptorTable::Instance();
- GUSISIOUXSocket * SIOUX = GUSISIOUXSocket::Instance();
- table->InstallSocket(SIOUX);
- table->InstallSocket(SIOUX);
- table->InstallSocket(SIOUX);
+ table->InstallSocket(GUSISIOUXSocket::Instance(0));
+ table->InstallSocket(GUSISIOUXSocket::Instance(1));
+ table->InstallSocket(GUSISIOUXSocket::Instance(2));
+}
+
+void PyMac_SetConsoleHandler(PyReadHandler stdinH, PyWriteHandler stdoutH, PyWriteHandler stderrH)
+{
+ if(stdinH && stdoutH && stderrH)
+ {
+ sInConsole = stdinH;
+ sOutConsole = stdoutH;
+ sErrConsole = stderrH;
+ }
+}
+
+long PyMac_DummyReadHandler(char *buffer, long n)
+{
+ return 0;
+}
+
+long PyMac_DummyWriteHandler(char *buffer, long n)
+{
+ return 0;
}