aboutsummaryrefslogtreecommitdiff
path: root/src/mac/CFMglue.c
blob: 4daf5d8669944454d5f6ee2c1b986afbbca90c48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <CodeFragments.h>
#include <Gestalt.h>
#include <Errors.h>

// Private function prototypes

static OSErr Find_Symbol(
	Ptr* pSymAddr,
	Str255 pSymName,
	ProcInfoType pProcInfo);

static pascal Boolean HaveCFM(void);

static pascal OSErr GetSystemArchitecture(OSType *archType);

/* This code is directly from Technote 1077 */

/*	changed Library name to be hardcoded at the top of the file
	instead in the middle of the code */

// Private functions

static pascal OSErr GetSystemArchitecture(OSType *archType)
{
	static long sSysArchitecture = 0; // static so we only Gestalt once.
	OSErr tOSErr = noErr;

	*archType = kAnyCFragArch;   // assume wild architecture

	// If we don't know the system architecture yet...
	if (sSysArchitecture == 0)
	// ...Ask Gestalt what kind of machine we are running on.
	tOSErr = Gestalt(gestaltSysArchitecture, &sSysArchitecture);

	if (tOSErr == noErr) // if no errors
	{
		if (sSysArchitecture == gestalt68k)   // 68k?
			*archType = kMotorola68KCFragArch;   
		else if (sSysArchitecture == gestaltPowerPC) // PPC?
			*archType = kPowerPCCFragArch;       
		else
			tOSErr = gestaltUnknownErr;  // who knows what might be next?
	}
	return tOSErr;
}

static pascal Boolean HaveCFM(void)
{
	long response;
	return ( (Gestalt (gestaltCFMAttr, &response) == noErr) &&
				(((response >> gestaltCFMPresent) & 1) != 0));
}

static OSErr Find_Symbol(
	Ptr* pSymAddr,
	Str255 pSymName,
	ProcInfoType pProcInfo)
{
	static CFragConnectionID sCID = 0;
	static OSType sArchType = kAnyCFragArch;
	static OSErr sOSErr = noErr;

	Str255 errMessage;
	Ptr mainAddr;
	CFragSymbolClass symClass;
	ISAType tISAType;

	if (sArchType == kAnyCFragArch)  // if architecture is undefined...
	{
		sCID = 0;     // ...force (re)connect to library
		sOSErr = GetSystemArchitecture(&sArchType); // determine architecture
		if (sOSErr != noErr)
		return sOSErr; // OOPS!
	}
	
	if (!HaveCFM()) {
		// If we don't have CFM68K, return a reasonable-looking error.
		sOSErr = cfragLibConnErr;
		return sOSErr;
	}

	if (sArchType == kMotorola68KCFragArch) // ...for CFM68K
		tISAType = kM68kISA | kCFM68kRTA;
	else if (sArchType == kPowerPCCFragArch)  // ...for PPC CFM
		tISAType = kPowerPCISA | kPowerPCRTA;
	else
		sOSErr = gestaltUnknownErr; // who knows what might be next?

	if (sCID == 0) // If we haven't connected to the library yet...
	{
		// NOTE: The library name is hard coded here.
		// I try to isolate the glue code, one file per library.
		// I have had developers pass in the Library name to allow
		// plug-in type support. Additional code has to be added to
		// each entry points glue routine to support multiple or
		// switching connection IDs.
		sOSErr = GetSharedLibrary(kLibraryName, sArchType, kLoadCFrag,
		&sCID, &mainAddr, errMessage);
		if (sOSErr != noErr)
		return sOSErr; // OOPS!
	}

	// If we haven't looked up this symbol yet...
	if ((Ptr) *pSymAddr == (Ptr) kUnresolvedCFragSymbolAddress)    
	{
		// ...look it up now
		sOSErr = FindSymbol(sCID,pSymName,pSymAddr,&symClass);
		if (sOSErr != noErr) // in case of error...
		// ...clear the procedure pointer
		*(Ptr*) &pSymAddr = (Ptr) kUnresolvedCFragSymbolAddress;
#	if !GENERATINGCFM // if this is classic 68k code...
			*pSymAddr = (Ptr)NewRoutineDescriptorTrap((ProcPtr) *pSymAddr,
			pProcInfo, tISAType);  // ...create a routine descriptor...
#	endif
	}
	return sOSErr;
}

/* --------------------------------- */
/* Autogenerated section starts here */
/* --------------------------------- */