3030#include " macglue.h"
3131extern Boolean SIOUXUseWaitNextEvent;
3232
33+ static PyReadHandler sInConsole = 0L ;
34+ static PyWriteHandler sOutConsole = 0L ;
35+ static PyWriteHandler sErrConsole = 0L ;
36+
37+ inline bool hasCustomConsole (void ) { return sInConsole != 0L ; }
38+
3339class GUSISIOUXSocket : public GUSISocket {
3440public:
3541 ~GUSISIOUXSocket ();
@@ -42,14 +48,12 @@ virtual int fstat(struct stat * buf);
4248virtual int isatty ();
4349bool select (bool * canRead, bool * canWrite, bool *);
4450
45- static GUSISIOUXSocket * Instance ();
46- private:
47- static GUSISIOUXSocket * sInstance ;
48-
49- GUSISIOUXSocket ();
50- bool initialized;
51- void Initialize ();
52- bool fDelayConsole ;
51+ static GUSISIOUXSocket * Instance (int fd);
52+ private:
53+ GUSISIOUXSocket (int fd);
54+ static bool initialized;
55+ static void Initialize ();
56+ int fFd ;
5357};
5458class GUSISIOUXDevice : public GUSIDevice {
5559public:
@@ -63,28 +67,19 @@ private:
6367
6468 static GUSISIOUXDevice * sInstance ;
6569};
66- GUSISIOUXSocket * GUSISIOUXSocket::sInstance ;
6770
68- GUSISIOUXSocket * GUSISIOUXSocket::Instance ()
71+ GUSISIOUXSocket * GUSISIOUXSocket::Instance (int fd )
6972{
70- if (!sInstance )
71- if (sInstance = new GUSISIOUXSocket)
72- sInstance ->AddReference ();
73-
74- return sInstance ;
73+ return new GUSISIOUXSocket (fd);
7574}
7675// This declaration lies about the return type
7776extern " C" void SIOUXHandleOneEvent (EventRecord *userevent);
7877
79- GUSISIOUXSocket::GUSISIOUXSocket ()
78+ bool GUSISIOUXSocket::initialized = false ;
79+
80+ GUSISIOUXSocket::GUSISIOUXSocket (int fd) : fFd(fd)
8081{
81- if (PyMac_GetDelayConsoleFlag ())
82- fDelayConsole = true ;
83- else
84- fDelayConsole = false ;
85- if ( fDelayConsole )
86- initialized = 0 ;
87- else
82+ if (!PyMac_GetDelayConsoleFlag () && !hasCustomConsole () && !initialized)
8883 Initialize ();
8984 /* Tell the upper layers there's no unseen output */
9085 PyMac_OutputSeen ();
@@ -93,24 +88,39 @@ GUSISIOUXSocket::GUSISIOUXSocket()
9388void
9489GUSISIOUXSocket::Initialize ()
9590{
96- initialized = 1 ;
97- InstallConsole (0 );
98- GUSISetHook (GUSI_EventHook+nullEvent, (GUSIHook)SIOUXHandleOneEvent);
99- GUSISetHook (GUSI_EventHook+mouseDown, (GUSIHook)SIOUXHandleOneEvent);
100- GUSISetHook (GUSI_EventHook+mouseUp, (GUSIHook)SIOUXHandleOneEvent);
101- GUSISetHook (GUSI_EventHook+updateEvt, (GUSIHook)SIOUXHandleOneEvent);
102- GUSISetHook (GUSI_EventHook+diskEvt, (GUSIHook)SIOUXHandleOneEvent);
103- GUSISetHook (GUSI_EventHook+activateEvt, (GUSIHook)SIOUXHandleOneEvent);
104- GUSISetHook (GUSI_EventHook+osEvt, (GUSIHook)SIOUXHandleOneEvent);
105- PyMac_InitMenuBar ();
91+ if (!initialized && !hasCustomConsole ())
92+ {
93+ initialized = true ;
94+ InstallConsole (0 );
95+ GUSISetHook (GUSI_EventHook+nullEvent, (GUSIHook)SIOUXHandleOneEvent);
96+ GUSISetHook (GUSI_EventHook+mouseDown, (GUSIHook)SIOUXHandleOneEvent);
97+ GUSISetHook (GUSI_EventHook+mouseUp, (GUSIHook)SIOUXHandleOneEvent);
98+ GUSISetHook (GUSI_EventHook+updateEvt, (GUSIHook)SIOUXHandleOneEvent);
99+ GUSISetHook (GUSI_EventHook+diskEvt, (GUSIHook)SIOUXHandleOneEvent);
100+ GUSISetHook (GUSI_EventHook+activateEvt, (GUSIHook)SIOUXHandleOneEvent);
101+ GUSISetHook (GUSI_EventHook+osEvt, (GUSIHook)SIOUXHandleOneEvent);
102+ PyMac_InitMenuBar ();
103+ }
106104}
107105GUSISIOUXSocket::~GUSISIOUXSocket ()
108106{
109- if ( !initialized ) return ;
107+ if ( !initialized || hasCustomConsole () )
108+ return ;
109+
110+ initialized = false ;
110111 RemoveConsole ();
111112}
112113ssize_t GUSISIOUXSocket::read (const GUSIScatterer & buffer)
113114{
115+ if (hasCustomConsole ())
116+ {
117+ if (fFd == 0 )
118+ return buffer.SetLength (
119+ sInConsole ((char *) buffer.Buffer (), (int )buffer.Length ()));
120+
121+ return 0 ;
122+ }
123+
114124 if ( !initialized ) Initialize ();
115125 GUSIStdioFlush ();
116126 PyMac_OutputSeen ();
@@ -121,6 +131,16 @@ ssize_t GUSISIOUXSocket::read(const GUSIScatterer & buffer)
121131}
122132ssize_t GUSISIOUXSocket::write (const GUSIGatherer & buffer)
123133{
134+ if (hasCustomConsole ())
135+ {
136+ if (fFd == 1 )
137+ return sOutConsole ((char *) buffer.Buffer (), (int )buffer.Length ());
138+ else if (fFd == 2 )
139+ return sErrConsole ((char *) buffer.Buffer (), (int )buffer.Length ());
140+
141+ return 0 ;
142+ }
143+
124144 ssize_t rv;
125145
126146 if ( !initialized ) Initialize ();
@@ -198,14 +218,33 @@ bool GUSISIOUXDevice::Want(GUSIFileToken & file)
198218}
199219GUSISocket * GUSISIOUXDevice::open (GUSIFileToken &, int )
200220{
201- return GUSISIOUXSocket::Instance ();
221+ return GUSISIOUXSocket::Instance (1 );
202222}
203223void GUSISetupConsoleDescriptors ()
204224{
205225 GUSIDescriptorTable * table = GUSIDescriptorTable::Instance ();
206- GUSISIOUXSocket * SIOUX = GUSISIOUXSocket::Instance ();
207226
208- table->InstallSocket (SIOUX);
209- table->InstallSocket (SIOUX);
210- table->InstallSocket (SIOUX);
227+ table->InstallSocket (GUSISIOUXSocket::Instance (0 ));
228+ table->InstallSocket (GUSISIOUXSocket::Instance (1 ));
229+ table->InstallSocket (GUSISIOUXSocket::Instance (2 ));
230+ }
231+
232+ void PyMac_SetConsoleHandler (PyReadHandler stdinH, PyWriteHandler stdoutH, PyWriteHandler stderrH)
233+ {
234+ if (stdinH && stdoutH && stderrH)
235+ {
236+ sInConsole = stdinH;
237+ sOutConsole = stdoutH;
238+ sErrConsole = stderrH;
239+ }
240+ }
241+
242+ long PyMac_DummyReadHandler (char *buffer, long n)
243+ {
244+ return 0 ;
245+ }
246+
247+ long PyMac_DummyWriteHandler (char *buffer, long n)
248+ {
249+ return 0 ;
211250}
0 commit comments