home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.mactech.com 2010
/
ftp.mactech.com.tar
/
ftp.mactech.com
/
online
/
source
/
c
/
compilers
/
Tickle-4.0.sit.hqx
/
Tickle-4.0
/
src
/
AEtcl.c
next >
Wrap
Text File
|
1993-11-18
|
29KB
|
1,139 lines
/*
** This source code was written by Tim Endres
** Email: time@ice.com.
** USMail: 8840 Main Street, Whitmore Lake, MI 48189
**
** Some portions of this application utilize sources
** that are copyrighted by ICE Engineering, Inc., and
** ICE Engineering retains all rights to those sources.
**
** Neither ICE Engineering, Inc., nor Tim Endres,
** warrants this source code for any reason, and neither
** party assumes any responsbility for the use of these
** sources, libraries, or applications. The user of these
** sources and binaries assumes all responsbilities for
** any resulting consequences.
*/
#include "tickle.h"
#include "tcl.h"
#include "tge.h"
#include "tclMac.h"
#include "stat.h"
#include <string.h>
#ifndef THINK_C
# include <AEBuild.h>
#endif
#pragma segment AppleEvent
#define kMiscEventClass 'misc'
#define kAEDoScript 'dosc'
#define MAX_AE_TARGETS 8
#define AET_UNUSED 0
#define AET_INUSE 1
struct
{
short state;
char name[32];
AEAddressDesc target;
} _ae_targets [MAX_AE_TARGETS];
static char *hexstr = "0123456789ABCDEF";
extern int macintoshErr;
int
Cmd_OpenAETarget(clientData, interp, argc, argv)
char *clientData;
Tcl_Interp *interp;
int argc;
char **argv;
{
int index, myerr;
TargetID targetID;
PortInfoRec targetPort;
WindowPtr myWindow;
char sigbuf[8], *a, *b, *c;
TargetID targ;
#pragma unused (clientData)
for (index = 0 ; index < MAX_AE_TARGETS ; index++)
{
if (_ae_targets[index].state == AET_UNUSED)
break;
if (strcmp(_ae_targets[index].name, argv[1]) == SAMESTR)
{
Tcl_AppendResult(interp, "\"", argv[0], "\" duplicate AETarget name '",
argv[1], "'", (char *) NULL);
return TCL_ERROR;
}
}
if (index >= MAX_AE_TARGETS)
{
Tcl_AppendResult(interp, "\"", argv[0], "\" max AETargets open", (char *) NULL);
return TCL_ERROR;
}
else
{
if (argc > 3)
{
if ( (a=strchr(argv[2], ',')) &&
(b=strchr(argv[3], ':')) &&
(c=strchr(argv[3], '@')) )
{
targ.name.nameScript = smRoman;
targ.name.portKindSelector = ppcByString;
*a = '\0';
strncpy((char *)targ.name.name, argv[2], 32);
targ.name.name[32] = '\0';
c2pstr(targ.name.name);
strncpy((char *)targ.name.u.portTypeStr, a + 1, 32);
targ.name.u.portTypeStr[32] = '\0';
c2pstr(targ.name.u.portTypeStr);
*a = ',';
*b = '\0'; *c = '\0';
targ.location.locationKindSelector = ppcNBPLocation;
strncpy((char *)targ.location.u.nbpEntity.objStr, argv[3], 32);
strncpy((char *)targ.location.u.nbpEntity.typeStr, b + 1, 32);
strncpy((char *)targ.location.u.nbpEntity.zoneStr, c + 1, 32);
targ.location.u.nbpEntity.objStr[32] = '\0';
targ.location.u.nbpEntity.typeStr[32] = '\0';
targ.location.u.nbpEntity.zoneStr[32] = '\0';
c2pstr(targ.location.u.nbpEntity.objStr);
c2pstr(targ.location.u.nbpEntity.typeStr);
c2pstr(targ.location.u.nbpEntity.zoneStr);
*b = ':'; *c = '@';
myerr = AECreateDesc(typeTargetID, (Ptr)&targ, sizeof(targ),
&_ae_targets[index].target);
if (myerr == noErr)
{
_ae_targets[index].state = AET_INUSE;
strcpy(_ae_targets[index].name, argv[1]);
}
else
{
macintoshErr = myerr;
Tcl_AppendResult(interp, "\"", argv[0], "\" AECreateDesc() for ",
argv[1], ":", argv[2], ".", argv[3],
Tcl_MacError(interp), (char *) NULL);
return TCL_ERROR;
}
}
else
{
Tcl_AppendResult(interp, "\"", argv[0], "\" error in port name syntax '",
argv[2], "' '", argv[3], "'", (char *) NULL);
return TCL_ERROR;
}
}
else if (argc > 2) /* APPL Signature or Name! */
{
if (argv[2][0] == '\'' && argv[2][5] == '\'')
{
sprintf(sigbuf, "%-4.4s", &argv[2][1]);
myerr = AECreateDesc(typeApplSignature, (Ptr)sigbuf, 4, &_ae_targets[index].target);
if (myerr == noErr)
{
_ae_targets[index].state = AET_INUSE;
strcpy(_ae_targets[index].name, argv[1]);
}
else
{
macintoshErr = myerr;
Tcl_AppendResult(interp, "\"", argv[0], "\" AECreateDesc() for '",
argv[1], ":", argv[2], "' ",
Tcl_MacError(interp), (char *) NULL);
return TCL_ERROR;
}
}
else
{
char name[256];
FSSpec spec;
ProcessSerialNumber process;
ProcessInfoRec infoRec;
process.highLongOfPSN = 0;
process.lowLongOfPSN = kNoProcess;
infoRec.processInfoLength = sizeof(ProcessInfoRec);
infoRec.processName = (StringPtr)name;
infoRec.processAppSpec = &spec;
c2pstr(argv[2]);
for ( ; (myerr = GetNextProcess(&process)) == noErr ; )
{
if (GetProcessInformation(&process, &infoRec) == noErr)
{
if ( argv[2][0] == infoRec.processName[0]
&& strncmp((char *)&argv[2][1], &infoRec.processName[1], argv[2][0])==0 )
break;
}
}
p2cstr(argv[2]);
if (myerr != noErr)
{
macintoshErr = myerr;
Tcl_AppendResult(interp, "\"", argv[0], "\" no process named '",
argv[1], "' ", Tcl_MacError(interp), (char *) NULL);
return TCL_ERROR;
}
else
{
myerr = AECreateDesc(typeProcessSerialNumber, (Ptr)&process, sizeof(process),
&_ae_targets[index].target);
if (myerr == noErr)
{
_ae_targets[index].state = AET_INUSE;
strcpy(_ae_targets[index].name, argv[1]);
}
else
{
macintoshErr = myerr;
Tcl_AppendResult(interp, "\"", argv[0], "\" AECreateDesc() for '",
argv[1], ":", argv[2], "' ",
Tcl_MacError(interp), (char *) NULL);
return TCL_ERROR;
}
}
}
}
else /* PPC Browser... */
{
#ifdef TCLAPPL
myWindow = FrontWindow();
if (WPeek->windowKind == tgeWKind)
tge_activate(myWindow, 0);
myerr = PPCBrowser("\pTarget", "\p", FALSE,
&targetID.location, &targetPort, (PPCFilterProcPtr)0, "\p");
if (myerr == noErr)
{
BlockMove(&targetPort.name, &targetID.name, sizeof(targetPort.name));
myerr = AECreateDesc(typeTargetID, (Ptr)&targetID, sizeof(targetID),
&_ae_targets[index].target);
if (myerr == noErr)
{
_ae_targets[index].state = AET_INUSE;
strcpy(_ae_targets[index].name, argv[1]);
}
else
{
macintoshErr = myerr;
Tcl_AppendResult(interp, "\"", argv[0], "\" AECreateDesc() for target: ",
Tcl_MacError(interp), (char *) NULL);
return TCL_ERROR;
}
}
else
{
macintoshErr = myerr;
Tcl_AppendResult(interp, "\"", argv[0], "\" PPCBrowser() getting target: ",
Tcl_MacError(interp), (char *) NULL);
return TCL_ERROR;
}
#else
Tcl_AppendResult(interp, "\"", argv[0], "\" PPCBrowser unimplemented in engine",
(char *) NULL);
return TCL_ERROR;
#endif /* TCLAPPL */
}
}
return TCL_OK;
}
int
Cmd_CloseAETarget(clientData, interp, argc, argv)
char *clientData;
Tcl_Interp *interp;
int argc;
char **argv;
{
int index, result = TCL_OK;
#pragma unused (clientData, argc)
for (index = 0 ; index < MAX_AE_TARGETS ; index++)
{
if (_ae_targets[index].state == AET_INUSE)
if (strcmp(_ae_targets[index].name, argv[1]) == SAMESTR)
break;
}
if (index >= MAX_AE_TARGETS)
{
Tcl_AppendResult(interp, "\"", argv[0], "\" AE Target '",
argv[1], "' unknown", (char *) NULL);
return TCL_ERROR;
}
else
{
_ae_targets[index].state = AET_UNUSED;
AEDisposeDesc(&_ae_targets[index].target);
}
return result;
}
int
Cmd_ListAEOpenTargets(clientData, interp, argc, argv)
char *clientData;
Tcl_Interp *interp;
int argc;
char **argv;
{
int index;
char buffer[2048];
#pragma unused (clientData, argc, argv)
buffer[0] = '\0';
for (index = 0 ; index < MAX_AE_TARGETS ; index++)
{
if (_ae_targets[index].state == AET_INUSE) {
if ( (strlen(buffer) + strlen(_ae_targets[index].name) + 3) > 2048 )
break;
strcat(buffer, _ae_targets[index].name);
strcat(buffer, " ");
}
}
Tcl_SetResult(interp, buffer, TCL_VOLATILE);
return TCL_OK;
}
int
Cmd_Coerce(clientData, interp, argc, argv)
char *clientData;
Tcl_Interp *interp;
int argc;
char **argv;
{
int i, myerr, length, result = TCL_OK;
int hexdata = 0, hexresult = 0;
DescType toType;
unsigned char high, low, ch;
char *arg0, *saveptr, *ptr, *hptr, tempstr[32];
AEDesc fDesc, tDesc;
#pragma unused (clientData, argc)
arg0 = argv[0];
argc--; argv++;
sprintf(tempstr, "%-4.4s", argv[0]);
strncpy((char *)&fDesc.descriptorType, tempstr, 4);
argc--; argv++;
if (argv[0][0] == '-' && argv[0][1] == 'x' && argv[0][2] == '\0')
{
hexdata = 1;
argc--; argv++;
}
length = strlen(argv[0]);
if (hexdata)
length >>= 1;
fDesc.dataHandle = NewHandle(length);
if (fDesc.dataHandle != NULL)
{
if (hexdata)
{
ptr = argv[0];
hptr = *fDesc.dataHandle;
for (i = 0 ; i < length ; i++)
{
if ((ch = *ptr++) == '\0')
break;
high = ( (ch >= '0' && ch <= '9') ? (ch - '0') : (((ch|0x20) - 'a') + 10) );
if ((ch = *ptr++) == '\0')
break;
low = ( (ch >= '0' && ch <= '9') ? (ch - '0') : (((ch|0x20) - 'a') + 10) );
*hptr++ = ( (high << 4) & 0xF0 ) | ( low & 0x0F );
}
}
else
{
BlockMove(argv[0], *fDesc.dataHandle, length);
}
argc--; argv++;
if (argv[0][0] == '-' && argv[0][1] == 'x' && argv[0][2] == '\0')
{
hexresult = 1;
argc--; argv++;
}
sprintf(tempstr, "%-4.4s", argv[0]);
strncpy((char *)&toType, tempstr, 4);
myerr = AECoerceDesc(&fDesc, toType, &tDesc);
if (myerr != noErr)
{
macintoshErr = myerr;
Tcl_AppendResult(interp, "\"", arg0, "\" coercing parm to '",
argv[0], "': ", Tcl_MacError(interp), (char *) NULL);
return TCL_ERROR;
}
else
{
length = GetHandleSize(tDesc.dataHandle);
saveptr = ptr = NewPtr( ( hexresult ? ((length << 1) + 2) : (length + 2) ) );
if (ptr != NULL)
{
HLock(tDesc.dataHandle);
hptr = *tDesc.dataHandle;
if (hexresult)
{
for (i=length; i > 0; i--, hptr++)
{
*ptr++ = *(hexstr + ((*hptr >> 4) & 0x0F));
*ptr++ = *(hexstr + (*hptr & 0x0F));
}
}
else
{
BlockMove(hptr, ptr, length);
ptr += length;
}
*ptr = '\0';
HUnlock(tDesc.dataHandle);
Tcl_SetResult(interp, saveptr, TCL_VOLATILE);
DisposPtr(saveptr);
}
else
{
macintoshErr = myerr;
Tcl_AppendResult(interp, "\"", arg0, "\" not enough memory coercing parm to '",
argv[0], "': ", Tcl_MacError(interp), (char *) NULL);
return TCL_ERROR;
}
AEDisposeDesc(&tDesc);
}
AEDisposeDesc(&fDesc);
}
else
{
macintoshErr = myerr;
Tcl_AppendResult(interp, "\"", arg0, "\" not enough memory coercing parm to '",
argv[0], "': ", Tcl_MacError(interp), (char *) NULL);
return TCL_ERROR;
}
return result;
}
int
Cmd_SendAppleEvent(clientData, interp, argc, argv)
char *clientData;
Tcl_Interp *interp;
int argc;
char **argv;
{
int i, index, argi, myerr, length, result = TCL_OK, timeout = 120,
want_stdout = 0, want_hexout = 0, want_longout = 0;
DescType class_type, event_type, param_key, param_type, returnType;
AEDesc theRDesc, aeBuildDesc;
AESendMode sendmode = 0;
long replyLong, actualSize;
char state, *argv0, *buffer, *ptr, *hptr;
char tempstr[32];
char errorStr[128];
AppleEvent theAEvent;
AppleEvent theREvent;
#pragma unused (clientData)
argv0 = argv[0];
for (index = 0 ; index < MAX_AE_TARGETS ; index++)
{
if (_ae_targets[index].state == AET_INUSE)
if (strcmp(_ae_targets[index].name, argv[1]) == SAMESTR)
break;
}
if (index >= MAX_AE_TARGETS)
{
Tcl_AppendResult(interp, "\"", argv0, "\" target '",
argv[1], "' unknown", (char *) NULL);
return TCL_ERROR;
}
else
{
argc -= 2; argv += 2;
sendmode = kAENoReply | kAENeverInteract;
for ( ; argc && argv[0][0] == '-' && argv[0][2] == '\0' ; )
{
if ((argv[0][1]|0x20) == 't')
{
timeout = atoi(argv[1]);
if (timeout <= 0)
timeout = 120;
argc -= 2; argv += 2;
}
else if ((argv[0][1]|0x20) == 'w')
{
sendmode |= kAEWaitReply;
argc--; argv++;
}
else if ((argv[0][1]|0x20) == 'i')
{
sendmode |= kAECanInteract;
argc--; argv++;
}
else if ((argv[0][1]|0x20) == 'o')
{
want_stdout = 1;
argc--; argv++;
}
else if ((argv[0][1]|0x20) == 'x')
{
want_hexout = 1;
argc--; argv++;
}
else if ((argv[0][1]|0x20) == 'l')
{
want_longout = 1;
argc--; argv++;
}
else
break;
}
sendmode |= kAEWantReceipt;
sprintf(tempstr, "%-4.4s", argv[0]);
strncpy((char *)&class_type, tempstr, 4);
sprintf(tempstr, "%-4.4s", argv[1]);
strncpy((char *)&event_type, tempstr, 4);
myerr = AECreateAppleEvent(class_type, event_type, &_ae_targets[index].target,
kAutoGenerateReturnID, kAnyTransactionID, &theAEvent);
if (myerr == noErr)
{
for (argi = 2 ; argi < argc ; argi++ )
{
if (argv[argi][0] != '-' && argv[argi][0] != '+')
continue;
if (argv[argi][0] == '-')
{
/* -xxxxyyyy syntax.... */
sprintf(tempstr, "%-4.4s", &argv[argi][1]);
strncpy((char *)¶m_key, tempstr, 4);
sprintf(tempstr, "%-4.4s", &argv[argi][5]);
strncpy((char *)¶m_type, tempstr, 4);
myerr = AEPutParamPtr( &theAEvent, param_key, param_type,
(argv[argi+1][0] == '-' ? "" : argv[argi+1]),
(argv[argi+1][0] == '-' ? 0 : strlen(argv[argi+1])) );
if (myerr != noErr)
{
macintoshErr = myerr;
Tcl_AppendResult(interp, "\"", argv0, "\" error adding parameter '",
argv[argi], "': ", Tcl_MacError(interp), (char *) NULL);
AEDisposeDesc(&theAEvent);
return TCL_ERROR;
break;
}
(argv[argi+1][0] == '-') ? argi : ++argi;
}
else
{
/* +xxxx[yyyy] syntax.... */
#ifdef THINK_C
Tcl_AppendResult(interp, "\"", argv0, "\" error AEBuild not supported under ThinkC '",
argv[argi+1], "'", (char *) NULL);
AEDisposeDesc(&theAEvent);
result = TCL_ERROR;
#else
sprintf(tempstr, "%-4.4s", &argv[argi][1]);
strncpy((char *)¶m_key, tempstr, 4);
myerr = AEBuild(&aeBuildDesc, argv[argi+1]);
if (myerr != noErr)
{
macintoshErr = myerr;
Tcl_AppendResult(interp, "\"", argv0, "\" building descriptor '",
argv[argi+1], "': ", Tcl_MacError(interp), (char *) NULL);
AEDisposeDesc(&theAEvent);
result = TCL_ERROR;
break;
}
else
{
myerr = AEPutParamDesc( &theAEvent, param_key, &aeBuildDesc);
if (myerr != noErr)
{
macintoshErr = myerr;
Tcl_AppendResult(interp, "\"", argv0, "\" error adding parameter '",
argv[argi+1], "': ", Tcl_MacError(interp), (char *) NULL);
result = TCL_ERROR;
break;
}
AEDisposeDesc(&aeBuildDesc);
}
#endif
++argi;
}
}
if (myerr == noErr)
{
WatchCursorOn();
myerr = AESend(&theAEvent, &theREvent, (kAEWaitReply | kAENeverInteract),
kAENormalPriority, timeout, (IdleProcPtr)0, (EventFilterProcPtr)0);
if (myerr == noErr)
{
myerr = AEGetParamPtr(&theREvent, keyErrorNumber, typeLongInteger, &returnType,
(Ptr)&replyLong, sizeof(replyLong), &actualSize);
if (myerr == errAEDescNotFound)
{
myerr = AEGetParamDesc(&theREvent,
(want_stdout ? keyStdOutObject : keyDirectObject),
typeWildCard, &theRDesc);
if (myerr == noErr)
{
if (want_hexout)
length = (GetHandleSize(theRDesc.dataHandle) << 1) + 4;
else
length = GetHandleSize(theRDesc.dataHandle) + 4;
if (want_longout)
length += 7;
buffer = NewPtr(length);
if (buffer != NULL)
{
if (want_longout)
sprintf(buffer, "{%-4.4s} ", &theRDesc.descriptorType);
ptr = &buffer[(want_longout ? 7 : 0)];
if (want_hexout)
{
state = HGetState(theRDesc.dataHandle);
HLock(theRDesc.dataHandle);
hptr = *theRDesc.dataHandle;
*ptr++ = '{';
for (i=GetHandleSize(theRDesc.dataHandle); i > 0; i--,hptr++)
{
*ptr++ = *(hexstr + ((*hptr >> 4) & 0x0F));
*ptr++ = *(hexstr + (*hptr & 0x0F));
}
*ptr++ = '}';
*ptr = '\0';
HSetState(theRDesc.dataHandle, state);
}
else
{
state = HGetState(theRDesc.dataHandle);
HLock(theRDesc.dataHandle);
sprintf(ptr, "{%.*s}",
GetHandleSize(theRDesc.dataHandle),
*(theRDesc.dataHandle));
HSetState(theRDesc.dataHandle, state);
}
Tcl_SetResult(interp, buffer, TCL_VOLATILE);
}
else
{
macintoshErr = MemError();
Tcl_AppendResult(interp, "\"", argv0, "\" not enough memory for result: ",
Tcl_MacError(interp), (char *) NULL);
result = TCL_ERROR;
}
AEDisposeDesc(&theRDesc);
}
else
{
Tcl_SetResult(interp, NULL, TCL_VOLATILE);
}
}
else
{
myerr = AEGetParamPtr(&theREvent, keyErrorString, typeChar, &returnType,
(Ptr)errorStr, sizeof(errorStr)-1, &actualSize);
macintoshErr = replyLong;
Tcl_AppendResult(interp, "\"", argv0, "\" Apple Event returns \"",
Tcl_MacError(interp), "\" ", (char *) NULL);
result = TCL_ERROR;
}
}
else
{
macintoshErr = myerr;
Tcl_AppendResult(interp, "\"", argv0, "\" error sending Apple Event: ",
Tcl_MacError(interp), (char *) NULL);
result = TCL_ERROR;
}
AEDisposeDesc(&theREvent);
}
else
#ifndef THINK_C
if (myerr != aeBuild_SyntaxErr)
#endif
{
macintoshErr = myerr;
Tcl_AppendResult(interp, "\"", argv0, "\" error inserting parameters: ",
Tcl_MacError(interp), (char *) NULL);
result = TCL_ERROR;
}
AEDisposeDesc(&theAEvent);
}
else
{
macintoshErr = myerr;
Tcl_AppendResult(interp, "\"", argv0, "\" error creating Apple Event: ",
Tcl_MacError(interp), (char *) NULL);
result = TCL_ERROR;
}
}
return result;
}
int
Cmd_AEInterAct(clientData, interp, argc, argv)
char *clientData;
Tcl_Interp *interp;
int argc;
char **argv;
{
#ifdef TCLAPPL
int myerr;
extern int errno;
#pragma unused (clientData, argc, argv)
myerr = AEInteractWithUser(kAEDefaultTimeout, (NMRecPtr)0, (IdleProcPtr)0);
if (myerr != noErr)
{
macintoshErr = myerr;
Tcl_AppendResult(interp, "\"", argv[0], "\" error interating with user: ",
Tcl_MacError(interp), (char *) NULL);
return TCL_ERROR;
}
else
{
Tcl_SetResult(interp, NULL, TCL_VOLATILE);
}
return TCL_OK;
#else
#pragma unused (clientData, interp, argc, argv)
Tcl_AppendResult(interp, "\"", argv[0], "\" unimplemented in engine", (char *) NULL);
return TCL_ERROR;
#endif
}
int
Cmd_ListAETargets(clientData, interp, argc, argv)
char *clientData;
Tcl_Interp *interp;
int argc;
char **argv;
{
int index, myerr;
long length;
Handle myhandle;
PPCPortRec pName;
LocationNameRec lName;
PortInfoRec myinfo;
IPCListPortsPBRec pb;
#pragma unused (clientData, argc)
myhandle = NewHandle(1);
if (myhandle == NULL)
{
macintoshErr = MemError();
Tcl_AppendResult(interp, "\"", argv[0], "\" not enough memory: ",
Tcl_MacError(interp), (char *) NULL);
return TCL_ERROR;
}
**myhandle = '\0';
pName.nameScript = smRoman;
pName.portKindSelector = ppcByString;
pName.name[0] = 1;
pName.name[1] = '=';
pName.u.portTypeStr[0] = 1;
pName.u.portTypeStr[1] = '=';
lName.locationKindSelector = ppcNBPLocation;
strncpy(lName.u.nbpEntity.objStr, argv[1], 32);
strncpy(lName.u.nbpEntity.typeStr, argv[2], 32);
strncpy(lName.u.nbpEntity.zoneStr, argv[3], 32);
lName.u.nbpEntity.objStr[32] = '\0';
lName.u.nbpEntity.typeStr[32] = '\0';
lName.u.nbpEntity.zoneStr[32] = '\0';
c2pstr(lName.u.nbpEntity.objStr);
c2pstr(lName.u.nbpEntity.typeStr);
c2pstr(lName.u.nbpEntity.zoneStr);
for ( index = 0 ; ++index ; )
{
pb.ioCompletion = 0;
pb.startIndex = index;
pb.requestCount = 1;
pb.portName = &pName;
pb.locationName = &lName;
pb.bufferPtr = &myinfo;
myerr = IPCListPorts(&pb, FALSE);
if (myerr != noErr)
{
DisposHandle(myhandle);
macintoshErr = myerr;
Tcl_AppendResult(interp, "\"", argv[0], "\" error from IPCListPorts(): ",
Tcl_MacError(interp), (char *) NULL);
return TCL_ERROR;
}
if (pb.actualCount < 1)
break;
length = GetHandleSize(myhandle);
SetHandleSize(myhandle, (length + myinfo.name.name[0] + myinfo.name.u.portTypeStr[0] + 6));
if (MemError() != noErr)
{
DisposHandle(myhandle);
macintoshErr = MemError();
Tcl_AppendResult(interp, "\"", argv[0], "\" error not enough memory: ",
Tcl_MacError(interp), (char *) NULL);
return TCL_ERROR;
}
else
{
HLock(myhandle);
sprintf(*myhandle + (length - 1), "{%.*s} {%.*s}\n",
myinfo.name.name[0], &myinfo.name.name[1],
myinfo.name.u.portTypeStr[0], &myinfo.name.u.portTypeStr[1]);
HUnlock(myhandle);
}
}
HLock(myhandle);
Tcl_SetResult(interp, *myhandle, TCL_VOLATILE);
HUnlock(myhandle);
DisposHandle(myhandle);
return TCL_OK;
}
run_AE_tcl_script(theFSS, result_handle, stdout_handle)
FSSpec *theFSS;
Handle result_handle;
Handle stdout_handle;
{
int result = noErr, wderr;
short wdrefnum;
Handle saveH, myhandle = NULL;
PFI saveproc;
Tcl_Interp *interp;
char command[128];
WatchCursorOn();
/* create a Tcl interpreter for the session */
interp = g_interp;
saveproc = Tcl_SetPrintProcedure(tcl_dev_null_output);
sprintf(command, "set AEVENT 1\n");
result = Tcl_Eval(interp, command, 0, (char **)0);
if (result != TCL_OK)
Feedback("ERROR %d on <%s>", result, command);
wderr = OpenWD(theFSS->vRefNum, theFSS->parID, 'ERIK', &wdrefnum);
if (wderr == noErr)
SetVol(NULL, wdrefnum);
else
Feedback("Error %d OpenWD().", wderr);
if (stdout_handle == NULL)
{
myhandle = NewHandle(0);
if (myhandle == NULL)
{
Feedback("Error #%d allocating a stdout handle.", MemError());
return -1770;
}
else
saveH = tcl_Houtput_sethdl(myhandle);
}
else
saveH = tcl_Houtput_sethdl(stdout_handle);
Tcl_SetPrintProcedure(tcl_handle_output);
sprintf(command, "source \"%.*s\"\n", theFSS->name[0], &theFSS->name[1]);
result = Tcl_Eval(interp, command, 0, (char **)0);
if (wderr == noErr)
wderr = CloseWD(wdrefnum);
if (result == TCL_OK)
{
result = noErr;
if (result_handle != NULL)
{
tcl_Houtput_sethdl(result_handle);
if (interp->result != NULL && *(interp->result) != '\0')
(* Tcl_GetPrintProcedure()) (interp->result);
}
}
else
{
result = -1771;
(* Tcl_GetPrintProcedure()) ( (result == TCL_ERROR) ? "\015Error: " : "\015Bad Result: " );
(* Tcl_GetPrintProcedure()) ( (interp->result == NULL) ? "<NULL>" : interp->result );
}
Tcl_SetPrintProcedure(tcl_dev_null_output);
sprintf(command, "set AEVENT 0\n");
Tcl_Eval(interp, command, 0, (char **)0);
Tcl_SetPrintProcedure(saveproc);
tcl_Houtput_sethdl(saveH);
if (myhandle != NULL)
DisposHandle(myhandle);
UInitCursor();
return result;
}
run_DoScript(script_handle, result_handle, stdout_handle)
Handle script_handle;
Handle result_handle;
Handle stdout_handle;
{
int result;
int delete_interp = 0;
PFI saveproc;
Handle myhandle = NULL;
char command[128];
Tcl_Interp *interp;
TclTickle_BegYield();
WatchCursorOn();
/* create a Tcl interpreter for the session */
interp = g_interp;
saveproc = Tcl_SetPrintProcedure(tcl_dev_null_output);
sprintf(command, "set AEVENT 1\n");
result = Tcl_Eval(interp, command, 0, (char **)0);
if (result != TCL_OK)
Feedback("ERROR %d on <%s>", result, command);
Tcl_SetPrintProcedure(saveproc);
result = Tcl_Interp_Handle(interp, script_handle, result_handle, stdout_handle);
if (result == TCL_OK)
{
result = noErr;
}
saveproc = Tcl_SetPrintProcedure(tcl_dev_null_output);
sprintf(command, "set AEVENT 0\n");
result = Tcl_Eval(interp, command, 0, (char **)0);
Tcl_SetPrintProcedure(saveproc);
TclTickle_EndYield();
UInitCursor();
return result;
}
int
XPROC_Eval_CallBack(cpb, script_handle, result_handle, stdout_handle)
XPROCPBPtr cpb;
Handle script_handle;
Handle result_handle;
Handle stdout_handle;
{
return Tcl_Interp_Handle(cpb->interp, script_handle, result_handle, stdout_handle);
}
int
Cmd_InstallXPROCS(clientData, interp, argc, argv)
char *clientData;
Tcl_Interp *interp;
int argc;
char **argv;
{
Handle myhandle = NULL,
result_handle = NULL;
int index, myerr, result = TCL_OK;
short saveref, proc_ref = -1, proctype;
short rsrcID;
ResType rsrcType;
DescType from_type, to_type, class_type, event_type;
char name[256], *ptr;
struct stat statbuf;
#pragma unused (clientData, argc, interp)
saveref = CurResFile();
if ( stat( argv[1], &statbuf ) < 0 )
{
Tcl_AppendResult(interp, "could not locate resource file \"",
argv[1], "\" ", Tcl_PosixError(interp), NULL);
return TCL_ERROR;
}
ptr = strrchr( argv[1], ':');
if (ptr != NULL)
strcpy(name, ptr+1);
else
strcpy(name, argv[1]);
c2pstr(name);
proc_ref = HOpenResFile( statbuf.st_dev, statbuf.st_parid,
(unsigned char *)name, fsRdPerm);
p2cstr(name);
if (proc_ref == -1)
{
macintoshErr = ResError();
Tcl_AppendResult(interp, "\"", argv[0], "\" error opening '",
argv[1], "': ", Tcl_MacError(interp), (char *) NULL);
return TCL_ERROR;
}
UseResFile(proc_ref);
for (index = 1 ; ; index++)
{
myhandle = Get1IndResource((ResType)'PROC', index);
if (myhandle == NULL)
break;
GetResInfo(myhandle, &rsrcID, &rsrcType, name);
if (name[0] < 8)
{
Feedback("Coercion resource <%.*s> INVALID.", name[0], &name[1]);
continue;
}
p2cstr(name);
if (strncmp(name, "CSPT", 4) == SAMESTR)
{
proctype = 'c';
strncpy((char *)&from_type, &name[4], 4);
strncpy((char *)&to_type, &name[8], 4);
}
else if (strncmp(name, "CSDS", 4) == SAMESTR)
{
proctype = 'C';
strncpy((char *)&from_type, &name[4], 4);
strncpy((char *)&to_type, &name[8], 4);
}
else if (strncmp(name, "AEVT", 4) == SAMESTR)
{
proctype = 'A';
strncpy((char *)&class_type, &name[4], 4);
strncpy((char *)&event_type, &name[8], 4);
}
else {
Feedback("Error unknown PROC type '%-4.4s' ", name);
continue;
}
LoadResource(myhandle);
DetachResource(myhandle);
MoveHHi(myhandle);
HNoPurge(myhandle);
HLock(myhandle);
if (proctype == 'C' || proctype == 'c')
{
myerr = AEInstallCoercionHandler(from_type, to_type, (ProcPtr)(*myhandle),
(long)&g_cbpb, (proctype == 'C'), FALSE);
if (myerr != noErr)
{
Feedback("Error %d installing coercion <%s> ", name);
}
else
{
Feedback("Coercion resource FROM '%-4.4s' TO '%-4.4s' installed. ",
&from_type, &to_type);
}
}
else if (proctype == 'A')
{
myerr = AEInstallEventHandler(class_type, event_type, (ProcPtr)(*myhandle),
(long)&g_cbpb, FALSE);
if (myerr != noErr)
{
Feedback("Error %d installing event handler <%s> ", name);
}
else
{
Feedback("Apple Event '%-4.4s' '%-4.4s' installed. ",
&class_type, &event_type);
}
}
}
CloseResFile(proc_ref);
UseResFile(saveref);
return TCL_OK;
}
InitAEtcl(interp)
Tcl_Interp *interp;
{
int i;
for (i = 0 ; i < MAX_AE_TARGETS ; i++)
{
_ae_targets[i].name[0] = '\0';
_ae_targets[i].state = AET_UNUSED;
}
Tcl_CreateCommand(interp, "aeinteract", Cmd_AEInterAct,
(ClientData)NULL, (void (*)())NULL);
Tcl_CreateCommand(interp, "aeopen", Cmd_OpenAETarget,
(ClientData)NULL, (void (*)())NULL);
Tcl_CreateCommand(interp, "aesend", Cmd_SendAppleEvent,
(ClientData)NULL, (void (*)())NULL);
Tcl_CreateCommand(interp, "aeclose", Cmd_CloseAETarget,
(ClientData)NULL, (void (*)())NULL);
Tcl_CreateCommand(interp, "aelist", Cmd_ListAEOpenTargets,
(ClientData)NULL, (void (*)())NULL);
Tcl_CreateCommand(interp, "aeload", Cmd_InstallXPROCS,
(ClientData)NULL, (void (*)())NULL);
Tcl_CreateCommand(interp, "aecoerce", Cmd_Coerce,
(ClientData)NULL, (void (*)())NULL);
Tcl_CreateCommand(interp, "aetargets", Cmd_ListAETargets,
(ClientData)NULL, (void (*)())NULL);
}