Fix an amazing number of typos & malformed sentences reported by Detlef
[python/dscho.git] / Mac / GUSI-mods / into-src / GUSISIOUX.cp
blob24b98b0a1e8cfdb1189be5e21826270c8b7acf02
1 /*********************************************************************
2 Project :       GUSI                            -       Grand unified socket interface
3 File            :       GUSISIOUX.cp    -       Interface to Metrowerks SIOUX library
4 Author  :       Matthias Neeracher
5 Language        :       MPW C/C++
7 $Log$
8 Revision 1.1  1998/08/18 14:52:38  jack
9 Putting Python-specific GUSI modifications under CVS.
11 *********************************************************************/
13 #include <GUSIFile_P.h>
14 #include <ioctl.h>
15 #include <console.h>
17 #include <Events.h>
18 #include <LowMem.h>
20 /************************ SIOUXSocket members ************************/
22 /* This declaration lies about the return type */
23 extern "C" void SIOUXHandleOneEvent(EventRecord *userevent);
25 GUSIEvtHandler  GUSISIOUXEvents[]       =       {
26         SIOUXHandleOneEvent,            // nullEvent
27         
28         SIOUXHandleOneEvent,            // mouseDown
29         SIOUXHandleOneEvent,            // mouseUp
30         nil,                                                    // keyDown
31         nil,
32         
33         nil,                                                    // autoKey
34         SIOUXHandleOneEvent,            // updateEvt
35         SIOUXHandleOneEvent,            // diskEvt
36         SIOUXHandleOneEvent,            // activateEvt
37         
38         nil,
39         nil,
40         nil,
41         nil,
42         
43         nil,
44         nil,
45         SIOUXHandleOneEvent,            // osEvt
46         nil,
47         
48         nil,
49         nil,
50         nil,
51         nil,
52         
53         nil,
54         nil,
55         nil,
58 /************************ Declaration of SIOUXSocket ************************/
60 class SIOUXSocket : public Socket       {               
61         friend class SIOUXSocketDomain; 
62         
63                                         SIOUXSocket();
64                                         
65         virtual                 ~SIOUXSocket();
66 protected:
67         int                     initialized;
68         void                    DoInitialize(void);
69 public:
70         virtual int     read(void * buffer, int buflen);
71         virtual int write(void * buffer, int buflen);
72         virtual int select(Boolean * canRead, Boolean * canWrite, Boolean * exception);
73         virtual int     ioctl(unsigned int request, void *argp);
74         virtual int     isatty();
75 };      
77 class SIOUXSocketDomain : public FileSocketDomain {
78         SIOUXSocket *   singleton;
79 public:
80         SIOUXSocketDomain()     :       FileSocketDomain(AF_UNSPEC, true, false), singleton(nil)        {       }
81         
82         virtual Boolean Yours(const GUSIFileRef & ref, Request request);
83         virtual Socket * open(const GUSIFileRef & ref, int oflag);
86 #if GENERATING68K
87 #pragma segment SIOUX
88 #endif
90 /************************ SIOUXSocket members ************************/
92 void SIOUXSocket::DoInitialize()
94         if ( initialized ) return;
95         initialized++;
96         InstallConsole(0);
97         GUSISetEvents(GUSISIOUXEvents);
100 SIOUXSocket::SIOUXSocket()
102         initialized = 0;
103         if ( !GUSIConfig.DelayConsole() )
104                 DoInitialize();
107 SIOUXSocket::~SIOUXSocket()
109         RemoveConsole();
112 int SIOUXSocket::ioctl(unsigned int request, void *)
114         if ( !initialized) DoInitialize();
115         switch (request)        {
116         case FIOINTERACTIVE:
117                 return 0;
118         default:
119                 return GUSI_error(EOPNOTSUPP);
120         }
123 int SIOUXSocket::read(void * buffer, int buflen)
125         if ( !initialized) DoInitialize();
126         fflush(stdout);
127         
128         return ReadCharsFromConsole((char *) buffer, buflen);
131 int SIOUXSocket::write(void * buffer, int buflen)
133         if ( !initialized) DoInitialize();
134         return WriteCharsToConsole((char *) buffer, buflen);
137 static Boolean input_pending()
139         QHdrPtr eventQueue = LMGetEventQueue();
140         EvQElPtr element = (EvQElPtr)eventQueue->qHead;
141         
142         // now, count the number of pending keyDown events.
143         while (element != nil) {
144                 if (element->evtQWhat == keyDown || element->evtQWhat == autoKey)
145                         return true;
146                 element = (EvQElPtr)element->qLink;
147         }
148         
149         return false;
152 int SIOUXSocket::select(Boolean * canRead, Boolean * canWrite, Boolean * exception)
154         int             goodies         =       0;
155                 
156         if ( !initialized) DoInitialize();
157         fflush(stdout);
158         
159         if (canRead) 
160                 if (*canRead = input_pending())
161                         ++goodies;
162         
163         if (canWrite) {
164                 *canWrite = true;
165                 ++goodies;
166         }
167         
168         if (exception)
169                 *exception = false;
170         
171         return goodies;
174 int SIOUXSocket::isatty()
176         return 1;
179 /********************* SIOUXSocketDomain members **********************/
181 #ifdef MSLGUSI
182 #ifndef SFIOGUSI
183         extern void GUSISetupMSLSIOUX();
184 #endif
185 #endif
187 extern "C" void GUSIwithSIOUXSockets()
189         static SIOUXSocketDomain        SIOUXSockets;
190         SIOUXSockets.DontStrip();
191 #ifdef MSLGUSI
192 #ifndef SFIOGUSI
193         GUSISetupMSLSIOUX();
194 #endif
195 #endif
198 Boolean SIOUXSocketDomain::Yours(const GUSIFileRef & ref, FileSocketDomain::Request request)
200         if (ref.spec || (request != willOpen && request != willStat))
201                 return false;
202         
203         switch (ref.name[4] | 0x20) {
204         case 's':
205                 if ((ref.name[5] | 0x20) != 't' || (ref.name[6] | 0x20) != 'd')
206                         return false;
207                 switch (ref.name[7] | 0x20) {
208                 case 'i':
209                         if ((ref.name[8] | 0x20) != 'n' || ref.name[9])
210                                 return false;
211                         return true;
212                 case 'o':
213                         if ((ref.name[8] | 0x20) != 'u' || (ref.name[9] | 0x20) != 't' || ref.name[10])
214                                 return false;
215                         return true;
216                 case 'e':
217                         if ((ref.name[8] | 0x20) != 'r' || (ref.name[9] | 0x20) != 'r' || ref.name[10])
218                                 return false;
219                         return true;
220                 default:
221                         return false;
222                 }
223         case 'c':
224                 if (    (ref.name[5] | 0x20) != 'o' || (ref.name[6] | 0x20) != 'n'
225                         || (ref.name[7] | 0x20) != 's' || (ref.name[8] | 0x20) != 'o'
226                         || (ref.name[9] | 0x20) != 'l' || (ref.name[10] | 0x20) != 'e')
227                         return false;
228                 switch (ref.name[11]) {
229                 case 0:
230                         return true;
231                 default:
232                         return false;
233                 }
234         default:
235                 return false;
236         }
239 Socket * SIOUXSocketDomain::open(const GUSIFileRef &, int)
241         if (!singleton)
242                 singleton = new SIOUXSocket();
243         ++*singleton;
244         
245         return singleton;