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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
/*
* aeLink.c
* UNIX environment handling stuff for macos
*
* These routines make MacCVS able to accept a standard UNIX command line,
* environment, output redirection, and startup directory via AppleEvents
* and redirect all output to AppleEvents, a file, or the SIOUX tty window.
*
* Michael Ladwig <mike@twinpeaks.prc.com> --- April 1996
*/
#include "mac_config.h"
#ifdef AE_IO_HANDLERS
#ifdef __POWERPC__
#include <MacHeadersPPC>
#else
#include <MacHeaders68K>
#endif
#include <AppleEvents.h>
#include <AERegistry.h>
#include <stdio.h>
#include <string.h>
#include <console.h>
#include <sys/fcntl.h>
extern char *xmalloc (size_t bytes);
enum { outToAE, outToFile };
static int outputMode = outToAE;
static char tempOutputFileName[256], outputFileName[256];
static int outputFile;
static int noLineBuffer;
AppleEvent gResponseEvent = {'null', nil};
AppleEvent gResponseReplyEvent = {'null', nil};
AEAddressDesc gResponseAddress;
static char aeCmdIn = 0;
static char aeLinkDone = 0;
char **Args;
char **EnvVars, **EnvVals;
int ArgC = 1;
int EnvC = 1;
char * CopyInfo(Handle info)
{
if( info )
{
char * retarg = xmalloc(GetHandleSize(info) + 1);
if (retarg)
{
memcpy(retarg, *info, GetHandleSize(info) );
retarg[GetHandleSize(info)] = 0;
}
return retarg;
}
else
return nil;
}
void MakeEnvironment(const AppleEvent *event)
{
AEDesc args;
long i, argCount;
if (AEGetParamDesc(event, 'ENVT', typeAEList, &args) || AECountItems(&args, &argCount) || !argCount)
{
EnvVars[EnvC] = nil;
EnvVals[EnvC] = nil;
return;
}
for (i = 0; i++<argCount;)
{
AEKeyword key;
AEDesc arg;
EnvC++;
// Variable
if (!AEGetNthDesc(&args, i, typeChar, &key, &arg))
{
HLock(arg.dataHandle);
EnvVars[EnvC-1] = CopyInfo(arg.dataHandle);
AEDisposeDesc(&arg);
}
// Value
i++;
if (!AEGetNthDesc(&args, i, typeChar, &key, &arg))
{
HLock(arg.dataHandle);
EnvVals[EnvC-1] = CopyInfo(arg.dataHandle);
AEDisposeDesc(&arg);
}
}
AEDisposeDesc(&args);
EnvVars[EnvC] = nil;
EnvVals[EnvC] = nil;
}
pascal OSErr DoScript(const AppleEvent *event, AppleEvent *reply, long refCon)
{
OSType mode;
DescType typeCode;
Size size;
AEDesc args;
long i, argCount, addrSize;
AEDesc env;
char *argString, *anArg, startPWD[1024];
TargetID requestAddr;
DescType requestAddrType;
Boolean flag;
if (AEGetParamDesc(event, '----', typeAEList, &args) || AECountItems(&args, &argCount) || !argCount)
return errAEEventFailed;
// Get the address of the requesting app to we can send information back
AEGetAttributePtr(event,
keyAddressAttr,
typeWildCard,
&requestAddrType,
(Ptr) &requestAddr,
sizeof(requestAddr),
&addrSize);
AECreateDesc( typeTargetID, &requestAddr, sizeof(requestAddr), &gResponseAddress );
// Pull the command line from the event
for (i = 0; i++<argCount;)
{
AEKeyword key;
AEDesc arg;
if (!AEGetNthDesc(&args, i, typeChar, &key, &arg))
{
HLock(arg.dataHandle);
argString = CopyInfo(arg.dataHandle);
AEDisposeDesc(&arg);
anArg = strtok( argString, " " );
Args[ArgC] = anArg;
ArgC++;
while( (anArg = strtok(NULL, " ")) != NULL )
{
Args[ArgC] = anArg;
ArgC++;
}
}
}
AEDisposeDesc(&args);
Args[ArgC] = nil;
// Pull the environment variables from the event
MakeEnvironment( event );
// Figure out what mode should be used to return results
if (AEGetParamPtr(event, 'MODE', typeEnumerated, &typeCode, &mode, 4, &size))
outputMode = outToAE;
else
{
switch (mode) {
// Batch (return results via Apple Events)
case 'TOAE':
outputMode = outToAE;
break;
// File (return results via a file)
case 'FILE':
outputMode = outToFile;
if (AEGetParamPtr(event, 'FILE', typeChar, &typeCode, &outputFileName, sizeof(outputFileName)-1, &size))
{
outputMode = outToAE;
fprintf(stderr, "MacCVS Error: No filename parameter\n" );
}
else
{
outputFileName[size] = 0;
strcpy( tempOutputFileName, outputFileName );
strcat( tempOutputFileName, ".TMP");
if( (outputFile = open(tempOutputFileName, O_WRONLY | O_CREAT | O_TRUNC)) == 1 )
{
outputMode = outToAE;
fprintf(stderr, "MacCVS Error: Unable to open '%s'\n", tempOutputFileName);
}
}
break;
}
}
// Check to see if there is a starting pathname for this invokation
if ( ! AEGetParamPtr(event, 'SPWD', typeChar, &typeCode, &startPWD, sizeof(startPWD)-1, &size) )
{
startPWD[size] = 0;
chdir(startPWD);
}
// Check to see if we should not line buffer results in AE return mode
if (AEGetParamPtr(event, 'LBUF', typeBoolean, &typeCode, (Ptr) &flag, 1, &size))
noLineBuffer = 0;
else
noLineBuffer = flag;
// All Done
aeLinkDone = 1;
return noErr;
}
void GetUnixCommandEnvironment( char *name )
{
long timeOutTicks;
EventRecord theEvent;
#ifdef __POWERPC__
AEInstallEventHandler( kAEMiscStandards, kAEDoScript, NewAEEventHandlerProc(DoScript), 0, false);
#else
AEInstallEventHandler( kAEMiscStandards, kAEDoScript, DoScript, 0, false);
#endif
// Allocate some storage for the command line and the environment
Args = (char **) xmalloc(ArgMax * sizeof(char *));
EnvVars = (char **) xmalloc(EnvMax * sizeof(char *));
EnvVals = (char **) xmalloc(EnvMax * sizeof(char *));
// Initialize the first arg to the process name
Args[0] = xmalloc(strlen(name)+1);
strcpy( Args[0], name );
// Defaults
ArgC = 1;
EnvC = 0;
outputMode = outToAE;
// Wait for the command line and environment
timeOutTicks = TickCount() + (60*AE_TIMEOUT_SECONDS); // Timeout seconds set in maccvs.pch
while( (TickCount() < timeOutTicks) && (!aeLinkDone) )
{
if (WaitNextEvent(everyEvent, &theEvent, 60, nil))
{
if( ! (SIOUXHandleOneEvent(&theEvent)) )
{
switch (theEvent.what)
{
case kHighLevelEvent:
AEProcessAppleEvent(&theEvent);
break;
}
}
}
}
}
char *
getenv( const char *var )
{
int i;
// Look it up in the environment
for( i=0; i<EnvC; i++ )
{
if( strcmp(EnvVars[i], var) == 0 ) return( EnvVals[i] );
}
return NULL;
}
/* Free the allocated memory */
void CleanUpArgsAndEnv( void )
{
int i;
// Clean up the args
for( i=0; i<ArgC; i++ )
free( Args[i] );
free( Args );
// Clean up the environment
for( i=0; i<EnvC; i++ )
{ free( EnvVars[i] ); free( EnvVals[i] ); }
free( EnvVars );
free( EnvVals );
}
/*
* The following blocks of code are related to the redirection of output to
* AppleEvents.
*/
static char outBuf[AE_OUTBUF_SIZE];
static int outBufLen = -1;
void InitOutBuffer( void )
{
outBufLen = 0;
}
void SendOutBuffer( char outputDone )
{
if( outBufLen )
{
AEPutParamPtr(
&gResponseEvent,
keyDirectObject,
typeChar,
outBuf,
outBufLen);
}
if( outputDone )
{
AEPutParamPtr(
&gResponseEvent,
'DONE',
typeChar,
"DONE",
4);
}
AESend(
&gResponseEvent,
&gResponseReplyEvent,
kAEWaitReply+kAENeverInteract,
kAENormalPriority,
kNoTimeOut,
nil, nil);
}
/*
* The following three routines override the "real thing" from the CW
* SIOUX library in order to divert output to AppleEvents.
*/
short
InstallConsole(short fd)
{
if (outputMode == outToFile)
return 0;
AECreateAppleEvent ('MCVS', 'DATA',
&gResponseAddress,
kAutoGenerateReturnID,
kAnyTransactionID,
&gResponseEvent);
return 0;
}
long WriteCharsToConsole( char *buf, long length )
{
char *tCh;
if( outputMode == outToFile )
{
write( outputFile, buf, length );
return length;
}
if( outBufLen == -1 ) InitOutBuffer();
if( (length + outBufLen) > AE_OUTBUF_SIZE )
{
SendOutBuffer( FALSE );
InitOutBuffer();
}
for( tCh = buf; tCh < (char *) (buf+length); tCh++ )
{
if( *tCh == '\012' ) *tCh = '\015';
outBuf[outBufLen] = *tCh;
outBufLen++;
}
if( noLineBuffer && ( *(buf+length) == '\015') )
{
SendOutBuffer( FALSE );
InitOutBuffer();
}
return length;
}
void RemoveConsole( void )
{
CleanUpArgsAndEnv();
if( outputMode == outToFile )
{
close(outputFile);
if( rename(tempOutputFileName, outputFileName) != 0 )
SysBeep( 100 );
return;
}
SendOutBuffer( TRUE );
AEDisposeDesc( &gResponseEvent );
AEDisposeDesc( &gResponseAddress );
}
#endif // AE_IO_HANDLERS
|