home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Utilities / Programming / EnterAct 3.5 / Minimal App7 ƒ / Call_Resource.c next >
Encoding:
C/C++ Source or Header  |  1995-01-13  |  35.9 KB  |  1,060 lines  |  [TEXT/KEEN]

  1. /* Call_Resource.c: file to be added to any applications wishing to
  2. call the code resources that come with EnterAct.
  3. THIS IS A COPY of the other Call_Resource.c file on your source disk,
  4. adapted for Minimal App. */
  5.  
  6. /* Copyright © 1991, 1992, 1993 the Free Software Foundation, Inc.
  7.  *         This file is free software; you can redistribute or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 1, or any later version.
  10.  *         This file is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13.  * GNU General Public License for more details.
  14.  *         You should have received a copy of the GNU General Public License
  15.  * along with GAWK; see the file "COPYING hAWK". If not, write to
  16.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  * Written for THINK C 4 on the Macintosh by Ken Earle (Dynabyte) Aug 1991.
  18.  */
  19.  
  20. /*
  21. To call EnterAct-style code resources from your application:
  22. 1    Add a copy of this file to your app.
  23. 2    Call "InitCallResources()" in your startup code, just before
  24.     entering your event loop. The arguments are:
  25.     vRefNumForApp : preferably your application working directory, or 0
  26.     codeFolderName : preferably (Ptr)"\pDrag_on Modules"
  27.     menuID : the integer menu id of the menu you wish code resources
  28.         to be appended to. Preferably not File or Edit.
  29. 3    Call "CallResource()" to handle the code resources picked from
  30.     the menu you have added them to. Typically looks something like
  31.         #define    MenuInQuestion    261
  32.         void DoMenuInQuestionCommand(short item)
  33.             {
  34.             switch (item)
  35.                 {
  36.             case 1:
  37.                 DoItemOne();
  38.             break;
  39.             case 2:
  40.                 DoItemTwo();
  41.             break;
  42.             ...etc...
  43.             default:
  44.                 if (item > 0)
  45.                     CallResource(MenuInQuestion, item);
  46.                     --or if you set VERSION2 == TRUE,
  47.                     CallResource(MenuInQuestion, item, true/false);
  48.                     --
  49.             break;
  50.                 }
  51.             }
  52. 4    Go through the extension ("callback") functions defined at the
  53.     bottom of this file, and decide which ones you wish to support.
  54.     You will probably not be able to support "InDictionary()", and
  55.     "GetNextMultiFile()" requires that your application keep a list
  56.     of files for some purpose or other (typically searching), but if
  57.     your application supports text files you should be able to provide
  58.     support for the others. Note "GetScreenHeight" and "GetScreenWidth"
  59.     are done here, no modification needed. The code resources supplied
  60.     with EnterAct can still do useful things even if no extensions are
  61.     supplied.
  62.     
  63.     If you decide not to support a particular extension, you need only
  64.     set the corresponding pointer to NULL in the function SetUpCodeCommunication().
  65.     For example, if you don't support InDictionary_Ext, then put
  66.         gacc.InDictionary_Ext = NULL;
  67.     in SetUpCodeCommunication().
  68.     
  69.     For more about the "DoEventLoopOnce()" callback, see 6 below.
  70.     
  71. 5    "ShowResult" and "SelectResult" defined below are called by
  72.     "CallResource" in response to a request by a code resource to display
  73.     the text results of the code resource run. If your application supports
  74.     TEXT documents, you should modify these functions to provide your
  75.     equivalents for these abilities. Otherwise, leave them as-is.
  76.  
  77. 6    Version 2 of hAWK (the one you received with this file) supports
  78. concurrent execution. This means you fire up a hAWK program, go back
  79. to working in your application (or even switch to some other app)
  80. and hAWK notifies you when the program is done. There is a
  81. penalty in speed of the hAWK program, usually far outweighed by
  82. the benefit of being able to go back to work instantly rather than
  83. having to sit there watching the watch cursor. Your calling application
  84. should not be noticeably affected, except when the hAWK program
  85. is doing something massive such as a huge "sort" or a large "copy".
  86.  
  87. The "VERSION2" defined constant just below should be set to TRUE if
  88. you wish to support concurrent execution of hAWK programs, FALSE
  89. if you do not. That's all that VERSION2 affects. Read Resource cannot
  90. run concurrently, and is not affected by the value of VERSION2.
  91.  
  92. To support concurrent exec of hAWK programs, follow these steps:
  93.     1    define VERSION2 just below to be TRUE
  94.     2    In the menu where you call Drag_on Modules, use
  95.         ...CallResource(MenuInQuestion, item, bool);
  96.         rather than just CallResource(MenuInQuestion, item);
  97.         -the "bool" variable is a Boolean, to be set to TRUE if
  98.         you want concurrent execution. Recommended interface is
  99.         to set this variable FALSE if the user has held down
  100.         either the <Shift> or <Option> key or both, and set it
  101.         TRUE otherwise.
  102.     3    One additional callback function needs to be provided,
  103.         in "DoEventLoopOnce()" below. Your callback should
  104.         have the form
  105.             extern void HandleOneEvent(void);
  106.         and can be a copy of your main event loop function,
  107.         except that it should handle just one event and then
  108.         return, and startup initializations if any can
  109.         be skipped (creating a region to track the mouse,
  110.         for example). If you use a global event record, you
  111.         can use the same global in your "HandleOneEvent"
  112.         function, but keep in mind that when "CallResource()"
  113.         returns, your global event record will reflect the
  114.         last event processed during your Drag_on Module run,
  115.         rather than the original menu call -- check what your
  116.         main event loop does with the event after the orginal
  117.         call, and verify that nothing important will be confused
  118.         by having the nature of the event changed during the
  119.         Drag_on Modules run.
  120.         (Note your event handler does not need to do any special
  121.         checking for the <Command><period> that stops hAWK.)
  122.     4    If your calling app is in the background when hAWK
  123.         finishes a run, a notice will be installed by the
  124.         DoNotify() routine below, flashing an icon in the
  125.         menu bar etc until you return to the calling app.
  126.         In your MAIN event handler (not the handle-one-event)
  127.         you should place the following bit of code to properly
  128.         handle the notify when your app resumes:
  129.             --declaration at the top of its file;
  130.                 -- to handle concurrent Drag_on Modules --
  131.                 Boolean gNotifying;
  132.                 Boolean gHawkIsRunning;
  133.                 NMRec    gNotifyRec;
  134.             
  135.             -- and in your main event handler, for the case of
  136.             "kSuspendResumeMessage";
  137.                 if (gNotifying)
  138.                     {
  139.                     NMRemove(&gNotifyRec);
  140.                     if (gNotifyRec.nmIcon)
  141.                         ReleaseResource(gNotifyRec.nmIcon);
  142.                     gNotifying = FALSE;
  143.                     ShowResultsAfterNotify();
  144.                     }
  145.         You will also need the standard global that records
  146.         whether or not your application is in the background,
  147.             Boolean gInBackground;
  148.         This variable should be set in your handle-one-event
  149.         function, and if your spelling of this variable name
  150.         differs from "gInBackground", change it here as well.
  151.         This file requires access to "gInBackground" in order
  152.         to determine if a notice should be posted at the end
  153.         of a hAWK program run.
  154.     5    In both your main event handler and the one-event
  155.         handler, calculate the "sleep" parameter for your
  156.         WaitNextEvent() call roughly as
  157.             gHawkIsRunning ? 2UL : GetCaretTime()
  158.         --adjust to suit your taste.
  159.     6    If you want the small icon "hAWK!" to flash for
  160.         notifications, move it to your application from "hAWK"
  161.         itself (it's just stored there).
  162.     7    One little wrinkle: what happens if you Quit your
  163.         application while hAWK is running? The recommended
  164.         solution here is to ask your user to issue a
  165.         <Command><period> to halt hAWK before proceeding
  166.         with the Quit. If you just pull the rug out from
  167.         under hAWK, files will probably be left open.
  168.         Sketchily:
  169.             Boolean DoQuit (...
  170.             extern Boolean gHawkIsRunning;...
  171.             if (gHawkIsRunning)
  172.                 {
  173.                 FlexAlert(justOK, stopIcon, "Please terminate your hAWK program \
  174.         with <Command><period> before quitting.");
  175.                 return(FALSE);
  176.                 }...
  177.     
  178. */
  179.  
  180. #include <string.h>
  181.  
  182. #ifndef NULL
  183. #define NULL        ((void *) 0)
  184. #endif
  185.  
  186. /* Change this to TRUE if you support concurrent hAWK - see above, point 6. */
  187. #define VERSION2 TRUE
  188.  
  189. #if VERSION2 == TRUE
  190. extern Boolean gInBackground;
  191. extern Boolean gNotifying;
  192. extern Boolean gHawkIsRunning;
  193. extern NMRec    gNotifyRec;
  194. #endif
  195.  
  196. /* Canned options, if you want to get going in a hurry:
  197. SUPPORT_LEVEL should be set to one of the following four options:
  198. MINIMAL - no extensions, no showing of results after a code resource run.
  199.     Note this doesn't mean the code resource won't be creating a text file,
  200.     it just means your application won't be showing it.
  201. RESULTSONLY - no extensions, but ability to show results supplied by you.
  202.     This means you should provide the hooks in ShowResult() and
  203.     SelectResult() - nothing else to do.
  204. BASICTEXT - in addition to providing the hooks in ShowResult() and
  205.     SelectResult(), fill in the blanks in GetFrontText() to allow the
  206.     code resource access to your front text window during a run.
  207. CUSTOMSUPPORT - ie avoid the above canned options.
  208.  
  209. There is one special case: if you support the "GetAppClip" extension,
  210. you’ll need to set SUPPORT_LEVEL to CUSTOMSUPPORT, and also
  211. define VERSION2 TRUE (getclip was not supported by version 1).
  212. OR you can do what's done here, and hack around the support_level
  213. restriction if, like Minimal App7, your app provides only minimal
  214. support but you want to implement the "getclip()" function in hAWK.
  215. */
  216.  
  217.  
  218. #define MINIMAL            1
  219. #define RESULTSONLY        2
  220. #define BASICTEXT        3
  221. #define CUSTOMSUPPORT    4
  222.  
  223. /* Pick yer pleasure - from the above 4 options only, please. */
  224. #define SUPPORT_LEVEL MINIMAL
  225.  
  226. /* Struct passed to all code resources. Everything should be optional.
  227. "stdInFileName" is primarily for code resources that insist on having input
  228. from an existing file - hAWK, for example. "stdOutFileName", however, is
  229. handy for use by any code resource that creates text, as this file can be
  230. shown by the calling application */
  231. typedef struct AppCodeComm
  232.     {
  233.     /* c strings except as noted */
  234.     char        *stdInFileName,    /* text from application to code resource */
  235.                 *stdInFileNameP, /* pascal version of above */
  236.                 *stdOutFileName, /* text from code resource to application */
  237.                 *stdErrFileName, /* error messages from code resource */
  238.                 *callerName,     /* the name of your application */
  239.                 *thisCodeName;     /* name of code resource being called */
  240.     short            result;
  241.     short            version; /* currently 2 */
  242. /* Extension ("callback") functions - ALL OPTIONAL. These pointers-to-functions
  243. are all set in  SetUpCodeCommunication() just before calling a code resource.*/
  244.     short            (*InDictionary_Ext)(char *tokenName);
  245.     Handle        (*GetFrontText_Ext)(Boolean getItAll);
  246.     void        (*GetNextMultiFile_Ext)(short *panePtr, short *indexPtr, 
  247.                 short *vRefNumPtr, char *fileName, Boolean clearFlag);
  248.     short         (*OKStopAlert_Ext)(Ptr cstringPtr);
  249.     void        (*MemoryAlert_Ext)(void);
  250.     short            (*GetScreenHeight_Ext)(void);
  251.     short            (*GetScreenWidth_Ext)(void);
  252.     void        (*ShowWatchCursor_Ext)(void);
  253. /* Concurrent exec, added for version 2 */
  254.     void        (*DoEventLoopOnce_Ext)(void);
  255. /* Access to scrap/clip of calling app */
  256.     Handle        (*GetAppClip_Ext)(void);
  257. /* Added for version 3 */
  258.     long        extendID; // Caller should set to 'VER3'
  259.     short        (*PutAppClip_Ext)(char *newClipStr);
  260.     } AppCodeComm, *ACCPtr;
  261.  
  262. static AppCodeComm    gacc;
  263.  
  264. /* Your application name goes here. */
  265. static char callerName[] = "MinimalApp7";
  266.  
  267. typedef void (*CallCode)(ACCPtr ac);
  268.  
  269. static char *stdPathP;
  270.  
  271. typedef struct SpecificFolder
  272.     {
  273.     char     volName[32];
  274.     long    theDirID;
  275.     short        vRefNum;
  276.     } SpecificFolder;
  277. static SpecificFolder codeFolder;
  278.  
  279. /* Functions defined in this file: */
  280. /* Call this one towards the end of your startup, just before event loop */
  281. void InitCallResources(short vRefNumForApp, char *codeFolderName, short menuID);
  282. /* Called by InitCallResource: */
  283. void SetUpStandardFileNames(short vRefNum);
  284. void ShowResourcesInMenu(short vRefNumForApp, 
  285.     char *codeFolderName, short menuID);
  286. /* Two support routines for above */
  287. long FindCodeResFolder(short vRefNumForApp, char *codeFolderName);
  288. void AddCodesToMenu(long theDirID, short menuID);
  289. void OpenWDForCODE(void);
  290. /* Call this one in the "default" part of the switch for the
  291. menu containing the code resource names */
  292. void CallResource(short menuID, short item
  293. #if VERSION2 == TRUE
  294.     , Boolean concurrent
  295. #endif
  296.     );
  297.  
  298. #if VERSION2 == TRUE
  299. /* Enable/disable Drag_ons if running concurrently */
  300. void XableDrag_ons(short menuID, Boolean enable);
  301. /* Post a notify - NOTE this must be cleared by calling app */
  302. void DoNotify(void);
  303. /* For delayed showing of results */
  304. void ShowResultsAfterNotify(void);
  305. /* Beep and flush events */
  306. void AnnounceCompletion(void);
  307. #endif
  308.  
  309. /* Called by CallResource(), decide which extension functions to pass along */
  310. Boolean SetUpCodeCommunication(char *progName);
  311. /* Called by CallResource() to display results of resource run */
  312. Boolean ShowResult(char *name); /* either stdout or stderr */
  313. void SelectResult(void);
  314. /* A few support functions: */
  315. /* Pascal strings */
  316. void CopyPStr(Byte *srcStr, Byte *dstStr);
  317. void AppendPStr(Byte *s1, Byte *s2);
  318. Boolean PasEqualStrs(char *aStr, char *bStr);
  319. /* Files, names and locations */
  320. Byte *FullPathNameFromDirectory(long DirID, short vRefNum, Byte *s);
  321. Byte *FullPathNameFromVRefNum(short vRefNum, Byte *s);
  322. short    OpenWorkingDirectoryFromFullName(char *name, short len);
  323.  
  324. /* The extension functions - wrappers for your own calls */
  325. short InDictionary(char *tokenName);
  326. Handle GetFrontText(Boolean getItAll);
  327. void GetNextMultiFile(short *panePtr, short *indexPtr, 
  328.             short *vRefNumPtr, char *fileName, Boolean clearFlag);
  329. short OKStopAlert(Ptr cstringPtr);
  330. void MemoryAlert(void);
  331. short GetScreenHeight(void);
  332. short GetScreenWidth(void);
  333. void ShowWatchCursor(void);
  334. void DoEventLoopOnce(void);
  335. Handle GetAppClip(void);
  336. short PutAppClip(char *newClipStr);
  337.  
  338.  
  339.  
  340.  
  341. /* Call just before entering your event loop.
  342. Sets up full path names for standard in/out/err files,
  343. adds any code resources present to the menu of your choice.
  344. */
  345. void InitCallResources(short vRefNumForApp, char *codeFolderName, short menuID)
  346.     {
  347.     SetUpStandardFileNames(vRefNumForApp);
  348.     ShowResourcesInMenu(vRefNumForApp, codeFolderName, menuID);
  349.     }
  350.  
  351. /* Called once at beginning of application. Sets up full path names
  352. for std in/out/err files, and as a bonus sets gacc.callerName.
  353. The vRefNum passed in should be the application's vRefNum, but
  354. can be any old thing you want.
  355. -you can determine your application vrefnum at startup with
  356. short        appVRefNum;
  357. if (GetVol(NULL, &appVRefNum))
  358.     appVRefNum = 0;
  359.  */
  360. void SetUpStandardFileNames(short vRefNum)
  361.     {
  362.     if (!vRefNum)
  363.         GetVol(NULL, &vRefNum);
  364.     stdPathP = NewPtr(256);
  365.     if (MemError() != noErr)
  366.         goto PathTrouble;
  367.     gacc.stdInFileName = NewPtr(256);
  368.     if (MemError() != noErr)
  369.         goto PathTrouble;
  370.     gacc.stdInFileNameP = NewPtr(256);
  371.     if (MemError() != noErr)
  372.         goto PathTrouble;
  373.     gacc.stdOutFileName = NewPtr(256);
  374.     if (MemError() != noErr)
  375.         goto PathTrouble;
  376.     gacc.stdErrFileName = NewPtr(256);
  377.     if (MemError() != noErr)
  378.         goto PathTrouble;
  379.     if (vRefNum)
  380.         (void)(FullPathNameFromVRefNum(vRefNum, (Byte *)stdPathP));
  381.     else
  382.         stdPathP[0] = 0;
  383.     PtoCstr((StringPtr)stdPathP);
  384.     strcpy((Ptr)(gacc.stdInFileName), (Ptr)stdPathP);
  385.     strcpy((Ptr)(gacc.stdInFileNameP), (Ptr)stdPathP);
  386.     strcpy((Ptr)(gacc.stdOutFileName), (Ptr)stdPathP);
  387.     strcpy((Ptr)(gacc.stdErrFileName), (Ptr)stdPathP);
  388.     strcat((Ptr)(gacc.stdInFileName), "$tempStdIn");
  389.     strcat((Ptr)(gacc.stdInFileNameP), "$tempStdIn");
  390.     strcat((Ptr)(gacc.stdOutFileName), "$tempStdOut");
  391.     strcat((Ptr)gacc.stdErrFileName, "$tempStdErr");
  392.     CtoPstr((Ptr)(gacc.stdInFileNameP));
  393.     gacc.callerName = NewPtr(strlen(callerName)+1);
  394.     if (MemError() != noErr)
  395.         goto PathTrouble;
  396.     strcpy(gacc.callerName, callerName);
  397.     
  398.     return;
  399. PathTrouble:
  400.     OKStopAlert("Out of memory right at the start! \
  401. Blew the tubes trying to allocate standard file names.");
  402.     ExitToShell();
  403.     }
  404.  
  405. /* Index through folders in application's folder, looking for
  406. "\pDrag_on Modules". If found, index through files in folder and add
  407. any resource files found. */
  408. void ShowResourcesInMenu(short vRefNumForApp, 
  409.     char *codeFolderName, short menuID)
  410.     {
  411.     long    theDirID;
  412.     
  413.     if (theDirID = FindCodeResFolder(vRefNumForApp, codeFolderName))
  414.         AddCodesToMenu(theDirID, menuID);
  415.     }
  416.  
  417. long FindCodeResFolder(short vRefNumForApp, char *codeFolderName)
  418.     {
  419.     HFileInfo    myCPB;
  420.     WDPBRec        theParms;
  421.     char        fName[32];
  422.     long        theDirID;
  423.     short            index = 1, len;
  424.     OSErr        err;
  425.     
  426.     /* First extract "\pVolName:", volRef, and dirID for application folder */
  427.     theParms.ioNamePtr = (StringPtr)(codeFolder.volName);
  428.     theParms.ioVRefNum = vRefNumForApp;
  429.     theParms.ioWDIndex = 0;
  430.     theParms.ioWDProcID = 0;
  431.     theParms.ioWDVRefNum = 0;
  432.     if (PBGetWDInfo(&theParms,false))
  433.         return(0L);
  434.     len = codeFolder.volName[0];
  435.     codeFolder.volName[len + 1] = ':';
  436.     codeFolder.volName[0] = len + 1;
  437.     codeFolder.vRefNum = theParms.ioWDVRefNum;
  438.     theDirID = theParms.ioWDDirID;
  439.     
  440.     myCPB.ioNamePtr = (StringPtr)fName;
  441.     myCPB.ioVRefNum = vRefNumForApp;
  442.     do
  443.         {
  444.         myCPB.ioFDirIndex = index;
  445.         myCPB.ioDirID = theDirID;
  446.         if ((err = PBGetCatInfo((CInfoPBPtr)&myCPB, FALSE)) == noErr)
  447.             {
  448.             if (((myCPB.ioFlAttrib>>4) & 0x01) == 1) /* a folder */
  449.                 {
  450.                 if (PasEqualStrs(fName, codeFolderName))
  451.                     return(myCPB.ioDirID);
  452.                 }
  453.             }
  454.         ++index;
  455.         } while (err == noErr);
  456.     return(0L);
  457.     }
  458.  
  459. /* Search thru folder for code resources and add all codes to the menu. */
  460. void AddCodesToMenu(long theDirID, short menuID)
  461.     {
  462.     HFileParam     fp;
  463.     MenuHandle     theMenu;
  464.     char        fName[32];
  465.     short            index = 1;
  466.     OSErr        err;
  467.     Boolean        firstAdd = TRUE;
  468.     
  469.     theMenu = GetMHandle(menuID);
  470.     codeFolder.theDirID = theDirID;
  471.     fp.ioNamePtr = (StringPtr)fName;;
  472.     fp.ioVRefNum = codeFolder.vRefNum;
  473.     fp.ioFVersNum = 0;
  474.     do
  475.         {
  476.         fp.ioFDirIndex = index;
  477.         fp.ioDirID = codeFolder.theDirID;
  478.         if ((err = PBHGetFInfoSync((HParmBlkPtr)&fp)) == noErr)
  479.             {
  480.             if (fp.ioFlFndrInfo.fdType == 'rsrc'
  481.                 && fp.ioFlFndrInfo.fdCreator == 'RSED')
  482.                 {
  483.                 if (firstAdd)
  484.                     {
  485.                     AppendMenu(theMenu, (StringPtr)"\P-");
  486.                     firstAdd = FALSE;
  487.                     }
  488.                 AppendMenu(theMenu, (StringPtr)fName);
  489.                 }
  490.             }
  491.         ++index;
  492.         } while (err == noErr);
  493.     /****** Open a working directory for the CODE folder: this is currently required
  494.     for hAWK, but will be eliminated for hAWK's next version. */
  495.     if (!firstAdd)
  496.         OpenWDForCODE();
  497.     }
  498.  
  499. /****** Open a working directory for the CODE folder: this is currently required
  500. for hAWK, but will be eliminated for hAWK's next version. */
  501. void OpenWDForCODE()
  502.     {
  503.     WDPBRec        theParms;
  504.     
  505.     theParms.ioCompletion = NULL;
  506.     theParms.ioVRefNum = codeFolder.vRefNum;
  507.     theParms.ioNamePtr = NULL;
  508.     theParms.ioWDDirID = codeFolder.theDirID;
  509.     theParms.ioWDProcID = 'ERIK';
  510.     if (!PBOpenWD(&theParms, FALSE)) /* IM IV pg 158 */
  511.         codeFolder.vRefNum = theParms.ioVRefNum;
  512.     }
  513.  
  514. /* The main event. Given a pick from your menu, load and call
  515. the resource. Display results if asked to by the resource.
  516. */
  517. void CallResource(short menuID, short item
  518. #if VERSION2 == TRUE
  519.     , Boolean concurrent
  520. #endif
  521.     )
  522.     {
  523.     MenuHandle     theMenu;
  524.     Handle        rHdle;
  525.     CallCode    codeCaller;
  526.     char        codeNameString[64];
  527.     long        eof;
  528.     short            saveVol, refNum;
  529.     
  530.     theMenu = GetMHandle(menuID);
  531.     GetItem(theMenu, item, (StringPtr)codeNameString);
  532.     if (31 < (unsigned char)(codeNameString[0]))
  533.         {
  534.         /* An odd error: if the code resource was added to the menu during
  535.         startup, how can it have a name that is longer than a file name?
  536.         The assumption here is that we are calling something that is not a
  537.         code resource - either the wrong menu or an inappropriate item.
  538.         Uncomment the following alert if you want to double-check this.
  539.         
  540.         OKStopAlert("Code resource name is too long.");
  541.         */
  542.         return;
  543.         }
  544.     gacc.thisCodeName = NULL;
  545.     if (GetVol(NULL, &saveVol))
  546.         saveVol = 0;
  547.     SetVol(NULL, codeFolder.vRefNum);
  548.     /* See if there's enough memory to load the code resource
  549.     - the brute force approach, if anything more strict than necessary. */
  550.     if (HOpenRF(codeFolder.vRefNum, codeFolder.theDirID,
  551.                 (StringPtr)codeNameString, fsRdPerm, &refNum))
  552.         {
  553.         if (saveVol)
  554.             SetVol(NULL, saveVol);
  555.         OKStopAlert("Couldn't open the code resource.");
  556.         return;
  557.         }
  558.     if (GetEOF(refNum, &eof))
  559.         {
  560.         FSClose(refNum);
  561.         if (saveVol)
  562.             SetVol(NULL, saveVol);
  563.         OKStopAlert("Code resource seems damaged or empty - giving up.");
  564.         return;
  565.         }
  566.     FSClose(refNum);
  567.     rHdle = NewHandle(eof + eof/10); /* a guess */
  568.     if (MemError() != noErr)
  569.         {
  570.         if (saveVol)
  571.             SetVol(NULL, saveVol);
  572.         OKStopAlert("Not enough memory to proceed, sorry.");
  573.         return;
  574.         }
  575.     DisposHandle(rHdle);
  576.     rHdle = NULL;
  577.     if ((refNum = HOpenResFile(codeFolder.vRefNum, codeFolder.theDirID,
  578.                 (StringPtr)codeNameString, fsRdPerm)) != -1 && refNum
  579.                 && ResError() == noErr)
  580.         {
  581.         /* load CODE 0, set up extensions etc, lock down and call
  582.         -after, show results */
  583.         rHdle = Get1Resource ('CODE', 0);
  584.         if (!rHdle)
  585.             {
  586.             /* In error thinking it was a code resource - this really isn't fair */
  587.             CloseResFile(refNum);
  588.             if (saveVol)
  589.                 SetVol(NULL, saveVol);
  590.             OKStopAlert("CODE 0 from that resource file seems to be missing.");
  591.             return;
  592.             }
  593. #if VERSION2 == TRUE
  594.         gHawkIsRunning = TRUE;
  595.         XableDrag_ons(menuID, FALSE);
  596.         HiliteMenu(0);
  597. #endif
  598.         HLock(rHdle);
  599.         codeCaller = (CallCode)*rHdle;
  600.         SetUpCodeCommunication(codeNameString);
  601. #if VERSION2 == TRUE
  602.         if (!concurrent)
  603.             gacc.DoEventLoopOnce_Ext = NULL;
  604. #endif
  605.         codeCaller(&gacc);
  606.             
  607.         HUnlock(rHdle);
  608.         ReleaseResource(rHdle);
  609.         CloseResFile (refNum);
  610. #if VERSION2 == TRUE
  611.         XableDrag_ons(menuID, TRUE);
  612. #endif
  613.         }
  614.     else
  615.         OKStopAlert("Could not open the resource fork for that code resource.");
  616.     if (saveVol)
  617.         SetVol(NULL, saveVol);
  618.     /* Show any file requested */
  619.     /* results:
  620.     <= -3 : no action at present
  621.     -2 : show stderr
  622.     -1 : user cancelled or error during dialog - no run
  623.     0  : run OK, do nothing special after
  624.     1  : show stdout
  625.     2  : show and select stdout
  626.     > 2 : no action at present (counts as equivalent to 0)
  627.     */
  628.  
  629. #if VERSION2 == TRUE
  630.     if (!(gHawkIsRunning && gInBackground))
  631.         {
  632.         AnnounceCompletion();
  633.         }
  634.     if (gHawkIsRunning && gInBackground)
  635.         DoNotify();
  636.     else
  637. #endif
  638.  
  639.     if (gacc.result == -2)
  640.         ShowResult(gacc.stdErrFileName);
  641.     else if (gacc.result == 1 || gacc.result == 2)
  642.         {
  643.         if (ShowResult(gacc.stdOutFileName) && gacc.result == 2)
  644.             SelectResult();
  645.         }
  646.  
  647. #if VERSION2 == TRUE
  648.     gHawkIsRunning = FALSE;
  649. #endif
  650.  
  651.     /* A small cleanup after */
  652.     if (gacc.thisCodeName)
  653.         DisposPtr(gacc.thisCodeName);
  654.     }
  655.  
  656. #if VERSION2 == TRUE
  657. void XableDrag_ons(short menuID, Boolean enable)
  658.     {
  659.     MenuHandle menu = GetMHandle(menuID);
  660.     char    mText[64];
  661.     short        i;
  662.     
  663.     if (!menu) return;
  664.     i = CountMItems(menu);
  665.     GetItem(menu, i, (StringPtr)mText);
  666.     while (i >= 1 && mText[1] != '-')
  667.         {
  668.         if (enable)
  669.             EnableItem(menu, i);
  670.         else
  671.             DisableItem(menu, i);
  672.         --i;
  673.         GetItem(menu, i, (StringPtr)mText);
  674.         }
  675.     }
  676.  
  677. /* Post a notify. Beep, small icon, diamond beside calling app's name.
  678. NOTE when the calling app resumes, it should remove this notify and
  679. show results, with
  680.         if (gNotifying)
  681.             {
  682.             NMRemove(&gNotifyRec);
  683.             if (gNotifyRec.nmIcon)
  684.                 ReleaseResource(gNotifyRec.nmIcon);
  685.             gNotifying = FALSE;
  686.             ShowResultsAfterNotify();
  687.             }
  688. */
  689. void DoNotify()
  690.     {
  691.     OSErr nmError;
  692.     
  693.     if (!gInBackground) return;
  694.     gNotifyRec.qType = nmType;
  695.     gNotifyRec.nmMark = 1;
  696.     gNotifyRec.nmIcon = GetResource('SICN', 128); /* or NULL */
  697.     HNoPurge(gNotifyRec.nmIcon);
  698.     gNotifyRec.nmSound = (Handle)-1L;
  699.     gNotifyRec.nmStr = 0L;
  700.     gNotifyRec.nmResp = (NMProcPtr)0L;
  701.     gNotifyRec.nmRefCon = 0L;
  702.     
  703.     nmError = NMInstall(&gNotifyRec);
  704.     gNotifying = TRUE;
  705.     }
  706.  
  707. void ShowResultsAfterNotify()
  708.     {
  709.     ;/* not implemented for Minimal App */
  710.     }
  711.  
  712. /* Beep when Drag_on is done, dealy a bit to allow user to
  713. react, flush events. */
  714. void AnnounceComult(vrefnum, filename));
  715. #else
  716.     return(FALSE);
  717. #endif
  718.  
  719. /* For reference, the entire "MyAppShowResult" function for EnterAct is
  720.     if (wdPtr = IsFileOpen(fileName, vRefNum))
  721.         DoForcedCloseWindow(wdPtr);
  722.     return(DoOpenFile(text, FALSE, vRefNum, fileName));
  723. */
  724.  
  725.     }
  726.  
  727. /* This function should select all of the text in the front window, after
  728. checking that the front window is indeed a text window. */
  729. void SelectResult()
  730.     {
  731. #if SUPPORT_LEVEL >= RESULTSONLY
  732.     extern void MyAppSelectResult(void);
  733.     
  734.     MyAppSelectResult();
  735. #endif
  736.     }
  737.  
  738. /* Pascal strings */
  739.  
  740. /* Copy one pascal string to another */
  741. void CopyPStr(Byte *srcStr, Byte *dstStr)
  742.     {
  743.     long   srcLen = srcStr[0];
  744.  
  745.     BlockMove(srcStr, dstStr, srcLen + 1);
  746.     }
  747.  
  748. /* Append pascal s2 to pascal s1, avoiding overflow. */
  749. void AppendPStr(Byte *s1, Byte *s2)
  750.     {
  751.     short    s1Len = s1[0];
  752.     short    s2Len = s2[0];
  753.  
  754.     if (s1Len + s2Len > 255)
  755.         s2Len = 255 - s1Len;
  756.  
  757.     if (s2Len)
  758.         {
  759.         BlockMove (s2 + 1, s1 + s1Len + 1, s2Len);
  760.         s1Len += s2Len;
  761.         s1[0] = s1Len;
  762.         }
  763.     }
  764.  
  765. Boolean PasEqualStrs(char *aStr, char *bStr)
  766.     {
  767.     short i, lena = aStr[0], lenb = bStr[0];
  768.     
  769.     if (!lena || !lenb || lena != lenb)return(FALSE);
  770.     for (i = 1; i <= lena; ++i)
  771.         {
  772.         if (aStr[i] != bStr[i])
  773.             return(FALSE);
  774.         }
  775.     return(TRUE);
  776.     }
  777.  
  778. /* Files, names and locations */
  779.  
  780. /* NOTE the following two functions are based on examples supplied
  781. by Apple on one of their DTS disks - error checking has been added,
  782. and these versions are independent of the signed vs unsigned char
  783. controversy surrounding str255. Byte is defined in MacTypes.h
  784. for THINK C v4. */
  785.  
  786. /* Warning, these calls can fail! And why not? Everything else can... */
  787. /* Bug, these two are not for use by unix imitations such as A/UX. */
  788.  
  789. /* Construct "\PDisk:folder1:folder2:...folderN:" where folderN
  790. contains the file of interest. */
  791. Byte *FullPathNameFromDirectory(long DirID, short vRefNum, Byte *s)
  792.     {
  793.     CInfoPBRec    pb;
  794.     Byte        directoryName[256];
  795.  
  796.     s[0] = 0;
  797.     pb.dirInfo.ioNamePtr = (StringPtr)directoryName;
  798.     pb.dirInfo.ioDrParID = DirID;
  799.  
  800.     do 
  801.         {
  802.         pb.dirInfo.ioVRefNum = vRefNum;
  803.         pb.dirInfo.ioFDirIndex = -1;
  804.         pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
  805.         if (PBGetCatInfo(&pb, FALSE))
  806.             {
  807.             break;
  808.             }
  809.         /* Append a colon  */
  810.         AppendPStr(directoryName, (Byte *)"\p:");
  811.         AppendPStr(directoryName, s);
  812.         CopyPStr(directoryName, s);
  813.         } while (pb.dirInfo.ioDrDirID != 2);
  814.     return(s);
  815.     }
  816.  
  817.  
  818. Byte *FullPathNameFromVRefNum(short vRefNum, Byte *s)
  819.     {
  820.  
  821.     WDPBRec    pb;
  822.  
  823.     pb.ioNamePtr = NULL;
  824.     pb.ioVRefNum = vRefNum;
  825.     pb.ioWDIndex = 0;
  826.     pb.ioWDProcID = 0;
  827.  
  828.     if (PBGetWDInfo(&pb,false))
  829.         {
  830.         s[0] = 0;
  831.         return(s);
  832.         }
  833.     return(FullPathNameFromDirectory(pb.ioWDDirID,pb.ioWDVRefNum,s));
  834.     }
  835.  
  836. /* Determine working directory for file based on full path name. */
  837. short    OpenWorkingDirectoryFromFullName(char *name, short len)
  838.     {
  839.     WDPBRec        theParms;
  840.     OSErr     IOResult;
  841.     char volname[256];
  842.     
  843.     volname[0] = len;
  844.     BlockMove(name, volname+1, len);
  845.     
  846.     theParms.ioCompletion = NULL;
  847.     theParms.ioVRefNum = 0;
  848.     theParms.ioNamePtr = (StringPtr)volname;
  849.     theParms.ioWDDirID = 0;
  850.     theParms.ioWDProcID = 'ERIK';
  851.     if (IOResult = PBOpenWD(&theParms, FALSE)) /* IM IV pg 158 */
  852.         {
  853.         OKStopAlert("Disk may not be on-line, \
  854. or file may have been moved, deleted, or renamed.");
  855.         theParms.ioVRefNum = 0;
  856.         }
  857.     return(theParms.ioVRefNum);
  858.     }
  859.  
  860.  
  861. /* The extension functions.*/
  862.  
  863. /* InDictionary() returns the general C type
  864. of a word, according to the following table:
  865. value    C type
  866. 0        none - keyword, comment word, local variable, operator etc
  867. 1        #define or macro name    eg    #define TAB '\t'
  868. 2        variable name with more than function scope
  869. 4        function or method name
  870. 8        enum constant
  871. 16        typedef name
  872. 32        struct tag
  873. 64        union tag
  874. 128        enum tag
  875.  
  876. See hAWK cross-referencing programs for an example of usage. It's unlikely
  877. that you will be able to provide an equivalent for this function, and
  878. there’s no great loss if you don’t.
  879.  */
  880. short InDictionary(char *tokenName)
  881.     {
  882. #if SUPPORT_LEVEL == CUSTOMSUPPORT
  883.     extern short InMyAppDictionary(char *s);
  884.     
  885.     return(InMyAppDictionary(tokenName));
  886. #else
  887.     return(0);
  888. #endif
  889.     }
  890.  
  891. /* This extension function should copy all or the selected part of the
  892. text in the frontmost window to a Handle. If the front window is not a
  893. text window, you should try the second-front window - if it isn't text
  894. either, return NULL. Front OR second front, because a dialog window may be
  895. the front window at the time - this is the case with hAWK, for example.
  896. */
  897. Handle GetFrontText(Boolean getItAll)
  898.     {
  899. #if SUPPORT_LEVEL >= BASICTEXT
  900.     extern Handle MyAppGetFrontText(Boolean getItAll);
  901.     
  902.     return(MyAppGetFrontText(getItAll));
  903. #else
  904.     return(NULL);
  905. #endif
  906.     }
  907.  
  908. /* This function should retrieve file names and vRefNums from a one
  909. or two-dimensional list of files. If *panePtr == -1, you are being asked
  910. for the first file, otherwise the next file. When there are no more files,
  911. set *indexPtr = -1. If you have a two-dimensional list, think of panePtr as
  912. the column index and indexPtr as the row index. For a one-dimensional list,
  913. use indexPtr to keep track of where you are - just remember to set panePtr to
  914. something != -1 during the first call. Other than setting panePtr to something
  915. besides -1 during the first call, and setting indexPtr to -1 when there are
  916. no more files, you can use them for tracking which file comes next in any way
  917. you want.
  918. If you perhaps use full path names, see OpenWorkingDirectoryFromFullName()
  919. above for hints on how to convert to filename/vRefNum.
  920. clearFlag TRUE means clear the file from your list; FALSE means leave it in the
  921. list. Normally FALSE is best - someone might want to reuse the list quite soon,
  922. as in running two hAWK programs on the same list of files.
  923. */
  924. void GetNextMultiFile(short *panePtr, short *indexPtr, 
  925.             short *vRefNumPtr, char *fileName, Boolean clearFlag)
  926.     {
  927. #if SUPPORT_LEVEL == CUSTOMSUPPORT
  928.     extern void GetNextFileToSearch(short *panePtr, short *indexPtr, 
  929.         short *vRefNumPtr, char *fileName, Boolean clearFlag);
  930.     
  931.     GetNextFileToSearch(panePtr, indexPtr, 
  932.                 vRefNumPtr, fileName, clearFlag);
  933. #else
  934.     *indexPtr = -1;
  935. #endif
  936.     }
  937.  
  938. /* If you have an alert mechanism with just an OK button that accepts C
  939. strings, insert it here. Return of 1 means alert was shown and user
  940. clicked OK, return of 0 means the alert was not shown. If this happens,
  941. it would be quite OK for your alert function to try to get more memory or
  942. show an out-of memory alert before returning the 0 or 1. */
  943. short OKStopAlert(Ptr cstringPtr)
  944.     {
  945. #if SUPPORT_LEVEL == CUSTOMSUPPORT
  946. /* Left in from EnterAct as an example - EnterAct uses FlexAlert(),
  947. essentially as published in MacTutor Jan '91. As a small refinement, it
  948. will display an out-of-memory alert if it runs into trouble. FlexAlert
  949. sizes the alert box to fit the text, and also formats the text nicely. */
  950.     extern short FlexAlert(short buttonMode, short whichIcon, Ptr csPtr);
  951. #define JUSTOK    0
  952. #define STOPICON    0
  953.     return(FlexAlert(JUSTOK, STOPICON, cstringPtr));
  954. #else
  955.     return(0);
  956. #endif
  957.     }
  958.  
  959. /* Advise the user that memory has run out during code resource execution.
  960. You must have something for this kicking around in your application - a
  961. text message is much more likeable than a beep if things fo worng. */
  962. void MemoryAlert()
  963.     {
  964. #if SUPPORT_LEVEL == CUSTOMSUPPORT
  965.     extern void DoMemoryAlert(short msgNum, long memLimit);
  966.  
  967. #define NONELEFT        4
  968.  
  969.     DoMemoryAlert(NONELEFT, 0L);
  970. #else
  971.     SysBeep(2);
  972. #endif
  973.     }
  974.  
  975. short GetScreenHeight(void)
  976.     {
  977.     return(screenBits.bounds.bottom - screenBits.bounds.top);
  978.     }
  979.  
  980. short GetScreenWidth()
  981.     {
  982.     return(screenBits.bounds.right - screenBits.bounds.left);
  983.     }
  984.  
  985. void ShowWatchCursor()
  986.     {
  987. #if SUPPORT_LEVEL == CUSTOMSUPPORT
  988.     extern void MySetWatchCursor(void);
  989.     
  990.     MySetWatchCursor();
  991. #endif
  992.  
  993.     /* this goes something like:
  994.     CursHandle        Watch; -- ToolboxUtil.h  enum constant --
  995.     Watch = GetCursor(watchCursor);
  996.     if (Watch)
  997.         SetCursor (*Watch);
  998.     else
  999.         InitCursor();
  1000.     */
  1001.     }
  1002.  
  1003. void DoEventLoopOnce()
  1004.     {
  1005. #if VERSION2 == TRUE
  1006.     extern void HandleOneEvent(void);
  1007.     
  1008.     HandleOneEvent();
  1009. #endif
  1010.     }
  1011.  
  1012. Handle GetAppClip()
  1013.     {
  1014. /******** hack, the innards of this function are normally within the
  1015. wrapper "#if SUPPORT_LEVEL == CUSTOMSUPPORT"..."#endif".
  1016. Further hack, since Minimal App7 does not have its own clip,
  1017. it "borrows" the system's desk scrap. */
  1018. #if VERSION2 == TRUE
  1019.     PScrapStuff     scrapStuff;
  1020.     long            offset, sizeH;
  1021.     static Handle    scrapH;
  1022.     static short    scrapCount;
  1023.     
  1024.     scrapStuff = InfoScrap();
  1025.     if (scrapStuff->scrapCount != scrapCount)
  1026.         {
  1027.         scrapCount = scrapStuff->scrapCount;
  1028.         if ((sizeH = GetScrap(NULL, 'TEXT', &offset)) > 0)
  1029.             {
  1030.             if (!scrapH)
  1031.                 scrapH = NewHandle(sizeH);
  1032.             else
  1033.                 SetHandleSize(scrapH, sizeH);
  1034.             if (MemError() != noErr)
  1035.                 {
  1036.                 if (scrapH)
  1037.                     {
  1038.                     DisposHandle(scrapH);
  1039.                     scrapH = NULL;
  1040.                     }
  1041.                 return NULL;
  1042.                 }
  1043.             SetHandleSize(scrapH, 0);
  1044.             GetScrap(scrapH, 'TEXT', &offset);
  1045.             }
  1046.         }
  1047.     return scrapH; /* Note it may be NULL or length 0. */
  1048. #endif
  1049.     return(NULL);
  1050.     }
  1051.  
  1052. short PutAppClip(char *newClipStr)
  1053.     {
  1054.     // If you want, look at the above function and persuade minimalapp
  1055.     // to write to the system's desk scrap. Yer author refuses to take
  1056.     // responsibility for the consequences....
  1057.     // return 0 for failure, 1 for success.
  1058.     return 0;
  1059.     }
  1060.