os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/mac/tclMacLibrary.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/mac/tclMacLibrary.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,248 @@
     1.4 +/*
     1.5 + * tclMacLibrary.c --
     1.6 + *
     1.7 + *	This file should be included in Tcl extensions that want to 
     1.8 + *	automatically open their resource forks when the code is linked. 
     1.9 + *	These routines should not be exported but should be compiled 
    1.10 + *	locally by each fragment.  Many thanks to Jay Lieske
    1.11 + *	<lieske@princeton.edu> who provide an initial version of this
    1.12 + *	file.
    1.13 + *
    1.14 + * Copyright (c) 1996 Sun Microsystems, Inc.
    1.15 + *
    1.16 + * See the file "license.terms" for information on usage and redistribution
    1.17 + * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
    1.18 + *
    1.19 + * RCS: @(#) $Id: tclMacLibrary.c,v 1.5 2001/11/23 01:27:39 das Exp $
    1.20 + */
    1.21 +
    1.22 +/*
    1.23 + * Here is another place that we are using the old routine names...
    1.24 + */
    1.25 +
    1.26 +#include <CodeFragments.h>
    1.27 +#include <Errors.h>
    1.28 +#include <Resources.h>
    1.29 +#include <Strings.h>
    1.30 +#include "tclMacInt.h"
    1.31 +
    1.32 +#if defined(TCL_REGISTER_LIBRARY) && defined(USE_TCL_STUBS)
    1.33 +#error "Can't use TCL_REGISTER_LIBRARY and USE_TCL_STUBS at the same time!"
    1.34 +/*
    1.35 + * Can't register a library with Tcl when using stubs in the current
    1.36 + * implementation, since Tcl_InitStubs hasn't been called yet
    1.37 + *  when OpenLibraryResource is executing. 
    1.38 + */
    1.39 +#endif
    1.40 +
    1.41 +/*
    1.42 + * These function are not currently defined in any header file.  The
    1.43 + * only place they should be used is in the Initialization and
    1.44 + * Termination entry points for a code fragment.  The prototypes
    1.45 + * are included here to avoid compile errors.
    1.46 + */
    1.47 +
    1.48 +OSErr TclMacInitializeFragment _ANSI_ARGS_((
    1.49 +			struct CFragInitBlock* initBlkPtr));
    1.50 +void TclMacTerminateFragment _ANSI_ARGS_((void));
    1.51 +
    1.52 +/*
    1.53 + * Static functions in this file.
    1.54 + */
    1.55 +
    1.56 +static OSErr OpenLibraryResource _ANSI_ARGS_((
    1.57 +			struct CFragInitBlock* initBlkPtr));
    1.58 +static void CloseLibraryResource _ANSI_ARGS_((void));
    1.59 +
    1.60 +/* 
    1.61 + * The refnum of the opened resource fork.
    1.62 + */
    1.63 +static short ourResFile = kResFileNotOpened;
    1.64 +
    1.65 +/*
    1.66 + * This is the resource token for the our resource file.
    1.67 + * It stores the name we registered with the resource facility.
    1.68 + * We only need to use this if we are actually registering ourselves.
    1.69 + */
    1.70 +  
    1.71 +#ifdef TCL_REGISTER_LIBRARY
    1.72 +static Tcl_Obj *ourResToken;
    1.73 +#endif
    1.74 +
    1.75 +/*
    1.76 + *----------------------------------------------------------------------
    1.77 + *
    1.78 + * TclMacInitializeFragment --
    1.79 + *
    1.80 + *	Called by MacOS CFM when the shared library is loaded. All this
    1.81 + *	function really does is give Tcl a chance to open and register
    1.82 + *	the resource fork of the library. 
    1.83 + *
    1.84 + * Results:
    1.85 + *	MacOS error code if loading should be canceled.
    1.86 + *
    1.87 + * Side effects:
    1.88 + *	Opens the resource fork of the shared library file.
    1.89 + *
    1.90 + *----------------------------------------------------------------------
    1.91 + */
    1.92 +
    1.93 +OSErr
    1.94 +TclMacInitializeFragment(
    1.95 +    struct CFragInitBlock* initBlkPtr)		/* Pointer to our library. */
    1.96 +{
    1.97 +    OSErr err = noErr;
    1.98 +
    1.99 +#ifdef __MWERKS__
   1.100 +    {
   1.101 +    	extern OSErr __initialize( CFragInitBlock* initBlkPtr);
   1.102 +    	err = __initialize((CFragInitBlock *) initBlkPtr);
   1.103 +    }
   1.104 +#endif
   1.105 +    if (err == noErr)
   1.106 +    	err = OpenLibraryResource( initBlkPtr);
   1.107 +    return err;
   1.108 +}
   1.109 +
   1.110 +/*
   1.111 + *----------------------------------------------------------------------
   1.112 + *
   1.113 + * TclMacTerminateFragment --
   1.114 + *
   1.115 + *	Called by MacOS CFM when the shared library is unloaded.
   1.116 + *
   1.117 + * Results:
   1.118 + *	None.
   1.119 + *
   1.120 + * Side effects:
   1.121 + *	The resource fork of the code fragment is closed.
   1.122 + *
   1.123 + *----------------------------------------------------------------------
   1.124 + */
   1.125 +
   1.126 +void 
   1.127 +TclMacTerminateFragment()
   1.128 +{
   1.129 +    CloseLibraryResource();
   1.130 +
   1.131 +#ifdef __MWERKS__
   1.132 +    {
   1.133 +    	extern void __terminate(void);
   1.134 +    	__terminate();
   1.135 +    }
   1.136 +#endif
   1.137 +}
   1.138 +
   1.139 +/*
   1.140 + *----------------------------------------------------------------------
   1.141 + *
   1.142 + * OpenLibraryResource --
   1.143 + *
   1.144 + *	This routine can be called by a MacOS fragment's initialiation 
   1.145 + *	function to open the resource fork of the file.  
   1.146 + *	Call it with the same data passed to the initialization function. 
   1.147 + *	If the fragment loading should fail if the resource fork can't 
   1.148 + *	be opened, then the initialization function can pass on this 
   1.149 + *	return value.
   1.150 + *
   1.151 + *      If you #define TCL_REGISTER_RESOURCE before compiling this resource, 
   1.152 + *	then your library will register its open resource fork with the
   1.153 + *      resource command.
   1.154 + *
   1.155 + * Results:
   1.156 + *	It returns noErr on success and a MacOS error code on failure.
   1.157 + *
   1.158 + * Side effects:
   1.159 + *	The resource fork of the code fragment is opened read-only and 
   1.160 + *	is installed at the head of the resource chain.
   1.161 + *
   1.162 + *----------------------------------------------------------------------
   1.163 + */
   1.164 +
   1.165 +static OSErr 
   1.166 +OpenLibraryResource(
   1.167 +    struct CFragInitBlock* initBlkPtr)
   1.168 +{
   1.169 +    /*
   1.170 +     * The 3.0 version of the Universal headers changed CFragInitBlock
   1.171 +     * to an opaque pointer type.  CFragSystem7InitBlock is now the
   1.172 +     * real pointer.
   1.173 +     */
   1.174 +     
   1.175 +#if !defined(UNIVERSAL_INTERFACES_VERSION) || (UNIVERSAL_INTERFACES_VERSION < 0x0300)
   1.176 +    struct CFragInitBlock *realInitBlkPtr = initBlkPtr;
   1.177 +#else 
   1.178 +    CFragSystem7InitBlock *realInitBlkPtr = (CFragSystem7InitBlock *) initBlkPtr;
   1.179 +#endif
   1.180 +    FSSpec* fileSpec = NULL;
   1.181 +    OSErr err = noErr;
   1.182 +    
   1.183 +
   1.184 +    if (realInitBlkPtr->fragLocator.where == kDataForkCFragLocator) {
   1.185 +    	fileSpec = realInitBlkPtr->fragLocator.u.onDisk.fileSpec;
   1.186 +    } else if (realInitBlkPtr->fragLocator.where == kResourceCFragLocator) {
   1.187 +    	fileSpec = realInitBlkPtr->fragLocator.u.inSegs.fileSpec;
   1.188 +    } else {
   1.189 +    	err = resFNotFound;
   1.190 +    }
   1.191 +
   1.192 +    /*
   1.193 +     * Open the resource fork for this library in read-only mode.  
   1.194 +     * This will make it the current res file, ahead of the 
   1.195 +     * application's own resources.
   1.196 +     */
   1.197 +    
   1.198 +    if (fileSpec != NULL) {
   1.199 +	ourResFile = FSpOpenResFile(fileSpec, fsRdPerm);
   1.200 +	if (ourResFile == kResFileNotOpened) {
   1.201 +	    err = ResError();
   1.202 +	} else {
   1.203 +#ifdef TCL_REGISTER_LIBRARY
   1.204 +	    ourResToken = Tcl_NewObj();
   1.205 +	    Tcl_IncrRefCount(ourResToken);
   1.206 +	    p2cstr(realInitBlkPtr->libName);
   1.207 +	    Tcl_SetStringObj(ourResToken, (char *) realInitBlkPtr->libName, -1);
   1.208 +	    c2pstr((char *) realInitBlkPtr->libName);
   1.209 +	    TclMacRegisterResourceFork(ourResFile, ourResToken,
   1.210 +	            TCL_RESOURCE_DONT_CLOSE);
   1.211 +#endif
   1.212 +            SetResFileAttrs(ourResFile, mapReadOnly);
   1.213 +	}
   1.214 +    }
   1.215 +    
   1.216 +    return err;
   1.217 +}
   1.218 +
   1.219 +/*
   1.220 + *----------------------------------------------------------------------
   1.221 + *
   1.222 + * CloseLibraryResource --
   1.223 + *
   1.224 + *	This routine should be called by a MacOS fragment's termination 
   1.225 + *	function to close the resource fork of the file 
   1.226 + *	that was opened with OpenLibraryResource.  
   1.227 + *
   1.228 + * Results:
   1.229 + *	None.
   1.230 + *
   1.231 + * Side effects:
   1.232 + *	The resource fork of the code fragment is closed.
   1.233 + *
   1.234 + *----------------------------------------------------------------------
   1.235 + */
   1.236 +
   1.237 +static void
   1.238 +CloseLibraryResource()
   1.239 +{
   1.240 +    if (ourResFile != kResFileNotOpened) {
   1.241 +#ifdef TCL_REGISTER_LIBRARY
   1.242 +        int length;
   1.243 +        TclMacUnRegisterResourceFork(
   1.244 +	        Tcl_GetStringFromObj(ourResToken, &length),
   1.245 +                NULL);
   1.246 +        Tcl_DecrRefCount(ourResToken);
   1.247 +#endif
   1.248 +	CloseResFile(ourResFile);
   1.249 +	ourResFile = kResFileNotOpened;
   1.250 +    }
   1.251 +}