/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 1, or (at your option)
** any later version.

** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.

** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
 * Author : Alexandre Parenteau <aubonbeurre@hotmail.com> --- February 2001
 */

/*
 * TclGlue.mac.cpp --- glue to the TCL language specific to mac/carbon
 */

#include "stdafx.h"

#if qCarbon

#ifndef NEW
#define NEW new
#endif

#include "TclGlue.mac.h"
#include <CFBundle.h>
#include <stdlib.h>
#include <string.h>

#if TARGET_RT_MAC_MACHO
# include "AppConsole.h"
# include <mach-o/dyld.h>
#endif

// struct to keep necessary logistic data for maintaining a connection
// to a shared library on various implementations
//
struct _CompatConnectID
{
	CFURLRef		  bundleURL;
	CFBundleRef		myBundle;
#if TARGET_RT_MAC_MACHO
	const struct mach_header *      dyld_header;
#endif
  _CompatConnectID() : 
      bundleURL(NULL), 
      myBundle(NULL)
#if TARGET_RT_MAC_MACHO
     ,dyld_header(NULL)
#endif
  {
  }
};

#if TARGET_RT_MAC_CFM
static ProcPtr getSymFunctionCFM(CompatConnectID connID, const Str255 name)
{
	long symCount;
	Ptr symAddr;
	int i;
	OSErr err = CountSymbols(connID, &symCount);
	if(err != noErr)
		return 0L;
	
	for(i = 0; i < symCount; i++)
	{
		CFragSymbolClass symClass;
		Str255 symName;
		err = GetIndSymbol(connID, i, symName, &symAddr, &symClass);
		if(err != noErr)
			return 0L;
		if(memcmp(symName, name, name[0]) == 0)
			break;
	}
	if(i == symCount)
		return 0L;
	return (ProcPtr)symAddr;
}
#endif // TARGET_RT_MAC_CFM

int CarbonCompatLoadLibrary(CompatConnectID * connID, const char *name_dll)
{
	if(connID == 0L)
		return 0;

#if TARGET_RT_MAC_CFM
	if (UEnvironment::GetOSVersion() < 0x1000)
	{
		OSErr err;
		Ptr mainAddr;
		Str255 errMessage;
		Str255 pname;

		c2pstrcpy(pname, name_dll);
		err = GetSharedLibrary(pname, kPowerPCCFragArch, kPrivateCFragCopy, connID, &mainAddr, errMessage);
		return err == noErr;
	}
	else
#endif // TARGET_RT_MAC_CFM
	{
		*connID = 0L;

		_CompatConnectID *c = new _CompatConnectID;
		
		*connID = (CompatConnectID)c;
		
		if(c == 0L)
			return 0;

		CFStringRef s = CFStringCreateWithCString(0L, name_dll, kCFStringEncodingUTF8);
		if(s == 0L)
			goto fail;
		
		c->bundleURL	= CFURLCreateWithFileSystemPath(0L, s, kCFURLPOSIXPathStyle, false );
		if(c->bundleURL == 0L)
			goto fail;

		c->myBundle	= CFBundleCreate(0L, c->bundleURL );
		if(c->myBundle == 0L)
		{
#if TARGET_RT_MAC_MACHO
      // check whether file exists
      //
      FSRef   libFile;
      FSCatalogInfo   libFileInfo;
      if ( CFURLGetFSRef(c->bundleURL, &libFile)  &&  
           FSGetCatalogInfo(&libFile, kFSCatInfoNodeFlags, &libFileInfo, NULL, NULL, NULL) == noErr  &&
           (libFileInfo.nodeFlags & kFSNodeIsDirectoryMask) == 0 )
      {
        CFRelease(c->bundleURL);
        c->bundleURL = 0L;

        c->dyld_header = NSAddImage(name_dll, NSADDIMAGE_OPTION_NONE);
        if ( c->dyld_header == NULL ) 
        {
      		cvs_err("Error loading %s\n", name_dll);
      		goto fail;
        }
      }
      else goto fail; // not a file or file not found
#else
			goto fail;
#endif
    }
    else
    {
  		if(!CFBundleLoadExecutable( c->myBundle ))
  			goto fail;
    }
    
		CFRelease( s );
		return 1;
	fail:
		if(s != 0L)
			CFRelease( s );
		if(c != 0L)
		{
			if(c->bundleURL != 0L)
			  CFRelease( c->bundleURL );
			if(c->myBundle != 0L)
			  CFRelease( c->myBundle );
			*connID = 0L;
			delete c;
		}
		return 0;
	}
}

