summaryrefslogtreecommitdiffstats
path: root/Mac/GUSI-mods/into-src/GUSISIOUX.cp
blob: 24b98b0a1e8cfdb1189be5e21826270c8b7acf02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*********************************************************************
Project	:	GUSI				-	Grand unified socket interface
File		:	GUSISIOUX.cp	-	Interface to Metrowerks SIOUX library
Author	:	Matthias Neeracher
Language	:	MPW C/C++

$Log$
Revision 1.1  1998/08/18 14:52:38  jack
Putting Python-specific GUSI modifications under CVS.

*********************************************************************/

#include <GUSIFile_P.h>
#include <ioctl.h>
#include <console.h>

#include <Events.h>
#include <LowMem.h>

/************************ SIOUXSocket members ************************/

/* This declaration lies about the return type */
extern "C" void SIOUXHandleOneEvent(EventRecord *userevent);

GUSIEvtHandler	GUSISIOUXEvents[]	=	{
	SIOUXHandleOneEvent,		// nullEvent
	
	SIOUXHandleOneEvent,		// mouseDown
	SIOUXHandleOneEvent,		// mouseUp
	nil,							// keyDown
	nil,
	
	nil,							// autoKey
	SIOUXHandleOneEvent,		// updateEvt
	SIOUXHandleOneEvent,		// diskEvt
	SIOUXHandleOneEvent,		// activateEvt
	
	nil,
	nil,
	nil,
	nil,
	
	nil,
	nil,
	SIOUXHandleOneEvent,		// osEvt
	nil,
	
	nil,
	nil,
	nil,
	nil,
	
	nil,
	nil,
	nil,
};

/************************ Declaration of SIOUXSocket ************************/

class SIOUXSocket : public Socket	{		
	friend class SIOUXSocketDomain;	
	
					SIOUXSocket();
					
	virtual 		~SIOUXSocket();
protected:
	int			initialized;
	void			DoInitialize(void);
public:
	virtual int	read(void * buffer, int buflen);
	virtual int write(void * buffer, int buflen);
	virtual int select(Boolean * canRead, Boolean * canWrite, Boolean * exception);
	virtual int	ioctl(unsigned int request, void *argp);
	virtual int	isatty();
};	

class SIOUXSocketDomain : public FileSocketDomain {
	SIOUXSocket *	singleton;
public:
	SIOUXSocketDomain()	:	FileSocketDomain(AF_UNSPEC, true, false), singleton(nil)	{	}
	
	virtual Boolean Yours(const GUSIFileRef & ref, Request request);
	virtual Socket * open(const GUSIFileRef & ref, int oflag);
};

#if GENERATING68K
#pragma segment SIOUX
#endif

/************************ SIOUXSocket members ************************/

void SIOUXSocket::DoInitialize()
{
	if ( initialized ) return;
	initialized++;
	InstallConsole(0);
	GUSISetEvents(GUSISIOUXEvents);
}

SIOUXSocket::SIOUXSocket()
{
	initialized = 0;
	if ( !GUSIConfig.DelayConsole() )
		DoInitialize();
}

SIOUXSocket::~SIOUXSocket()
{
	RemoveConsole();
}

int SIOUXSocket::ioctl(unsigned int request, void *)
{
	if ( !initialized) DoInitialize();
	switch (request)	{
	case FIOINTERACTIVE:
		return 0;
	default:
		return GUSI_error(EOPNOTSUPP);
	}
}

int SIOUXSocket::read(void * buffer, int buflen)
{
	if ( !initialized) DoInitialize();
	fflush(stdout);
	
	return ReadCharsFromConsole((char *) buffer, buflen);
}

int SIOUXSocket::write(void * buffer, int buflen)
{
	if ( !initialized) DoInitialize();
	return WriteCharsToConsole((char *) buffer, buflen);
}

static Boolean input_pending()
{
	QHdrPtr eventQueue = LMGetEventQueue();
	EvQElPtr element = (EvQElPtr)eventQueue->qHead;
	
	// now, count the number of pending keyDown events.
	while (element != nil) {
		if (element->evtQWhat == keyDown || element->evtQWhat == autoKey)
			return true;
		element = (EvQElPtr)element->qLink;
	}
	
	return false;
}

int SIOUXSocket::select(Boolean * canRead, Boolean * canWrite, Boolean * exception)
{
	int		goodies 	= 	0;
		
	if ( !initialized) DoInitialize();
	fflush(stdout);
	
	if (canRead) 
		if (*canRead = input_pending())
			++goodies;
	
	if (canWrite) {
		*canWrite = true;
		++goodies;
	}
	
	if (exception)
		*exception = false;
	
	return goodies;
}

int SIOUXSocket::isatty()
{
	return 1;
}

/********************* SIOUXSocketDomain members **********************/

#ifdef MSLGUSI
#ifndef SFIOGUSI
	extern void GUSISetupMSLSIOUX();
#endif
#endif

extern "C" void GUSIwithSIOUXSockets()
{
	static SIOUXSocketDomain	SIOUXSockets;
	SIOUXSockets.DontStrip();
#ifdef MSLGUSI
#ifndef SFIOGUSI
	GUSISetupMSLSIOUX();
#endif
#endif
}

Boolean SIOUXSocketDomain::Yours(const GUSIFileRef & ref, FileSocketDomain::Request request)
{
	if (ref.spec || (request != willOpen && request != willStat))
		return false;
	
	switch (ref.name[4] | 0x20) {
	case 's':
		if ((ref.name[5] | 0x20) != 't' || (ref.name[6] | 0x20) != 'd')
			return false;
		switch (ref.name[7] | 0x20) {
		case 'i':
			if ((ref.name[8] | 0x20) != 'n' || ref.name[9])
				return false;
			return true;
		case 'o':
			if ((ref.name[8] | 0x20) != 'u' || (ref.name[9] | 0x20) != 't' || ref.name[10])
				return false;
			return true;
		case 'e':
			if ((ref.name[8] | 0x20) != 'r' || (ref.name[9] | 0x20) != 'r' || ref.name[10])
				return false;
			return true;
		default:
			return false;
		}
	case 'c':
		if (	(ref.name[5] | 0x20) != 'o' || (ref.name[6] | 0x20) != 'n'
			|| (ref.name[7] | 0x20) != 's' || (ref.name[8] | 0x20) != 'o'
			|| (ref.name[9] | 0x20) != 'l' || (ref.name[10] | 0x20) != 'e')
			return false;
		switch (ref.name[11]) {
		case 0:
			return true;
		default:
			return false;
		}
	default:
		return false;
	}
}

Socket * SIOUXSocketDomain::open(const GUSIFileRef &, int)
{
	if (!singleton)
		singleton = new SIOUXSocket();
	++*singleton;
	
	return singleton;
}