home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / MPW / gzip 1.2.2 / cextras-1.0 / toolserver.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-08  |  6.1 KB  |  211 lines  |  [TEXT/MPS ]

  1. /*
  2.     ToolServer.c
  3.     
  4.     This function is sort of like a traditional Unix `system' command.
  5.     The command is executed via ToolServer, which must be available on the
  6.     current computer or through a network connection.
  7.     
  8.     Copyright (c) 1993 Anthony C. Ard.
  9.  
  10.     This program is free software; you can redistribute it and/or
  11.     modifiy it under the terms of the GNU General Public License
  12.     as published by the Free Software Foundation; either version 2
  13.     of the License, or (at your option) any later version.
  14.     
  15.     This program is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18.     GNU General Public License for more details.
  19.     
  20.     You should have received a copy of the GNU General Public License
  21.     along with this program; if not, write to the Free Software
  22.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24.  
  25. #include <Quickdraw.h>
  26. #include <Fonts.h>
  27. #include <Processes.h>
  28. #include <AppleEvents.h>
  29. #include <Errors.h>
  30. #include <CursorCtl.h>
  31. #include <StdIO.h>
  32. #include <StdLib.h>
  33. #include <String.h>
  34. #include <Strings.h>
  35. #include <Files.h>
  36. #include <PLStringFuncs.h>
  37.  
  38. #define kMesgClass    'misc'        /* 'Do Script' Apple Event. */
  39. #define kMesgID        'dosc'
  40.  
  41. OSErr PathNameFromDirID( long dirID, short vRefNum, char *fullPathName )
  42. {
  43.     Str255        dirName = "\p", fullName = "\p", tmpString;
  44.     CInfoPBRec    myCInfo;
  45.     OSErr        err = noErr;
  46.     
  47.     myCInfo.dirInfo.ioNamePtr = dirName;
  48.     myCInfo.dirInfo.ioDrParID = dirID;
  49.     
  50.     do {
  51.         myCInfo.dirInfo.ioVRefNum = vRefNum;
  52.         myCInfo.dirInfo.ioFDirIndex = -1;
  53.         myCInfo.dirInfo.ioDrDirID = myCInfo.dirInfo.ioDrParID;
  54.         
  55.         err = PBGetCatInfo( &myCInfo, false );
  56.         
  57.         if( err == noErr ) {
  58.             PLstrcat( &dirName, "\p:" );
  59.             if( (PLstrlen( &dirName ) + PLstrlen( &fullName )) > 255 ) err = bdNamErr;
  60.             else {
  61.                 PLstrcpy( &tmpString, dirName );
  62.                 PLstrcat( &tmpString, fullName );
  63.                 PLstrcpy( &fullName, tmpString );
  64.             }
  65.         }
  66.     } while( (myCInfo.dirInfo.ioDrDirID != fsRtDirID) && (err == noErr) );
  67.     p2cstr( &fullName );
  68.     strcpy( fullPathName, fullName );
  69.     return err;
  70. }
  71.  
  72. OSErr PathNameFromWD( int wdRefNum, char *pathName )
  73. {
  74.     int        dirID, procID;
  75.     short    vRefNum;
  76.     OSErr    err;
  77.     
  78.     err = GetWDInfo( wdRefNum, &vRefNum, &dirID, &procID );
  79.     
  80.     if( err == noErr ) err = PathNameFromDirID( dirID, vRefNum, pathName );
  81.     
  82.     return err;
  83. }
  84.  
  85. OSErr GetCurrentDirectory( char *pathName )
  86. {
  87.     return (PathNameFromDirID( 0, 0, pathName ));
  88. }
  89.  
  90. static OSErr LaunchToolServer( ProcessSerialNumber *tsPSN )
  91. {
  92.     char                tsPath[256];
  93.     FSSpec                tsSpec;
  94.     OSErr                myErr;
  95.     LaunchParamBlockRec    myLaunchParams;
  96.     
  97.     strcpy( &tsPath, getenv( "ToolServerPath" ) );
  98.     
  99.     myErr = FSMakeFSSpec( 0, 0, (StringPtr)c2pstr( &tsPath ), (FSSpecPtr)&tsSpec );
  100.     if( myErr == noErr ) {
  101.         myLaunchParams.launchBlockID = extendedBlock;
  102.         myLaunchParams.launchEPBLength = extendedBlockLen;
  103.         myLaunchParams.launchFileFlags = 0;
  104.         myLaunchParams.launchControlFlags = launchContinue + launchNoFileFlags + launchDontSwitch;
  105.         myLaunchParams.launchAppSpec = &tsSpec;
  106.         myLaunchParams.launchAppParameters = nil;
  107.         
  108.         myErr = LaunchApplication( &myLaunchParams );
  109.         if( myErr == noErr )
  110.             memcpy( (char *)tsPSN, (char *)&myLaunchParams.launchProcessSN,
  111.                     sizeof( ProcessSerialNumber ) );
  112.         /* Cause process manager to actually launch the application at the next
  113.            WaitNextEvent call. */
  114.         SpinCursor( 0 );
  115.     }
  116.     return myErr;
  117. }
  118.  
  119. OSErr SendScriptEvent( AEAddressDesc *serverAddress, const char *command )
  120. {
  121.     AppleEvent    theAppleEvent, reply;
  122.     DescType    returnedType;
  123.     long        actualSize;
  124.     OSErr        myErr;
  125.     char        *scriptResult;
  126.     
  127.     myErr = AECreateAppleEvent( kMesgClass, kMesgID, serverAddress, kAutoGenerateReturnID,
  128.                                 kAnyTransactionID, &theAppleEvent );
  129.     if( myErr == noErr ) {
  130.         myErr = AEPutParamPtr( &theAppleEvent, '----', 'TEXT', command,
  131.                                strlen( command ) * sizeof( char ) );
  132.         if( myErr == noErr ) {
  133.             myErr = AESend( &theAppleEvent, &reply, kAEWaitReply + kAENeverInteract,
  134.                             kAENormalPriority, kNoTimeOut, nil, nil );
  135.             if( myErr == noErr ) {
  136.                 myErr = AESizeOfParam( &reply, keyDirectObject, &returnedType, &actualSize );
  137.                 if( myErr == noErr ) {
  138.                     scriptResult = (char *) malloc( actualSize + sizeof( char ) );
  139.                     if( scriptResult ) {
  140.                         myErr = AEGetParamPtr( &reply, keyDirectObject, typeChar, &returnedType,
  141.                                                (Ptr) scriptResult, actualSize, &actualSize );
  142.                         if( myErr == noErr ) {
  143.                             scriptResult[actualSize] = '\0';
  144.                             fprintf( stdout, "%s\n", scriptResult );
  145.                             free( scriptResult );
  146.                         }
  147.                     } else myErr = memFullErr;
  148.                 } else if( myErr == errAEDescNotFound ) myErr = noErr;    /* ToolServer had no output. */
  149.             } else {
  150.                 /* The Apple Event wasn't successfully dispatched, the request timed out,
  151.                    the user canceled, or other error. */
  152.             }
  153.         }
  154.     }
  155.     AEDisposeDesc( &theAppleEvent ); AEDisposeDesc( &reply );
  156.     return myErr;
  157. }
  158.  
  159. int ToolServer( const char *command )
  160. {
  161.     OSErr            myErr;
  162.     AEDesc            tsAddress;
  163.     char            pathName[256];
  164.     char            tempString[256] = "SetDirectory '";
  165.     static Boolean                firstTime = true;
  166.     static ProcessSerialNumber    tsPSN;
  167.     
  168.     if( firstTime ) {
  169.         myErr = LaunchToolServer( &tsPSN );
  170.         if( myErr == noErr ) {
  171.             myErr = AECreateDesc( typeProcessSerialNumber, (Ptr) &tsPSN, sizeof( tsPSN ), &tsAddress );
  172.             if( myErr == noErr ) {
  173.                 firstTime = false;
  174.  
  175.                 /* Get pathname of current working directory. */
  176.                 GetCurrentDirectory( &pathName );
  177.                 strcat( &tempString, pathName );
  178.                 strcat( &tempString, "'" );
  179.             
  180.                 /* Sync ToolServer's directory with MPW Shell's */
  181.                 myErr = SendScriptEvent( &tsAddress, &tempString );
  182.                 
  183.                 /* Send the command */
  184.                 if( myErr == noErr ) myErr = SendScriptEvent( &tsAddress, command );
  185.             }
  186.         }
  187.     } else {
  188.         myErr = AECreateDesc( typeProcessSerialNumber, (Ptr) &tsPSN, sizeof( tsPSN ), &tsAddress );
  189.         if( myErr == noErr ) myErr = SendScriptEvent( &tsAddress, command );
  190.     }
  191.     AEDisposeDesc( &tsAddress );
  192.     return ((int) myErr);
  193. }
  194.  
  195. #ifdef TEST
  196. int main()
  197. {
  198.     int err;
  199.     
  200.     InitGraf( &qd.thePort );
  201.     SetFScaleDisable( true );
  202.     InitCursorCtl( nil );
  203.     SpinCursor( 0 );
  204.     
  205.     err = ToolServer( "Files -l" );
  206.     printf( "\n### ToolServer result: %d\n", err );
  207.  
  208.     return 0;
  209. }
  210. #endif /* TEST */
  211.