osacomponent.c

Go to the documentation of this file.
00001 
00002 /*  $Id: osacomponent.c 1294 2006-04-18 17:51:46Z andreradke $    */
00003 
00004 /******************************************************************************
00005 
00006     UserLand Frontier(tm) -- High performance Web content management,
00007     object database, system-level and Internet scripting environment,
00008     including source code editing and debugging.
00009 
00010     Copyright (C) 1992-2004 UserLand Software, Inc.
00011 
00012     This program is free software; you can redistribute it and/or modify
00013     it under the terms of the GNU General Public License as published by
00014     the Free Software Foundation; either version 2 of the License, or
00015     (at your option) any later version.
00016 
00017     This program is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020     GNU General Public License for more details.
00021 
00022     You should have received a copy of the GNU General Public License
00023     along with this program; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 
00026 ******************************************************************************/
00027 
00028 #include "frontier.h"
00029 #include "standard.h"
00030 
00031 /*This file has been forked to protect the shipping version. The Carbon version is at the top, the "good"
00032 version at the bottom. This file should be reconciled later.*/
00033 /*
00034     2004-10-27 aradke: Reconciled the Carbon and Classic versions.
00035 */
00036 
00037 //#pragma options (pack_enums) /* 2002-10-13 AR: pragma not supported by CodeWarrior */
00038 
00039 #include <land.h>
00040 #include <UserTalk.h>
00041 #include "dialogs.h"
00042 #include "error.h"
00043 #include "file.h"
00044 #include "font.h"
00045 #include "kb.h"
00046 #include "launch.h"
00047 #include "memory.h"
00048 #include "ops.h"
00049 #include "resources.h"
00050 #include "strings.h"
00051 #include "timedate.h"
00052 #include "lang.h"
00053 #include "langexternal.h"
00054 #include "langinternal.h"
00055 #include "langipc.h"
00056 #include "langsystem7.h"
00057 #include "shellhooks.h"
00058 #include "scripts.h"
00059 #include "process.h"
00060 #include "processinternal.h"
00061 #include "tablestructure.h"
00062 #include "osacomponent.h"
00063 #include "osadroplet.h"
00064 #include "osainternal.h"
00065 #include "osamenus.h"
00066 #include "osaparseaete.h"
00067 #include "osawindows.h"
00068 #include <SetUpA5.h>
00069 #include "byteorder.h"
00070 
00071 #if TARGET_API_MAC_CARBON == 1 /*PBS 03/14/02: AE OS X fix.*/
00072     #include "aeutils.h"
00073 #endif
00074 
00075 
00076 /* forward declarations for static functions */
00077 
00078 static boolean osabackgroundtask (boolean);
00079 
00080 static boolean osadebugger (hdltreenode);
00081 
00082 static boolean osapartialeventloop (short);
00083 
00084 static pascal OSErr osaclientactive (long refcon);
00085 
00086 static pascal OSErr osaclientsend (const AppleEvent *, AppleEvent *, AESendMode, AESendPriority, long, AEIdleUPP, AEFilterUPP, long);
00087 
00088 static pascal Boolean osaclientidleproc (EventRecord *ev, long *sleep, RgnHandle *mousergn);
00089 
00090 static pascal ComponentResult cmpclose (Handle storage, ComponentInstance self);
00091 
00092 static pascal OSAError osaLoad (hdlcomponentglobals hglobals,
00093             const AEDesc*       scriptData,
00094             long                modeFlags,
00095             OSAID*              resultingCompiledScriptID);
00096 
00097 static pascal ComponentResult cmpcando (short selector);
00098 
00099 static pascal ComponentResult cmpversion (void);
00100 
00101 static pascal OSAError osaStore (
00102             hdlcomponentglobals hglobals,
00103             OSAID               compiledScriptID, 
00104             DescType            desiredType,
00105             long                modeFlags,
00106             AEDesc*             resultingScriptData);
00107 
00108 static pascal OSAError osaExecute (
00109             hdlcomponentglobals hglobals,
00110             OSAID               compiledScriptID,
00111             OSAID               contextID,
00112             long                modeFlags,
00113             OSAID*              resultingScriptValueID);
00114 
00115 static pascal OSAError osaSetScriptInfo (
00116             hdlcomponentglobals hglobals,
00117             OSAID               scriptID,
00118             OSType              selector,
00119             long                value);
00120             
00121 static pascal OSAError osaGetScriptInfo (
00122             hdlcomponentglobals hglobals,
00123             OSAID               scriptID,
00124             OSType              selector,
00125             long*               result);
00126             
00127 static pascal OSAError osaCompile (
00128             hdlcomponentglobals hglobals,
00129             const AEDesc*       sourceData,
00130             long                modeFlags,
00131             OSAID*              scriptID);
00132             
00133 static pascal OSAError osaGetSource (
00134             hdlcomponentglobals hglobals,
00135             OSAID               scriptID,
00136             DescType            desiredType,
00137             AEDesc*             resultingSourceData);
00138             
00139 static pascal OSAError osaCoerceFromDesc (
00140             hdlcomponentglobals hglobals,
00141             const AEDesc*       scriptData,
00142             long                modeFlags,
00143             OSAID*              resultingScriptID);
00144 
00145 
00146 static pascal OSAError osaCoerceToDesc (
00147             hdlcomponentglobals hglobals,
00148             OSAID               scriptID,
00149             DescType            desiredType,
00150             long                modeFlags,
00151             AEDesc*             result);
00152             
00153 static pascal OSAError osaStartRecording (
00154             hdlcomponentglobals hglobals,
00155             OSAID               *compiledScriptToModifyID);
00156 
00157 static pascal OSAError osaStopRecording (
00158             hdlcomponentglobals hglobals,
00159             OSAID               compiledScriptID);
00160 
00161 static pascal OSAError osaScriptingComponentName (
00162             hdlcomponentglobals hglobals,
00163             AEDesc*             resultingScriptingComponentName);
00164 
00165 static pascal OSAError osaLoadExecute (
00166             hdlcomponentglobals hglobals,
00167             const AEDesc*       scriptData,
00168             OSAID               contextID,
00169             long                modeFlags,
00170             OSAID*              resultingScriptValueID);
00171 static pascal OSAError osaMakeContext (
00172             hdlcomponentglobals hglobals,
00173             const AEDesc*       contextName,
00174             OSAID               parentContext,
00175             OSAID*              resultingContextID);
00176             
00177 static pascal OSAError osaDisplay (
00178             hdlcomponentglobals hglobals,
00179             OSAID               scriptValueID,
00180             DescType            desiredType,
00181             long                modeFlags,
00182             AEDesc*             resultingText);
00183     
00184 
00185 static pascal OSAError osaSetResumeDispatchProc (
00186                 hdlcomponentglobals hglobals,
00187                 AEEventHandlerUPP   resumeDispatchProc,
00188                 long                refCon);
00189                 
00190 static pascal OSAError osaGetResumeDispatchProc (
00191                 hdlcomponentglobals hglobals,
00192                 AEEventHandlerUPP*  resumeDispatchProc,
00193                 long*               refCon);
00194                 
00195 static pascal OSAError osaExecuteEvent (
00196                 hdlcomponentglobals hglobals,
00197                 AppleEvent*         event,
00198                 OSAID               contextID,
00199                 long                modeFlags,
00200                 OSAID*              resultingScriptValueID);
00201 
00202 static pascal OSAError osaDoEvent (
00203             hdlcomponentglobals hglobals,
00204             AppleEvent*         event,
00205             OSAID               contextID,
00206             long                modeFlags,
00207             AppleEvent*         reply);
00208             
00209 static pascal OSAError osaSetDebugProc (
00210             hdlcomponentglobals hglobals,
00211             OSADebugUPP         debugProc,
00212             long                refCon);
00213             
00214 static pascal OSAError osaDebug (
00215             hdlcomponentglobals hglobals,
00216             OSType              selector,
00217             const AEDesc*       selectorData,
00218             DescType            desiredType,
00219             AEDesc*             resultingDebugInfoOrDataToSet);
00220 
00221 static pascal OSAError osaGetSendProc (
00222             hdlcomponentglobals hglobals,
00223             OSASendUPP*         sendProc,
00224             long*               refCon);
00225 
00226 static pascal OSAError osaSetCreateProc (
00227             hdlcomponentglobals         hglobals,
00228             OSACreateAppleEventUPP      createProc,
00229             long                        refCon);
00230             
00231 static pascal OSAError osaGetCreateProc (
00232             hdlcomponentglobals         hglobals,
00233             OSACreateAppleEventUPP*     createProc,
00234             long*                       refCon);
00235             
00236 
00237 #if TARGET_RT_MAC_CFM || TARGET_RT_MAC_MACHO
00238 
00239     /* proc infos for building routine descriptors and universal procedure pointers */
00240     
00241     enum {
00242             cmpcloseProcInfo = kPascalStackBased
00243                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00244                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(Handle)))
00245                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(ComponentInstance)))
00246         };
00247         
00248         enum {
00249             cmpcandoProcInfo = kPascalStackBased
00250                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00251                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(short)))
00252         };
00253         
00254         enum {
00255             cmpversionProcInfo = kPascalStackBased
00256                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00257         };
00258         
00259         enum {
00260             osaLoadProcInfo = kPascalStackBased
00261                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00262                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00263                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(AEDesc *)))
00264                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(long)))
00265                  | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(OSAID *)))
00266         };
00267         
00268         enum {
00269             osaStoreProcInfo = kPascalStackBased
00270                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00271                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00272                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(OSAID)))
00273                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(DescType)))
00274                  | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(long)))
00275                  | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof(AEDesc *)))
00276         };
00277         
00278         enum {
00279             osaExecuteProcInfo = kPascalStackBased
00280                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00281                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00282                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(OSAID)))
00283                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(OSAID)))
00284                  | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(long)))
00285                  | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof(OSAID*)))
00286         };
00287         
00288         enum {
00289             osaDisplayProcInfo = kPascalStackBased
00290                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00291                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00292                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(OSAID)))
00293                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(DescType)))
00294                  | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(long)))
00295                  | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof(AEDesc *)))
00296         };
00297         
00298         enum {
00299             osaScriptErrorProcInfo = kPascalStackBased
00300                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00301                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00302                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(OSType)))
00303                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(DescType)))
00304                  | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(AEDesc *)))
00305         };
00306         
00307         enum {
00308             osaDisposeProcInfo = kPascalStackBased
00309                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00310                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00311                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(OSAID)))
00312         };
00313         
00314         enum {
00315             osaSetScriptInfoProcInfo = kPascalStackBased
00316                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00317                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00318                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(OSAID)))
00319                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(OSType)))
00320                  | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(long)))
00321         };
00322         
00323         enum {
00324             osaGetScriptInfoProcInfo = kPascalStackBased
00325                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00326                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00327                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(OSAID)))
00328                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(OSType)))
00329                  | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(long *)))
00330         };
00331         
00332         enum {
00333             osaCompileProcInfo = kPascalStackBased
00334                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00335                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00336                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(AEDesc *)))
00337                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(long)))
00338                  | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(OSAID*)))
00339         };
00340         
00341         enum {
00342             osaGetSourceProcInfo = kPascalStackBased
00343                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00344                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00345                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(OSAID)))
00346                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(DescType)))
00347                  | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(AEDesc *)))
00348         };
00349         
00350         enum {
00351             osaCoerceFromDescProcInfo = kPascalStackBased
00352                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00353                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00354                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(AEDesc*)))
00355                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(long)))
00356                  | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(OSAID*)))
00357         };
00358         
00359         enum {
00360             osaCoerceToDescProcInfo = kPascalStackBased
00361                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00362                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00363                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(OSAID)))
00364                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(DescType)))
00365                  | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(long)))
00366                  | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof(AEDesc*)))
00367         };
00368         
00369         enum {
00370             osaStartRecordingProcInfo = kPascalStackBased
00371                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00372                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00373                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(OSAID *)))
00374         };
00375         
00376         enum {
00377             osaStopRecordingProcInfo = kPascalStackBased
00378                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00379                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00380                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(OSAID)))
00381         };
00382         
00383         enum {
00384             osaScriptingComponentNameProcInfo = kPascalStackBased
00385                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00386                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00387                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(AEDesc *)))
00388         };
00389         
00390         enum {
00391             osaLoadExecuteProcInfo = kPascalStackBased
00392                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00393                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00394                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(AEDesc*)))
00395                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(OSAID)))
00396                  | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(long)))
00397                  | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof(OSAID*)))
00398         };
00399         
00400         enum {
00401             osaCompileExecuteProcInfo = kPascalStackBased
00402                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00403                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00404                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(AEDesc*)))
00405                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(OSAID)))
00406                  | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(long)))
00407                  | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof(OSAID*)))
00408         };
00409         
00410         enum {
00411             osaDoScriptProcInfo = kPascalStackBased
00412                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00413                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00414                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(AEDesc*)))
00415                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(OSAID)))
00416                  | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(DescType)))
00417                  | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof(long)))
00418                  | STACK_ROUTINE_PARAMETER(6, SIZE_CODE(sizeof(AEDesc*)))
00419         };
00420         
00421         enum {
00422             osaMakeContextProcInfo = kPascalStackBased
00423                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00424                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00425                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(AEDesc*)))
00426                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(OSAID)))
00427                  | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(OSAID*)))
00428         };
00429         
00430         enum {
00431             osaSetResumeDispatchProcProcInfo = kPascalStackBased
00432                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00433                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00434                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(AEEventHandlerUPP)))
00435                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(long)))
00436         };
00437         
00438         enum {
00439             osaGetResumeDispatchProcProcInfo = kPascalStackBased
00440                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00441                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00442                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(AEEventHandlerUPP*)))
00443                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(long*)))
00444         };
00445         
00446         enum {
00447             osaExecuteEventProcInfo = kPascalStackBased
00448                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00449                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00450                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(AppleEvent*)))
00451                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(OSAID)))
00452                  | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(long)))
00453                  | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof(OSAID*)))
00454         };
00455         
00456         enum {
00457             osaDoEventProcInfo = kPascalStackBased
00458                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00459                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00460                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(AppleEvent*)))
00461                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(OSAID)))
00462                  | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(long)))
00463                  | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof(AppleEvent*)))
00464         };
00465         
00466         enum {
00467             osaSetActiveProcProcInfo = kPascalStackBased
00468                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00469                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00470                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(OSAActiveUPP)))
00471                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(long)))
00472         };
00473         
00474         enum {
00475             osaSetDebugProcProcInfo = kPascalStackBased
00476                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00477                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00478                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(OSADebugUPP)))
00479                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(long)))
00480         };
00481         
00482         enum {
00483             osaDebugProcInfo = kPascalStackBased
00484                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00485                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00486                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(OSType)))
00487                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(AEDesc*)))
00488                  | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(DescType)))
00489                  | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof(AEDesc*)))
00490         };
00491         
00492         enum {
00493             osaSetSendProcProcInfo = kPascalStackBased
00494                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00495                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00496                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(OSASendUPP)))
00497                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(long)))
00498         };
00499         
00500         enum {
00501             osaGetSendProcProcInfo = kPascalStackBased
00502                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00503                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00504                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(OSASendUPP*)))
00505                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(long*)))
00506         };
00507         
00508         enum {
00509             osaSetCreateProcProcInfo = kPascalStackBased
00510                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00511                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00512                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(OSACreateAppleEventUPP)))
00513                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(long)))
00514         };
00515         enum {
00516             osaGetCreateProcProcInfo = kPascalStackBased
00517                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00518                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00519                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(OSACreateAppleEventUPP*)))
00520                  | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(long*)))
00521         };
00522         enum {
00523             osaSetDefaultTargetProcInfo = kPascalStackBased
00524                  | RESULT_SIZE(SIZE_CODE(sizeof(OSAError)))
00525                  | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(hdlcomponentglobals)))
00526                  | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(AEAddressDesc*)))
00527         };
00528 
00529     
00530     #if TARGET_API_MAC_CARBON
00531         
00532         /*
00533             For Carbon we have to build univeral procedure pointers at runtime.
00534             So we just declare the UPPs here.
00535         */
00536         
00537         ComponentRoutineUPP cmpcloseDesc;
00538         ComponentRoutineUPP cmpcandoDesc;
00539         ComponentRoutineUPP cmpversionDesc;
00540         ComponentRoutineUPP osaLoadDesc;
00541         ComponentRoutineUPP osaStoreDesc;
00542         ComponentRoutineUPP osaExecuteDesc;
00543         ComponentRoutineUPP osaDisplayDesc;
00544         ComponentRoutineUPP osaScriptErrorDesc;
00545         ComponentRoutineUPP osaDisposeDesc;
00546         ComponentRoutineUPP osaSetScriptInfoDesc;
00547         ComponentRoutineUPP osaGetScriptInfoDesc;
00548         ComponentRoutineUPP osaCompileDesc;
00549         ComponentRoutineUPP osaGetSourceDesc;
00550         ComponentRoutineUPP osaCoerceFromDescDesc;
00551         ComponentRoutineUPP osaCoerceToDescDesc;
00552         ComponentRoutineUPP osaStartRecordingDesc;
00553         ComponentRoutineUPP osaStopRecordingDesc;
00554         ComponentRoutineUPP osaScriptingComponentNameDesc;
00555         ComponentRoutineUPP osaLoadExecuteDesc;
00556         ComponentRoutineUPP osaCompileExecuteDesc;
00557         ComponentRoutineUPP osaDoScriptDesc;
00558         ComponentRoutineUPP osaMakeContextDesc;
00559         ComponentRoutineUPP osaSetResumeDispatchProcDesc;
00560         ComponentRoutineUPP osaGetResumeDispatchProcDesc;
00561         ComponentRoutineUPP osaExecuteEventDesc;
00562         ComponentRoutineUPP osaDoEventDesc;
00563         ComponentRoutineUPP osaSetActiveProcDesc;
00564         ComponentRoutineUPP osaSetDebugProcDesc;
00565         ComponentRoutineUPP osaDebugDesc;
00566         ComponentRoutineUPP osaSetSendProcDesc;
00567         ComponentRoutineUPP osaGetSendProcDesc;
00568         ComponentRoutineUPP osaSetCreateProcDesc;
00569         ComponentRoutineUPP osaGetCreateProcDesc;
00570     
00571         OSAActiveUPP    osaclientactiveDesc;
00572         AEIdleUPP   osaclientidleDesc;
00573         OSASendUPP  osaclientsendDesc;
00574         
00575         #define osaclientactiveUPP (osaclientactiveDesc)
00576         #define osaclientidleUPP (osaclientidleDesc)
00577         #define osaclientsendUPP (osaclientsendDesc)
00578 
00579     #else
00580         
00581         /*
00582             For the Classic Mac OS API, routine descriptors are built by the compiler.
00583             Just define the UPPs as pointers to these routine descriptors.
00584         */
00585     
00586         static RoutineDescriptor cmpcloseDesc = BUILD_ROUTINE_DESCRIPTOR (cmpcloseProcInfo, cmpclose);
00587         static RoutineDescriptor cmpcandoDesc = BUILD_ROUTINE_DESCRIPTOR (cmpcandoProcInfo, cmpcando);
00588         static RoutineDescriptor cmpversionDesc = BUILD_ROUTINE_DESCRIPTOR (cmpversionProcInfo, cmpversion);
00589         static RoutineDescriptor osaLoadDesc = BUILD_ROUTINE_DESCRIPTOR (osaLoadProcInfo, osaLoad);
00590         static RoutineDescriptor osaStoreDesc = BUILD_ROUTINE_DESCRIPTOR (osaStoreProcInfo, osaStore);
00591         static RoutineDescriptor osaExecuteDesc = BUILD_ROUTINE_DESCRIPTOR (osaExecuteProcInfo, osaExecute);
00592         static RoutineDescriptor osaDisplayDesc = BUILD_ROUTINE_DESCRIPTOR (osaDisplayProcInfo, osaDisplay);
00593         static RoutineDescriptor osaScriptErrorDesc = BUILD_ROUTINE_DESCRIPTOR (osaScriptErrorProcInfo, osaScriptError);
00594         static RoutineDescriptor osaDisposeDesc = BUILD_ROUTINE_DESCRIPTOR (osaDisposeProcInfo, osaDispose);
00595         static RoutineDescriptor osaSetScriptInfoDesc = BUILD_ROUTINE_DESCRIPTOR (osaSetScriptInfoProcInfo, osaSetScriptInfo);
00596         static RoutineDescriptor osaGetScriptInfoDesc = BUILD_ROUTINE_DESCRIPTOR (osaGetScriptInfoProcInfo, osaGetScriptInfo);
00597         static RoutineDescriptor osaCompileDesc = BUILD_ROUTINE_DESCRIPTOR (osaCompileProcInfo, osaCompile);
00598         static RoutineDescriptor osaGetSourceDesc = BUILD_ROUTINE_DESCRIPTOR (osaGetSourceProcInfo, osaGetSource);
00599         static RoutineDescriptor osaCoerceFromDescDesc = BUILD_ROUTINE_DESCRIPTOR (osaCoerceFromDescProcInfo, osaCoerceFromDesc);
00600         static RoutineDescriptor osaCoerceToDescDesc = BUILD_ROUTINE_DESCRIPTOR (osaCoerceToDescProcInfo, osaCoerceToDesc);
00601         static RoutineDescriptor osaStartRecordingDesc = BUILD_ROUTINE_DESCRIPTOR (osaStartRecordingProcInfo, osaStartRecording);
00602         static RoutineDescriptor osaStopRecordingDesc = BUILD_ROUTINE_DESCRIPTOR (osaStopRecordingProcInfo, osaStopRecording);
00603         static RoutineDescriptor osaScriptingComponentNameDesc = BUILD_ROUTINE_DESCRIPTOR (osaScriptingComponentNameProcInfo, osaScriptingComponentName);
00604         static RoutineDescriptor osaLoadExecuteDesc = BUILD_ROUTINE_DESCRIPTOR (osaLoadExecuteProcInfo, osaLoadExecute);
00605         static RoutineDescriptor osaCompileExecuteDesc = BUILD_ROUTINE_DESCRIPTOR (osaCompileExecuteProcInfo, osaCompileExecute);
00606         static RoutineDescriptor osaDoScriptDesc = BUILD_ROUTINE_DESCRIPTOR (osaDoScriptProcInfo, osaDoScript);
00607         static RoutineDescriptor osaMakeContextDesc = BUILD_ROUTINE_DESCRIPTOR (osaMakeContextProcInfo, osaMakeContext);
00608         static RoutineDescriptor osaSetResumeDispatchProcDesc = BUILD_ROUTINE_DESCRIPTOR (osaSetResumeDispatchProcProcInfo, osaSetResumeDispatchProc);
00609         static RoutineDescriptor osaGetResumeDispatchProcDesc = BUILD_ROUTINE_DESCRIPTOR (osaGetResumeDispatchProcProcInfo, osaGetResumeDispatchProc);
00610         static RoutineDescriptor osaExecuteEventDesc = BUILD_ROUTINE_DESCRIPTOR (osaExecuteEventProcInfo, osaExecuteEvent);
00611         static RoutineDescriptor osaDoEventDesc = BUILD_ROUTINE_DESCRIPTOR (osaDoEventProcInfo, osaDoEvent);
00612         static RoutineDescriptor osaSetActiveProcDesc = BUILD_ROUTINE_DESCRIPTOR (osaSetActiveProcProcInfo, osaSetActiveProc);
00613         static RoutineDescriptor osaSetDebugProcDesc = BUILD_ROUTINE_DESCRIPTOR (osaSetDebugProcProcInfo, osaSetDebugProc);
00614         static RoutineDescriptor osaDebugDesc = BUILD_ROUTINE_DESCRIPTOR (osaDebugProcInfo, osaDebug);
00615         static RoutineDescriptor osaSetSendProcDesc = BUILD_ROUTINE_DESCRIPTOR (osaSetSendProcProcInfo, osaSetSendProc);
00616         static RoutineDescriptor osaGetSendProcDesc = BUILD_ROUTINE_DESCRIPTOR (osaGetSendProcProcInfo, osaGetSendProc);
00617         static RoutineDescriptor osaSetCreateProcDesc = BUILD_ROUTINE_DESCRIPTOR (osaSetCreateProcProcInfo, osaSetCreateProc);
00618         static RoutineDescriptor osaGetCreateProcDesc = BUILD_ROUTINE_DESCRIPTOR (osaGetCreateProcProcInfo, osaGetCreateProc);
00619     
00620         static RoutineDescriptor osaclientactiveDesc = BUILD_ROUTINE_DESCRIPTOR (uppOSAActiveProcInfo, osaclientactive);
00621         static RoutineDescriptor osaclientidleDesc = BUILD_ROUTINE_DESCRIPTOR (uppAEIdleProcInfo, osaclientidleproc);
00622         static RoutineDescriptor osaclientsendDesc = BUILD_ROUTINE_DESCRIPTOR (uppOSASendProcInfo, osaclientsend);
00623 
00624         #define cmpcloseUPP (&cmpcloseDesc)
00625         #define cmpcandoUPP (&cmpcandoDesc)
00626         #define cmpversionUPP (&cmpversionDesc)
00627         #define osaLoadUPP (&osaLoadDesc)
00628         #define osaStoreUPP (&osaStoreDesc)
00629         #define osaExecuteUPP (&osaExecuteDesc)
00630         #define osaDisplayUPP (&osaDisplayDesc)
00631         #define osaScriptErrorUPP (&osaScriptErrorDesc)
00632         #define osaDisposeUPP (&osaDisposeDesc)
00633         #define osaSetScriptInfoUPP (&osaSetScriptInfoDesc)
00634         #define osaGetScriptInfoUPP (&osaGetScriptInfoDesc)
00635         #define osaCompileUPP (&osaCompileDesc)
00636         #define osaGetSourceUPP (&osaGetSourceDesc)
00637         #define osaCoerceFromDescUPP (&osaCoerceFromDescDesc)
00638         #define osaCoerceToDescUPP (&osaCoerceToDescDesc)
00639         #define osaStartRecordingUPP (&osaStartRecordingDesc)
00640         #define osaStopRecordingUPP (&osaStopRecordingDesc)
00641         #define osaScriptingComponentNameUPP (&osaScriptingComponentNameDesc)
00642         #define osaLoadExecuteUPP (&osaLoadExecuteDesc)
00643         #define osaCompileExecuteUPP (&osaCompileExecuteDesc)
00644         #define osaDoScriptUPP (&osaDoScriptDesc)
00645         #define osaMakeContextUPP (&osaMakeContextDesc)
00646         #define osaSetResumeDispatchProcUPP (&osaSetResumeDispatchProcDesc)
00647         #define osaGetResumeDispatchProcUPP (&osaGetResumeDispatchProcDesc)
00648         #define osaExecuteEventUPP (&osaExecuteEventDesc)
00649         #define osaDoEventUPP (&osaDoEventDesc)
00650         #define osaSetActiveProcUPP (&osaSetActiveProcDesc)
00651         #define osaSetDebugProcUPP (&osaSetDebugProcDesc)
00652         #define osaDebugUPP (&osaDebugDesc)
00653         #define osaSetSendProcUPP (&osaSetSendProcDesc)
00654         #define osaGetSendProcUPP (&osaGetSendProcDesc)
00655         #define osaSetCreateProcUPP (&osaSetCreateProcDesc)
00656         #define osaGetCreateProcUPP (&osaGetCreateProcDesc)
00657         #define osaSetDefaultTargetUPP (&osaSetDefaultTargetDesc)
00658 
00659         #define osaclientactiveUPP (&osaclientactiveDesc)
00660         #define osaclientidleUPP (&osaclientidleDesc)
00661         #define osaclientsendUPP (&osaclientsendDesc)
00662 
00663     #endif  /*TARGET_API_MAC_CARBON*/
00664     
00665     /*
00666     static RoutineDescriptor osaSetDefaultTargetDesc = BUILD_ROUTINE_DESCRIPTOR (osaSetDefaultTargetProcInfo, osaSetDefaultTarget);
00667     */
00668 
00669 #else 
00670 
00671     /* For Mac 68k (non-CFM) just use function pointers */
00672     
00673     #define cmpcloseUPP ((ComponentFunctionUPP) cmpclose)
00674     #define cmpcandoUPP ((ComponentFunctionUPP) cmpcando)
00675     #define cmpversionUPP   ((ComponentFunctionUPP) cmpversion)
00676     #define osaLoadUPP  ((ComponentFunctionUPP) osaLoad)
00677     #define osaStoreUPP ((ComponentFunctionUPP) osaStore)
00678     #define osaExecuteUPP   ((ComponentFunctionUPP) osaExecute)
00679     #define osaDisplayUPP   ((ComponentFunctionUPP) osaDisplay)
00680     #define osaScriptErrorUPP   ((ComponentFunctionUPP) osaScriptError)
00681     #define osaDisposeUPP   ((ComponentFunctionUPP) osaDispose)
00682     #define osaSetScriptInfoUPP ((ComponentFunctionUPP) osaSetScriptInfo)
00683     #define osaGetScriptInfoUPP ((ComponentFunctionUPP) osaGetScriptInfo)
00684     #define osaCompileUPP   ((ComponentFunctionUPP) osaCompile)
00685     #define osaGetSourceUPP ((ComponentFunctionUPP) osaGetSource)
00686     #define osaCoerceFromDescUPP    ((ComponentFunctionUPP) osaCoerceFromDesc)
00687     #define osaCoerceToDescUPP  ((ComponentFunctionUPP) osaCoerceToDesc)
00688     #define osaStartRecordingUPP    ((ComponentFunctionUPP) osaStartRecording)
00689     #define osaStopRecordingUPP ((ComponentFunctionUPP) osaStopRecording)
00690     #define osaScriptingComponentNameUPP    ((ComponentFunctionUPP) osaScriptingComponentName)
00691     #define osaLoadExecuteUPP   ((ComponentFunctionUPP) osaLoadExecute)
00692     #define osaCompileExecuteUPP    ((ComponentFunctionUPP) osaCompileExecute)
00693     #define osaDoScriptUPP  ((ComponentFunctionUPP) osaDoScript)
00694     #define osaMakeContextUPP   ((ComponentFunctionUPP) osaMakeContext)
00695     #define osaSetResumeDispatchProcUPP ((ComponentFunctionUPP) osaSetResumeDispatchProc)
00696     #define osaGetResumeDispatchProcUPP ((ComponentFunctionUPP) osaGetResumeDispatchProc)
00697     #define osaExecuteEventUPP  ((ComponentFunctionUPP) osaExecuteEvent)
00698     #define osaDoEventUPP   ((ComponentFunctionUPP) osaDoEvent)
00699     #define osaSetActiveProcUPP ((ComponentFunctionUPP) osaSetActiveProc)
00700     #define osaSetDebugProcUPP  ((ComponentFunctionUPP) osaSetDebugProc)
00701     #define osaDebugUPP ((ComponentFunctionUPP) osaDebug)
00702     #define osaSetSendProcUPP   ((ComponentFunctionUPP) osaSetSendProc)
00703     #define osaGetSendProcUPP   ((ComponentFunctionUPP) osaGetSendProc)
00704     #define osaSetCreateProcUPP ((ComponentFunctionUPP) osaSetCreateProc)
00705     #define osaGetCreateProcUPP ((ComponentFunctionUPP) osaGetCreateProc)
00706     #define osaSetDefaultTargetUPP  ((ComponentFunctionUPP) osaSetDefaultTarget)
00707 
00708     #define osaclientactiveUPP (&osaclientactive)
00709     #define osaclientidleUPP (&osaclientidleproc)
00710     #define osaclientsendUPP (&osaclientsend)
00711 
00712 #endif
00713 
00714 
00715 #define kOSAScriptIsBeingEdited         'edit'
00716     // Selector returns boolean.
00717 
00718 #define kOSAScriptIsBeingRecorded       'recd'
00719     // Selector returns boolean.
00720 
00721 
00722 typedef struct tyservercomponent {
00723     
00724     struct tyservercomponent **hnext;
00725     
00726     ComponentInstance instance;
00727     
00728     ProcessSerialNumber clientpsn; /*3.015*/
00729     
00730     OSType type;
00731     } tyservercomponent, *ptrservercomponent, **hdlservercomponent;
00732 
00733 
00734 typedef struct tyclientlist {
00735     
00736     hdlcomponentglobals hfirst;
00737     
00738     } tyclientlist, **hdlclientlist;
00739 
00740 
00741 enum { /*recording strings*/
00742     
00743     nullstring = 1,
00744     
00745     afterstring,
00746     
00747     beforestring,
00748     
00749     beginningofstring,
00750     
00751     endofstring,
00752     
00753     replacingstring,
00754     
00755     insertionlocstring,
00756     
00757     withobjectmodelstring,
00758     
00759     bringtofrontstring,
00760     
00761     sysbringapptofrontstring,
00762     
00763     idstring,
00764     
00765     appleeventstring,
00766     
00767     noverbtablestring,
00768     
00769     multipleclientsstring,
00770     
00771     specificclientstring
00772     };
00773 
00774 
00775 Component osacomponent = nil;
00776 
00777 boolean flosashutdown = false;
00778 
00779 
00780 static hdlservercomponent hserverlist = nil;
00781 
00782 static hdlclientlist hclientlist = nil;
00783 
00784 
00785 static byte bssource [] = "\p_source";
00786 
00787 static byte bscode [] = "\p_code";
00788 
00789 
00790 /*
00791 static byte bscontext [] = "\p_context";
00792 
00793 static byte bsmodeflags [] = "\p_flags";
00794 */
00795 
00796 
00797 
00798 /*
00799 typedef struct tystubrecord {
00800     
00801     unsigned short jmp;
00802     
00803     ProcPtr adr;
00804     } tystubrecord, **hdlstubrecord;
00805 */
00806 
00807 
00808 typedef struct tystylerecord {
00809     
00810     short ctstyles;
00811     
00812     ScrpSTElement styles [1];
00813     } tystylerecord;
00814 
00815 
00816 static THz homezone;
00817 
00818 #if !TARGET_API_MAC_CARBON
00819     static Handle homeresmap;
00820 #endif
00821 
00822 static short homeresfile;
00823 
00824 static ProcessSerialNumber homepsn;
00825 
00826 
00827 hdlcomponentglobals osaglobals = nil;
00828 
00829 static long osacoercionhandlerinstalled = 0;
00830 
00831 static long osabackgroundtime = 0;
00832 
00833 
00834 
00835 void disposecomponentglobals (hdlcomponentglobals hglobals) {
00836     
00837     /*
00838     dispose globals entirely.
00839     
00840     need to swap in our tablestack while disposing storage table in case 
00841     global environment isn't hospitable
00842     */
00843     
00844     register hdlcomponentglobals hcg = hglobals;
00845     register hdltablestack hs = hashtablestack; /*save*/
00846     long ctbytes = longinfinity;
00847     
00848     if (hcg == nil)
00849         return;
00850     
00851     listunlink ((hdllinkedlist) hclientlist, (hdllinkedlist) hcg);
00852     
00853     hashtablestack = (**(**hcg).clientthreadglobals).htablestack;
00854         
00855     #if TARGET_API_MAC_CARBON
00856         DisposeOSAActiveUPP((**hcg).activeproc);
00857         DisposeOSASendUPP((**hcg).sendproc);
00858         DisposeOSACreateAppleEventUPP((**hcg).createproc);
00859         
00860         DisposeComponentFunctionUPP((**hcg).cmpcloseUPP);
00861         DisposeComponentFunctionUPP((**hcg).cmpcandoUPP);
00862         DisposeComponentFunctionUPP((**hcg).cmpversionUPP);
00863         DisposeComponentFunctionUPP((**hcg).osaLoadUPP);
00864         DisposeComponentFunctionUPP((**hcg).osaStoreUPP);
00865         DisposeComponentFunctionUPP((**hcg).osaExecuteUPP);
00866         DisposeComponentFunctionUPP((**hcg).osaDisplayUPP);
00867         DisposeComponentFunctionUPP((**hcg).osaScriptErrorUPP);
00868         DisposeComponentFunctionUPP((**hcg).osaDisposeUPP);
00869         DisposeComponentFunctionUPP((**hcg).osaSetScriptInfoUPP);
00870         DisposeComponentFunctionUPP((**hcg).osaGetScriptInfoUPP);
00871         DisposeComponentFunctionUPP((**hcg).osaCompileUPP);
00872         DisposeComponentFunctionUPP((**hcg).osaGetSourceUPP);
00873         DisposeComponentFunctionUPP((**hcg).osaCoerceFromDescUPP);
00874         DisposeComponentFunctionUPP((**hcg).osaCoerceToDescUPP);
00875         DisposeComponentFunctionUPP((**hcg).osaStartRecordingUPP);
00876         DisposeComponentFunctionUPP((**hcg).osaStopRecordingUPP);
00877         DisposeComponentFunctionUPP((**hcg).osaScriptingComponentNameUPP);
00878         DisposeComponentFunctionUPP((**hcg).osaLoadExecuteUPP);
00879         DisposeComponentFunctionUPP((**hcg).osaCompileExecuteUPP);
00880         DisposeComponentFunctionUPP((**hcg).osaDoScriptUPP);
00881         DisposeComponentFunctionUPP((**hcg).osaMakeContextUPP);
00882         DisposeComponentFunctionUPP((**hcg).osaSetResumeDispatchProcUPP);
00883         DisposeComponentFunctionUPP((**hcg).osaGetResumeDispatchProcUPP);
00884         DisposeComponentFunctionUPP((**hcg).osaExecuteEventUPP);
00885         DisposeComponentFunctionUPP((**hcg).osaDoEventUPP);
00886         DisposeComponentFunctionUPP((**hcg).osaSetActiveProcUPP);
00887         DisposeComponentFunctionUPP((**hcg).osaSetDebugProcUPP);
00888         DisposeComponentFunctionUPP((**hcg).osaDebugUPP);
00889         DisposeComponentFunctionUPP((**hcg).osaSetSendProcUPP);
00890         DisposeComponentFunctionUPP((**hcg).osaGetSendProcUPP);
00891         DisposeComponentFunctionUPP((**hcg).osaSetCreateProcUPP);
00892         DisposeComponentFunctionUPP((**hcg).osaGetCreateProcUPP);   
00893     #endif
00894         
00895     disposehashtable ((**hcg).storagetable, false);
00896     
00897     hashtablestack = hs; /*restore*/
00898     
00899     // assert ((**hcg).clientthreadglobals != getcurrentthreadglobals ());
00900     
00901     disposethreadglobals ((**hcg).clientthreadglobals);
00902     
00903     /*
00904     disposehandle ((Handle) (**hcg).hMSglobals);
00905     */
00906     
00907     disposehandle ((Handle) hcg);
00908     
00909     hashflushcache (&ctbytes); // 5.1b23 dmb: don't reuse any handles allocated
00910     } /*disposecomponentglobals*/
00911 
00912 
00913 pascal OSErr osadefaultactiveproc (long refcon) {
00914 #pragma unused (refcon)
00915     /*
00916     see if user pressed cmd-period.
00917     
00918     this gets called in client's context, so don't call any of our routines. in 
00919     any case, keyboardescape has its own timing mechanism that we don't want.
00920     
00921     4.1b13 dmb: use new iscmdperiodevent
00922     */
00923     
00924     EventRecord ev;
00925     //Code change by Timothy Paustian Friday, June 16, 2000 1:35:10 PM
00926     //Changed to Opaque call for Carbon
00927     //updated to new call
00928     if (EventAvail (keyDownMask, &ev)) {
00929         
00930         if (iscmdperiodevent (ev.message, ev.what, ev.modifiers))
00931             return (userCanceledErr);
00932         }
00933     
00934     return (noErr);
00935     } /*osadefaultactiveproc*/
00936 
00937 
00938 static pascal OSErr
00939 osadefaultcreate (
00940         AEEventClass         class,
00941         AEEventID            id,
00942         const AEAddressDesc *target,
00943         short                returnID,
00944         long                 transactionID,
00945         AppleEvent          *result,
00946         long                 refcon)
00947 {
00948 #pragma unused (refcon)
00949 
00950     return (AECreateAppleEvent(class, id, target, returnID, transactionID, result));
00951     } /*osadefaultcreate*/
00952 
00953 
00954 static pascal OSErr
00955 osadefaultsend (
00956         const AppleEvent    *event,
00957         AppleEvent          *reply,
00958         AESendMode           sendmode,
00959         AESendPriority       priority,
00960         long                 timeout,
00961         AEIdleUPP            idleproc,
00962         AEFilterUPP          filterproc,
00963         long                 refcon)
00964 {
00965 #pragma unused (refcon)
00966 
00967     return (AESend (event, reply, sendmode, priority, timeout, idleproc, filterproc));
00968     } /*osadefaultsend*/
00969 
00970 
00971 #if TARGET_RT_MAC_CFM || TARGET_RT_MAC_MACHO
00972 
00973     //Code change by Timothy Paustian Friday, July 21, 2000 10:52:57 PM
00974     //I think I can get away with this because only frontier code calls it.
00975 
00976     #if TARGET_API_MAC_CARBON
00977 
00978         OSAActiveUPP osadefaultactiveDesc = nil;
00979         OSACreateAppleEventUPP osadefaultcreateDesc = nil;
00980         OSASendUPP osadefaultsendDesc = nil;
00981     
00982         #define osadefaultactiveUPP (osadefaultactiveDesc)
00983         #define osadefaultcreateUPP (osadefaultcreateDesc)
00984         #define osadefaultsendUPP (osadefaultsendDesc)
00985         
00986     #else
00987 
00988         static RoutineDescriptor osadefaultactiveDesc = BUILD_ROUTINE_DESCRIPTOR (uppOSAActiveProcInfo, osadefaultactiveproc);
00989         static RoutineDescriptor osadefaultcreateDesc = BUILD_ROUTINE_DESCRIPTOR (uppOSACreateAppleEventProcInfo, osadefaultcreate);
00990         static RoutineDescriptor osadefaultsendDesc = BUILD_ROUTINE_DESCRIPTOR (uppOSASendProcInfo, osadefaultsend);
00991     
00992         #define osadefaultactiveUPP (&osadefaultactiveDesc)
00993         #define osadefaultcreateUPP (&osadefaultcreateDesc)
00994         #define osadefaultsendUPP (&osadefaultsendDesc)
00995 
00996     #endif
00997     
00998 #else
00999 
01000     #define osadefaultactiveUPP ((OSAActiveProcPtr) &osadefaultactiveproc)
01001     #define osadefaultcreateUPP ((OSACreateAppleEventProcPtr) &osadefaultcreate)
01002     #define osadefaultsendUPP ((OSASendProcPtr) &osadefaultsend)
01003 
01004 #endif 
01005 
01006 
01007 boolean newcomponentglobals (Component self, long clienta5, hdlcomponentglobals *hglobals) {
01008     
01009     /*
01010     encapsulate as much state information as possible for this component instance.
01011     
01012     we include an entire set of thread globals to avoid ad-hoc saving & restoring of 
01013     shell and lang globals & callbacks.
01014     
01015     5.0b7 dmb: allocate storage table in our own heap; langhash caches them
01016     */
01017     
01018     register hdlcomponentglobals hcg;
01019     register hdlthreadglobals htg;
01020     hdlhashtable storagetable;
01021     hdlthreadglobals hthreadglobals;
01022     ProcessSerialNumber psn;
01023     OSType appid;
01024     Boolean flsame;
01025     boolean fl;
01026     
01027     if (!newclearhandle (sizeof (tycomponentglobals), (Handle *) hglobals))
01028         return  (false);
01029     
01030     hcg = *hglobals;
01031     
01032     listlink ((hdllinkedlist) hclientlist, (hdllinkedlist) hcg);
01033     
01034     GetCurrentProcess (&psn);
01035     
01036     appid = getprocesscreator ();
01037     
01038     if ((SameProcess (&psn, &homepsn, &flsame) == noErr) && flsame)
01039         (**hcg).isHomeProcess = true;
01040     
01041     (**hcg).self = self;
01042     
01043     (**hcg).clienta5 = clienta5;
01044     
01045     //Code change by Timothy Paustian Friday, June 16, 2000 1:39:07 PM
01046     //Changed to Opaque call for Carbon - you can't use this in carbon anyway
01047     #if TARGET_API_MAC_CARBON == 1
01048         (**hcg).clientzone = LMGetApplZone();
01049     #else   
01050         (**hcg).clientzone = GetZone ();
01051     #endif
01052         
01053     (**hcg).clientid = appid;
01054     
01055     (**hcg).clientpsn = psn;
01056     
01057     #if TARGET_API_MAC_CARBON == 1
01058         LMSetApplZone(homezone);
01059     #else   
01060         SetZone (homezone);
01061     #endif
01062         
01063     fl = newhashtable (&storagetable);
01064     
01065     #if TARGET_API_MAC_CARBON == 1
01066         LMSetApplZone((**hcg).clientzone);
01067     #else   
01068         SetZone ((**hcg).clientzone);
01069     #endif
01070     
01071     if (!fl) {
01072     
01073         disposehandle ((Handle) hcg);
01074     
01075         return (false);
01076         }
01077     
01078     (**hcg).storagetable = storagetable;
01079     
01080     #if TARGET_API_MAC_CARBON
01081     
01082         (**hcg).activeproc = NewOSAActiveUPP(osadefaultactiveproc);
01083         (**hcg).createproc = NewOSACreateAppleEventUPP(osadefaultcreate);
01084         (**hcg).sendproc = NewOSASendUPP(osadefaultsend);
01085     
01086         //Code change by Timothy Paustian Sunday, September 3, 2000 9:57:20 PM
01087         //We have to create theses all and store them in the globals variables.
01088     
01089         (**hcg).cmpcloseUPP = NewComponentFunctionUPP(cmpclose, cmpcloseProcInfo);
01090         (**hcg).cmpcandoUPP = NewComponentFunctionUPP((ProcPtr) cmpcando, cmpcandoProcInfo);
01091         (**hcg).cmpversionUPP = NewComponentFunctionUPP(cmpversion, cmpversionProcInfo);
01092         (**hcg).osaLoadUPP = NewComponentFunctionUPP(osaLoad, osaLoadProcInfo);
01093         (**hcg).osaStoreUPP = NewComponentFunctionUPP(osaStore, osaStoreProcInfo);
01094         (**hcg).osaExecuteUPP = NewComponentFunctionUPP(osaExecute, osaExecuteProcInfo);
01095         (**hcg).osaDisplayUPP = NewComponentFunctionUPP(osaDisplay, osaDisplayProcInfo);
01096         (**hcg).osaScriptErrorUPP = NewComponentFunctionUPP(osaScriptError, osaScriptErrorProcInfo);
01097         (**hcg).osaDisposeUPP = NewComponentFunctionUPP(osaDispose, osaDisposeProcInfo);
01098         (**hcg).osaSetScriptInfoUPP = NewComponentFunctionUPP(osaSetScriptInfo, osaSetScriptInfoProcInfo);
01099         (**hcg).osaGetScriptInfoUPP = NewComponentFunctionUPP(osaGetScriptInfo, osaGetScriptInfoProcInfo);
01100         (**hcg).osaCompileUPP = NewComponentFunctionUPP(osaCompile, osaCompileProcInfo);
01101         (**hcg).osaGetSourceUPP = NewComponentFunctionUPP(osaGetSource, osaGetSourceProcInfo);
01102         (**hcg).osaCoerceFromDescUPP = NewComponentFunctionUPP(osaCoerceFromDesc, osaCoerceFromDescProcInfo);
01103         (**hcg).osaCoerceToDescUPP = NewComponentFunctionUPP(osaCoerceToDesc, osaCoerceToDescProcInfo);
01104         (**hcg).osaStartRecordingUPP = NewComponentFunctionUPP(osaStartRecording, osaStartRecordingProcInfo);
01105         (**hcg).osaStopRecordingUPP = NewComponentFunctionUPP(osaStopRecording, osaStopRecordingProcInfo);
01106         (**hcg).osaScriptingComponentNameUPP = NewComponentFunctionUPP(osaScriptingComponentName, osaScriptingComponentNameProcInfo);
01107         (**hcg).osaLoadExecuteUPP = NewComponentFunctionUPP(osaLoadExecute, osaLoadExecuteProcInfo);
01108         (**hcg).osaCompileExecuteUPP = NewComponentFunctionUPP(osaCompileExecute, osaCompileExecuteProcInfo);
01109         (**hcg).osaDoScriptUPP = NewComponentFunctionUPP(osaDoScript, osaDoScriptProcInfo);
01110         (**hcg).osaMakeContextUPP = NewComponentFunctionUPP(osaMakeContext, osaMakeContextProcInfo);
01111         (**hcg).osaSetResumeDispatchProcUPP = NewComponentFunctionUPP(osaSetResumeDispatchProc, osaSetResumeDispatchProcProcInfo);
01112         (**hcg).osaGetResumeDispatchProcUPP = NewComponentFunctionUPP(osaGetResumeDispatchProc, osaGetResumeDispatchProcProcInfo);
01113         (**hcg).osaExecuteEventUPP = NewComponentFunctionUPP(osaExecuteEvent, osaExecuteEventProcInfo);
01114         (**hcg).osaDoEventUPP = NewComponentFunctionUPP(osaDoEvent, osaDoEventProcInfo);
01115         (**hcg).osaSetActiveProcUPP = NewComponentFunctionUPP(osaSetActiveProc, osaSetActiveProcProcInfo);
01116         (**hcg).osaSetDebugProcUPP = NewComponentFunctionUPP(osaSetDebugProc, osaSetDebugProcProcInfo);
01117         (**hcg).osaDebugUPP = NewComponentFunctionUPP(osaDebug, osaDebugProcInfo);
01118         (**hcg).osaSetSendProcUPP = NewComponentFunctionUPP(osaSetSendProc, osaSetSendProcProcInfo);
01119         (**hcg).osaGetSendProcUPP = NewComponentFunctionUPP(osaGetSendProc, osaGetSendProcProcInfo);
01120         (**hcg).osaSetCreateProcUPP = NewComponentFunctionUPP(osaSetCreateProc, osaSetCreateProcProcInfo);
01121         (**hcg).osaGetCreateProcUPP = NewComponentFunctionUPP(osaGetCreateProc, osaGetCreateProcProcInfo);
01122     #else
01123         
01124         (**hcg).activeproc = osadefaultactiveUPP;
01125         (**hcg).createproc = osadefaultcreateUPP;
01126         (**hcg).sendproc = osadefaultsendUPP;
01127 
01128     #endif
01129             
01130     if (!newthreadglobals (&hthreadglobals)) {
01131         
01132         disposecomponentglobals (hcg);
01133         
01134         return (false);
01135         }
01136     
01137     htg = hthreadglobals;
01138     
01139     (**hcg).clientthreadglobals = htg;  // 4.1b3 dmb: use local
01140     
01141     (**htg).htable = storagetable;
01142     
01143     (**htg).applicationid = appid;
01144     
01145     #if version42orgreater
01146         // 2/28/97 dmb: set up langcallbacks here so they'll always be in effect
01147         
01148         (**htg).langcallbacks.backgroundtaskcallback = &osabackgroundtask;
01149         
01150         (**htg).langcallbacks.debuggercallback = &osadebugger;
01151         
01152         (**htg).langcallbacks.pushsourcecodecallback = &scriptpushsourcecode;
01153         
01154         (**htg).langcallbacks.popsourcecodecallback = &scriptpopsourcecode;
01155         
01156         (**htg).langcallbacks.partialeventloopcallback = &osapartialeventloop;
01157         
01158         (**htg).fldisableyield = true;
01159     #endif
01160     
01161     return (true);
01162     } /*newcomponentglobals*/
01163 
01164 
01165 OSAError osageterror (void) {
01166     
01167     long n;
01168     
01169     n = getoserror ();
01170     
01171     if (n == noErr)
01172         n = errOSAScriptError;
01173     
01174     return (n);
01175     } /*osageterror*/
01176 
01177 
01178 static boolean inosasource (void) {
01179     
01180     /*
01181     very carefully call back to the client's debugging proc.
01182     */
01183     
01184     register hdlerrorstack hs = langcallbacks.scripterrorstack;
01185     
01186     if ((hs != nil) && ((**hs).toperror > 1))
01187         if ((**hs).stack [(**hs).toperror - 1].errorrefcon != -1L) /*we're in a call to another script*/
01188             return (false);
01189     
01190     return (true);
01191     } /*inosasource*/
01192 
01193 
01194 static boolean osaerrormessage (bigstring bs, ptrvoid refcon) {
01195 #pragma unused (refcon)
01196 
01197     AEDesc
01198         list,
01199         rec,
01200         desc;
01201     long n;
01202     OSErr err;
01203     
01204     list = (**osaglobals).errordesc;
01205     
01206     AEDisposeDesc (&list);
01207     
01208     n = osageterror ();
01209     
01210     err = AECreateList (nil, 0, true, &list);
01211     
01212     if (err != noErr) {
01213         
01214         #if TARGET_API_MAC_CARBON == 1 /*PBS 03/14/02: AE OS X fix.*/
01215         
01216             newdescnull (&list, typeNull);
01217         
01218         #else
01219         
01220             list.descriptorType = typeNull;
01221         
01222             list.dataHandle = nil;
01223             
01224         #endif
01225         }
01226     else {
01227         
01228         err = AEPutKeyPtr (&list, kOSAErrorNumber, typeLongInteger, (Ptr) &n, sizeof (n));
01229         
01230         err = AEPutKeyPtr (&list, kOSAErrorMessage, typeChar, (Ptr) bs + 1, stringlength (bs));
01231         
01232         err = AEPutKeyPtr (&list, kOSAErrorBriefMessage, typeChar, (Ptr) bs + 1, stringlength (bs));
01233         
01234         err = AECreateList (nil, 0, true, &rec);
01235         
01236         if (err == noErr) {
01237             
01238             long ix;
01239             
01240             if (inosasource ())
01241                 ix = langgetsourceoffset (ctscanlines, ctscanchars);
01242             else
01243                 ix = 0;
01244             
01245             err = AEPutKeyPtr (&rec, keyOSASourceStart, typeLongInteger, (Ptr) &ix, sizeof (ix));
01246             
01247             err = AEPutKeyPtr (&rec, keyOSASourceEnd, typeLongInteger, (Ptr) &ix, sizeof (ix));
01248             
01249             err = AECoerceDesc (&rec, typeOSAErrorRange, &desc);
01250             
01251             AEDisposeDesc (&rec);
01252             
01253             if (err == noErr) {
01254                 
01255                 err = AEPutKeyDesc (&list, kOSAErrorRange, &desc);
01256                 
01257                 AEDisposeDesc (&desc);
01258                 }
01259             }
01260         }
01261     
01262     (**osaglobals).errordesc = list;
01263     
01264     return (false); /*consume the error*/
01265     } /*osaerrormessage*/
01266 
01267 
01268 static pascal OSErr
01269 coerceTEXTtoSTXT (
01270         DescType     fromtype,
01271         Ptr          pdata,
01272         long         size,
01273         DescType     totype,
01274         long         refcon,
01275         AEDesc      *result) {
01276 #pragma unused(fromtype, refcon)
01277     /*
01278     2.1b2 dmb: don't use clearbytes so we don't have to set up a5
01279     */
01280     
01281     tystylerecord stylerecord;
01282     OSErr err;
01283     AEDesc list;
01284     register ScrpSTElement *pstyle;
01285     
01286     #ifdef fldebug  // 2006-04-04 - kw --- this was fldegug
01287     
01288     if (totype != typeStyledText)
01289         DebugStr ("\punexpected coercion");
01290     
01291     #endif
01292     
01293     stylerecord.ctstyles = 1;
01294     
01295     pstyle = &stylerecord.styles [0];
01296     
01297     (*pstyle).scrpStartChar = 0;
01298     
01299     (*pstyle).scrpHeight = 14;
01300     
01301     (*pstyle).scrpAscent = 12;
01302     
01303     (*pstyle).scrpFont = geneva;
01304     
01305     (*pstyle).scrpSize = 9;
01306     
01307     (*pstyle).scrpFace = 0;
01308     
01309     (*pstyle).scrpColor.red = 0;
01310     
01311     (*pstyle).scrpColor.green = 0;
01312     
01313     (*pstyle).scrpColor.blue = 0;
01314     
01315     err = AECreateList (nil, 0, true, &list);
01316     
01317     if (err == noErr) {
01318         
01319         err = AEPutKeyPtr (&list, 'ksty', 'styl', (Ptr) &stylerecord, sizeof (stylerecord));
01320         
01321         if (err == noErr)
01322             err = AEPutKeyPtr (&list, 'ktxt', typeChar, pdata, size);
01323         
01324         if (err == noErr)
01325             err = AECoerceDesc (&list, typeStyledText, result);
01326         
01327         AEDisposeDesc (&list);
01328         }
01329     
01330     return (err);
01331     } /*coerceTEXTtoSTXT*/
01332 
01333 
01334 static pascal OSErr
01335 coerceTypetoObj (
01336         AEDesc      *desc,
01337         DescType     totype,
01338         long         refcon,
01339         AEDesc      *result) {
01340 #pragma unused(totype, refcon)
01341 
01342     /*
01343     2.1b1 dmb: if Frontier passes a string4 identifier where an object specifier 
01344     is required, this coercion handler will take care of it
01345     */
01346     
01347     AEDesc containerdesc;
01348     OSErr err;
01349     
01350     #if !TARGET_API_MAC_CARBON
01351     long curA5 = SetUpAppA5 ();
01352     #endif
01353     
01354     #ifdef fldebug
01355         if ((*desc).descriptorType != typeType)
01356             DebugStr ("\punexpected coercion");
01357     #endif
01358     
01359     #if TARGET_API_MAC_CARBON == 1 /*PBS 03/14/02: AE OS X fix.*/
01360         newdescnull (&containerdesc, typeNull);
01361     #else
01362         containerdesc.descriptorType = typeNull;
01363         containerdesc.dataHandle = nil;
01364     #endif
01365     
01366     err = CreateObjSpecifier (cProperty, &containerdesc, formPropertyID, desc, false, result);
01367     
01368     #if !TARGET_API_MAC_CARBON
01369     RestoreA5 (curA5);
01370     #endif
01371     
01372     return (err);
01373     } /*coerceTypetoObj*/
01374 
01375 
01376 //Code change by Timothy Paustian Friday, June 16, 2000 1:38:13 PM
01377 //Changed to Opaque call for Carbon
01378 #if TARGET_RT_MAC_CFM || TARGET_RT_MAC_MACHO
01379 
01380     #if TARGET_API_MAC_CARBON
01381 
01382         AECoercionHandlerUPP    coerceTEXTtoSTXTDesc;
01383         AECoercionHandlerUPP    coerceTypetoObjDesc;
01384     
01385         #define coerceTEXTtoSTXTUPP (coerceTEXTtoSTXTDesc)
01386         #define coerceTypetoObjUPP (coerceTypetoObjDesc)
01387 
01388     #else
01389 
01390         static RoutineDescriptor coerceTEXTtoSTXTDesc = BUILD_ROUTINE_DESCRIPTOR (uppAECoercePtrProcInfo, coerceTEXTtoSTXT);
01391         static RoutineDescriptor coerceTypetoObjDesc = BUILD_ROUTINE_DESCRIPTOR (uppAECoerceDescProcInfo, coerceTypetoObj);
01392     
01393         #define coerceTEXTtoSTXTUPP ((AECoercionHandlerUPP) &coerceTEXTtoSTXTDesc)
01394         #define coerceTypetoObjUPP ((AECoercionHandlerUPP) &coerceTypetoObjDesc)
01395 
01396     #endif
01397 
01398 #else
01399 
01400     #define coerceTEXTtoSTXTUPP ((AECoercionHandlerUPP) &coerceTEXTtoSTXT)
01401     
01402     #define coerceTypetoObjUPP ((AECoercionHandlerUPP) &coerceTypetoObj)
01403     
01404 #endif
01405 
01406 
01407 static void osapushfastcontext (hdlcomponentglobals hglobals) {
01408     
01409     /*
01410     4/20/93 dmb: always set client's ccglobals to nil. if root has been closed, 
01411     we want nil globals to persist, or new globals to be picked up
01412     
01413     3.0b14 dmb: handling nested osa contexts
01414     
01415     4.1b3 dmb: nesting wasn't thoroughly handled by the hserverosaglobals field.
01416     added ctpushes field that handles nesting better, assuming it's simple nesting 
01417     of the same globals. to handle arbitrary, daisy chain globals nesting, we'd 
01418     need to keep a list or a stack of serverthreadglobals.
01419     */
01420     
01421     register hdlcomponentglobals hcg = hglobals;
01422     register hdlthreadglobals htg;
01423     
01424     /*
01425     short rnum = OpenComponentResFile ((**hcg).self);
01426     */
01427     
01428     if ((**hcg).ctpushes++ > 0) { // 4.1b3 dmb: nesting of same globals
01429     
01430         assert (osaglobals == hcg);
01431         
01432         assert ((**hcg).serverthreadglobals);
01433         
01434         assert ((**hcg).clientthreadglobals == getcurrentthreadglobals ());
01435         }
01436     else {
01437     
01438         (**hcg).hserverosaglobals = osaglobals; /*in case we're nested*/
01439         
01440         osaglobals = hcg; /*make visible to callbacks*/
01441         
01442         htg = getcurrentthreadglobals ();
01443         
01444         (**hcg).serverthreadglobals = htg;
01445         
01446         copythreadglobals (htg); /*save*/
01447         
01448         htg = (**hcg).clientthreadglobals;
01449         
01450         #if !flruntime
01451         
01452         (**htg).hccglobals = nil; /*want to leave them untouched*/
01453         
01454         #endif
01455         
01456         swapinthreadglobals (htg);
01457         }
01458     
01459     if (++osacoercionhandlerinstalled == 1) {
01460         #if TARGET_API_MAC_CARBON
01461             if(coerceTEXTtoSTXTUPP == nil)
01462                 coerceTEXTtoSTXTUPP = NewAECoerceDescUPP((AECoerceDescProcPtr)coerceTEXTtoSTXT);
01463         #endif
01464 
01465         AEInstallCoercionHandler (typeChar, typeStyledText, coerceTEXTtoSTXTUPP, 0, false, false);
01466         }
01467     
01468     if (!(**hcg).isHomeProcess) {
01469     
01470         (**hcg).clientresfile = CurResFile ();
01471         
01472         //Code change by Timothy Paustian Friday, June 16, 2000 2:08:20 PM
01473         //Changed to Opaque call for Carbon
01474         //This is not supported in carbon. You cannot use res files in OS X
01475         //We may have some serious rewriting to do for this.
01476         #if !TARGET_API_MAC_CARBON
01477             (**hcg).clientresmaphandle = LMGetTopMapHndl ();
01478             
01479             LMSetTopMapHndl (homeresmap);
01480         #endif
01481         
01482         UseResFile (homeresfile);
01483         }
01484     } /*osapushfastcontext*/
01485 
01486 
01487 static void osapopfastcontext (hdlcomponentglobals hglobals) {
01488     
01489     /*
01490     3.0b14 dmb: handling nested osa contexts
01491     */
01492     
01493     register hdlcomponentglobals hcg = hglobals;
01494     
01495     /*
01496     CloseComponentResFile (rnum);
01497     */
01498     
01499     if (--(**hcg).ctpushes == 0) { // 4.1b3 dmb: no nesting of same globals
01500     
01501         copythreadglobals ((**hcg).clientthreadglobals);
01502         
01503         swapinthreadglobals ((**hcg).serverthreadglobals);
01504         
01505         osaglobals = (**hcg).hserverosaglobals; /*in case we were nested*/
01506         }
01507     
01508     if (--osacoercionhandlerinstalled == 0)
01509     {
01510         AERemoveCoercionHandler (typeChar, typeStyledText, coerceTEXTtoSTXTUPP, false);
01511 
01512         //Code change by Timothy Paustian Friday, July 21, 2000 11:02:21 PM
01513         //added dispose of coercion handler
01514         #if TARGET_API_MAC_CARBON
01515             if(coerceTEXTtoSTXTUPP != nil) {
01516                 DisposeAECoerceDescUPP(coerceTEXTtoSTXTUPP);
01517                 coerceTEXTtoSTXTUPP = nil;
01518                 }
01519         #endif
01520     }
01521     if (!(**hcg).isHomeProcess) {
01522 
01523         #if !TARGET_API_MAC_CARBON
01524             LMSetTopMapHndl ((**hcg).clientresmaphandle);
01525         #endif
01526 
01527         UseResFile ((**hcg).clientresfile);
01528         }
01529     } /*osapopfastcontext*/
01530 
01531 
01532 long osapreclientcallback (hdlcomponentglobals hglobals) {
01533     
01534     /*
01535     before we put up standard file, or the ppc browser, we need to put the 
01536     clients environment in order, and make sure that the Frontier environment 
01537     is restored, since it may get swapped in by the process manager.
01538     
01539     case in point: Norton Directory Assistance trashes both our heap & the 
01540     client's if we don't do this.
01541     
01542     since we're split into a pre- and post- routine, our caller has to manage 
01543     A5 for us. (we could probably get around this, but I think it would be 
01544     even messier.)
01545     */
01546     
01547     register hdlcomponentglobals hcg = hglobals;
01548     
01549     osapopfastcontext (hcg);
01550     
01551     return ((**hcg).clienta5);
01552     } /*osapreclientcallback*/
01553 
01554 
01555 void osapostclientcallback (hdlcomponentglobals hglobals) {
01556     
01557     osapushfastcontext (hglobals);
01558     } /*osapostclientcallback*/
01559 
01560 
01561 #if TARGET_API_MAC_OS8
01562     
01563     static UniversalProcPtr installpatch (short trapnum, GNEUPP patch) {
01564         
01565         UniversalProcPtr origtrap = nil;
01566         
01567         origtrap = NGetTrapAddress (trapnum, ToolTrap);
01568         
01569         NSetTrapAddress ((UniversalProcPtr) patch, trapnum, ToolTrap);
01570         
01571         return (origtrap);
01572         } /*installpatch*/
01573     
01574     
01575     static void removepatch (short trapnum, GNEUPP origtrap) {
01576         
01577         NSetTrapAddress ((UniversalProcPtr) origtrap, trapnum, ToolTrap);
01578     
01579         } /*removepatch*/
01580 
01581 
01582 static pascal Boolean osagetnextevent (short, EventRecord *);
01583 
01584 
01585 #if TARGET_RT_MAC_CFM
01586 
01587     static RoutineDescriptor osagetnexteventDesc = BUILD_ROUTINE_DESCRIPTOR (uppGNEProcInfo, osagetnextevent);
01588     
01589     #define osagetnexteventUPP (&osagetnexteventDesc)
01590     
01591 #else
01592 
01593     #define osagetnexteventUPP (&osagetnextevent)
01594 
01595 #endif
01596 
01597 
01598 GNEUPP osainstallpatch (hdlcomponentglobals hglobals) {
01599     
01600     /*
01601     3.0a dmb: return the original value of getnexteventproc so it can 
01602     be restored in osaremovepath. this allows patching pairs to be 
01603     nested, so calls to handlerunscript can be nested.
01604     */
01605     
01606     register hdlcomponentglobals hcg = hglobals;
01607     GNEUPP origproc;
01608     UniversalProcPtr origtrap;
01609     
01610     if ((**hcg).isHomeProcess)
01611         origproc = nil;
01612         
01613     else {
01614     
01615         origtrap = installpatch (_GetNextEvent, osagetnexteventUPP);
01616         
01617         origproc = (**hcg).getnexteventproc;
01618         
01619         (**hcg).getnexteventproc = (GNEUPP) origtrap;
01620         }
01621     
01622     return (origproc);
01623     } /*osainstallpatch*/
01624 
01625 
01626 void osaremovepatch (hdlcomponentglobals hglobals, GNEUPP origproc) {
01627 #pragma unused (origproc)
01628 
01629     register hdlcomponentglobals hcg = hglobals;
01630     
01631     if ((**hcg).isHomeProcess)
01632         ;   
01633     else {
01634     
01635         assert ((**hcg).getnexteventproc != nil);
01636         
01637         removepatch (_GetNextEvent, (**hcg).getnexteventproc);
01638                 
01639         (**hcg).getnexteventproc = origproc;
01640         }
01641     } /*osaremovepatch*/
01642 
01643 #else
01644 
01645 GNEUPP osainstallpatch (hdlcomponentglobals hglobals) {
01646 #pragma unused (hglobals)
01647 
01648     return (nil);
01649     } /*osainstallpatch*/
01650 
01651 
01652 void osaremovepatch (hdlcomponentglobals hglobals, GNEUPP origproc) {
01653 #pragma unused (hglobals, origproc)
01654 
01655     } /*osaremovepatch*/
01656 
01657 #endif
01658 
01659 
01660 static boolean osapartialeventloop (short desiredevents) {
01661     
01662     /*
01663     2.1b8 dmb: we can't call waitnextevent on behalf of the client, because 
01664     the OSA doesn't provide an API for safely doing so. so we return false.
01665     
01666     in case we're being called to handle a process switch, we'd better 
01667     un-hilite any menu. otherwise, the menu manager gets confused, and 
01668     the menubar can be trashed when the no-longer-current app calls 
01669     HiliteMenu (0)
01670     
01671     2.1b11 dmb: one way we can call WNE for our client is to send an 
01672     apple event. so we'll send one to Frontier.
01673     
01674     3.0b15 dmb: if we're our own client, sending the noop event won't do 
01675     use any good -- the AE will be short-circuited and return EventNotHandled, 
01676     and the idleproc won't be called. so we want to call our client thread's 
01677     partialeventloop. this should be retrievable through the clientthreadglobals 
01678     of our osaglobals, but we're going to take a little shortcut and just 
01679     call shellpartialeventloop directly. not as good as ccpartialeventloop (the 
01680     normal langpartialeventloop), but it'll do for now. time to ship!
01681     
01682     3.0a dmb: must do HiliteMenu (0) even if we are our own client; the 
01683     problem shows up in Runtime's own shared menus.
01684     */
01685     
01686     HiliteMenu (0);
01687     
01688     if (iscurrentapplication (homepsn))
01689         return (shellpartialeventloop (desiredevents));
01690     
01691     return (langipcnoop ());
01692     } /*osapartialeventloop*/
01693 
01694 
01695 THz osasethomezone (hdlcomponentglobals hglobals) {
01696 #pragma unused (hglobals)
01697 
01698     /*
01699     2.1b13 dmb: added this to balance the setclientzone call, and to 
01700     allow us to keep the homezone global private.
01701     
01702     in the future, this routine might reset the clientzone in hgobals. 
01703     now's not the time to fiddle with this, though.
01704     */
01705     
01706     register THz origzone;
01707 
01708     #if TARGET_API_MAC_CARBON == 1
01709         origzone = LMGetApplZone();
01710         LMSetApplZone(homezone);
01711     #else
01712         origzone = GetZone ();
01713         SetZone (homezone);
01714     #endif
01715         
01716     return (origzone);
01717     } /*osasethomezone*/
01718 
01719 
01720 THz osasetclientzone (hdlcomponentglobals hglobals) {
01721     
01722     /*
01723     2.1b11 dmb: snippet for setting the clients zone.  we return 
01724     the current zone because most callers will want to restore it
01725     */
01726     
01727     register hdlcomponentglobals hcg = hglobals;
01728     register THz origzone;
01729 
01730     #if TARGET_API_MAC_CARBON == 1
01731         origzone = LMGetApplZone();
01732         LMSetApplZone((**hcg).clientzone);
01733     #else
01734         origzone = GetZone ();
01735         SetZone ((**hcg).clientzone);
01736     #endif
01737         
01738     return (origzone);
01739     } /*osasetclientzone*/
01740 
01741 
01742 #if TARGET_API_MAC_OS8
01743 
01744 static pascal Boolean osagetnextevent (short mask, EventRecord *event) {
01745     
01746     /*
01747     if any toolbox call that we use calls GetNextEvent or EventAvail, we 
01748     need to take many of the same precautions as we do for background 
01749     tasking. in particular, we need to make sure that the Frontier environment 
01750     is restored, since it may get swapped in by the process manager
01751     */
01752     
01753     register hdlcomponentglobals hcg;
01754     register GNEUPP getnexteventproc;
01755     register THz origzone;
01756     Boolean fl;
01757     
01758     long curA5 = SetUpAppA5 ();
01759     
01760     hcg = osaglobals;
01761     
01762     getnexteventproc = (**hcg).getnexteventproc;
01763     
01764     osapopfastcontext (hcg);
01765     
01766     origzone = osasetclientzone (hcg);
01767     
01768     osaremovepatch (hcg, nil); /*unpatch*/
01769     
01770     RestoreA5 (curA5);
01771     
01772     fl = CallGNEProc (getnexteventproc, mask, event);
01773 
01774     curA5 = SetUpAppA5 ();
01775     
01776     osainstallpatch (hcg); /*repatch*/
01777 
01778     #if TARGET_API_MAC_CARBON == 1
01779         LMSetApplZone(origzone);
01780     #else
01781         SetZone (origzone);
01782     #endif
01783         
01784     osapushfastcontext (hcg);
01785     
01786     RestoreA5 (curA5);
01787     
01788     return (fl);
01789     } /*osagetnextevent*/
01790 
01791 #endif  /*TARGET_API_MAC_OS8*/
01792 
01793 
01794 static pascal OSErr osacreateevent (AEEventClass class, AEEventID id,
01795                     const AEAddressDesc *target, short returnID,
01796                     long transactionID, AppleEvent *result) {
01797 
01798     /*
01799     2.1b4 dmb: as part of "event sending" support, we need to do this. it 
01800     should be reasonable to assume that a client's "create" proc won't 
01801     call WNE, so we don't do quite as much futzing around as when sending 
01802     events
01803     */
01804     
01805     register hdlcomponentglobals hcg = osaglobals;
01806     register THz origzone;
01807     OSErr err;
01808     long clienta5, appA5;
01809     
01810     osapopfastcontext (hcg);
01811     
01812     origzone = osasetclientzone (hcg);
01813     
01814     clienta5 = (**hcg).clienta5;
01815     
01816     appA5 = SetUpCurA5 ();  // SetUpThisA5 (clienta5);  // 4.0b5: was: SetUpCurA5 ();
01817     
01818     #if TARGET_API_MAC_CARBON == 1
01819         err = InvokeOSACreateAppleEventUPP (class, id, target, returnID, transactionID, result, (**hcg).createprocrefcon, (**hcg).createproc);
01820     #else
01821         err = CallOSACreateAppleEventProc ((**hcg).createproc, class, id, target, returnID, transactionID, result, (**hcg).createprocrefcon);
01822     #endif
01823     
01824     RestoreA5 (appA5);
01825 
01826     #if TARGET_API_MAC_CARBON == 1
01827         LMSetApplZone(origzone);
01828     #else
01829         SetZone (origzone);
01830     #endif
01831         
01832     osapushfastcontext (hcg);
01833     
01834     return (err);
01835     } /*osacreateevent*/
01836 
01837 
01838 static pascal OSErr
01839 osasendevent (
01840         const AppleEvent    *event,
01841         AppleEvent          *reply,
01842         AESendMode           mode,
01843         AESendPriority       priority,
01844         long                 timeout,
01845         AEIdleUPP            idleproc,
01846         AEFilterUPP          filterproc )
01847 {
01848 #pragma unused (idleproc, filterproc)
01849     
01850     /*
01851     2/16/93 dmb: in case the event is to be direct dispatched correctly, we need 
01852     to set up the client's A5 world
01853     
01854     2.1b5 dmb: watch out for nil sendProc -- the AS script editor sets this.
01855     */
01856     
01857     register hdlcomponentglobals hcg = osaglobals;
01858     OSErr err;
01859     long clienta5, appA5;
01860     register GNEUPP getnexteventproc;
01861     register THz origzone;
01862     OSASendUPP sendproc;
01863     
01864     sendproc = (**hcg).sendproc;
01865     
01866     getnexteventproc = (**hcg).getnexteventproc;
01867     
01868     osapopfastcontext (hcg);
01869     
01870     origzone = osasetclientzone (hcg);
01871     
01872     if (getnexteventproc != nil)
01873         osaremovepatch (hcg, nil); /*unpatch*/
01874     
01875     clienta5 = (**hcg).clienta5;
01876     
01877     appA5 = SetUpCurA5 ();  // SetUpThisA5 (clienta5);  // 4.0b5: was: SetUpCurA5 ();
01878     
01879     #if TARGET_API_MAC_CARBON == 1
01880         err = InvokeOSASendUPP (event, reply, mode, priority, timeout, nil, nil, (**hcg).sendprocrefcon, sendproc);
01881     #else
01882         err = CallOSASendProc (sendproc, event, reply, mode, priority, timeout, nil, nil, (**hcg).sendprocrefcon);
01883     #endif
01884     
01885     RestoreA5 (appA5);
01886     
01887     if (getnexteventproc != nil)
01888         osainstallpatch (hcg); /*repatch*/
01889     
01890     #if TARGET_API_MAC_CARBON == 1
01891         LMSetApplZone(origzone);
01892     #else   
01893         SetZone (origzone);
01894     #endif
01895         
01896     osapushfastcontext (hcg);
01897     
01898     return (err);
01899     } /*osasendevent*/
01900 
01901 
01902 static boolean osabackgroundtask (boolean flresting) {
01903     
01904     /*
01905     very carefully call back to the client.
01906     */
01907     
01908     register hdlcomponentglobals hcg = osaglobals;
01909     THz origzone;
01910     OSErr err;
01911     long clienta5, appA5;
01912     
01913     if ((**hcg).activeproc == nil) /*no callback provided*/
01914         return (true);
01915     
01916     if (langdialogrunning ()) /*no can do*/
01917         return (true);
01918     
01919     if (langerrorenabled ()) // 5.0b7 dmb: another time not to background
01920         return (true);
01921 
01922     if ((gettickcount () - osabackgroundtime < 20) && (!flresting)) /*not time*/
01923         return (true);
01924     
01925     flscriptresting = flresting;
01926     
01927     osapopfastcontext (hcg);
01928     
01929     origzone = osasetclientzone (hcg);
01930     
01931     osaremovepatch (hcg, nil); /*unpatch*/
01932     
01933     clienta5 = (**hcg).clienta5;
01934         
01935     appA5 = SetUpThisA5 (clienta5); // 4.0b5: was: SetUpCurA5 ();
01936     
01937     #if TARGET_API_MAC_CARBON == 1
01938         err = InvokeOSAActiveUPP ((**hcg).activeprocrefcon, (**hcg).activeproc);
01939     #else
01940         err = CallOSAActiveProc ((**hcg).activeproc, (**hcg).activeprocrefcon);
01941     #endif
01942     
01943     RestoreA5 (appA5);
01944     
01945     osainstallpatch (hcg); /*repatch*/
01946     
01947     #if TARGET_API_MAC_CARBON == 1
01948         LMSetApplZone(origzone);
01949     #else   
01950         SetZone (origzone);
01951     #endif
01952         
01953     osapushfastcontext (hcg);
01954     
01955     flscriptresting = false;
01956     
01957     osabackgroundtime = gettickcount ();
01958     
01959     return (!oserror (err));
01960     } /*osabackgroundtask*/
01961 
01962 
01963 static boolean osadebugger (hdltreenode hnode) {
01964     
01965     /*
01966     very carefully call back to the client's debugging proc.
01967     */
01968     
01969     register hdlcomponentglobals hcg = osaglobals;
01970     register hdltreenode hn = hnode;
01971     tytreetype op;
01972     THz origzone;
01973     OSErr err;
01974     long clienta5, appA5;
01975     
01976     if (languserescaped (false)) /*stop running the script immediately*/
01977         return (false);
01978     
01979     if ((**hcg).debugproc == nil) /*no callback provided*/
01980         return (true);
01981     
01982     if (!debuggingcurrentprocess ())
01983         return (true);
01984     
01985     if (!inosasource ())
01986         return (true);
01987     
01988     op = (**hn).nodetype; /*test for "meaty" op*/
01989     
01990     if ((op == moduleop) || (op == noop) || (op == bundleop) || (op == localop)) /*never stop on these*/
01991         return (true);
01992     
01993     (**hcg).debugnode = hn;
01994     
01995     osapopfastcontext (hcg);
01996     
01997     origzone = osasetclientzone (hcg);
01998     
01999     osaremovepatch (hcg, nil); /*unpatch*/
02000     
02001     clienta5 = (**hcg).clienta5;
02002     
02003     appA5 = SetUpCurA5 ();  // SetUpThisA5 (clienta5);  // 4.0b5: was: SetUpCurA5 ();
02004         
02005     err = CallOSADebugProc ((**hcg).debugproc, (**hcg).debugprocrefcon);
02006     
02007     RestoreA5 (appA5);
02008     
02009     osainstallpatch (hcg); /*repatch*/
02010     
02011     #if TARGET_API_MAC_CARBON == 1
02012         LMSetApplZone(origzone);
02013     #else
02014         SetZone (origzone);
02015     #endif
02016         
02017     osapushfastcontext (hcg);
02018     
02019     return (!oserror (err));
02020     } /*osadebugger*/
02021 
02022 
02023 #if !flruntime
02024 
02025 static boolean osaprocessstarted (void) {
02026     
02027     /*
02028     we don't want Frontier's menus to dim when serving osa scripts
02029     */
02030     
02031     processnotbusy ();
02032     
02033     return (true);
02034     } /*osaprocessstarted*/
02035 
02036 #endif
02037 
02038 
02039 static boolean osahandlerunscript (hdlcomponentglobals hglobals, hdltreenode hcode, 
02040     hdlhashtable hcontext, long modeflags, tyvaluerecord *resultval) {
02041     
02042     /*
02043     12/31/92 dmb: cloned from langipc.c, this will need to deal with 
02044     lang/threadglobals more carefully in the future
02045     
02046     1/21/93 dmb: it now deals with lang/threadglobal very carefully indeed! final 
02047     touch: we path getnextevent so we can deal with it like a background callback. 
02048     note that we may need to do this with eventavail, waitnextevent as well.
02049     
02050     6/2/93 dmb: no longer override msg verb, since it now works OK along with 
02051     other frontier process-based verbs
02052     
02053     2.1b5 dmb: pass -1 for errorrefcon instead of zero to prevent top level 
02054     lexical scope from being transparent (just like QuickScript does). for 
02055     isosasource to work, we also need to hook up push/popsourcecode callbacks.
02056     
02057     2.1b12 dmb: restore all langcallbacks that we set
02058     
02059     2.1b13 dmb: added processstarted routine to prevent Frontier's menus from
02060     dimming when running OSA scripts.
02061     
02062     3.0b15 dmb: test heap space for 2K before trying to run a process.
02063     
02064     3.0.1b1 dmb: generate error message when no file is open
02065     */
02066     
02067     register hdlcomponentglobals hcg = hglobals;
02068     hdlprocessrecord hprocess;
02069     register hdlprocessrecord hp;
02070     register boolean fl = false;
02071     GNEUPP origproc;
02072     long errorrefcon = -1;
02073     
02074     if (roottable == nil) {
02075         
02076         langerror (nofileopenerror); /*3.0.1b1*/
02077         
02078         return (false);
02079         }
02080     
02081     if ((modeflags & kOSAModeTransparentScope) != 0)
02082         errorrefcon = 0;
02083         
02084     if (!newprocess (hcode, true, nil, errorrefcon, &hprocess))
02085         goto exit;
02086     
02087     hp = hprocess; /*copy into register*/
02088     
02089     (**hp).processrefcon = (long) hglobals;
02090     
02091     (**hp).errormessagecallback = &osaerrormessage;
02092     
02093     (**hp).hcontext = hcontext;
02094     
02095     #if !flruntime
02096         
02097         (**hp).fldebugging = bitboolean (modeflags & kOSAModeDebug);
02098         
02099         (**hp).processstartedroutine = &osaprocessstarted;
02100         
02101     #endif
02102     
02103     #if !version42orgreater
02104         langcallbacks.backgroundtaskcallback = &osabackgroundtask;
02105         
02106         langcallbacks.debuggercallback = &osadebugger;
02107         
02108         langcallbacks.pushsourcecodecallback = &scriptpushsourcecode;
02109         
02110         langcallbacks.popsourcecodecallback = &scriptpopsourcecode;
02111         
02112         langcallbacks.partialeventloopcallback = &osapartialeventloop;
02113         
02114         fldisableyield = true;
02115     #endif
02116     
02117     osabackgroundtime = gettickcount () + 30;
02118     
02119     origproc = osainstallpatch (hcg);
02120     
02121     #if TARGET_API_MAC_CARBON == 1
02122         LMSetApplZone(homezone);
02123     #else   
02124         SetZone (homezone);
02125     #endif
02126         
02127     if (testheapspace (2 * 1024)) /*enough memory to run a process*/
02128         fl = processruncode (hp, resultval);
02129     else
02130         fl = false;
02131     
02132     osasetclientzone (hcg);
02133     
02134     osaremovepatch (hcg, origproc);
02135     
02136     #if !version42orgreater
02137         langcallbacks.backgroundtaskcallback = (langbooleancallback) &truenoop;
02138         
02139         langcallbacks.debuggercallback = (langtreenodecallback) &truenoop;
02140         
02141         langcallbacks.pushsourcecodecallback = (langsourcecodecallback) &truenoop;
02142         
02143         langcallbacks.popsourcecodecallback = (langvoidcallback) &truenoop;
02144         
02145         langcallbacks.partialeventloopcallback = (langshortcallback) &falsenoop;
02146     #endif
02147     
02148     (**hp).hcode = nil; /*we don't own it*/
02149     
02150     disposeprocess (hp);
02151     
02152     exit:
02153     
02154     return (fl);
02155     } /*osahandlerunscript*/
02156 
02157 
02158 static pascal boolean osacreatemodulecontext (hdltreenode htree, hdlhashtable *hcontext) {
02159     
02160     /*
02161     create a new table containing any module definitions in htree
02162     */
02163     
02164     register hdltreenode h = (**htree).param1;
02165     tyvaluerecord val;
02166     
02167     if (!newhashtable (hcontext))
02168         return (false);
02169     
02170     pushhashtable (*hcontext);
02171     
02172     for (h = (**htree).param1; h != nil; h = (**h).link) {
02173         
02174         if ((**h).nodetype == moduleop)
02175             evaluatetree (h, &val);
02176         }
02177     
02178     pophashtable ();
02179     
02180     return (true);
02181     } /*osacreatemodulecontext*/
02182 
02183 
02184 static boolean lookupeventname (hdlcomponentglobals hglobals, AEEventClass class, AEEventID id, bigstring bsname) {
02185     
02186     /*
02187     try to find the name of the specified event in the 'aete' or 'aeut' resource
02188     
02189     12/7/93, 3.x dmb: use client's context and check its aete.
02190     */
02191     
02192     register hdlcomponentglobals hcg = hglobals;
02193     Handle haete;
02194     boolean flgotname;
02195     long paramoffset;
02196     
02197     osapopfastcontext (hcg);
02198     
02199     haete = GetIndResource ('aete', 1);
02200     
02201     HNoPurge (haete);
02202     
02203     flgotname = osaparseaete (haete, class, id, bsname, &paramoffset);
02204     
02205     HPurge (haete);
02206     
02207     osapushfastcontext (hcg);
02208     
02209     if (!flgotname) {
02210         
02211         haete = GetIndResource ('aeut', 1);
02212         
02213         flgotname = osaparseaete (haete, class, id, bsname, &paramoffset);
02214         }
02215     
02216     return (flgotname);
02217     
02218     /*
02219     h = GetIndResource ('aeut', 1);
02220     
02221     if (osaparseaete (h, class, id, bsname, &paramoffset))
02222         return (true);
02223     
02224     return (false);
02225     */
02226     } /*lookupeventname*/
02227 
02228 
02229 static boolean osahandletrapverb (hdlcomponentglobals hglobals, hdlverbrecord hverb, 
02230     hdltreenode hmodule, long modeflags, tyvaluerecord *vreturned) {
02231     
02232     /*
02233     2.1b3 dmb: added support for subroutine event
02234     
02235     12/7/93 3.x dmb: lookupeventname now takes hglobals
02236     */
02237     
02238     register boolean fl = false;
02239     register hdlverbrecord hv = hverb;
02240     bigstring bsname;
02241     tyvaluerecord val;
02242     hdltreenode hname;
02243     hdltreenode hparams;
02244     hdltreenode hcode;
02245     hdlhashtable hcontext;
02246     AEEventClass class;
02247     AEEventID id;
02248     
02249     if (!osacreatemodulecontext (hmodule, &hcontext))
02250         return (false);
02251     
02252     class = (**hv).verbclass;
02253     
02254     id = (**hv).verbtoken;
02255     
02256     if ((class == kOSASuite) && (id == kASSubroutineEvent)) {
02257         
02258         if (!landgetstringparam (hv, keyASSubroutineName, bsname)) 
02259             return (false);
02260         }
02261     else {
02262         
02263         ostypetostring (id, bsname);
02264         
02265         poptrailingwhitespace (bsname);
02266         }
02267     
02268     if (!hashtablesymbolexists (hcontext, bsname)) {
02269         
02270         // 2/14/97 dmb: what if it's an event intended to be handled by the odb, 
02271         // not by this script itself?
02272         // 2/27/97 dmb: answer - osahandleevent will now dispatch the event to Frontier's 
02273         // main ae handler.
02274         
02275         if (!lookupeventname (hglobals, class, id, bsname) || !hashtablesymbolexists (hcontext, bsname)) {
02276             
02277             if ((class == 'aevt') && (id == 'oapp')) /*no run handler, exec main body*/
02278                 fl = osahandlerunscript (hglobals, hmodule, hcontext, modeflags, vreturned);
02279             else
02280                 oserror (errAEEventNotHandled);
02281             
02282             goto exit;
02283             }
02284         }
02285     
02286     initvalue (&val, stringvaluetype);
02287     
02288     if (!newtexthandle (bsname, &val.data.stringvalue))
02289         goto exit;
02290     
02291     if (!newidnode (val, &hname))
02292         goto exit;
02293     
02294     if (!langipcbuildparamlist (nil, hv, &hparams)) {
02295         
02296         langdisposetree (hname);
02297         
02298         goto exit;
02299         }
02300     
02301     if (!pushbinaryoperation (functionop, hname, hparams, &hcode)) /*consumes input parameters*/
02302         goto exit;
02303     
02304     if (!pushbinaryoperation (moduleop, hcode, nil, &hcode)) /*needs this level*/
02305         goto exit;
02306     
02307     fl = osahandlerunscript (hglobals, hcode, hcontext, modeflags, vreturned);
02308     
02309     langdisposetree (hcode);
02310     
02311     exit:
02312     
02313     disposehashtable (hcontext, true);
02314     
02315     return (fl);
02316     } /*osahandletrapverb*/
02317 
02318 
02319 static boolean osabuildsubroutineevent (bigstring bsname, hdltreenode hparam1, AppleEvent *event) {
02320     
02321     /*
02322     2.1b3 dmb: build an OSA subroutine event given the name and parameter list
02323     
02324     2.1b12 dmb: the guts have been moved into langipcbuildsubroutineevent
02325     */
02326     
02327     AEDesc desc = {typeNull, nil};
02328     register OSErr err;
02329     
02330     err = AECreateAppleEvent (kOSASuite, kASSubroutineEvent, &desc, kAutoGenerateReturnID, kAnyTransactionID, event);
02331     
02332     if (oserror (err))
02333         return (false);
02334     
02335     return (langipcbuildsubroutineevent (event, bsname, hparam1)); /*disposes event on error*/
02336     } /*osabuildsubroutineevent*/
02337 
02338 
02339 static boolean setstorageval (hdlcomponentglobals hglobals, tyvaluerecord *val, OSAID id) {
02340 #pragma unused (hglobals)
02341 
02342     /*
02343     add val to the id table using the indicated id. on error, dispose of the value
02344     */
02345     
02346     bigstring bs;
02347     
02348     numbertostring (id, bs);
02349     
02350     if (hashassign (bs, *val)) {
02351         
02352         exemptfromtmpstack (val);
02353         
02354         return (true);
02355         }
02356     
02357     disposevaluerecord (*val, false);
02358     
02359     return (false);
02360     } /*setstorageval*/
02361 
02362 
02363 static boolean addstorageval (hdlcomponentglobals hglobals, tyvaluerecord *val, OSAID *id) {
02364     
02365     *id = (OSAID) ++(**hglobals).idcounter;
02366     
02367     return (setstorageval (hglobals, val, *id));
02368     } /*addstorageval*/
02369 
02370 
02371 static boolean getstorageval (hdlcomponentglobals hglobals, OSAID id, tyvaluerecord *val, hdlhashnode * hnode) {
02372 #pragma unused (hglobals)
02373 
02374     bigstring bs;
02375     
02376     numbertostring (id, bs);
02377     
02378     if (!hashlookup (bs, val, hnode)) {
02379         
02380         oserror (errOSAInvalidID);
02381         
02382         return (false);
02383         }
02384     
02385     return (true);
02386     } /*getstorageval*/
02387 
02388 
02389 static boolean deletestorageval (hdlcomponentglobals hglobals, OSAID id) {
02390 #pragma unused (hglobals)
02391 
02392     bigstring bs;
02393     
02394     numbertostring (id, bs);
02395     
02396     return (hashdelete (bs, true, false));
02397     } /*deletestorageval*/
02398 
02399 
02400 static boolean storagevaltodesc (tyvaluerecord *val, OSType desctype, AEDesc *result) {
02401     
02402     /*
02403     create a descriptor containing a copy of val's data, coercing 
02404     to the requested type
02405     */
02406     
02407     register tyvaluerecord *v;
02408     AEDesc desc;
02409     tyvaluerecord vtemp;
02410     OSErr err;
02411     tyvaluetype valtype;
02412     
02413     if (!copyvaluerecord (*val, &vtemp))
02414         return (false);
02415     
02416     v = &vtemp; /*copy into register*/
02417     
02418     if (desctype == typeStyledText)
02419         valtype = stringvaluetype;
02420     else
02421         valtype = langgetvaluetype (desctype);
02422     
02423     if (langgoodbinarytype (valtype)) { /*desired type is a valid Frontier type*/
02424         
02425         if (!coercevalue (v, valtype)) { /*apply UserTalk coercion*/
02426             
02427             disposevaluerecord (*v, true);
02428             
02429             return (false);
02430             }
02431         }
02432     
02433     if (!valuetodescriptor (v, &desc))
02434         return (false);
02435     
02436     if ((desctype != typeWildCard) && (desctype != desc.descriptorType)) { /*AE coercion needed*/
02437         
02438         err = AECoerceDesc (&desc, desctype, result);
02439         
02440         AEDisposeDesc (&desc);
02441         
02442         return (!oserror (err));
02443         }
02444     
02445     *result = desc;
02446     
02447     return (true);
02448     } /*storagevaltodesc*/
02449 
02450 
02451 static boolean osagetcontext (hdlcomponentglobals hglobals, OSAID id, hdlhashtable *hcontext) {
02452     
02453     tyvaluerecord vcontext;
02454     hdlhashnode hnode;
02455     
02456     if (id == kOSANullScript)
02457         *hcontext = nil;
02458     
02459     else {
02460         
02461         if (!getstorageval (hglobals, id, &vcontext, &hnode))
02462             return (false);
02463         
02464         if (!langexternalvaltotable (vcontext, hcontext, hnode)) {
02465             
02466             oserror (errOSAInvalidID);
02467             
02468             return (false);
02469             }
02470         }
02471     
02472     return (true);
02473     } /*osagetcontext*/
02474 
02475 
02476 static boolean osanewvalue (tyexternalid id, Handle hdata, tyvaluerecord *val) {
02477     
02478     /*
02479     5.1b23 dmb: langexternalnewvalue does stuff with prefs that may load an odb table 
02480     into memory. we need our own zone.
02481     */
02482     
02483     register THz origzone;
02484     boolean fl;
02485 
02486     #if TARGET_API_MAC_CARBON == 1
02487         origzone = LMGetApplZone();
02488         LMSetApplZone(homezone);
02489     #else
02490         origzone = GetZone ();
02491         SetZone (homezone);
02492     #endif
02493 
02494     fl = langexternalnewvalue (id, hdata, val);
02495     
02496     #if TARGET_API_MAC_CARBON == 1
02497         LMSetApplZone(origzone);
02498     #else   
02499         SetZone (origzone);
02500     #endif
02501         
02502     return (fl);
02503     } /*osanewvalue*/
02504 
02505 
02506 static pascal OSAError
02507 osaLoad (
02508         hdlcomponentglobals  hglobals,
02509         const AEDesc        *scriptData,
02510         long                 modeFlags,
02511         OSAID               *resultingCompiledScriptID)
02512 {
02513 #pragma unused (modeFlags)
02514     
02515     /*
02516     2.1b1 dmb: don't insist that the loaded value is a context (i.e. a table)
02517     */
02518     Handle      descData = nil;
02519     tyvaluerecord vscript, vsource, vcode;
02520     hdlhashtable hcontext;
02521     Handle hsource;
02522     hdltreenode hcode;
02523     Handle hdata;
02524     DescType subtype;
02525     boolean fl = false;
02526     OSAError err = noErr;
02527     hdlhashnode hnodesource;
02528     hdlhashnode hnodecode;
02529     
02530     #if TARGET_API_MAC_CARBON == 1
02531         if (!copydatahandle ((AEDesc*)scriptData, &descData))   /* AE OS X fix */
02532             return (memFullErr);
02533     #else
02534         descData = scriptData->dataHandle;
02535     #endif
02536     
02537     switch (scriptData->descriptorType) {
02538         
02539         case typeLAND:
02540             
02541             fl = langunpackvalue (descData, &vscript);
02542             
02543             break;
02544         
02545         case typeOSAGenericStorage:
02546     
02547             err = OSAGetStorageType ((AEDataStorage) descData, &subtype);
02548             
02549             if (err != noErr)
02550                 break;
02551             
02552             if (subtype != typeLAND) {
02553                 err = errOSABadStorageType;
02554                 break;
02555                 }
02556             
02557             if (!copyhandle (descData, &hdata)) {
02558                 err = memFullErr;
02559                 break;
02560                 }
02561             
02562             #ifdef SWAP_BYTE_ORDER
02563             {
02564                 /*
02565                 2006-04-17 aradke: This is a MAJOR HACK (FIXME)!
02566                 
02567                     Compiled osa scripts of the generic storage type have a 12-byte trailer:
02568                     
02569                     Bytes 0-3 contain the signature of the OSA component that knows
02570                     how to execute the script, e.g. 'LAND' for the UserTalk component.
02571                     The meaning of bytes 4-5 is unknown.
02572                     Bytes 6-7 seem to contain the length of the trailer as a 16bit integer.
02573                     Bytes 8-11 contain 0xFADEDEAD as a magic signature for the trailer.
02574                                 
02575                     Example: 4C41 4E44 0001 000C FADE DEAD (see 'scpt' resource #1024 in iowaruntime.r)
02576                     
02577                     OSARemoveStorageType is supposed to remove this trailer if it is present.
02578                     However, it doesn't perform byte-order swapping on the length bytes in the trailer.
02579                     The call below ends up removing 3072 bytes instead of 12 bytes from hdata.
02580                     
02581                     Until we figure out how to properly deal with this bug(?), we use our own
02582                     implementation of OSARemoveStorageType on Intel Macs.
02583                 */
02584                 
02585                 long hlen = gethandlesize (hdata);
02586                 char * p = *hdata;
02587                 long marker;
02588                 
02589                 p += (hlen - 4);
02590                 
02591                 marker = *(long *) p;
02592                 
02593                 disktomemlong (marker);
02594                 
02595                 if (marker == 0xFADEDEAD) {
02596                     sethandlesize (hdata, hlen - 12);   //remove storage type trailer
02597                     }
02598             }
02599             #else
02600                 err = OSARemoveStorageType ((AEDataStorage) hdata);
02601             #endif
02602             
02603             if (err == noErr)
02604                 fl = langunpackvalue (hdata, &vscript);
02605 
02606             disposehandle (hdata);
02607             
02608             break;
02609         
02610         default:
02611             err = errOSABadStorageType;
02612         }
02613 
02614     #if TARGET_API_MAC_CARBON == 1
02615         if(descData != nil) {
02616 
02617             disposehandle(descData);    /* AE OS X fix */
02618 
02619             descData = nil;
02620             }
02621     #endif
02622     
02623     if (err != noErr)
02624         return (err);
02625     
02626     if (fl) {
02627 
02628         if (langexternalvaltotable (vscript, &hcontext, HNoNode)) {
02629             
02630             pushhashtable (hcontext);
02631             
02632             if (!hashlookup (bscode, &vcode, &hnodecode) && hashlookup (bssource, &vsource, &hnodesource)) { /*no code, but have source*/
02633                 
02634                 fl = copyhandle (vsource.data.stringvalue, &hsource);
02635                 
02636                 if (fl)
02637                     fl = langbuildtree (hsource, false, &hcode); /*syntax error*/
02638                 
02639                 if (fl) {
02640                     
02641                     initvalue (&vcode, codevaluetype);
02642                     
02643                     vcode.data.codevalue = hcode;
02644                     
02645                     fl = hashinsert (bscode, vcode);
02646                     }
02647                 }
02648             
02649             (**hcontext).fldirty = false;
02650             
02651             pophashtable ();
02652             }
02653         }
02654 
02655     if (fl)
02656         fl = addstorageval (hglobals, &vscript, resultingCompiledScriptID);
02657     
02658     if (!fl)
02659         return (osageterror ());
02660     
02661     return (noErr);
02662     } /*osaLoad*/
02663 
02664 
02665 static pascal OSAError osaStore (
02666             hdlcomponentglobals hglobals,
02667             OSAID               compiledScriptID, 
02668             DescType            desiredType,
02669             long                modeFlags,
02670             AEDesc*             resultingScriptData) {
02671     
02672     /*
02673     4/26/93 dmb: support kOSAModePreventGetSource
02674     */
02675     
02676     tyvaluerecord val;
02677     AEDesc desc;
02678     DescType descType;
02679     hdlhashtable hcontext;
02680     hdlhashnode hnode;
02681     boolean flunlinkedsource = false;
02682     boolean fl;
02683     OSAError err;
02684     Handle hpacked = nil;
02685     
02686     
02687     if (compiledScriptID == kOSANullScript)
02688         return (errOSAInvalidID);
02689     
02690     if ((desiredType != typeOSAGenericStorage) && (desiredType != typeLAND))
02691         return (errOSABadStorageType);
02692     
02693     if (!getstorageval (hglobals, compiledScriptID, &val, &hnode))
02694         return (errOSAInvalidID);
02695     
02696     if (langexternalvaltotable (val, &hcontext, hnode)) {
02697         
02698         if (modeFlags & kOSAModePreventGetSource) {
02699             
02700             pushhashtable (hcontext);
02701             
02702             flunlinkedsource = hashunlink (bssource, &hnode);
02703             
02704             pophashtable ();
02705             }
02706         }
02707 
02708     fl = langpackvalue (val, &hpacked, HNoNode);
02709     
02710     if (flunlinkedsource)
02711         hashinsertnode (hnode, hcontext);
02712     
02713     if (!fl)
02714         return (memFullErr);
02715 
02716     if (desiredType == typeOSAGenericStorage) {
02717         
02718         err = OSAAddStorageType ((AEDataStorage) hpacked, typeLAND);
02719 
02720         if (err != noErr) {
02721             disposehandle (hpacked);
02722             return (err);
02723             }
02724         
02725         descType = typeOSAGenericStorage;
02726         }
02727     else {
02728         descType = typeLAND;
02729         }
02730 
02731     #if TARGET_API_MAC_CARBON == 1
02732         fl = newdescwithhandle (&desc, descType, hpacked);
02733     #else
02734         desc.dataHandle = hpacked;
02735         desc.descriptorType = descType;
02736     #endif
02737 
02738     *resultingScriptData = desc;
02739     
02740     return (noErr);
02741     } /*osaStore*/
02742 
02743 
02744 pascal OSAError osaDispose (
02745             hdlcomponentglobals hglobals,
02746             OSAID               scriptID) {
02747     
02748     boolean fl;
02749     
02750     fl = deletestorageval (hglobals, scriptID);
02751     
02752     if (!fl)
02753         return (osageterror ());
02754     
02755     return (noErr);
02756     } /*osaDispose*/
02757 
02758 
02759 static pascal OSAError osaSetScriptInfo (
02760             hdlcomponentglobals hglobals,
02761             OSAID               scriptID,
02762             OSType              selector,
02763             long                value) {
02764     
02765     tyvaluerecord vscript;
02766     hdlhashtable hcontext;
02767     hdlhashnode hnode;
02768     
02769     if (scriptID == kOSANullScript)
02770         return (errOSAInvalidID);
02771     
02772     if (!getstorageval (hglobals, scriptID, &vscript, &hnode))
02773         return (errOSAInvalidID);
02774     
02775     switch (selector) {
02776         
02777         case kOSAScriptIsModified:
02778             if (!langexternalvaltotable (vscript, &hcontext, hnode))
02779                 return (errOSABadSelector);
02780             
02781             (**hcontext).fldirty = value != 0;
02782             
02783             break;
02784         
02785         default:
02786             return (errOSABadSelector);
02787         }
02788     
02789     return (noErr);
02790     } /*osaSetScriptInfo*/
02791 
02792 
02793 static pascal OSAError osaGetScriptInfo (
02794             hdlcomponentglobals hglobals,
02795             OSAID               scriptID,
02796             OSType              selector,
02797             long*               result) {
02798     
02799     /*
02800     2.1b4 dmb: added code for kOSACanGetSource and kASHasOpenHandler selectors
02801     */
02802     
02803     register hdlcomponentglobals hcg = hglobals;
02804     tyvaluerecord vscript;
02805     tyvaluerecord vcode;
02806     hdlhashtable hcontext;
02807     hdlhashnode hnode;
02808     
02809     *result = 0;
02810     
02811     if (scriptID == kOSANullScript)
02812         return (errOSAInvalidID);
02813     
02814     if (!getstorageval (hcg, scriptID, &vscript, &hnode))
02815         return (errOSAInvalidID);
02816     
02817     if (!langexternalvaltotable (vscript, &hcontext, hnode))
02818         hcontext = nil;
02819     
02820     switch (selector) {
02821         
02822         case kOSAScriptIsModified:
02823             if (hcontext == nil)
02824                 return (errOSABadSelector);
02825             
02826             *result = (**hcontext).fldirty;
02827             
02828             break;
02829         
02830         case kOSAScriptIsBeingEdited:
02831             break;
02832         
02833         case kOSAScriptIsBeingRecorded:
02834             *result = (**hcg).recordingstate.flrecording;
02835             
02836             break;
02837         
02838         case kOSAScriptIsTypeCompiledScript:
02839             break;
02840         
02841         case kOSAScriptIsTypeScriptValue:
02842             *result = hcontext == nil;
02843             
02844             break;
02845         
02846         case kOSAScriptIsTypeScriptContext:
02847             *result = hcontext != nil;
02848             
02849             break;
02850         
02851         case kOSAScriptBestType:
02852             *result = typeChar;
02853             
02854             break;
02855         
02856         case kOSACanGetSource:
02857             if (hcontext == nil)
02858                 return (errOSABadSelector);
02859             
02860             *result = hashtablesymbolexists (hcontext, bssource);
02861             
02862             break;
02863         
02864         case kASHasOpenHandler:
02865             if (hcontext == nil)
02866                 return (errOSABadSelector);
02867             
02868             if (!hashtablelookup (hcontext, bscode, &vcode, &hnode))
02869                 break;
02870             
02871             if (!osacreatemodulecontext (vcode.data.codevalue, &hcontext))
02872                 return (false);
02873             
02874             *result = hashtablesymbolexists (hcontext, "\podoc") || hashtablesymbolexists (hcontext, "\popen");
02875             
02876             disposehashtable (hcontext, true);
02877             
02878             break;
02879         
02880         default:
02881             return (errOSABadSelector);
02882         }
02883     
02884     return (noErr);
02885     } /*osaGetScriptInfo*/
02886 
02887 
02888 static pascal OSAError osaSetResumeDispatchProc (
02889                 hdlcomponentglobals hglobals,
02890                 AEEventHandlerUPP   resumeDispatchProc,
02891                 long                refCon) {
02892     
02893     /*
02894     2.1b4 dmb: we're not currently using the dispatch proc. UserTalk doesn't 
02895     have an equivalent to AS's "continue" statement, but a script can do 
02896     a scripterror (-1708) to get exectueevent to return that error, in which 
02897     case the client should invoke its own dispatch proc
02898     
02899     in any case, we're keeping track of what the caller sets up, just for 
02900     the hell of it.
02901     */
02902     
02903     register hdlcomponentglobals hcg = hglobals;
02904     
02905     (**hcg).resumedispatchproc = resumeDispatchProc;
02906     
02907     (**hcg).resumedispatchprocrefcon = refCon;
02908     
02909     return (noErr);
02910     } /*osaSetResumeDispatchProc*/
02911 
02912 
02913 static pascal OSAError
02914 osaGetResumeDispatchProc (
02915         hdlcomponentglobals  hglobals,
02916         AEEventHandlerUPP   *resumeDispatchProc,
02917         long                *refCon)
02918 {
02919 #pragma unused (hglobals)
02920     /*
02921     we're never invoking the resumedispatch proc, so be honest and 
02922     return these constants
02923     */
02924     
02925     *resumeDispatchProc = (AEEventHandlerUPP) kOSANoDispatch;
02926     
02927     *refCon = 0;
02928     
02929     return (noErr);
02930     } /*osaGetResumeDispatchProc*/
02931 
02932 
02933 pascal OSAError osaSetActiveProc (
02934             hdlcomponentglobals hglobals,
02935             OSAActiveUPP        activeProc,
02936             long                refCon) {
02937     
02938     register hdlcomponentglobals hcg = hglobals;
02939     
02940     (**hcg).activeproc = activeProc;
02941     
02942     (**hcg).activeprocrefcon = refCon;
02943     
02944     return (noErr);
02945     } /*osaSetActiveProc*/
02946 
02947 
02948 pascal OSAError osaSetSendProc (
02949             hdlcomponentglobals hglobals,
02950             OSASendUPP          sendProc,
02951             long                refCon) {
02952     
02953     register hdlcomponentglobals hcg = hglobals;
02954     
02955     if (sendProc == nil)
02956         sendProc = osadefaultsendUPP;
02957     
02958     (**hcg).sendproc = sendProc;
02959     
02960     (**hcg).sendprocrefcon = refCon;
02961     
02962     return (noErr);
02963     } /*osaSetSendProc*/
02964 
02965 
02966 static pascal OSAError osaGetSendProc (
02967             hdlcomponentglobals hglobals,
02968             OSASendUPP*         sendProc,
02969             long*               refCon) {
02970     
02971     register hdlcomponentglobals hcg = hglobals;
02972     
02973     *sendProc = (**hcg).sendproc;
02974     
02975     *refCon = (**hcg).sendprocrefcon;
02976     
02977     return (noErr);
02978     } /*osaGetSendProc*/
02979 
02980 
02981 static pascal OSAError osaSetCreateProc (
02982             hdlcomponentglobals         hglobals,
02983             OSACreateAppleEventUPP      createProc,
02984             long                        refCon) {
02985     
02986     register hdlcomponentglobals hcg = hglobals;
02987     
02988     if (createProc == nil)
02989         createProc = osadefaultcreateUPP;
02990     
02991     (**hcg).createproc = createProc;
02992     
02993     (**hcg).createprocrefcon = refCon;
02994     
02995     return (noErr);
02996     } /*osaSetCreateProc*/
02997 
02998 
02999 static pascal OSAError osaGetCreateProc (
03000             hdlcomponentglobals         hglobals,
03001             OSACreateAppleEventUPP*     createProc,
03002             long*                       refCon) {
03003     
03004     register hdlcomponentglobals hcg = hglobals;
03005     
03006     *createProc = (**hcg).createproc;
03007     
03008     *refCon = (**hcg).createprocrefcon;
03009     
03010     return (noErr);
03011     } /*osaGetCreateProc*/
03012 
03013 
03014 #if 0
03015 
03016     static pascal OSAError osaSetDefaultTarget (
03017                 hdlcomponentglobals hglobals,
03018                 const AEAddressDesc*    target) {
03019         
03020         return (noErr);
03021         } /*osaSetDefaultTarget*/
03022 
03023 #endif
03024 
03025 
03026 static pascal OSAError osacompiledesc (
03027             const AEDesc*       sourceData,
03028             tyvaluerecord*      vcode) {
03029     
03030     Handle htext;
03031     hdltreenode hcode;
03032     
03033     if ((*sourceData).descriptorType != typeChar)
03034         return (errAECoercionFail);
03035     
03036     #if TARGET_API_MAC_CARBON == 1 /*PBS 03/14/02: AE OS X fix.*/
03037     
03038         if (!copydatahandle ((AEDesc*) sourceData, &htext)) /*don't let langbuildtree consume caller's text*/
03039             return (memFullErr);
03040     
03041     #else
03042     
03043         if (!copyhandle ((*sourceData).dataHandle, &htext)) /*don't let langbuildtree consume caller's text*/
03044             return (memFullErr);
03045         
03046     #endif
03047     
03048     if (!langbuildtree (htext, false, &hcode)) /*syntax error*/
03049         return (osageterror ());
03050     
03051     initvalue (vcode, codevaluetype);
03052     
03053     (*vcode).data.codevalue = hcode;
03054     
03055     return (noErr);
03056     } /*osacompiledesc*/
03057 
03058 
03059 static pascal OSAError osaCompile (
03060             hdlcomponentglobals hglobals,
03061             const AEDesc*       sourceData,
03062             long                modeFlags,
03063             OSAID*              scriptID) {
03064     
03065     /*
03066     first cut: handle contexts, but not augmenting contexts.
03067     */
03068     
03069     OSAError err = noErr;
03070     hdlhashtable hcontext;
03071     tyvaluerecord vcode, vcontext, vsource;
03072     OSAID id = *scriptID;
03073     Handle htext;
03074     boolean flkeepsource = false;
03075     hdlhashnode hnode;
03076     
03077     err = osacompiledesc (sourceData, &vcode);
03078     
03079     if (err != noErr)
03080         return (err);
03081     
03082     if ((modeFlags & kOSAModePreventGetSource) == 0) { /*bit not set*/
03083         
03084         flkeepsource = true;
03085         
03086         #if TARGET_API_MAC_CARBON == 1 /*PBS 03/14/02: AE OS X fix.*/
03087             
03088             if (!copydatahandle ((AEDesc*) sourceData, &htext)) /*make copy for eventual getsource*/
03089                 return (memFullErr);        
03090         
03091         #else
03092         
03093             if (!copyhandle ((*sourceData).dataHandle, &htext)) /*make copy for eventual getsource*/
03094                 return (memFullErr);
03095         
03096         #endif
03097         
03098         if (!setheapvalue (htext, stringvaluetype, &vsource))
03099             return (osageterror ());
03100         }
03101     
03102     if (id == kOSANullScript) {
03103         
03104         if (!osanewvalue (idtableprocessor, nil, &vcontext))
03105             return (memFullErr);
03106         
03107         langexternalvaltotable (vcontext, &hcontext, HNoNode);
03108         }
03109     else {
03110         
03111         if (!getstorageval (hglobals, id, &vcontext, &hnode))
03112             return (osageterror ());
03113         
03114         if (!langexternalvaltotable (vcontext, &hcontext, hnode))
03115             return (errOSAInvalidID);
03116         }
03117     
03118     pushhashtable (hcontext);
03119     
03120     hashassign (bscode, vcode);
03121     
03122     if (flkeepsource)
03123         flkeepsource = hashassign (bssource, vsource);
03124     
03125     pophashtable ();
03126     
03127     if (flkeepsource)
03128         exemptfromtmpstack (&vsource);
03129     
03130     if (id == kOSANullScript) {
03131         
03132         if (!addstorageval (hglobals, &vcontext, scriptID))
03133             return (osageterror ());
03134         }
03135     
03136     return (noErr);
03137     } /*osaCompile*/
03138 
03139 
03140 static pascal OSAError osaGetSource (
03141             hdlcomponentglobals hglobals,
03142             OSAID               scriptID,
03143             DescType            desiredType,
03144             AEDesc*             resultingSourceData) {
03145     
03146     tyvaluerecord val;
03147     hdlhashtable hcontext;
03148     hdlhashnode hnode;
03149     
03150     if (scriptID == kOSANullScript)
03151         return (errOSAInvalidID);
03152     
03153     if (!osagetcontext (hglobals, scriptID, &hcontext))
03154         goto error;
03155     
03156     if (!hashtablelookup (hcontext, bssource, &val, &hnode))
03157         return (errOSASourceNotAvailable);
03158     
03159     if (val.valuetype == novaluetype)
03160         return (errOSASourceNotAvailable);
03161     
03162     if (!storagevaltodesc (&val, desiredType, resultingSourceData))
03163         goto error;
03164     
03165     return (noErr);
03166     
03167     error:
03168         return (osageterror ());
03169     
03170     } /*osaGetSource*/
03171 
03172 
03173 static pascal OSAError
03174 osaCoerceFromDesc (
03175         hdlcomponentglobals  hglobals,
03176         const AEDesc        *scriptData,
03177         long                 modeFlags,
03178         OSAID               *resultingScriptID)
03179 {
03180 #pragma unused (modeFlags)
03181     
03182     /*
03183     3.0a dmb: fixed leak when val is an externalvaluetype.
03184     */
03185     
03186     AEDesc desc;
03187     tyvaluerecord val;
03188     register boolean fl;
03189     
03190     if (oserror (AEDuplicateDesc (scriptData, &desc)))
03191         goto error;
03192     
03193     if (langgetvaluetype (desc.descriptorType) >= outlinevaluetype) {
03194         
03195         val.valuetype = externalvaluetype;
03196         
03197         #if TARGET_API_MAC_CARBON == 1 /*PBS 03/14/02: AE OS X fix.*/
03198             {
03199             Handle h;
03200             
03201             copydatahandle (&desc, &h);
03202             
03203             fl = langexternalmemoryunpack (h, (hdlexternalhandle *) &val.data.externalvalue);
03204             
03205             disposehandle (h);
03206             }
03207         #else
03208             fl = langexternalmemoryunpack (desc.dataHandle, (hdlexternalhandle *) &val.data.externalvalue);
03209         #endif
03210         
03211         AEDisposeDesc (&desc);
03212         
03213         if (!fl)
03214             goto error;
03215         }
03216     else {
03217         
03218         if (!setdescriptorvalue (desc, &val))
03219             goto error;
03220         }
03221     
03222     if (!addstorageval (hglobals, &val, resultingScriptID))
03223         goto error;
03224     
03225     return (noErr);
03226     
03227     error:
03228         return (osageterror ());
03229     } /*osaCoerceFromDesc*/
03230 
03231 
03232 static pascal OSAError
03233 osaCoerceToDesc (
03234         hdlcomponentglobals  hglobals,
03235         OSAID                scriptID,
03236         DescType             desiredType,
03237         long                 modeFlags,
03238         AEDesc              *result)
03239 {
03240 #pragma unused (modeFlags)
03241     
03242     tyvaluerecord val;
03243     hdlhashnode hnode;
03244     
03245     if (scriptID == kOSANullScript)
03246         return (errOSAInvalidID);
03247     
03248     if (!getstorageval (hglobals, scriptID, &val, &hnode))
03249         goto error;
03250     
03251     if (!storagevaltodesc (&val, desiredType, result))
03252         goto error;
03253     
03254     return (noErr);
03255     
03256     error:
03257         return (osageterror ());
03258     
03259     } /*osaCoerceToDesc*/
03260 
03261 
03262 static Handle getcomponentstringhandle (short id) {
03263     
03264     /*
03265     2.1b11 dmb: return a handle to the specified string in the component 
03266     string list
03267     */
03268     
03269     bigstring bs;
03270     
03271     if (!getstringlist (componentlistnumber, id, bs))
03272         return (nil);
03273     
03274     return ((Handle) NewString (bs));
03275     } /*getcomponentstringhandle*/
03276 
03277 
03278 static boolean getrecordingstring (short id, bigstring bs) {
03279     
03280     return (getstringlist (recordinglistnumber, id, bs));
03281     } /*getrecordingstring*/
03282 
03283 
03284 static pascal OSErr
03285 coerceInsltoTEXT (
03286         const AEDesc    *desc,
03287         DescType         totype,
03288         long             refcon,
03289         AEDesc          *result)
03290 {
03291 #pragma unused(totype, refcon)
03292 
03293     /*
03294     2.1b2 dmb: this is installed as a typeInsertionLoc -> typeObjectSpecifier 
03295     coercer, but it's actually generating source text for the recorder.  it's 
03296     not installed as a -> typeChar coercer to prevent getobjectmodeldisplaystring 
03297     from putting quotes around the result
03298     
03299     2.1b3 dmb: if position isn't one of the standard 5, call insertionLoc
03300     */
03301     
03302     AEDesc rec;
03303     AEDesc obj;
03304     byte bsverb [16];
03305     byte bspos [64];
03306     bigstring bsobj;
03307     bigstring bs;
03308     tyvaluerecord val;
03309     short ix;
03310     OSErr err;
03311     DescType type;
03312     long size;
03313     OSType pos;
03314     
03315     #if !TARGET_API_MAC_CARBON
03316         long curA5 = SetUpAppA5 ();
03317     #endif
03318     
03319     #ifdef fldebug // 2006-04-04 - kw --- this was fldegug
03320     
03321     if ((*desc).descriptorType != typeInsertionLoc)
03322         DebugStr ("\punexpected coercion");
03323     
03324     #endif
03325     
03326     err = AECoerceDesc (desc, typeAERecord, &rec);
03327     
03328     if (err != noErr)
03329         goto exit;
03330     
03331     err = AEGetKeyPtr (&rec, keyAEPosition, typeEnumeration, &type, &pos, sizeof (pos), &size);
03332     
03333     if (err == noErr)
03334         err = AEGetKeyDesc (&rec, keyAEObject, typeWildCard, &obj);
03335     
03336     AEDisposeDesc (&rec);
03337     
03338     if (err != noErr)
03339         goto exit;
03340     
03341     setemptystring (bspos);
03342     
03343     switch (pos) {
03344         
03345         case kAEBefore:
03346             ix = beforestring; break;
03347         
03348         case kAEBeginning:
03349             ix = beginningofstring; break;
03350         
03351         case kAEAfter:
03352             ix = afterstring; break;
03353         
03354         case kAEEnd:
03355             ix = endofstring; break;
03356         
03357         case kAEReplace:
03358             ix = replacingstring; break;
03359         
03360         default:
03361             ix = insertionlocstring;
03362             
03363             setostypevalue (pos, &val);
03364             
03365             getobjectmodeldisplaystring (&val, bspos);
03366             
03367             pushstring ("\p, ", bspos);
03368             
03369             break;
03370         }
03371     
03372     getrecordingstring (ix, bsverb);
03373     
03374     setdescriptorvalue (obj, &val);
03375     
03376     getobjectmodeldisplaystring (&val, bsobj);
03377     
03378     disposevaluerecord (val, true);
03379     
03380     if (isemptystring (bsobj))
03381         copystring ("\p\"\"", bsobj);
03382     
03383     parsedialogstring ("\p^0 (^1^2)", bsverb, bspos, bsobj, nil, bs);
03384     
03385     err = AECreateDesc (typeChar, bs + 1, stringlength (bs), result);
03386     
03387 exit:
03388     
03389     #if !TARGET_API_MAC_CARBON
03390         RestoreA5(curA5);
03391     #endif
03392     
03393     return (err);
03394     } /*coerceInsltoTEXT*/
03395 
03396 
03397 //Code change by Timothy Paustian Friday, June 16, 2000 1:38:13 PM
03398 //Changed to Opaque call for Carbon
03399 #if TARGET_RT_MAC_CFM || TARGET_RT_MAC_MACHO
03400 
03401     #if TARGET_API_MAC_CARBON
03402 
03403         AECoercionHandlerUPP    coerceInsltoTEXTDesc;
03404         
03405         #define coerceInsltoTEXTUPP (coerceInsltoTEXTDesc);
03406 
03407     #else
03408 
03409         static RoutineDescriptor coerceInsltoTEXTDesc = BUILD_ROUTINE_DESCRIPTOR (uppAECoerceDescProcInfo, coerceInsltoTEXT);
03410     
03411         #define coerceInsltoTEXTUPP ((AECoercionHandlerUPP) &coerceInsltoTEXTDesc)
03412 
03413     #endif
03414 
03415 #else
03416 
03417     #define coerceInsltoTEXTUPP ((AECoercionHandlerUPP) &coerceInsltoTEXT)
03418     
03419 #endif
03420 
03421 
03422 static pascal OSErr
03423 sendrecordingevent (
03424         hdlcomponentglobals      hglobals,
03425         AEEventID                id)
03426 {
03427 #pragma unused (hglobals)
03428     
03429     /*
03430     2.1b5 dmb: don't use sendproc for these events; they're not part of 
03431     script execution
03432     */
03433     
03434     AEDesc desc;
03435     OSErr err;
03436     ProcessSerialNumber psn;
03437     AppleEvent event, reply;
03438     
03439     GetCurrentProcess (&psn);
03440     
03441     err = AECreateDesc (typeProcessSerialNumber, (Ptr) &psn, sizeof (psn), &desc);
03442     
03443     if (err == noErr) {
03444         
03445         err = AECreateAppleEvent (kCoreEventClass, id, &desc, kAutoGenerateReturnID, kAnyTransactionID, &event);
03446         
03447         AEDisposeDesc (&desc);
03448         
03449         if (err == noErr) {
03450             
03451             err = AESend (&event, &reply, 
03452                 
03453                 (AESendMode) kAEDontRecord + kAECanSwitchLayer + kAECanInteract + kAENoReply, 
03454                 
03455                 (AESendPriority) kAENormalPriority, (long) kAEDefaultTimeout, nil, nil);
03456             
03457             AEDisposeDesc (&event);
03458             
03459             AEDisposeDesc (&reply);
03460             }
03461         }
03462     
03463     return (err);
03464     } /*sendrecordingevent*/
03465 
03466 
03467 static pascal OSErr sendrecordedtextevent (hdlcomponentglobals hcg, bigstring bs) {
03468     
03469     /* 
03470     2.1b5 dmb: maintain our own version of the recorded text
03471     
03472     2.1b8 dmb: set up currentA5 and client context for AESend call.
03473     it will be short-circuited to client app, so it's like a callback.
03474     */
03475     
03476     AppleEvent event, reply;
03477     AEDesc desc;
03478     ProcessSerialNumber psn;
03479     OSErr err;
03480     Handle htext;
03481     bigstring bssend;
03482     THz origzone;
03483     
03484     copystring (bs, bssend);
03485     
03486     pushchar (chreturn, bssend);
03487     
03488     htext = (**hcg).recordingstate.hrecordedtext;
03489     
03490     if (htext != nil)
03491         pushtexthandle (bssend, htext); /*if this fails, so will subsequent calls*/
03492     
03493     psn.highLongOfPSN = 0;
03494     
03495     psn.lowLongOfPSN = kCurrentProcess;
03496     
03497     err = AECreateDesc (typeProcessSerialNumber, (Ptr) &psn, sizeof (psn), &desc);
03498     
03499     if (err == noErr) {
03500         
03501         err = AECreateAppleEvent (kOSASuite, kOSARecordedText, &desc, kAutoGenerateReturnID, kAnyTransactionID, &event);
03502         
03503         AEDisposeDesc (&desc);
03504         
03505         if (err == noErr) {
03506             
03507             long appA5;
03508             
03509             err = AEPutParamPtr (&event, keyDirectObject, typeChar, (Ptr) bssend + 1, stringlength (bssend));
03510             
03511             osapreclientcallback (hcg);
03512             
03513             origzone = osasetclientzone (hcg);
03514             
03515             appA5 = SetUpCurA5 ();
03516             
03517             err = AESend (&event, &reply, 
03518                 
03519                 (AESendMode) kAENoReply + kAEDontRecord, 
03520                 
03521                 (AESendPriority) kAENormalPriority, (long) kNoTimeOut, nil, nil);
03522             
03523             RestoreA5 (appA5);
03524             
03525             #if TARGET_API_MAC_CARBON == 1
03526             LMSetApplZone(origzone);
03527             #else   
03528             SetZone (origzone);
03529             #endif      
03530             osapostclientcallback (hcg);
03531             
03532             AEDisposeDesc (&event);
03533             
03534             AEDisposeDesc (&reply);
03535             }
03536         }
03537     
03538     return (err);
03539     } /*sendrecordedtextevent*/
03540 
03541 
03542 #if 0   //not used
03543 
03544 static pascal OSErr targettoprocessinfo (const TargetID *target, ProcessSerialNumber *psn, FSSpec *fs, OSType *signature) {
03545 
03546     #if !TARGET_API_MAC_CARBON
03547 
03548         ProcessInfoRec info;
03549         OSErr ec;
03550         
03551         if ((*target).location.locationKindSelector == ppcNoLocation) { /*local program*/
03552             
03553             ec = GetProcessSerialNumberFromPortName ((PPCPortPtr) &(*target).name, psn);
03554             
03555             if (ec != noErr)
03556                 return (ec);
03557             
03558             info.processInfoLength = (long) sizeof (info);
03559             
03560             info.processName = nil;
03561             
03562             info.processAppSpec = fs;
03563             
03564             ec = GetProcessInformation (psn, &info);
03565             
03566             if (ec != noErr)
03567                 return (ec);
03568             
03569             *signature = info.processSignature;
03570             }
03571         else
03572 
03573     #endif
03574          { /*not a local program*/
03575         
03576         clearbytes (psn, sizeof (ProcessSerialNumber));
03577         
03578         clearbytes (fs, sizeof (FSSpec)); /*null*/
03579         
03580         if ((*target).name.portKindSelector == ppcByCreatorAndType)
03581             *signature = (*target).name.u.port.portCreator;
03582         else
03583             BlockMove ((Ptr) (*target).name.u.portTypeStr + 1, (Ptr) signature, 4L); /*kloooge*/
03584         }
03585 
03586     return (noErr);
03587     } /*targettoprocessinfo*/
03588 
03589 #endif
03590 
03591 
03592 static pascal OSErr pusheventparameter (const AppleEvent *event, AEKeyword key, boolean flpushkey, bigstring bsparam, bigstring bsevent) {
03593     
03594     AEDesc desc;
03595     tyvaluerecord val;
03596     bigstring bsval;
03597     byte bskey [6];
03598     byte bscoerce [16];
03599     OSErr err;
03600     
03601     err = AEGetParamDesc (event, key, typeWildCard, &desc);
03602     
03603     if (err != noErr)
03604         return (err);
03605     
03606     if (!setdescriptorvalue (desc, &val))
03607         return (getoserror ());
03608     
03609     getobjectmodeldisplaystring (&val, bsval);
03610     
03611     switch (val.valuetype) { /*see if coercion is needed*/
03612         
03613         case filespecvaluetype:
03614         case aliasvaluetype:
03615             langgettypestring (val.valuetype, bscoerce);
03616             
03617             pushstring ("\p (", bscoerce);
03618             
03619             insertstring (bscoerce, bsval);
03620             
03621             pushchar (')', bsval);
03622             
03623             break;
03624         
03625         default:
03626             break;
03627         }
03628     
03629     disposevaluerecord (val, false);
03630     
03631     if (flpushkey) {
03632         
03633         pushstring ("\p, '", bsevent);
03634         
03635         ostypetostring (key, bskey);
03636         
03637         pushstring (bskey, bsevent);
03638         
03639         pushchar ('\'', bsevent);
03640         }
03641     
03642     if (bsevent [stringlength (bsevent)] != '(') /*not 1st item in param list*/
03643         pushstring ("\p, ", bsevent);
03644     
03645     if (!isemptystring (bsparam)) {
03646         
03647         pushstring (bsparam, bsevent);
03648         
03649         pushstring ("\p: ", bsevent);
03650         }
03651     
03652     pushstring (bsval, bsevent);
03653     
03654     return (noErr);
03655 } /*pusheventparameter*/
03656 
03657 
03658 static pascal OSErr
03659 handlerecordableevent (
03660         const AppleEvent    *event,
03661         AppleEvent          *reply,
03662         SInt32               refcon)
03663 {
03664 #pragma unused (reply)
03665 
03666     /*
03667     map the event to a line of source code, and send the text in a Recorded Text event
03668     
03669     2.1b2: added bringToFront calls
03670     
03671     2.1b3: include braces & semicolons; script.c will strip them out
03672     
03673     2.1b4: added ugly special case for comment events. if there's no glue for an 
03674     event, use appleevent verb. allocate 8 bytes for ostype strings to leave room 
03675     for single quotes and length.
03676     
03677     2.1b5: do everything in our heap. finding the app table is especially important; 
03678     we don't want our tables loaded in another heap. the same probably applies to any 
03679     resource strings that might be used, though we can probably preload what we need 
03680     if we want to keep the clients heap set for same reason.
03681     
03682     2.1b7: ignore errors from sendrecordedtextevent. we want to continue 
03683     accumulating our version of the text no matter what.
03684     
03685     3.0b15 dmb: call SetResLoad (false) before opening recorded app's resource 
03686     fork, or all of its preload resources will be loaded into our heap
03687     
03688     3.0a dmb: if no app table is found, add comment to that effect to "with" 
03689     statement.
03690     */
03691     
03692     register hdlcomponentglobals hcg = (hdlcomponentglobals) refcon;
03693     AEDesc desc;
03694     OSErr err;
03695     AEEventClass class;
03696     AEEventID id;
03697     DescType type;
03698     long size;
03699     boolean flgotname;
03700     long paramoffset;
03701     bigstring bs;
03702     bigstring bsname;
03703     bigstring bsparam;
03704     byte bssignature [8];
03705     byte bsclass [8];
03706     byte bsid [8];
03707     hdlhashtable happtable = nil;
03708     FSSpec fs;
03709     OSType signature;
03710     ProcessSerialNumber psn;
03711     boolean flisfront;
03712     boolean flpushkeys;
03713     boolean flignore;
03714     short rnum = 0;
03715     Handle haete = nil;
03716     
03717     #if !TARGET_API_MAC_CARBON
03718         long curA5 = SetUpAppA5 ();
03719     #endif
03720     
03721     err = landsystem7getsenderinfo (event, &psn, &fs, &signature);
03722     
03723     if (err != noErr) {
03724         
03725         #if !TARGET_API_MAC_CARBON
03726             RestoreA5 (curA5);
03727         #endif
03728         
03729         return (err);
03730         }
03731     
03732     osapushfastcontext (hcg);
03733     
03734     #if TARGET_API_MAC_CARBON   
03735         coerceInsltoTEXTDesc = NewAECoerceDescUPP(coerceInsltoTEXT);
03736         AEInstallCoercionHandler (typeInsertionLoc, typeObjectSpecifier, coerceInsltoTEXTDesc, 0, true, false);
03737     #else
03738         AEInstallCoercionHandler (typeInsertionLoc, typeObjectSpecifier, coerceInsltoTEXTUPP, 0, true, false);
03739     #endif
03740     
03741     #if TARGET_API_MAC_CARBON == 1
03742         LMSetApplZone(homezone);
03743     #else   
03744         SetZone (homezone);
03745     #endif
03746 
03747     if (signature == (**hcg).recordingstate.lastappid)
03748         happtable = (**hcg).recordingstate.lastapptable;
03749     
03750     else {
03751         
03752         if ((**hcg).recordingstate.lastappid != 0) {
03753             
03754             sendrecordedtextevent (hcg, "\p\t};");
03755             
03756             /*
03757             if (err != noErr)
03758                 goto exit;
03759             */
03760             }
03761         
03762         flisfront = isfrontapplication (psn);
03763         
03764         getrecordingstring (withobjectmodelstring, bs);
03765         
03766         if (langipcfindapptable (signature, false, &happtable, bsname)) {
03767             
03768             pushstring ("\p, ", bs);
03769             
03770             pushstring (bsname, bs);
03771             
03772             pushstring ("\p {", bs);
03773             
03774             sendrecordedtextevent (hcg, bs);
03775             
03776             /*
03777             if (err != noErr)
03778                 goto exit;
03779             */
03780             
03781             if (flisfront) {
03782                 
03783                 getrecordingstring (bringtofrontstring, bs);
03784                 
03785                 sendrecordedtextevent (hcg, bs);
03786                 
03787                 /*
03788                 if (err != noErr)
03789                     goto exit;
03790                 */
03791                 }
03792             }
03793         else {
03794             
03795             pushstring ("\p { ", bs);
03796             
03797             getrecordingstring (noverbtablestring, bsparam); /*3.0a*/
03798             
03799             getprocessname (psn, bsname, &flignore);
03800             
03801             parsedialogstring (bsparam, bsname, nil, nil, nil, bsparam);
03802             
03803             pushstring (bsparam, bs);
03804             
03805             sendrecordedtextevent (hcg, bs);
03806             
03807             /*
03808             if (err != noErr)
03809                 goto exit;
03810             */
03811             
03812             if (flisfront) {
03813                 
03814                 ostypetostring (signature, bssignature);
03815                 
03816                 getrecordingstring (sysbringapptofrontstring, bs);
03817                 
03818                 parsedialogstring (bs, bssignature, nil, nil, nil, bs);
03819                 
03820                 sendrecordedtextevent (hcg, bs);
03821                 }
03822             }
03823         
03824         (**hcg).recordingstate.lastappid = signature;
03825         
03826         (**hcg).recordingstate.lastapptable = happtable;
03827         }
03828     
03829     err = AEGetAttributePtr (event, keyEventClassAttr, typeType, &type, &class, sizeof (class), &size);
03830     
03831     if (err == noErr)
03832         err = AEGetAttributePtr (event, keyEventIDAttr, typeType, &type, &id, sizeof (id), &size);
03833     
03834     if (err != noErr)
03835         goto exit;
03836     
03837     setemptystring (bsparam);
03838     
03839     flgotname = false;
03840     
03841     if ((class == kASAppleScriptSuite) && (id == kASCommentEvent)) { /*special case*/
03842         
03843         setemptystring (bs);
03844         
03845         err = AEGetParamDesc (event, keyDirectObject, typeChar, &desc);
03846         
03847         if (err == noErr) {
03848             
03849             #if TARGET_API_MAC_CARBON == 1 /*PBS 03/14/02: AE OS X fix.*/
03850                 datahandletostring (&desc, bs);
03851             #else
03852                 texthandletostring (desc.dataHandle, bs);
03853             #endif
03854             
03855             insertstring ("\p\tĒ", bs);
03856             
03857             AEDisposeDesc (&desc);
03858             }
03859         
03860         goto exit;
03861         }
03862     
03863     if (happtable != nil) {
03864         
03865         pushhashtable (happtable);
03866         
03867         SetResLoad (false);
03868         
03869         rnum = FSpOpenResFile (&fs, fsRdPerm);
03870         
03871         SetResLoad (true);
03872         
03873         if (rnum != -1) {
03874             
03875             haete = Get1IndResource ('aete', 1);
03876             
03877             flgotname = osaparseaete (haete, class, id, bsname, &paramoffset);
03878             }
03879         
03880         if (!flgotname) {
03881             
03882             haete = GetIndResource ('aeut', 1);
03883             
03884             flgotname = osaparseaete (haete, class, id, bsname, &paramoffset);
03885             }
03886         
03887         if (flgotname && !hashsymbolexists (bsname)) /*event doesn't have verb glue*/
03888             flgotname = false;
03889         }
03890     
03891     if (flgotname) {
03892         
03893         parsedialogstring ("\p\t^0 (", bsname, nil, nil, nil, bs);
03894         
03895         flpushkeys = false;
03896         }
03897     else {
03898         
03899         if (happtable == nil) {
03900             
03901             ostypetostring (signature, bssignature);
03902             
03903             insertchar ('\'', bssignature);
03904             
03905             pushchar ('\'', bssignature);
03906             }
03907         else
03908             getrecordingstring (idstring, bssignature);
03909         
03910         ostypetostring (class, bsclass);
03911         
03912         ostypetostring (id, bsid);
03913         
03914         getrecordingstring (appleeventstring, bs);
03915         
03916         parsedialogstring (bs, bssignature, bsclass, bsid, nil, bs);
03917         
03918         flpushkeys = true;
03919         }
03920     
03921     pusheventparameter (event, keyDirectObject, flpushkeys, bsparam, bs);
03922     
03923     while (AEGetAttributePtr (event, keyMissedKeywordAttr, typeKeyword, &type, &id, sizeof (id), &size) == noErr) {
03924         
03925         if (flgotname) {
03926             
03927             if (osaparseaeteparamlist (haete, paramoffset, id, bsparam)) {
03928                 
03929                 if (hashtablesymbolexists (hkeywordtable, bsparam)) { /*it's a keyword*/
03930                     
03931                     bsparam [1] = toupper (bsparam [1]);
03932                     
03933                     insertstring (bsname, bsparam); /*prepend verb name using innerCase*/
03934                     }
03935                 }
03936             }
03937         
03938         err = pusheventparameter (event, id, flpushkeys, bsparam, bs);
03939         
03940         if (err != noErr)
03941             break;
03942         }
03943     
03944     /*
03945     while (AEGetAttributePtr (event, keyOptionalKeywordAttr, typeAEList, ...) == noErr)
03946         pusheventparameter (event, id, flpushkeys, bs);
03947     */
03948     
03949     if (happtable != nil) {
03950         
03951         if (rnum != -1)
03952             CloseResFile (rnum);
03953         
03954         pophashtable ();
03955         }
03956     
03957     pushstring ("\p);", bs);
03958     
03959 exit:
03960     
03961     if (err == noErr)
03962         err = sendrecordedtextevent (hcg, bs);
03963     
03964     osasetclientzone (hcg);
03965     
03966     //Code change by Timothy Paustian Friday, July 21, 2000 11:03:49 PM
03967     //Get rid of the UPP wer are done.
03968     #if TARGET_API_MAC_CARBON
03969         AERemoveCoercionHandler (typeInsertionLoc, typeChar, coerceInsltoTEXTDesc, false);
03970         DisposeAECoerceDescUPP(coerceInsltoTEXTDesc);
03971     #else
03972         AERemoveCoercionHandler (typeInsertionLoc, typeChar, coerceInsltoTEXTUPP, false);
03973     #endif
03974     osapopfastcontext (hcg);
03975     
03976     #if !TARGET_API_MAC_CARBON
03977         RestoreA5 (curA5);
03978     #endif
03979     
03980     return (err);
03981     } /*handlerecordableevent*/
03982 
03983 
03984 #if TARGET_RT_MAC_CFM || TARGET_RT_MAC_MACHO
03985 
03986     #if TARGET_API_MAC_CARBON
03987 
03988         AEEventHandlerUPP handlerecordableeventDesc = nil;
03989 
03990         #define handlerecordableeventUPP (handlerecordableeventDesc)
03991 
03992     #else
03993 
03994         static RoutineDescriptor handlerecordableeventDesc = BUILD_ROUTINE_DESCRIPTOR (uppAEEventHandlerProcInfo, handlerecordableevent);
03995 
03996         #define handlerecordableeventUPP (&handlerecordableeventDesc)
03997 
03998     #endif
03999 
04000 #else
04001 
04002     #define handlerecordableeventUPP handlerecordableevent
04003     
04004 #endif
04005 
04006 
04007 static pascal OSAError osaStartRecording (
04008             hdlcomponentglobals hglobals,
04009             OSAID               *compiledScriptToModifyID) {
04010     
04011     /*
04012     2.1b2 dmb: check gestaltAppleEventsAttr before attempting recording
04013     */
04014     
04015     register hdlcomponentglobals hcg = hglobals;
04016     tyvaluerecord vcontext;
04017     hdlhashtable hcontext;
04018     long result;
04019     Handle htext;
04020     OSAError err;
04021     
04022     if (!gestalt (gestaltAppleEventsAttr, &result) || (result <= 1))
04023         return (errAENewerVersion);
04024     
04025     if ((**hcg).recordingstate.flrecording)
04026         return (errOSARecordingIsAlreadyOn);
04027     
04028     if (*compiledScriptToModifyID == kOSANullScript) {
04029         
04030         if (!osanewvalue (idtableprocessor, nil, &vcontext))
04031             return (memFullErr);
04032         
04033         langexternalvaltotable (vcontext, &hcontext, HNoNode);
04034         
04035         if (!addstorageval (hglobals, &vcontext, compiledScriptToModifyID))
04036             return (osageterror ());
04037         
04038         newemptyhandle (&htext);
04039         }
04040     else
04041         htext = nil;
04042     
04043     (**hcg).recordingstate.hrecordedtext = htext;
04044     
04045     err = sendrecordingevent (hglobals, kAEStartRecording);
04046     
04047     if (err == noErr) {
04048         
04049         (**hcg).recordingstate.flrecording = true;
04050         
04051         (**hcg).recordingstate.lastappid = 0;
04052 
04053         //Code change by Timothy Paustian Friday, July 28, 2000 2:40:45 PM
04054         //we need to create the UPP now that we want to use it.
04055         #if TARGET_API_MAC_CARBON
04056         if (handlerecordableeventDesc == nil)
04057             handlerecordableeventDesc = NewAEEventHandlerUPP(handlerecordableevent);
04058         #endif
04059         
04060         err = AEInstallEventHandler (kCoreEventClass, kAENotifyRecording, handlerecordableeventUPP, (long) hglobals, false);
04061         }
04062     
04063     return (err);
04064     } /*osaStartRecording*/
04065 
04066 
04067 static pascal OSAError osaStopRecording (
04068             hdlcomponentglobals hglobals,
04069             OSAID               compiledScriptID) {
04070     
04071     /*
04072     2.1b5 dmb: compile the recorded text if necessary
04073     */
04074     
04075     register hdlcomponentglobals hcg = hglobals;
04076     OSAError err;
04077     AEDesc sourcedesc;
04078     
04079     if (!(**hcg).recordingstate.flrecording)
04080         return (noErr);
04081     
04082     if ((**hcg).recordingstate.lastappid != 0) /*need to terminate "with" statement*/
04083         err = sendrecordedtextevent (hcg, "\p\t}");
04084     
04085     err = sendrecordingevent (hglobals, kAEStopRecording);
04086     
04087     if ((err == noErr) && (compiledScriptID != kOSANullScript)) {
04088     
04089         #if TARGET_API_MAC_CARBON == 1 /*PBS 03/14/02: AE OS X fix.*/
04090             newdescwithhandle (&sourcedesc, typeChar, (**hcg).recordingstate.hrecordedtext);
04091         #else
04092             sourcedesc.dataHandle = (**hcg).recordingstate.hrecordedtext;
04093         #endif
04094 
04095         if ((**hcg).recordingstate.hrecordedtext != nil) {  /*script was created by recording process*/
04096 
04097             sourcedesc.descriptorType = typeChar;
04098             
04099             err = osaCompile (hcg, &sourcedesc, kOSAModeNull, &compiledScriptID);
04100             }
04101         }
04102     
04103     (**hcg).recordingstate.flrecording = false;
04104     
04105     disposehandle ((**hcg).recordingstate.hrecordedtext);
04106 
04107     //Code change by Timothy Paustian Friday, July 28, 2000 2:43:14 PM
04108     //I am assuming the osaStopRecording is called every time osaStartRecording is called.
04109     #if TARGET_API_MAC_CARBON
04110         DisposeAEEventHandlerUPP(handlerecordableeventDesc);
04111         handlerecordableeventDesc = nil;
04112     #endif
04113 
04114     return (err);
04115     } /*osaStopRecording*/
04116 
04117 
04118 static pascal OSAError
04119 osaScriptingComponentName (
04120         hdlcomponentglobals  hglobals,
04121         AEDesc              *resultingScriptingComponentName)
04122 {
04123 #pragma unused (hglobals)
04124 
04125     bigstring bs;
04126     
04127     getstringlist (componentlistnumber, usertalkstring, bs);
04128     
04129     return (AECreateDesc (typeChar, (Ptr) bs + 1, stringlength (bs), resultingScriptingComponentName));
04130     } /*osaScriptingComponentName*/
04131 
04132 
04133 static pascal OSAError osaExecute (
04134             hdlcomponentglobals hglobals,
04135             OSAID               compiledScriptID,
04136             OSAID               contextID,
04137             long                modeFlags,
04138             OSAID*              resultingScriptValueID) {
04139     
04140     tyvaluerecord vcode;
04141     tyvaluerecord vresult;
04142     hdlhashtable hscript;
04143     hdlhashtable hcontext = nil;
04144     hdlhashnode hnode;
04145     
04146     if (compiledScriptID == kOSANullScript)
04147         return (errOSAInvalidID);
04148     
04149     if (!osagetcontext (hglobals, compiledScriptID, &hscript))
04150         goto error;
04151     
04152     if (!hashtablelookup (hscript, bscode, &vcode, &hnode))
04153         return (errOSACorruptData);
04154     
04155     if (!osagetcontext (hglobals, contextID, &hcontext))
04156         return (errOSAInvalidID);
04157     
04158     if (!osahandlerunscript (hglobals, vcode.data.codevalue, hcontext, modeFlags, &vresult))
04159         goto error;
04160     
04161     if (!addstorageval (hglobals, &vresult, resultingScriptValueID))
04162         goto error;
04163     
04164     return (noErr);
04165     
04166     error:
04167         return (osageterror ());
04168     
04169     } /*osaExecute*/
04170 
04171 
04172 static pascal OSAError
04173 osaDisplay (
04174         hdlcomponentglobals  hglobals,
04175         OSAID                scriptValueID,
04176         DescType             desiredType,
04177         long                 modeFlags,
04178         AEDesc              *resultingText)
04179 {
04180 #pragma unused (desiredType, modeFlags)
04181     
04182     /*
04183     ****should handle desiredType other than typechar
04184     */
04185     
04186     tyvaluerecord val;
04187     bigstring bs;
04188     OSAError err;
04189     hdlhashnode hnode;
04190     
04191     if (scriptValueID == kOSANullScript)
04192         return (errOSAInvalidID);
04193     
04194     if (!getstorageval (hglobals, scriptValueID, &val, &hnode))
04195         goto error;
04196     
04197     if (!hashgetvaluestring (val, bs))
04198         goto error;
04199     
04200     err = AECreateDesc (typeChar, (Ptr) bs + 1, stringlength (bs), resultingText);
04201     
04202     /*
04203     if ((err == noErr) && (desiredType != typeChar) && (desiredType != typeWildCard)) {
04204         
04205         AEDesc tempdesc = *resultingText;
04206         
04207         err = AECoerceDesc (&tempdesc, desiredType, resultingText);
04208         
04209         AEDisposeDesc (&tempdesc);
04210         }
04211     */
04212     
04213     return (err);
04214     
04215     error:
04216         return (osageterror ());
04217     
04218     } /*osaDisplay*/
04219 
04220 
04221 pascal OSAError osaScriptError (
04222             hdlcomponentglobals hglobals,
04223             OSType              selector,
04224             DescType            desiredType,
04225             AEDesc*             resultingErrorDescription) {
04226     
04227     AEDesc errordesc;
04228     
04229     errordesc = (**hglobals).errordesc;
04230     
04231     return (AEGetKeyDesc (&errordesc, selector, desiredType, resultingErrorDescription));
04232     } /*osaScriptError*/
04233 
04234 
04235 static pascal OSAError osaLoadExecute (
04236             hdlcomponentglobals hglobals,
04237             const AEDesc*       scriptData,
04238             OSAID               contextID,
04239             long                modeFlags,
04240             OSAID*              resultingScriptValueID) {
04241     
04242     OSAID scriptid;
04243     OSAError err;
04244     
04245     err = osaLoad (hglobals, scriptData, modeFlags, &scriptid);
04246     
04247     if (err == noErr) {
04248         
04249         err = osaExecute (hglobals, scriptid, contextID, modeFlags, resultingScriptValueID);
04250         
04251         osaDispose (hglobals, scriptid);
04252         }
04253     
04254     return (err);
04255     } /*osaLoadExecute*/
04256 
04257 
04258 pascal OSAError osaCompileExecute (
04259             hdlcomponentglobals hglobals,
04260             const AEDesc*       sourceData,
04261             OSAID               contextID,
04262             long                modeFlags,
04263             OSAID*              resultingScriptValueID) {
04264     
04265     OSAID scriptid;
04266     OSAError err;
04267     
04268     scriptid = kOSANullScript;
04269     
04270     err = osaCompile (hglobals, sourceData, modeFlags | kOSAModePreventGetSource, &scriptid);
04271     
04272     if (err == noErr) {
04273         
04274         err = osaExecute (hglobals, scriptid, contextID, modeFlags, resultingScriptValueID);
04275         
04276         osaDispose (hglobals, scriptid);
04277         }
04278     
04279     return (err);
04280     } /*osaCompileExecute*/
04281 
04282 
04283 pascal OSAError osaDoScript (
04284             hdlcomponentglobals hglobals,
04285             const AEDesc*       sourceData,
04286             OSAID               contextID,
04287             DescType            desiredType,
04288             long                modeFlags,
04289             AEDesc*             resultingText) {
04290     
04291     /*
04292     3/29/93 dmb: set menusharingglobals.idscript so that script can be cancelled
04293     */
04294     
04295     register hdlcomponentglobals hcg = hglobals;
04296     tyvaluerecord vcode;
04297     tyvaluerecord vresult;
04298     hdlhashtable hcontext;
04299     boolean fl;
04300     OSAError err;
04301     
04302     if (!osagetcontext (hglobals, contextID, &hcontext))
04303         return (errOSAInvalidID);
04304     
04305     err = osacompiledesc (sourceData, &vcode);
04306     
04307     if (err != noErr)
04308         return (err);
04309     
04310     (**hcg).menusharingglobals.idscript = (long) vcode.data.codevalue;
04311     
04312     fl = osahandlerunscript (hcg, vcode.data.codevalue, hcontext, modeFlags, &vresult);
04313     
04314     langdisposetree (vcode.data.codevalue);
04315     
04316     if (!fl) {
04317         
04318         osaScriptError (hcg, kOSAErrorMessage, desiredType, resultingText);
04319         
04320         if (osageterror () == userCanceledErr)
04321             return (userCanceledErr);
04322         else
04323             return (errOSAScriptError);
04324         }
04325     
04326     fl = storagevaltodesc (&vresult, desiredType, resultingText);
04327     
04328     disposetmpvalue (&vresult);
04329     
04330     if (!fl)
04331         return (osageterror ());
04332     
04333     return (noErr);
04334     } /*osaDoScript*/
04335 
04336 
04337 static pascal OSAError
04338 osaMakeContext (
04339         hdlcomponentglobals  hglobals,
04340         const AEDesc        *contextName,
04341         OSAID                parentContext,
04342         OSAID               *resultingContextID)
04343 {
04344 #pragma unused (contextName, parentContext)
04345     
04346     tyvaluerecord vcontext;
04347     
04348     *resultingContextID = kOSANullScript;
04349     
04350     if (!osanewvalue (idtableprocessor, nil, &vcontext))
04351         return (memFullErr);
04352     
04353     addstorageval (hglobals, &vcontext, resultingContextID);
04354     
04355     return (noErr);
04356     } /*osaMakeContext*/
04357 
04358 
04359 static boolean getverbresult (hdlverbrecord hv, tyvaluerecord *vresult) {
04360 
04361     typaramrecord result;
04362     AppleEvent event, reply;
04363     
04364     landsystem7geteventrecords (hv, &event, &reply);
04365     
04366     if (landsystem7getparam (&reply, returnedvaluekey, notype, &result))
04367         setdescriptorvalue (result.desc, vresult);
04368     else
04369         initvalue (vresult, novaluetype);
04370     
04371     return (true);
04372     } /*getverbresult*/
04373 
04374 
04375 static pascal Boolean osahandleevent (
04376                 hdlcomponentglobals hglobals,
04377                 hdlverbrecord       hverb,
04378                 OSAID               contextID,
04379                 long                modeFlags,
04380                 tyvaluerecord*      vresult) {
04381     
04382     /*
04383     2/27/97 dmb: major improvement. first, fix bug where null context id
04384     would lead to crash in hashtablelookup with a nil table. next, if the 
04385     verb isn't handled by the given context, pass if off to the global 
04386     handleverbroutine for default processing by Frontier
04387     */
04388     
04389     tyvaluerecord vcode;
04390     hdlhashtable hcontext;
04391     hdlhashnode hnode;
04392     
04393     if (contextID == kOSANullScript) {
04394     
04395         oserror (errAEEventNotHandled);
04396         
04397         goto error;
04398         }
04399     
04400     if (!osagetcontext (hglobals, contextID, &hcontext))
04401         goto error;
04402     
04403     if (!hashtablelookup (hcontext, bscode, &vcode, &hnode)) {
04404         
04405         oserror (errOSACorruptData);
04406         
04407         goto error;
04408         }
04409     
04410     if (!osahandletrapverb (hglobals, hverb, vcode.data.codevalue, modeFlags, vresult))
04411         goto error;
04412     
04413     return (true);
04414     
04415     error:
04416     
04417     #if version42orgreater
04418     
04419         if (osageterror () == errAEEventNotHandled) { // verb not handled by context
04420         
04421             /***this is where the ResumeDispatchProc should be invoked*/
04422     
04423             register hdlcomponentglobals hcg = osaglobals;
04424             hdllandglobals hg = landgetglobals (); // get global verb handler
04425             GNEUPP origproc;
04426             boolean fl;
04427             
04428             origproc = osainstallpatch (hcg);
04429             
04430             #if TARGET_API_MAC_CARBON == 1
04431             LMSetApplZone(homezone);
04432             #else   
04433             SetZone (homezone);
04434             #endif      
04435             fl = (*(**hg).handleverbroutine) (hverb);
04436             
04437             getverbresult (hverb, vresult);
04438             
04439             osasetclientzone (hcg);
04440             
04441             osaremovepatch (hcg, origproc);
04442             
04443             return (fl);
04444             }
04445     #endif
04446         
04447         return (false);
04448     } /*osahandleevent*/
04449 
04450 
04451 static pascal OSAError osaExecuteEvent (
04452                 hdlcomponentglobals hglobals,
04453                 AppleEvent*         event,
04454                 OSAID               contextID,
04455                 long                modeFlags,
04456                 OSAID*              resultingScriptValueID) {
04457     
04458     hdlverbrecord hverb = nil;
04459     tyvaluerecord vresult;
04460     boolean fl = false;
04461     
04462     if (contextID == kOSANullScript)
04463         return (errOSAInvalidID);
04464     
04465     if (!landsystem7unpackverb (event, nil, &hverb))
04466         goto exit;
04467     
04468     if (!osahandleevent (hglobals, hverb, contextID, modeFlags, &vresult))
04469         goto exit;
04470     
04471     if (!addstorageval (hglobals, &vresult, resultingScriptValueID))
04472         goto exit;
04473     
04474     fl = true;
04475     
04476     exit: {
04477         
04478         landdisposeverb (hverb);
04479         
04480         if (!fl)
04481             return (osageterror ());
04482         
04483         return (noErr);
04484         }
04485     } /*osaExecuteEvent*/
04486 
04487 
04488 static pascal OSAError osaDoEvent (
04489             hdlcomponentglobals hglobals,
04490             AppleEvent*         event,
04491             OSAID               contextID,
04492             long                modeFlags,
04493             AppleEvent*         reply) {
04494     
04495     /*
04496     2.1b4 dmb: if an error occurs running the script, try filling out error reply
04497     
04498     4.0.2b1 dmb: create reply is caller hasn't already
04499     */
04500     
04501     register hdlcomponentglobals hcg = hglobals;
04502     hdlverbrecord hverb = nil;
04503     tyvaluerecord vresult;
04504     boolean fl = false;
04505     
04506     if (contextID == kOSANullScript)
04507         return (errOSAInvalidID);
04508     
04509     if ((*reply).descriptorType == typeNull)    /*dmb 6.17.96*/
04510         if (oserror (AECreateList (nil, 0, true, reply)))
04511             goto exit;
04512     
04513     if (!landsystem7unpackverb (event, reply, &hverb))
04514         goto exit;
04515     
04516     if (!osahandleevent (hcg, hverb, contextID, modeFlags, &vresult))
04517         goto exit;
04518     
04519     fl = true;
04520     
04521     landstartreturn (hverb);
04522     
04523     langipcpushparam (&vresult, returnedvaluekey, hverb);
04524     
04525     exit: {
04526         
04527         landdisposeverb (hverb);
04528         
04529         if (!fl) {
04530             
04531             AEDesc desc;
04532             OSAError err = osageterror ();
04533             
04534             if (err == errOSAScriptError) {
04535                 
04536                 err = errOSAGeneralError;
04537                 
04538                 if (osaScriptError (hcg, kOSAErrorMessage, typeChar, &desc) == noErr) {
04539                     
04540                     AEPutKeyDesc (reply, keyErrorString, &desc);
04541                     
04542                     AEDisposeDesc (&desc);
04543                     }
04544                 
04545                 if (osaScriptError (hcg, kOSAErrorNumber, typeShortInteger, &desc) == noErr) {
04546                     
04547                     #if TARGET_API_MAC_CARBON == 1 /*PBS 03/14/02: AE OS X fix.*/
04548                     
04549                         {
04550                         Handle h;
04551                         
04552                         copydatahandle (&desc, &h);
04553                         
04554                         err = numberfromhandle (h);
04555                         
04556                         disposehandle (h);
04557                         } 
04558                     
04559                     #else
04560                     
04561                         err = numberfromhandle (desc.dataHandle);
04562                     
04563                     #endif
04564                     
04565                     AEPutKeyDesc (reply, keyErrorNumber, &desc);
04566                     
04567                     AEDisposeDesc (&desc);
04568                     }
04569                 }
04570             
04571             return (err);
04572             }
04573         
04574         return (noErr);
04575         }
04576     } /*osaDoEvent*/
04577 
04578 
04579 static pascal OSAError osaSetDebugProc (
04580             hdlcomponentglobals hglobals,
04581             OSADebugUPP         debugProc,
04582             long                refCon) {
04583     
04584     register hdlcomponentglobals hcg = hglobals;
04585     
04586     (**hcg).debugproc = debugProc;
04587     
04588     (**hcg).debugprocrefcon = refCon;
04589     
04590     return (noErr);
04591     } /*osaSetDebugProc*/
04592 
04593 
04594 static pascal OSAError osaDebug (
04595             hdlcomponentglobals hglobals,
04596         OSType               selector,
04597         const AEDesc        *selectorData,
04598         DescType             desiredType,
04599         AEDesc              *resultingDebugInfoOrDataToSet) {
04600 #pragma unused (hglobals, desiredType)
04601 
04602     OSAError err = noErr;
04603     hdlhashnode hnode;
04604 
04605     switch (selector) {
04606 
04607         case kOSADebugRange: {
04608             AEDesc rec;
04609             
04610             err = AECreateList (nil, 0, true, &rec);
04611             
04612             if (err == noErr) {
04613                 
04614                 long ix = langgetsourceoffset (ctscanlines, ctscanchars);
04615                 
04616                 err = AEPutKeyPtr (&rec, keyOSADebugRangeStart, typeLongInteger, (Ptr) &ix, sizeof (ix));
04617                 
04618                 err = AEPutKeyPtr (&rec, keyOSADebugRangeEnd, typeLongInteger, (Ptr) &ix, sizeof (ix));
04619                 
04620                 if (err == noErr)
04621                     err = AECoerceDesc (&rec, typeOSADebugRange, resultingDebugInfoOrDataToSet);
04622                 
04623                 AEDisposeDesc (&rec);
04624                 }
04625             
04626             break;
04627             }
04628         
04629         case kOSADebugSetData: {
04630             bigstring bs;
04631             tyvaluerecord val;
04632             
04633             if ((*selectorData).descriptorType != typeChar) {
04634                 
04635                 err = errAEWrongDataType;
04636                 
04637                 break;
04638                 }
04639             
04640             #if TARGET_API_MAC_CARBON == 1 /*PBS 03/14/02: AE OS X fix.*/
04641             
04642                 datahandletostring ((AEDesc*) selectorData, bs);
04643             
04644             #else
04645             
04646                 texthandletostring ((*selectorData).dataHandle, bs);
04647             
04648             #endif
04649             
04650             if (!langgetsymbolval (bs, &val, &hnode)) {
04651                 
04652                 err = errOSAScriptError;
04653                 
04654                 break;
04655                 }
04656             
04657             if (!copyvaluerecord (val, &val)) {
04658                 
04659                 err = memFullErr;
04660                 
04661                 break;
04662                 }
04663             
04664             /*
04665             if (!coercetobinary (&val)) {
04666                 
04667                 disposevaluerecord (val, true);
04668                 
04669                 break;
04670                 }
04671             
04672             binarytodesc (val.data.binaryvalue, resultingDebugInfoOrDataToSet);
04673             */
04674             
04675             if (!valuetodescriptor (&val, resultingDebugInfoOrDataToSet))
04676                 err = memFullErr;
04677             
04678             break;
04679             }
04680         
04681         default:
04682             return (errOSABadSelector);
04683         }
04684     
04685     return (err);
04686     } /*osaDebug*/
04687 
04688 
04689 static pascal ComponentResult cmpclose (Handle storage, ComponentInstance self) {
04690 #pragma unused(self)
04691 
04692     register hdlcomponentglobals hglobals = (hdlcomponentglobals) storage;
04693     
04694     if (hglobals != nil) { /*defensive driving -- can be necessary during shutdown*/
04695         
04696         osaStopRecording (hglobals, kOSANullScript); /*make sure we don't leave recoding in progress*/
04697         
04698         disposecomponentglobals (hglobals);
04699         }
04700     
04701     return (noErr);
04702     } /*cmpclose*/
04703 
04704 
04705 static pascal ComponentResult cmpcando (short selector) {
04706     
04707     switch (selector) {
04708         
04709         case kComponentOpenSelect:
04710         case kComponentCloseSelect:
04711         case kComponentCanDoSelect:
04712         case kComponentVersionSelect:
04713         case kOSASelectLoad:
04714         case kOSASelectStore:
04715         case kOSASelectExecute:
04716         case kOSASelectDisplay:
04717         case kOSASelectScriptError:
04718         case kOSASelectDispose:
04719         case kOSASelectSetScriptInfo:
04720         case kOSASelectGetScriptInfo:
04721         case kOSASelectCompile:
04722         case kOSASelectGetSource:
04723         case kOSASelectCoerceFromDesc:
04724         case kOSASelectCoerceToDesc:
04725         case kOSASelectStartRecording:
04726         case kOSASelectStopRecording:
04727         case kOSASelectScriptingComponentName:
04728         case kOSASelectLoadExecute:
04729         case kOSASelectCompileExecute:
04730         case kOSASelectDoScript:
04731         case kOSASelectMakeContext:
04732         case kOSASelectSetResumeDispatchProc:
04733         case kOSASelectGetResumeDispatchProc:
04734         case kOSASelectExecuteEvent:
04735         case kOSASelectDoEvent:
04736         case kOSASelectSetActiveProc:
04737         case kOSASelectSetDebugProc:
04738         case kOSASelectDebug:
04739         
04740         case kOSASelectSetSendProc:
04741         case kOSASelectGetSendProc:
04742         case kOSASelectSetCreateProc:
04743         case kOSASelectGetCreateProc:
04744         /*
04745         case kOSASelectSetDefaultTarget:
04746         */
04747             return (true);
04748         }
04749     
04750     return (false);
04751     } /*cmpcando*/
04752 
04753 
04754 static pascal ComponentResult cmpversion (void) {
04755 
04756     return (0x04100100);
04757     } /*cmpversion*/
04758 
04759 
04760 pascal ComponentResult callosafunction (Handle storage, register ComponentParameters *params, ComponentFunctionUPP func) {
04761     
04762     /*
04763     4/13/93 dmb: account for re-entrancy, though I don't think it's handled everywhere
04764     
04765     2.1b5 dmb: re-entrancy is pretty good at this point (better be!), but we do 
04766     need to clear fllangerror before exiting. (we might do this at the beginning 
04767     instead; shouldn't really matter.)
04768     
04769     3.0a dmb: that was the wrong place to clear fllangerror, since it was affecting 
04770     the background context, not the osa context. the cleanest place to clear it is 
04771     before we call the osa function, but we really need to clear it when we're done. 
04772     that way, this is a nested call, an error here doesn't force the outer call to 
04773     die.
04774     */
04775     
04776     hdlcomponentglobals hcg = (hdlcomponentglobals) storage;
04777     hdllandglobals hlg;
04778     hdlhashtable ht;
04779     ComponentResult result;
04780 
04781     //Code change by Timothy Paustian Monday, June 26, 2000 9:38:15 PM
04782     //
04783     #if TARGET_API_MAC_CARBON == 1
04784         CGrafPtr saveport;
04785         saveport = GetQDGlobalsThePort();
04786     #else
04787         GrafPtr saveport;
04788         saveport = quickdrawglobal (thePort);
04789     #endif
04790         
04791     osapushfastcontext (hcg);
04792     
04793     ht = (**hcg).storagetable;
04794     
04795     if (ht == currenthashtable) /*avoid redundant push -- save stack space*/
04796         ht = nil;
04797     
04798     if (ht != nil)
04799         pushhashtable (ht);
04800     
04801     shellpusherrorhook ((errorhookcallback) &osaerrormessage);
04802     
04803     flthreadkilled = false; /*reset*/
04804     
04805     langcallbacks.errormessagecallback = &osaerrormessage;
04806     
04807     hlg = landgetglobals ();
04808     
04809     (**hlg).eventcreatecallback = &osacreateevent;
04810     
04811     (**hlg).eventsendcallback = &osasendevent;
04812 
04813     result = CallComponentFunctionWithStorage ((Handle) hcg, params, func);
04814     
04815     shellpoperrorhook ();
04816     
04817     if (ht != nil)
04818         pophashtable ();
04819     
04820     fllangerror = false; /*clear in case we're nested*/
04821     
04822     osapopfastcontext (hcg);
04823     
04824     oserror (noErr); /*clear it out to avoid conflicts with current thread*/
04825 
04826     //Code change by Timothy Paustian Monday, June 26, 2000 9:37:17 PM
04827     //
04828         {
04829         #if TARGET_API_MAC_CARBON == 1
04830             CGrafPtr thePort;
04831             thePort = GetQDGlobalsThePort();
04832         #else
04833             GrafPtr thePort;
04834             thePort = quickdrawglobal (thePort);
04835         #endif
04836     
04837         if (thePort != saveport)
04838             SetPort (saveport);
04839         }
04840 
04841     return (result);
04842     } /*callosafunction*/
04843 
04844 
04845 static pascal ComponentResult osaDispatch (ComponentParameters *params, Handle storage) {
04846     
04847     ComponentResult result = noErr;
04848     short what = (*params).what;
04849     
04850     #if TARGET_API_MAC_CARBON
04851 
04852         hdlcomponentglobals theGlobals = nil;
04853 
04854         if(what != kComponentOpenSelect) //else we create the globals
04855             theGlobals = (hdlcomponentglobals)storage;
04856 
04857     #endif
04858     
04859     if (what < 0) { /*negative selectors are component manager calls*/
04860         
04861         switch (what) {
04862             
04863             case kComponentOpenSelect: { /*can't call subroutine because a5 isn't ours*/
04864                 
04865                 hdlcomponentglobals hglobals;
04866                 Component self = (Component) (*params).params [0];
04867                 long selfa5;
04868                 long clienta5;
04869                 
04870                 selfa5 = GetComponentRefcon (self);
04871                 
04872                 #ifdef THINK_C
04873                     
04874                     asm {
04875                         move.l  a5,clienta5
04876                         move.l  a5,-(a7)
04877                         move.l  selfa5,a5
04878                         }
04879                     
04880                 #else
04881                 
04882                     clienta5 = SetUpAppA5 ();
04883                     
04884                     // 5.0.1: might not be - assert (clienta5 == (long) LMGetCurrentA5 ());
04885                     
04886                 #endif
04887                 //Code change by Timothy Paustian Monday, June 26, 2000 9:39:54 PM
04888                 //We don't need this
04889                 #if !TARGET_API_MAC_CARBON
04890                 SetComponentInstanceA5 ((ComponentInstance) self, selfa5);
04891                 #endif
04892                 
04893                 if (newcomponentglobals (self, clienta5, &hglobals))
04894                     SetComponentInstanceStorage ((ComponentInstance) self, (Handle) hglobals);
04895                 else
04896                     result = memFullErr;
04897                 #if !TARGET_API_MAC_CARBON
04898                 RestoreA5 (clienta5);
04899                 #endif
04900                 
04901                 
04902                 break;
04903                 }
04904             
04905             case kComponentCloseSelect:
04906                 #if TARGET_API_MAC_CARBON
04907                 result = CallComponentFunctionWithStorage (storage, params, (**theGlobals).cmpcloseUPP);                                
04908                 #else
04909                 result = CallComponentFunctionWithStorage (storage, params, cmpcloseUPP);
04910                 #endif
04911                 break;
04912             
04913             case kComponentCanDoSelect:
04914                 #if TARGET_API_MAC_CARBON
04915                 result = CallComponentFunction (params, (**theGlobals).cmpcandoUPP);            
04916                 #else
04917                 result = CallComponentFunction(params, cmpcandoUPP);
04918                 #endif
04919                 break;
04920             
04921             case kComponentVersionSelect:
04922                 #if TARGET_API_MAC_CARBON
04923                 result = CallComponentFunction(params, (**theGlobals).cmpversionUPP);           
04924                 #else
04925                 result = CallComponentFunction(params, cmpversionUPP);
04926                 #endif
04927                 break;
04928             
04929             default:
04930                 result = badComponentSelector;
04931                 break;
04932             }
04933         }
04934     else {  /*positive selectors are OSA calls*/
04935         
04936         ComponentFunctionUPP func = nil;
04937 
04938         switch (what) {
04939             
04940             case kOSASelectLoad:
04941                 #if TARGET_API_MAC_CARBON
04942                 func = (**theGlobals).osaLoadUPP;
04943                 #else
04944                 func = osaLoadUPP;
04945                 #endif
04946                 break;
04947             
04948             case kOSASelectStore:
04949                 #if TARGET_API_MAC_CARBON
04950                 func = (**theGlobals).osaStoreUPP;
04951                 #else
04952                 func = osaStoreUPP;
04953                 #endif
04954                 break;
04955             
04956             case kOSASelectExecute:
04957                 #if TARGET_API_MAC_CARBON
04958                 func = (**theGlobals).osaExecuteUPP;
04959                 #else
04960                 func = osaExecuteUPP;
04961                 #endif
04962                 break;
04963             
04964             case kOSASelectDisplay:
04965                 #if TARGET_API_MAC_CARBON
04966                 func = (**theGlobals).osaDisplayUPP;
04967                 #else
04968                 func = osaDisplayUPP;
04969                 #endif
04970                 break;
04971             
04972             case kOSASelectScriptError:
04973                 #if TARGET_API_MAC_CARBON
04974                 func = (**theGlobals).osaScriptErrorUPP;
04975                 #else
04976                 func = osaScriptErrorUPP;
04977                 #endif
04978                 break;
04979             
04980             case kOSASelectDispose:
04981                 #if TARGET_API_MAC_CARBON
04982                 func = (**theGlobals).osaDisposeUPP;
04983                 #else
04984                 func = osaDisposeUPP;
04985                 #endif
04986                 break;
04987             
04988             case kOSASelectSetScriptInfo:
04989                 #if TARGET_API_MAC_CARBON
04990                 func = (**theGlobals).osaSetScriptInfoUPP;
04991                 #else
04992                 func = osaSetScriptInfoUPP;
04993                 #endif
04994                 break;
04995             
04996             case kOSASelectGetScriptInfo:
04997                 #if TARGET_API_MAC_CARBON
04998                 func = (**theGlobals).osaGetScriptInfoUPP;
04999                 #else
05000                 func = osaGetScriptInfoUPP;
05001                 #endif
05002                 break;
05003             
05004             case kOSASelectCompile:
05005                 #if TARGET_API_MAC_CARBON
05006                 func = (**theGlobals).osaCompileUPP;
05007                 #else
05008                 func = osaCompileUPP;
05009                 #endif
05010                 break;
05011             
05012             case kOSASelectGetSource:
05013                 #if TARGET_API_MAC_CARBON
05014                 func = (**theGlobals).osaGetSourceUPP;
05015                 #else
05016                 func = osaGetSourceUPP;
05017                 #endif
05018                 break;
05019             
05020             case kOSASelectCoerceFromDesc:
05021                 #if TARGET_API_MAC_CARBON
05022                 func = (**theGlobals).osaCoerceFromDescUPP;
05023                 #else
05024                 func = osaCoerceFromDescUPP;
05025                 #endif
05026                 break;
05027             
05028             case kOSASelectCoerceToDesc:
05029                 #if TARGET_API_MAC_CARBON
05030                 func = (**theGlobals).osaCoerceToDescUPP;
05031                 #else
05032                 func = osaCoerceToDescUPP;
05033                 #endif
05034                 break;
05035             
05036             case kOSASelectStartRecording:
05037                 #if TARGET_API_MAC_CARBON
05038                 func = (**theGlobals).osaStartRecordingUPP;
05039                 #else
05040                 func = osaStartRecordingUPP;
05041                 #endif
05042                 break;
05043             
05044             case kOSASelectStopRecording:
05045                 #if TARGET_API_MAC_CARBON
05046                 func = (**theGlobals).osaStopRecordingUPP;
05047                 #else
05048                 func = osaStopRecordingUPP;
05049                 #endif
05050                 break;
05051             
05052             case kOSASelectScriptingComponentName:
05053                 #if TARGET_API_MAC_CARBON
05054                 func = (**theGlobals).osaScriptingComponentNameUPP;
05055                 #else
05056                 func = osaScriptingComponentNameUPP;
05057                 #endif
05058                 break;
05059             
05060             case kOSASelectLoadExecute:
05061                 #if TARGET_API_MAC_CARBON
05062                 func = (**theGlobals).osaLoadExecuteUPP;
05063                 #else
05064                 func = osaLoadExecuteUPP;
05065                 #endif
05066                 break;
05067             
05068             case kOSASelectCompileExecute:
05069                 #if TARGET_API_MAC_CARBON
05070                 func = (**theGlobals).osaCompileExecuteUPP;
05071                 #else
05072                 func = osaCompileExecuteUPP;
05073                 #endif
05074                 break;
05075             
05076             case kOSASelectDoScript:
05077                 #if TARGET_API_MAC_CARBON
05078                 func = (**theGlobals).osaDoScriptUPP;
05079                 #else
05080                 func = osaDoScriptUPP;
05081                 #endif
05082                 break;
05083             
05084             case kOSASelectMakeContext:
05085                 #if TARGET_API_MAC_CARBON
05086                 func = (**theGlobals).osaMakeContextUPP;
05087                 #else
05088                 func = osaMakeContextUPP;
05089                 #endif
05090                 break;
05091             
05092             case kOSASelectSetResumeDispatchProc:
05093                 #if TARGET_API_MAC_CARBON
05094                 func = (**theGlobals).osaSetResumeDispatchProcUPP;
05095                 #else
05096                 func = osaSetResumeDispatchProcUPP;
05097                 #endif
05098                 break;
05099             
05100             case kOSASelectGetResumeDispatchProc:
05101                 #if TARGET_API_MAC_CARBON
05102                 func = (**theGlobals).osaGetResumeDispatchProcUPP;
05103                 #else
05104                 func = osaGetResumeDispatchProcUPP;
05105                 #endif
05106                 break;
05107             
05108             case kOSASelectExecuteEvent:
05109                 #if TARGET_API_MAC_CARBON
05110                 func = (**theGlobals).osaExecuteEventUPP;
05111                 #else
05112                 func = osaExecuteEventUPP;
05113                 #endif
05114                 break;
05115             
05116             case kOSASelectDoEvent:
05117                 #if TARGET_API_MAC_CARBON
05118                 func = (**theGlobals).osaDoEventUPP;
05119                 #else
05120                 func = osaDoEventUPP;
05121                 #endif
05122                 break;
05123             
05124             case kOSASelectSetActiveProc:
05125                 #if TARGET_API_MAC_CARBON
05126                 func = (**theGlobals).osaSetActiveProcUPP;
05127                 #else
05128                 func = osaSetActiveProcUPP;
05129                 #endif
05130                 break;
05131             
05132             case kOSASelectSetDebugProc:
05133                 #if TARGET_API_MAC_CARBON
05134                 func = (**theGlobals).osaSetDebugProcUPP;
05135                 #else
05136                 func = osaSetDebugProcUPP;
05137                 #endif
05138                 break;
05139             
05140             case kOSASelectDebug:
05141                 #if TARGET_API_MAC_CARBON
05142                 func = (**theGlobals).osaDebugUPP;
05143                 #else
05144                 func = osaDebugUPP;
05145                 #endif
05146                 break;
05147             
05148             case kOSASelectSetSendProc:
05149                 #if TARGET_API_MAC_CARBON
05150                 func = (**theGlobals).osaSetSendProcUPP;
05151                 #else
05152                 func = osaSetSendProcUPP;
05153                 #endif
05154                 break;
05155             
05156             case kOSASelectGetSendProc:
05157                 #if TARGET_API_MAC_CARBON
05158                 func = (**theGlobals).osaGetSendProcUPP;
05159                 #else
05160                 func = osaGetSendProcUPP;
05161                 #endif
05162                 break;
05163             
05164             case kOSASelectSetCreateProc:
05165                 #if TARGET_API_MAC_CARBON
05166                 func = (**theGlobals).osaSetCreateProcUPP;
05167                 #else
05168                 func = osaSetCreateProcUPP;
05169                 #endif
05170                 break;
05171             
05172             case kOSASelectGetCreateProc:
05173                 #if TARGET_API_MAC_CARBON
05174                 func = (**theGlobals).osaGetCreateProcUPP;
05175                 #else
05176                 func = osaGetCreateProcUPP;
05177                 #endif
05178                 break;
05179             
05180             #if 0
05181             
05182             case kOSASelectSetDefaultTarget:
05183                 #if TARGET_API_MAC_CARBON
05184                 func = (**theGlobals).osaSetDefaultTargetUPP;
05185                 #else
05186                 func = osaSetDefaultTargetUPP;
05187                 #endif
05188                 break;
05189             
05190             #endif
05191             
05192             } /*switch*/
05193         
05194         if (func == nil)
05195             result = badComponentSelector;
05196         else
05197             result = callosafunction (storage, params, func);
05198         }
05199     
05200     return (result);
05201     } /*osaDispatch*/
05202 
05203 
05204 static boolean osacomponenterror (ComponentInstance comp, OSAID idscript, OSAError err) {
05205     
05206     /*
05207     2.1b4 dmb: get error number and check for user canceled
05208     
05209     2.1b11 dmb: talked to William Cook at Apple. it turns out that AS can 
05210     only return a valid error range if you first ask for the source. 
05211     otherwise, it doesn't know what dialect the script is in.
05212     */
05213     
05214     AEDesc errordesc;
05215     AEDesc scriptsource;
05216     
05217     if (err != errOSAScriptError)
05218         return (oserror (err));
05219     
05220     if (OSAScriptError (comp, kOSAErrorNumber, typeLongInteger, &errordesc) == noErr) {
05221         
05222         #if TARGET_API_MAC_CARBON == 1 /*PBS 03/14/02: AE OS X fix.*/
05223             {
05224             Handle h;
05225             
05226             copydatahandle (&errordesc, &h);
05227             
05228             err = numberfromhandle (h);
05229             
05230             disposehandle (h);
05231             }
05232         #else
05233             err = numberfromhandle (errordesc.dataHandle);
05234         #endif
05235         
05236         AEDisposeDesc (&errordesc);
05237         
05238         if (err == userCanceledErr)
05239             return (true);
05240         }
05241     
05242     if (idscript != kOSANullScript) { /*ask for source so AS knows dialect for range info*/
05243         
05244         if (OSAGetSource (comp, idscript, typeWildCard, &scriptsource) == noErr)
05245             AEDisposeDesc (&scriptsource);
05246         }
05247     
05248     if (OSAScriptError (comp, kOSAErrorRange, typeOSAErrorRange, &errordesc) == noErr) {
05249         
05250         AERecord rec;
05251         unsigned long offset, type;
05252         long size;
05253         
05254         if (AECoerceDesc (&errordesc, typeAERecord, &rec) == noErr) {
05255             
05256             if (AEGetKeyPtr (&rec, keyOSASourceEnd, typeLongInteger, &type, (Ptr) &offset, sizeof (offset), &size) == noErr)
05257                 langsetsourceoffset (offset);
05258             
05259             AEDisposeDesc (&rec);
05260             }
05261         
05262         AEDisposeDesc (&errordesc);
05263         }
05264     
05265     if (OSAScriptError (comp, kOSAErrorMessage, typeChar, &errordesc) == noErr) {
05266         
05267         bigstring bserror;
05268         
05269         #if TARGET_API_MAC_CARBON == 1 /*PBS 03/14/02: AE OS X fix.*/
05270             datahandletostring (&errordesc, bserror);
05271         #else
05272             texthandletostring (errordesc.dataHandle, bserror);
05273         #endif
05274         
05275         AEDisposeDesc (&errordesc);
05276         
05277         langerrormessage (bserror);
05278         }
05279     else
05280         oserror (err);
05281     
05282     return (true);
05283     } /*osacomponenterror*/
05284 
05285 
05286 static boolean addosaserver (ComponentInstance instance, OSType type) {
05287     
05288     /*
05289     3.0b16 dmb: the client's psn is now part of the server record; server 
05290     instances cannot be safely shared between clients
05291     */
05292     
05293     tyservercomponent server;
05294     hdlservercomponent hserver;
05295     
05296     server.instance = instance;
05297     
05298     server.type = type;
05299     
05300     GetCurrentProcess (&server.clientpsn); /*3.015*/
05301     
05302     if (!newfilledhandle (&server, sizeof (server), (Handle *) &hserver))
05303         return (false);
05304     
05305     listlink ((hdllinkedlist) hserverlist, (hdllinkedlist) hserver);
05306     
05307     return (true);
05308     } /*addosaserver*/
05309 
05310 
05311 ComponentInstance getosaserver (OSType type) {
05312     
05313     /*
05314     3.0b16 dmb: instances are no longer shared between client processes
05315     
05316     3.0a dmb: set A5 to CurA5 when opening a component
05317     */
05318     
05319     register hdlservercomponent hserver;
05320     register ComponentInstance instance;
05321     long appA5;
05322     
05323     for (hserver = hserverlist; hserver != nil; hserver = (**hserver).hnext) {
05324         
05325         if ((**hserver).type == type) {
05326             
05327             if (!iscurrentapplication ((**hserver).clientpsn)) /*3.0b16*/
05328                 continue;
05329             
05330             instance = (**hserver).instance;
05331             
05332             if (GetComponentVersion (instance) == badComponentInstance) { /*no longer valid*/
05333                 
05334                 listunlink ((hdllinkedlist) hserverlist, (hdllinkedlist) hserver);
05335                 
05336                 disposehandle ((Handle) hserver);
05337                 
05338                 break;
05339                 }
05340             
05341             return (instance);
05342             }
05343         }
05344     
05345     appA5 = SetUpCurA5 (); /*3.0a*/
05346     
05347     instance = OpenDefaultComponent (kOSAComponentType, type);
05348     
05349     RestoreA5 (appA5);
05350     
05351     if (instance != nil)
05352         addosaserver (instance, type);
05353     
05354     return (instance);
05355     } /*getosaserver*/
05356 
05357 
05358 static boolean initosaservers (void) {
05359     
05360     /*
05361     initialize the list of open servers. once open, we never close a server 
05362     until quit time.   the first record that we allocate here is just  the 
05363     list header.
05364     */
05365     
05366     return (newclearhandle (longsizeof (tyservercomponent), (Handle *) &hserverlist));
05367     } /*initosaservers*/
05368 
05369 
05370 void closeosaservers (void) {
05371     
05372     /*
05373     cleanly close each open server and remove from the server list.
05374     
05375     3.0b16 dmb: only close servers that were opened in this process
05376     */
05377     
05378     register hdlservercomponent hserver;
05379     register hdlservercomponent hnext;
05380     
05381     for (hserver = hserverlist; hserver != nil; hserver = hnext) {
05382         
05383         hnext = (**hserver).hnext;
05384         
05385         if (!iscurrentapplication ((**hserver).clientpsn)) /*3.0b16*/
05386             continue;
05387         
05388         if ((**hserver).instance != nil) {
05389             
05390             CloseComponent ((**hserver).instance);
05391             
05392             listunlink ((hdllinkedlist) hserverlist, (hdllinkedlist) hserver);
05393             
05394             disposehandle ((Handle) hserver);
05395             }
05396         }
05397     } /*closeosaservers*/
05398 
05399 
05400 static boolean openscriptcomponent (const tyvaluerecord *osaval, AEDesc *scriptdata, OSType *subtype, ComponentInstance *comp) {
05401     
05402     /*
05403     common code: htree is an osascript node. create an AEDesc that contains 
05404     the compiled script, and open the component for the script.
05405     
05406     2.1b4 dmb: try to get storage type directly instead of relying on the 
05407     generic component to translate
05408     */
05409     
05410     Handle hdata;
05411     OSType idserver;
05412     
05413     assert ((*osaval).valuetype == binaryvaluetype);
05414     
05415     if ((*osaval).valuetype != binaryvaluetype) /*not a script*/
05416         return (false);
05417     
05418     if (!copyhandle ((*osaval).data.binaryvalue, &hdata))
05419         return (false);
05420     
05421     binarytodesc (hdata, scriptdata);
05422     
05423     #if TARGET_API_MAC_CARBON == 1 /*PBS 03/14/02: AE OS X fix.*/
05424         {
05425         Handle h;
05426         
05427         copydatahandle (scriptdata, &h);
05428         
05429         if (OSAGetStorageType ((AEDataStorage) h, &idserver) != noErr)
05430             idserver = (*scriptdata).descriptorType;
05431             
05432         disposehandle (h);
05433         }
05434     #else
05435         if (OSAGetStorageType ((*scriptdata).dataHandle, &idserver) != noErr)
05436             idserver = (*scriptdata).descriptorType;
05437     #endif
05438     
05439     *comp = getosaserver (idserver);
05440     
05441     if (*comp == nil) {
05442         
05443         AEDisposeDesc (scriptdata);
05444         
05445         langostypeparamerror (cantopencomponenterror, idserver);
05446         
05447         return (false);
05448         }
05449     
05450     *subtype = idserver;
05451     
05452     return (true);
05453     } /*openscriptcomponent*/
05454 
05455 
05456 boolean osagetcode (Handle htext, OSType idserver, boolean fljustexecutable, tyvaluerecord *vcode) {
05457     
05458     /*
05459     caller owns htext; we don't dispose it
05460     
05461     3.0b15 dmb: fixed memory leak -- must dispose script id after 
05462     it's been stored.
05463     */
05464     
05465     ComponentInstance comp;
05466     AEDesc scriptsource, scriptdata;
05467     OSAID id;
05468     long mode;
05469     boolean fl = false;
05470     OSAError err;
05471     
05472     comp = getosaserver (idserver);
05473     
05474     if (comp == nil) {
05475         
05476         langostypeparamerror (cantopencomponenterror, idserver);
05477         
05478         return (false);
05479         }
05480     
05481     #if TARGET_API_MAC_CARBON == 1 /*PBS 03/14/02: AE OS X fix.*/
05482         newdescwithhandle (&scriptsource, typeChar, htext);
05483     #else
05484         scriptsource.dataHandle = htext;
05485         scriptsource.descriptorType = typeChar;
05486     #endif
05487     
05488     id = kOSANullScript;
05489     
05490     err = OSACompile (comp, &scriptsource, kOSAModeCompileIntoContext, &id);
05491 
05492     #if TARGET_API_MAC_CARBON == 1
05493         AEDisposeDesc (&scriptsource);  /* 2004-10-27 aradke: we're done with it */
05494     #endif
05495     
05496     if (!osacomponenterror (comp, kOSANullScript, err)) {
05497         
05498         if (fljustexecutable)
05499             mode = kOSAModePreventGetSource;
05500         else
05501             mode = kOSANullMode;
05502         
05503         err = OSAStore (comp, id, typeOSAGenericStorage, mode, &scriptdata);
05504         
05505         fl = !oserror (err);
05506         
05507         OSADispose (comp, id); /*we're done with the id*/
05508         }
05509     
05510     if (!fl)
05511         return (false);
05512     
05513     #if TARGET_API_MAC_CARBON == 1 /*PBS 03/14/02: AE OS X fix.*/
05514         {
05515         Handle h;
05516         
05517         copydatahandle (&scriptdata, &h);
05518         
05519         fl = setbinaryvalue (h, scriptdata.descriptorType, vcode);
05520         
05521         AEDisposeDesc (&scriptdata);    /* 2004-10-27 aradke: we're done with it */
05522 
05523         return (fl);
05524         }
05525     #else
05526         return (setbinaryvalue (scriptdata.dataHandle, scriptdata.descriptorType, vcode));
05527     #endif
05528     } /*osagetcode*/
05529 
05530 
05531 boolean osagetsource (const tyvaluerecord *osaval, OSType *idserver, tyvaluerecord *vsource) {
05532     
05533     ComponentInstance comp;
05534     AEDesc scriptsource, scriptdata;
05535     OSAID idscript;
05536     OSAError err;
05537     
05538     if (!openscriptcomponent (osaval, &scriptdata, idserver, &comp))
05539         return (false);
05540     
05541     err = OSALoad (comp, &scriptdata, kOSANullMode, &idscript);
05542     
05543     AEDisposeDesc (&scriptdata);
05544     
05545     if (err == noErr) {
05546         
05547         err = OSAGetSource (comp, idscript, typeChar, &scriptsource);
05548         
05549         OSADispose (comp, idscript);
05550         }
05551     
05552     if (osacomponenterror (comp, kOSANullScript, err))
05553         return (false);
05554     
05555     #if TARGET_API_MAC_CARBON == 1 /*PBS 03/14/02: AE OS X fix.*/
05556         {
05557         Handle h;
05558         
05559         copydatahandle (&scriptsource, &h);
05560         
05561         AEDisposeDesc (&scriptsource);  /* 2004-10-28 aradke: we're done with it */
05562         
05563         return (setheapvalue (h, stringvaluetype, vsource));
05564         }
05565     #else
05566         return (setheapvalue (scriptsource.dataHandle, stringvaluetype, vsource));
05567     #endif
05568     } /*osagetsource*/
05569 
05570 
05571 boolean isosascriptnode (hdltreenode htree, tyvaluerecord *osaval) {
05572     
05573     register hdltreenode h = htree;
05574     
05575     if ((**h).nodetype == moduleop)
05576         h = (**h).param1;
05577     
05578     if ((**h).nodetype != osascriptop)
05579         return (false);
05580     
05581     *osaval = (**h).nodeval;
05582     
05583     return (true);
05584     } /*isosascriptnode*/
05585 
05586 
05587 #if 0 //not used
05588 
05589 boolean isosascriptvalue (const tyvaluerecord *val) {
05590     
05591     /*
05592     enforce a strong definition of an osascriptvalue: a binary whose type
05593     is 'scpt', that might be confused with a packed Frontier script
05594     */
05595     
05596     Handle x;
05597     OSErr err;
05598     OSType subtype;
05599     
05600     if ((*val).valuetype != binaryvaluetype)
05601         return (false);
05602     
05603     x = (*val).data.binaryvalue;
05604     
05605     if (getbinarytypeid (x) != typeOSAGenericStorage) /*it might be; can't tell*/
05606         return (false);
05607     
05608     err = OSAGetStorageType (x, &subtype);
05609     
05610     return (err == noErr);
05611     } /*isosascriptvalue*/
05612 
05613 #endif
05614 
05615 
05616 static pascal OSErr osaclientactive (long refcon) {
05617 #pragma unused (refcon)
05618 
05619     /*
05620     while executing an osa script, we want to check for cmd-period, 
05621     and allow other threads to run. this does part of the job.
05622     */
05623     
05624     OSErr err = noErr;
05625     
05626     #if !TARGET_API_MAC_CARBON
05627         long curA5 = SetUpAppA5 ();
05628     #endif
05629     
05630     if (!langbackgroundtask (false) || languserescaped (false))
05631         err = userCanceledErr;
05632     
05633     #if !TARGET_API_MAC_CARBON
05634         RestoreA5 (curA5);
05635     #endif
05636     
05637     return (err);
05638     } /*osaclientactive*/
05639 
05640 
05641 static pascal Boolean osaclientidleproc (EventRecord *ev, long *sleep, RgnHandle *mousergn) {
05642     
05643     /*
05644     2.1b11 dmb: we don't want to lose events when the server sends an 
05645     apple event.
05646     
05647     2.1b14 dmb: don't process null events, or scriptbackgroundtask will be 
05648     thrown off, and we won't be able to cancel
05649     */
05650     
05651     #pragma unused (sleep, mousergn)
05652     
05653     if ((*ev).what != nullEvent)
05654         shellprocessevent (ev);
05655     
05656     return (false); /*keep waiting*/
05657     } /*osaclientidleproc*/
05658 
05659 
05660 static pascal OSErr
05661 osaclientsend (
05662         const AppleEvent    *event,
05663         AppleEvent          *reply,
05664         AESendMode           sendmode,
05665         AESendPriority       priority,
05666         long                 timeout,
05667         AEIdleUPP            idleproc,
05668         AEFilterUPP          filterproc,
05669         long                 refcon)
05670 {
05671 #pragma unused (refcon, idleproc)
05672 
05673     /*
05674     with AppleScript, at least, the activeproc doesn't get called enough 
05675     for decent thread cooperation. ideally, we'd send this event using 
05676     the same logic as landsystem7send, sleeping this thread if possible. 
05677     but that would take too much new coding and testing to do now.  instead, 
05678     we'll just do what the active proc does before sending the event.
05679     
05680     3.0b14 dmb: must go through landglobals send callback; we're running as 
05681     part of a normal script.
05682     */
05683     
05684     OSErr err;
05685     register hdllandglobals hlg;
05686 
05687     #if !TARGET_API_MAC_CARBON
05688         long curA5 = SetUpAppA5 ();
05689     #endif
05690     
05691     hlg = landgetglobals ();
05692     
05693     err = (*(**hlg).eventsendcallback) (event, reply, sendmode, priority, timeout, osaclientidleUPP, filterproc);
05694 
05695     /*
05696     err = AESend (event, reply, sendmode, priority, timeout, osaclientidleUPP, filterproc);
05697     */
05698     
05699     if (err == noErr) {
05700         
05701         if (!langbackgroundtask (false) || languserescaped (false))
05702             err = userCanceledErr;
05703         }
05704     
05705     #if !TARGET_API_MAC_CARBON
05706         RestoreA5 (curA5);
05707     #endif
05708     
05709     return (err);
05710     } /*osaclientsend*/
05711 
05712 
05713 boolean evaluateosascript (const tyvaluerecord *osaval, hdltreenode hparam1, bigstring bsname, tyvaluerecord *vreturned) {
05714     
05715     /*
05716     2.1b5 dmb: set up an activeproc
05717     
05718     2.1b6 dmb: work around AS requirement that the subroutine name be 
05719     "canonified". I had a back-and-forth about this with Warren of the 
05720     AS team, and he's having their internationalization people "look 
05721     into" this problem.
05722     
05723     2.1b11 dmb: set up a send proc along with the activeproc.  also, don't 
05724     dispose of the script until we call osacomponenterror, so it can 
05725     be decompiled
05726     
05727     3.0b16 dmb: added call to dispose the script result id
05728     
05729     5.0d8 dmb: make sure to return false if an error is encountered.
05730     */
05731     
05732     ComponentInstance comp;
05733     OSType subtype;
05734     AEDesc scriptdata, scriptresult;
05735     OSAID idscript, idresult;
05736     AppleEvent event;
05737     OSAError err;
05738     boolean fl = false;
05739     
05740     if (!openscriptcomponent (osaval, &scriptdata, &subtype, &comp))
05741         return (false);
05742     
05743     err = OSALoad (comp, &scriptdata, kOSANullMode, &idscript);
05744     
05745     AEDisposeDesc (&scriptdata);
05746     
05747     if (err == errOSABadStorageType) {
05748         
05749         langparamerror (notfunctionerror, bsname);
05750         
05751         return (false);
05752         }
05753     
05754     if (oserror (err))
05755         return (false);
05756     
05757     //Code change by Timothy Paustian Sunday, September 3, 2000 10:45:59 PM
05758     //are theses globasl a problem?
05759     OSASetActiveProc (comp, osaclientactiveUPP, 0L); /*make sure we can cancel*/
05760     
05761     OSASetSendProc (comp, osaclientsendUPP, 0L); /*allows for better thread cooperation*/
05762             
05763     if (hparam1 == nil) { /*no parameters, just execute the code*/
05764         
05765         fl = true;
05766         
05767         err = OSAExecute (comp, idscript, kOSANullScript, kOSANullMode, &idresult);
05768         }
05769     else {
05770         
05771         if (subtype == 'ascr') /*work around AS bug; it requires "canonified" name as of 1.0.1*/
05772             alllower (bsname);
05773         
05774         fl = osabuildsubroutineevent (bsname, hparam1, &event);
05775         
05776         if (fl) {
05777             
05778             err = OSAExecuteEvent (comp, &event, idscript, kOSANullMode, &idresult);
05779             
05780             AEDisposeDesc (&event);
05781             }
05782         }
05783     
05784     OSASetActiveProc (comp, nil, 0L); /*clear it out -- AS does this too*/
05785     
05786     OSASetSendProc (comp, nil, 0L); /* ditto */
05787     
05788     if (fl)
05789         fl = !osacomponenterror (comp, idscript, err);
05790     
05791     if (fl) {
05792         
05793         if (idresult == kOSANullScript)
05794             fl = setbooleanvalue (true, vreturned);
05795         
05796         else {
05797             
05798             err = OSACoerceToDesc (comp, idresult, typeWildCard, kOSANullMode, &scriptresult);
05799             
05800             OSADispose (comp, idresult); /*3.0b16*/
05801             
05802             fl = !oserror (err) && setdescriptorvalue (scriptresult, vreturned);
05803             }
05804         }
05805     
05806     OSADispose (comp, idscript);
05807     
05808     return (fl);
05809     } /*evaluateosascript*/
05810 
05811 
05812 boolean evaluateosascriptevent (const tyvaluerecord *osaval, const AppleEvent *event, AppleEvent *reply) {
05813     
05814     /*
05815     2.1b11 dmb: pass a valid script id to osacomponent error for proper 
05816     reporting
05817     */
05818     
05819     ComponentInstance comp;
05820     AEDesc scriptdata;
05821     OSAID idscript;
05822     OSType tosstype;
05823     OSAError err;
05824     boolean fl = false;
05825     
05826     if (!openscriptcomponent (osaval, &scriptdata, &tosstype, &comp))
05827         return (false);
05828     
05829     err = OSALoad (comp, &scriptdata, kOSANullMode, &idscript);
05830     
05831     AEDisposeDesc (&scriptdata);
05832     
05833     if (!osacomponenterror (comp, kOSANullScript, err)) {
05834         
05835         err = OSADoEvent (comp, event, idscript, kOSANullMode, reply);
05836         
05837         fl = !osacomponenterror (comp, idscript, err);
05838         
05839         OSADispose (comp, idscript);
05840         }
05841     
05842     return (fl);
05843     } /*evaluateosascriptevent*/
05844 
05845 
05846 boolean osafindclienteventfilter (long clienta5, long *eventfilter) {
05847     
05848     /*
05849     3.0b15 dmb: look to see if any menu sharing client has the specified 
05850     a5 value and a non-nil event filter. if such a client exists, set  
05851     eventfilter and return true.
05852     
05853     we should really create a generalize client visit routine, but this 
05854     is a very last-minute change, so I'm doing as little as possible.
05855     */
05856     
05857     register hdlcomponentglobals hclient;
05858     
05859     for (hclient = (**hclientlist).hfirst; hclient != nil; hclient = (**hclient).hnext) {
05860         
05861         if ((**hclient).clienta5 == clienta5) { /*maybe the one*/
05862             
05863             *eventfilter = (long) (**hclient).menusharingglobals.eventfiltercallback;
05864             
05865             if (*eventfilter != 0)
05866                 return (true);
05867             }
05868         }
05869     
05870     return (false);
05871     } /*osafindclienteventfilter*/
05872 
05873 
05874 static boolean osagethomeresfile (void) {
05875     
05876     homeresfile = CurResFile ();
05877     
05878     #if !TARGET_API_MAC_CARBON
05879     homeresmap = LMGetTopMapHndl ();
05880     #endif
05881 
05882     return (true);
05883     } /*osagethomeresfile*/
05884 
05885 
05886 Component osaregistercomponent (OSType type, long flags, ComponentRoutine dispatch, short idname, short iddescription) {
05887     
05888     /*
05889     2.1b11 dmb: new routine consolodates code for registering all 
05890     Frontier components.  strings are now in a single STR# resource
05891     
05892     7.0b17 AR: For Radio UserLand (Pike), register all OSA components as local.
05893     It looks like this will allow us to run Frontier and Radio UserLand simultaneously.
05894     */
05895     
05896     ComponentDescription desc;
05897     Handle hname, hdescription, hicon;
05898     Component comp;
05899     #ifdef PIKE
05900         short global = 0;
05901     #else
05902         short global = registerComponentGlobal;
05903     #endif
05904     
05905     desc.componentType = type;
05906     
05907     desc.componentSubType = 'LAND';
05908     
05909     desc.componentManufacturer = 'LAND';
05910     
05911     desc.componentFlags = flags;
05912     
05913     desc.componentFlagsMask = 0;
05914     
05915     hname = getcomponentstringhandle (idname);
05916     
05917     hdescription = getcomponentstringhandle (iddescription);
05918     
05919     hicon = GetIcon (129);
05920     
05921     #if TARGET_API_MAC_CARBON == 1
05922         comp = RegisterComponent (&desc, NewComponentRoutineUPP (dispatch),
05923                                         global, hname, hdescription, hicon);
05924     #else
05925         comp = RegisterComponent (&desc, NewComponentRoutineProc (dispatch),
05926                                         global, hname, hdescription, hicon);
05927     #endif
05928     
05929     disposehandle (hname);
05930     
05931     disposehandle (hdescription);
05932     
05933     #if !TARGET_API_MAC_CARBON
05934         if (comp != nil)
05935             SetComponentRefcon (comp, (long) LMGetCurrentA5 ());
05936     #endif
05937 
05938     return (comp);
05939     } /*osaregistercomponent*/
05940 
05941 
05942 static boolean initosacomponent (void) {
05943     
05944     /*
05945     register the scripting component.
05946     
05947     all attempts to use a resource-based thng failed miserably. this might be 
05948     a bug in the system, but the only reason to was attempted was to get use 
05949     of the OpenComponentResFile call, which we do manually.
05950     
05951     7.0b17 AR: After we have registered the component successfully,
05952     open it and create an instance for our own use. This is to avoid
05953     cross-talk between Frontier and Radio UserLand when running MacBird cards.
05954     */
05955     
05956     long flags = 0;
05957     
05958     #if !TARGET_API_MAC_CARBON
05959         RememberA5 ();
05960     #endif
05961     
05962     #if TARGET_API_MAC_CARBON == 1
05963         homezone = LMGetApplZone();
05964     #else   
05965         homezone = GetZone ();
05966     #endif
05967 
05968     osagethomeresfile();
05969     
05970     //Code change by Timothy Paustian Friday, July 21, 2000 11:18:39 PM
05971     //create all the osa UPPs
05972     #if TARGET_API_MAC_CARBON
05973         osaclientactiveDesc = NewOSAActiveUPP(osaclientactive);
05974         osaclientsendDesc = NewOSASendUPP(osaclientsend);
05975         osaclientidleDesc = NewAEIdleUPP(osaclientidleproc);
05976         osadefaultactiveDesc = NewOSAActiveUPP(osadefaultactiveproc);
05977         osadefaultcreateDesc = NewOSACreateAppleEventUPP(osadefaultcreate);
05978         osadefaultsendDesc = NewOSASendUPP(osadefaultsend);
05979     #endif
05980         
05981     shellpushfilehook (&osagethomeresfile);
05982     
05983     flags = kOSASupportsCompiling |
05984             kOSASupportsGetSource |
05985             kOSASupportsConvenience |
05986             kOSASupportsRecording |
05987             kOSASupportsAECoercion |
05988             kOSASupportsAESending |
05989             kOSASupportsEventHandling;
05990     
05991             
05992     osacomponent = osaregistercomponent (kOSAComponentType, flags, &osaDispatch, usertalkstring, scriptingcomponentstring);
05993     
05994     if (osacomponent != nil) { /*7.0b17 AR*/
05995 
05996         ComponentInstance instance;
05997         
05998         instance = OpenComponent (osacomponent);
05999         
06000         if (instance != nil)
06001             addosaserver (instance, 'LAND');
06002         }
06003 
06004     
06005     return (osacomponent != nil);
06006     } /*initosacomponent*/
06007 
06008 
06009 boolean havecomponentmanager (void) {
06010 
06011     long result;
06012     
06013     if (!gestalt (gestaltComponentMgr, &result))
06014         return (false);
06015     
06016     return (result != 0);
06017     
06018     } /*havecomponentmanager*/
06019 
06020 
06021 boolean getprocessname (ProcessSerialNumber psn, bigstring bsname, boolean *flbackgroundonly) {
06022     
06023     ProcessInfoRec info;
06024     
06025     info.processInfoLength = (long) sizeof (info);
06026     
06027     info.processName = bsname;
06028     
06029     info.processAppSpec = nil;
06030     
06031     if (GetProcessInformation (&psn, &info) != noErr)
06032         return (false);
06033     
06034     *flbackgroundonly = (info.processMode & modeOnlyBackground) != 0;
06035     
06036     return (true);
06037     } /*getprocessname*/
06038 
06039 
06040 boolean osacomponentverifyshutdown (void) {
06041     
06042     /*
06043     see if we have any active clients; if so, warn user before and confirm 
06044     shutdown.
06045     
06046     note: need to closeosaservers first since we may be our own client
06047     
06048     2.1b5 dmb: check clientlist for nil in case it was never created
06049     
06050     2.1b11 dmb: don't alert users about menusharing clients
06051     */
06052     
06053     register hdlcomponentglobals hclient;
06054     short ctclients;
06055     short lidstring;
06056     bigstring bs;
06057     bigstring bsprompt;
06058     typrocessid psn, lastpsn;
06059     boolean flbackgroundonly;
06060     Boolean flsame;
06061     
06062     if (hclientlist == nil) /*list never initted, probably no component manager*/
06063         return (true);
06064     
06065     closeosaservers (); /*this is ok to do, event if we don't end up quitting*/
06066     
06067     ctclients = 0;
06068     
06069     lastpsn.highLongOfPSN = lastpsn.lowLongOfPSN = kNoProcess;
06070     
06071     for (hclient = (**hclientlist).hfirst; hclient != nil; hclient = (**hclient).hnext) {
06072         
06073         psn = (**hclient).clientpsn;
06074         
06075         if ((**hclient).menusharingglobals.clientid != 0) /*ignore menusharing clients*/
06076             return (true);
06077         
06078         if (!getprocessname (psn, bs, &flbackgroundonly)) /*must be gone*/
06079             continue;
06080         
06081         if (flbackgroundonly)
06082             continue;
06083         
06084         if ((SameProcess (&lastpsn, &psn, &flsame) == noErr) && flsame)
06085             continue;
06086         
06087         if (iscurrentapplication (psn)) /*frontier is it's own client -- maybe ran a card?*/
06088             continue;
06089         
06090         lastpsn = psn;
06091         
06092         ++ctclients;
06093         }
06094     
06095     if (ctclients > 0) {
06096         
06097         if (ctclients == 1)
06098             lidstring = specificclientstring;
06099         
06100         else {
06101             numbertostring (ctclients, bs);
06102             
06103             lidstring = multipleclientsstring;
06104             }
06105         
06106         if (getrecordingstring (lidstring, bsprompt)) {
06107             
06108             parsedialogstring (bsprompt, bs, nil, nil, nil, bsprompt);
06109             
06110             if (!msgdialog (bsprompt))
06111                 return (false);
06112             }
06113         }
06114     
06115     return (true);
06116     } /*osacomponentverifyshutdown*/
06117 
06118 
06119 static boolean servingsharedmenus (hdlcomponentglobals *hclient) {
06120     
06121     /*
06122     look to see if any component client has a set of shared menus 
06123     in place. if such a client exists and is a valid process, set 
06124     bs to its name and return true. otherwise, return false.
06125     
06126     2.1b13 dmb: changed the criteria for deciding whether we're serving 
06127     shared menus. if the menusharingglobals clientid is non-zero, we 
06128     need to wait for a disconnect event when hsharedmenus is nil. 
06129     otherwise, we won't have a chance to remove our event handler from 
06130     the client
06131     
06132     2.1b13 dmb: for runtime, don't wait for own disconnect, because it's 
06133     already happened.
06134     
06135     3.0.4b6 dmb: return component globals of found client, not its process
06136     name. that way, caller can send it an apple event.
06137     */
06138     
06139     register hdlcomponentglobals hcg;
06140     typrocessid psn;
06141     bigstring bs;
06142     boolean flbackgroundonly;
06143     
06144     for (hcg = (**hclientlist).hfirst; hcg != nil; hcg = (**hcg).hnext) {
06145         
06146         psn = (**hcg).clientpsn;
06147         
06148         if (!getprocessname (psn, bs, &flbackgroundonly)) /*must be gone*/
06149             continue;
06150         
06151         #if flruntime
06152         
06153         if (iscurrentapplication (psn)) /*runtime is it's own menusharing client*/
06154             continue;
06155         
06156         #endif
06157         
06158         if ((**hcg).menusharingglobals.clientid != 0) {
06159         
06160             *hclient = hcg;
06161             
06162             return (true);
06163             }
06164         }
06165     
06166     return (false);
06167     } /*servingsharedmenus*/
06168 
06169 
06170 static pascal OSErr sendmenusharingshutdownevent (hdlcomponentglobals hcg) {
06171     
06172     /*
06173     3.0.4b6 dmb: send event to menu sharing client that will be handled by 
06174     the handler we installed during initialization. the handler will 
06175     shutdown cleanly within the client process.  If the client had menus 
06176     installed, it should have already receive one of these as we disposed 
06177     the menubar.
06178     
06179     5.0d3 dmb: send event with kAENoReply, not kAEWaitReply. Otherwise we can 
06180     deallock. Our caller is looping and will keep sending the event until it
06181     is handled.
06182     */
06183     
06184     AEDesc desc;
06185     OSErr err;
06186     ProcessSerialNumber psn = (**hcg).clientpsn;
06187     AppleEvent event, reply;
06188     
06189     err = AECreateDesc (typeProcessSerialNumber, (Ptr) &psn, sizeof (psn), &desc);
06190     
06191     if (err == noErr) {
06192         
06193         err = AECreateAppleEvent ((**hcg).clientid, idupdatemenus, &desc, kAutoGenerateReturnID, kAnyTransactionID, &event);
06194         
06195         AEDisposeDesc (&desc);
06196         
06197         if (err == noErr) {
06198             
06199             err = AESend (&event, &reply, 
06200                 
06201                 (AESendMode) kAEDontRecord + kAECanSwitchLayer + kAECanInteract + kAENoReply, 
06202                 
06203                 (AESendPriority) kAEHighPriority, (long) kAEDefaultTimeout, nil, nil);
06204             
06205             AEDisposeDesc (&event);
06206             
06207             AEDisposeDesc (&reply);
06208             }
06209         }
06210     
06211     return (err);
06212     } /*sendmenusharingshutdownevent*/
06213 
06214 
06215 #define shutdowntimeout 10 /*seconds we'll wait for clients to receive update menus event*/
06216 
06217 
06218 void osacomponentshutdown (void) {
06219     
06220     /*
06221     2.1b8 dmb: wait for up to 4 seconds for menu sharing clients to 
06222     receive langipcmenu.c's 'done' message and dipose their shared 
06223     menus. after that, it's safe for us to go away
06224     
06225     2.1b13 dmb: oops, timenow is seconds, not ticks; we were waiting 
06226     600 seconds instead of 10.
06227     
06228     3.0.1b2 dmb: check clientlist for nil in case it was never created
06229     
06230     3.0.4b6 dmb: force client to shutdown menus explicitly by sending 
06231     it a dirty menus event. If it never had menus, it wouldn't otherwise
06232     receive this event.
06233     */
06234     
06235     unsigned long startloop;
06236     hdlcomponentglobals hclient;
06237     
06238     flosashutdown = true; /*make sure we remove AE handlers*/
06239     
06240     if (hclientlist == nil) /*list never initted, probably no component manager*/
06241         return;
06242     
06243     closeosaservers ();
06244     
06245     startloop = timenow ();
06246     
06247     while (servingsharedmenus (&hclient)) {
06248         
06249         sendmenusharingshutdownevent (hclient); // 3.0.4b6 dmb
06250         
06251         shellpartialeventloop (osMask);
06252         
06253         if (timenow () - startloop > shutdowntimeout)
06254             break;
06255         }
06256     
06257     AERemoveCoercionHandler (typeType, typeObjectSpecifier, coerceTypetoObjUPP, true);
06258 
06259     #if TARGET_API_MAC_CARBON
06260         DisposeAECoerceDescUPP(coerceTypetoObjDesc);
06261         
06262         //the next three are used to send apple events
06263         DisposeOSAActiveUPP(osaclientactiveDesc);
06264         DisposeOSASendUPP(osaclientsendDesc);
06265         DisposeAEIdleUPP(osaclientidleDesc);
06266         
06267         DisposeOSAActiveUPP(osadefaultactiveDesc);
06268         DisposeOSACreateAppleEventUPP(osadefaultcreateDesc);
06269         DisposeOSASendUPP(osadefaultsendDesc);
06270     #endif
06271     } /*osacomponentshutdown*/
06272 
06273 
06274 boolean osacomponentstart (void) {
06275     
06276     /*
06277     2.1b4 dmb: register any thng's that might have been added to Frontier's 
06278     resource fork.  (we may want to bake in an app runner at some point, or 
06279     perhaps something else.)
06280     
06281     3.0b15 dmb: grab our own psn now.  used in osapartialeventloop.
06282     */
06283     
06284     if (!havecomponentmanager ())
06285         return (false);
06286     
06287     GetCurrentProcess (&homepsn);
06288 
06289     if (!initosaservers ())
06290         return (false);
06291     
06292     initosacomponent ();
06293     
06294     #if !defined(PIKE) && !TARGET_API_MAC_CARBON
06295         initmenusharingcomponent ();
06296     #endif
06297     
06298     #if !flruntime
06299         initwindowsharingcomponent ();
06300     #endif
06301     
06302     #ifdef dropletcomponent
06303         initdropletcomponent ();
06304     #endif
06305     
06306     RegisterComponentResourceFile (filegetapplicationrnum (), true); /*2.1b4*/
06307     
06308     #if TARGET_API_MAC_CARBON
06309         coerceTypetoObjDesc = NewAECoerceDescUPP((AECoerceDescProcPtr)coerceTypetoObj);
06310     #endif
06311 
06312     AEInstallCoercionHandler (typeType, typeObjectSpecifier, coerceTypetoObjUPP, 0, true, true);
06313     
06314     return (newclearhandle (longsizeof (tyclientlist), (Handle *) &hclientlist));
06315     } /*osacomponentstart*/
06316 

Generated on Wed May 31 18:19:58 2006 for frontierkernel 10.1.10a by  doxygen 1.4.6