Update URL.
[python/dscho.git] / Mac / Unsupported / mactcp / dnrglue.c
blob5474b73cedaf0b06ec814d6469e5efd146c52e55
1 /* DNR.c - DNR library for MPW
3 (c) Copyright 1988 by Apple Computer. All rights reserved
5 Modifications by Jim Matthews, Dartmouth College, 5/91
6 Again modified for use with python by Jack Jansen, CWI, October 1994.
8 */
10 #include <Traps.h>
11 #include <OSUtils.h>
12 #include <Errors.h>
13 #include <Files.h>
14 #include <Resources.h>
15 #include <Memory.h>
16 #include <Traps.h>
17 #include <GestaltEqu.h>
18 #include <Folders.h>
19 #include <ToolUtils.h>
20 #include <MacTCPCommonTypes.h>
21 #include "AddressXlation.h"
23 TrapType GetTrapType(unsigned long theTrap);
24 Boolean TrapAvailable(unsigned long trap);
25 void GetSystemFolder(short *vRefNumP, long *dirIDP);
26 void GetCPanelFolder(short *vRefNumP, long *dirIDP);
27 short SearchFolderForDNRP(long targetType, long targetCreator, short vRefNum, long dirID);
28 short OpenOurRF(void);
30 #define OPENRESOLVER 1L
31 #define CLOSERESOLVER 2L
32 #define STRTOADDR 3L
33 #define ADDRTOSTR 4L
34 #define ENUMCACHE 5L
35 #define ADDRTONAME 6L
36 #define HINFO 7L
37 #define MXINFO 8L
39 Handle codeHndl = nil;
41 OSErrProcPtr dnr = nil;
43 TrapType GetTrapType(theTrap)
44 unsigned long theTrap;
46 if (BitAnd(theTrap, 0x0800) > 0)
47 return(ToolTrap);
48 else
49 return(OSTrap);
52 Boolean TrapAvailable(trap)
53 unsigned long trap;
55 TrapType trapType = ToolTrap;
56 unsigned long numToolBoxTraps;
58 if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
59 numToolBoxTraps = 0x200;
60 else
61 numToolBoxTraps = 0x400;
63 trapType = GetTrapType(trap);
64 if (trapType == ToolTrap) {
65 trap = BitAnd(trap, 0x07FF);
66 if (trap >= numToolBoxTraps)
67 trap = _Unimplemented;
69 return(NGetTrapAddress(trap, trapType) != NGetTrapAddress(_Unimplemented, ToolTrap));
73 void GetSystemFolder(short *vRefNumP, long *dirIDP)
75 SysEnvRec info;
76 long wdProcID;
78 SysEnvirons(1, &info);
79 if (GetWDInfo(info.sysVRefNum, vRefNumP, dirIDP, &wdProcID) != noErr) {
80 *vRefNumP = 0;
81 *dirIDP = 0;
85 void GetCPanelFolder(short *vRefNumP, long *dirIDP)
87 Boolean hasFolderMgr = false;
88 long feature;
90 if (Gestalt(gestaltFindFolderAttr, &feature) == noErr) hasFolderMgr = true;
91 if (!hasFolderMgr) {
92 GetSystemFolder(vRefNumP, dirIDP);
93 return;
95 else {
96 if (FindFolder(kOnSystemDisk, kControlPanelFolderType, kDontCreateFolder, vRefNumP, dirIDP) != noErr) {
97 *vRefNumP = 0;
98 *dirIDP = 0;
103 /* SearchFolderForDNRP is called to search a folder for files that might
104 contain the 'dnrp' resource */
105 short SearchFolderForDNRP(long targetType, long targetCreator, short vRefNum, long dirID)
107 HParamBlockRec fi;
108 Str255 filename;
109 short refnum;
111 fi.fileParam.ioCompletion = nil;
112 fi.fileParam.ioNamePtr = filename;
113 fi.fileParam.ioVRefNum = vRefNum;
114 fi.fileParam.ioDirID = dirID;
115 fi.fileParam.ioFDirIndex = 1;
117 while (PBHGetFInfo(&fi, false) == noErr) {
118 /* scan system folder for driver resource files of specific type & creator */
119 if (fi.fileParam.ioFlFndrInfo.fdType == targetType &&
120 fi.fileParam.ioFlFndrInfo.fdCreator == targetCreator) {
121 /* found the MacTCP driver file? */
122 refnum = HOpenResFile(vRefNum, dirID, filename, fsRdPerm);
123 if (GetIndResource('dnrp', 1) == NULL)
124 CloseResFile(refnum);
125 else
126 return refnum;
128 /* check next file in system folder */
129 fi.fileParam.ioFDirIndex++;
130 fi.fileParam.ioDirID = dirID; /* PBHGetFInfo() clobbers ioDirID */
132 return(-1);
135 /* OpenOurRF is called to open the MacTCP driver resources */
137 short OpenOurRF()
139 short refnum;
140 short vRefNum;
141 long dirID;
143 /* first search Control Panels for MacTCP 1.1 */
144 GetCPanelFolder(&vRefNum, &dirID);
145 refnum = SearchFolderForDNRP('cdev', 'ztcp', vRefNum, dirID);
146 if (refnum != -1) return(refnum);
148 /* next search System Folder for MacTCP 1.0.x */
149 GetSystemFolder(&vRefNum, &dirID);
150 refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
151 if (refnum != -1) return(refnum);
153 /* finally, search Control Panels for MacTCP 1.0.x */
154 GetCPanelFolder(&vRefNum, &dirID);
155 refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
156 if (refnum != -1) return(refnum);
158 return -1;
162 OSErr OpenResolver(fileName)
163 char *fileName;
165 short refnum;
166 OSErr rc;
168 if (dnr != nil)
169 /* resolver already loaded in */
170 return(noErr);
172 /* open the MacTCP driver to get DNR resources. Search for it based on
173 creator & type rather than simply file name */
174 refnum = OpenOurRF();
176 /* ignore failures since the resource may have been installed in the
177 System file if running on a Mac 512Ke */
179 /* load in the DNR resource package */
180 codeHndl = GetIndResource('dnrp', 1);
181 if (codeHndl == nil) {
182 /* can't open DNR */
183 return(ResError());
186 DetachResource(codeHndl);
187 if (refnum != -1) {
188 CloseWD(refnum);
189 CloseResFile(refnum);
192 /* lock the DNR resource since it cannot be reloated while opened */
193 HLock(codeHndl);
194 dnr = (OSErrProcPtr) *codeHndl;
196 /* call open resolver */
197 rc = (*dnr)(OPENRESOLVER, fileName);
198 if (rc != noErr) {
199 /* problem with open resolver, flush it */
200 HUnlock(codeHndl);
201 DisposHandle(codeHndl);
202 dnr = nil;
204 return(rc);
208 OSErr CloseResolver()
210 if (dnr == nil)
211 /* resolver not loaded error */
212 return(notOpenErr);
214 /* call close resolver */
215 (void) (*dnr)(CLOSERESOLVER);
217 /* release the DNR resource package */
218 HUnlock(codeHndl);
219 DisposHandle(codeHndl);
220 dnr = nil;
221 return(noErr);
224 OSErr StrToAddr(hostName, rtnStruct, resultproc, userDataPtr)
225 char *hostName;
226 struct hostInfo *rtnStruct;
227 ResultProcPtr resultproc;
228 char *userDataPtr;
230 if (dnr == nil)
231 /* resolver not loaded error */
232 return(notOpenErr);
234 return((*dnr)(STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
237 OSErr AddrToStr(addr, addrStr)
238 unsigned long addr;
239 char *addrStr;
241 if (dnr == nil)
242 /* resolver not loaded error */
243 return(notOpenErr);
245 (*dnr)(ADDRTOSTR, addr, addrStr);
246 return(noErr);
249 OSErr EnumCache(resultproc, userDataPtr)
250 EnumResultProcPtr resultproc;
251 char *userDataPtr;
253 if (dnr == nil)
254 /* resolver not loaded error */
255 return(notOpenErr);
257 return((*dnr)(ENUMCACHE, resultproc, userDataPtr));
261 OSErr AddrToName(addr, rtnStruct, resultproc, userDataPtr)
262 unsigned long addr;
263 struct hostInfo *rtnStruct;
264 ResultProcPtr resultproc;
265 char *userDataPtr;
267 if (dnr == nil)
268 /* resolver not loaded error */
269 return(notOpenErr);
271 return((*dnr)(ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
275 extern OSErr HInfo(hostName, returnRecPtr, resultProc, userDataPtr)
276 char *hostName;
277 struct returnRec *returnRecPtr;
278 ResultProc2Ptr resultProc;
279 char *userDataPtr;
281 if (dnr == nil)
282 /* resolver not loaded error */
283 return(notOpenErr);
285 return((*dnr)(HINFO, hostName, returnRecPtr, resultProc, userDataPtr));
289 extern OSErr MXInfo(hostName, returnRecPtr, resultProc, userDataPtr)
290 char *hostName;
291 struct returnRec *returnRecPtr;
292 ResultProc2Ptr resultProc;
293 char *userDataPtr;
295 if (dnr == nil)
296 /* resolver not loaded error */
297 return(notOpenErr);
299 return((*dnr)(MXINFO, hostName, returnRecPtr, resultProc, userDataPtr));