int CarbonCompatCloseLibrary(CompatConnectID * connID)
{
	if(connID == 0L)
		return 0;

#if TARGET_RT_MAC_CFM
	if (UEnvironment::GetOSVersion() < 0x1000)
	{
		CloseConnection(connID);
	}
	else
#endif // TARGET_RT_MAC_CFM
	{
		_CompatConnectID *c = (_CompatConnectID *)*connID;
		if(c != 0L)
		{
#if TARGET_RT_MAC_MACHO
			if ( c->dyld_header )
			{
// %%% ??? how to unload an image ???
			}
			else
#endif
			{
  			CFBundleUnloadExecutable( c->myBundle );
  			CFRelease( c->bundleURL );
  			CFRelease( c->myBundle );
  		}
			delete c;
		}
		*connID = 0L;
	}
	return 1;
}

void *_CarbonCompatCallLibrary(CompatConnectID connID, const char *name_func)
{
#if TARGET_RT_MAC_CFM
	if (UEnvironment::GetOSVersion() < 0x1000)
	{
		ProcPtr symAddr;
		Str255 pname;

		c2pstrcpy(pname, name_func);

		symAddr = getSymFunctionCFM(connID, pname);
#if qCvsDebug
    if ( symAddr == NULL ) cvs_err("_CarbonCompatCallLibrary(CFM): symbol '%s' not found!\n", name_func);
#endif
		return symAddr;
	}
	else
#endif // TARGET_RT_MAC_CFM
	{
		_CompatConnectID *c = (_CompatConnectID *)connID;
#if TARGET_RT_MAC_MACHO
    if ( c->dyld_header )
    {
    	char funcname[258];

    	sprintf(funcname, "_%.200s", name_func);
    	if (NSIsSymbolNameDefinedInImage(c->dyld_header, funcname)) {
    	  NSSymbol theSym;
    	  void* p;
    		theSym = NSLookupSymbolInImage(c->dyld_header, funcname, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND);
    		p = NSAddressOfSymbol(theSym);
#if qCvsDebug
        if ( p == NULL ) cvs_err("_CarbonCompatCallLibrary(dyld): symbol '%s' not found!\n", name_func);
#endif
    		return p;
    	}
    	
#if qCvsDebug
      cvs_err("_CarbonCompatCallLibrary(dyld): symbol '%s' not found!\n", name_func);
#endif
    	return 0L;
    }
    else
#endif
		{
  		CFStringRef s = CFStringCreateWithCString(0L, name_func, kCFStringEncodingMacRoman);
  		if(s == 0L)
  			return 0L;
  		
  		void *ptr = CFBundleGetFunctionPointerForName(c->myBundle, s);
  		CFRelease( s );
    
#if qCvsDebug
      if ( ptr == NULL ) cvs_err("_CarbonCompatCallLibrary(CFBundle): symbol '%s' not found!\n", name_func);
#endif
		  return ptr;
    }
	}
	
#if qCvsDebug
  cvs_err("_CarbonCompatCallLibrary: symbol '%s' not found!\n", name_func);
#endif

	return 0L;
}

#define CompatLoadLibrary CarbonCompatLoadLibrary
#define CompatCloseLibrary CarbonCompatCloseLibrary
#define _CompatCallLibrary _CarbonCompatCallLibrary

#if TARGET_RT_MAC_CFM
void *_tcl_cfm_macho(void *func)
{
	static UInt32 templ[6] = {0x3D800000, 0x618C0000, 0x800C0000, 0x804C0004, 0x7C0903A6, 0x4E800420};

	// nothing to do on OS9, cause we call a CFM code in this case
	if (UEnvironment::GetOSVersion() < 0x1000)
		return func;
	
#pragma message(" Memory leak here, fix me !")
	UInt32	*mfp = (UInt32*) NewPtr( sizeof(templ) );		//	Must later dispose of allocated memory
	if(mfp == 0L)
		return 0L;
	
    mfp[0] = templ[0] | ((UInt32)func >> 16);
    mfp[1] = templ[1] | ((UInt32)func & 0xFFFF);
    mfp[2] = templ[2];
    mfp[3] = templ[3];
    mfp[4] = templ[4];
    mfp[5] = templ[5];
    MakeDataExecutable( mfp, sizeof(templ) );
    
    return mfp;
}
#endif // TARGET_RT_MAC_CFM

#if 0 // TARGET_RT_MAC_MACHO
typedef void *dl_funcptr;

static dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname, const char *pathname)
{
	dl_funcptr p = NULL;
	char funcname[258];

	sprintf(funcname, "_init%.200s", shortname);

	/* This is also NeXT-specific. However, frameworks (the new style
	of shared library) and rld() can't be used in the same program;
	instead, you have to use dyld, which is mostly unimplemented. */
	{
		NSObjectFileImageReturnCode rc;
		NSObjectFileImage image;
		NSModule newModule;
		NSSymbol theSym;
		const char *errString;
	
		if (NSIsSymbolNameDefined(funcname)) {
			theSym = NSLookupAndBindSymbol(funcname);
			p = (dl_funcptr)NSAddressOfSymbol(theSym);
			return p;
		}
		rc = NSCreateObjectFileImageFromFile(pathname, &image);
		switch(rc) {
		    default:
		    case NSObjectFileImageFailure:
		    case NSObjectFileImageFormat:
		    /* for these a message is printed on stderr by dyld */
			errString = "Can't create object file image";
			break;
		    case NSObjectFileImageSuccess:
			errString = NULL;
			break;
		    case NSObjectFileImageInappropriateFile:
			errString = "Inappropriate file type for dynamic loading";
			break;
		    case NSObjectFileImageArch:
			errString = "Wrong CPU type in object file";
			break;
		    case NSObjectFileImageAccess:
			errString = "Can't read object file (no access)";
			break;
		}
		if (errString == NULL) {
			newModule = NSLinkModule(image, pathname,
				NSLINKMODULE_OPTION_BINDNOW|NSLINKMODULE_OPTION_RETURN_ON_ERROR);
			if (!newModule)
				errString = "Failure linking new module";
		}
		if (errString != NULL) {
			cvs_err("%s\n", errString);
			return NULL;
		}
		if (!NSIsSymbolNameDefined(funcname)) {
			/* UnlinkModule() isn't implemented in current versions, but calling it does no harm */
			NSUnLinkModule(newModule, FALSE);
			cvs_err("Loaded module does not contain symbol %.200s", funcname);
			return NULL;
		}
		theSym = NSLookupAndBindSymbol(funcname);
		p = (dl_funcptr)NSAddressOfSymbol(theSym);
 	}

	return p;
}

void *_tcl_cfm_macho(void *func)
{
  return func;
}

int CarbonCompatLoadLibrary(CompatConnectID * connID, const char *name_dll)
{
	NSObjectFileImageReturnCode rc;
	NSObjectFileImage image;
	NSModule newModule;
	NSSymbol theSym;
	const char *errString;

	*connID = 0L;
	
	rc = NSCreateObjectFileImageFromFile(name_dll, &image);
	switch(rc) {
	    default:
	    case NSObjectFileImageFailure:
	    case NSObjectFileImageFormat:
	    /* for these a message is printed on stderr by dyld */
		errString = "Can't create object file image";
		break;
	    case NSObjectFileImageSuccess:
		errString = NULL;
		break;
	    case NSObjectFileImageInappropriateFile:
		errString = "Inappropriate file type for dynamic loading";
		break;
	    case NSObjectFileImageArch:
		errString = "Wrong CPU type in object file";
		break;
	    case NSObjectFileImageAccess:
		errString = "Can't read object file (no access)";
		break;
	}
	if (errString == NULL) {
		newModule = NSLinkModule(image, name_dll,
			NSLINKMODULE_OPTION_BINDNOW|NSLINKMODULE_OPTION_RETURN_ON_ERROR);
		if (!newModule)
			errString = "Failure linking new module";
	}
	if (errString != NULL) {
		cvs_err(errString);
		return 0;
	}
	
	*connID = (CompatConnectID)newModule;
	
	return 1;
}

int CarbonCompatCloseLibrary(CompatConnectID * connID)
{
	if(connID == 0L || *connID == 0L)
		return 0;

	NSUnLinkModule((NSModule)*connID, FALSE);
	*connID = 0L;

	return 1;
}

void *_CarbonCompatCallLibrary(CompatConnectID connID, const char *name_func)
{
	dl_funcptr p = NULL;
	char funcname[258];

	sprintf(funcname, "_init%.200s", name_func);

	/* This is also NeXT-specific. However, frameworks (the new style
	of shared library) and rld() can't be used in the same program;
	instead, you have to use dyld, which is mostly unimplemented. */
	NSSymbol theSym;

	if (NSIsSymbolNameDefined(funcname)) {
		theSym = NSLookupAndBindSymbol(funcname);
		p = (dl_funcptr)NSAddressOfSymbol(theSym);
		return p;
	}
	
	return 0L;
}

#endif // TARGET_RT_MAC_MACHO

#endif // qCarbon
