home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Applications / Nuntius 1.2 / src / Nuntius / dnr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-20  |  7.0 KB  |  303 lines  |  [TEXT/MPS ]

  1. /*     DNR.c - DNR library for MPW
  2.  
  3.     (c) Copyright 1988 by Apple Computer.  All rights reserved
  4.     Further modifications by Steve Falkenburg, Apple MacDTS 8/91
  5.     
  6. */
  7.  
  8. #include <Types.h>
  9. #include <OSUtils.h>
  10. #include <Errors.h>
  11. #include <Files.h>
  12. #include <Resources.h>
  13. #include <Memory.h>
  14. #include <Traps.h>
  15. #include <GestaltEqu.h>
  16. #include <Folders.h>
  17. #include <ToolUtils.h>
  18. #include "AddressXlation.h"
  19. #include <Aliases.h>
  20.  
  21.  
  22. #pragma segment MyComm
  23.  
  24. #define OPENRESOLVER    1
  25. #define CLOSERESOLVER    2
  26. #define STRTOADDR        3
  27. #define    ADDRTOSTR        4
  28. #define    ENUMCACHE        5
  29. #define ADDRTONAME        6
  30. #define    HINFO            7
  31. #define MXINFO            8
  32.  
  33. Handle codeHndl = nil;
  34.  
  35. typedef OSErr (*OSErrProcPtr)(long,...);    /* added proto for Think C compatibility -->SJF<-- */
  36. OSErrProcPtr dnr = nil;                     /* ... otherwise, first arg would be passed as short*/
  37.  
  38.  
  39. TrapType GetTrapType(theTrap)
  40. unsigned long theTrap;
  41. {
  42.     if (BitAnd(theTrap, 0x0800) > 0)
  43.         return(ToolTrap);
  44.     else
  45.         return(OSTrap);
  46.     }
  47.     
  48. Boolean TrapAvailable(trap)
  49. unsigned long trap;
  50. {
  51. TrapType trapType = ToolTrap;
  52. unsigned long numToolBoxTraps;
  53.  
  54.     if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  55.         numToolBoxTraps = 0x200;
  56.     else
  57.         numToolBoxTraps = 0x400;
  58.  
  59.     trapType = GetTrapType(trap);
  60.     if (trapType == ToolTrap) {
  61.         trap = BitAnd(trap, 0x07FF);
  62.         if (trap >= numToolBoxTraps)
  63.             trap = _Unimplemented;
  64.         }
  65.     return(NGetTrapAddress(trap, trapType) != NGetTrapAddress(_Unimplemented, ToolTrap));
  66.  
  67. }
  68.  
  69. short GetCPanelFolder()
  70. {
  71.     short vRefNum = 0;
  72.     long dirID = 0;
  73.     short wdRef;
  74.     
  75.     if (FindFolder(kOnSystemDisk, kControlPanelFolderType, kDontCreateFolder, &vRefNum, &dirID) != noErr) return(0);
  76.     if (OpenWD(vRefNum, dirID, 'dnrp', &wdRef) == noErr)
  77.         return(wdRef);
  78.     else
  79.         return(0);
  80.     }
  81.  
  82. /* OpenOurRF is called to open the MacTCP driver resources */
  83.  
  84. short OpenOurRF()
  85. {
  86.     HParamBlockRec fi;
  87.     Str255 filename;
  88.     short vRefNum;
  89.     long dirID;
  90.     FSSpec spec;
  91.     Boolean targetIsFolder, wasAliased;
  92.     
  93.     FindFolder(kOnSystemDisk, kControlPanelFolderType, kCreateFolder, &vRefNum, &dirID);
  94.     fi.fileParam.ioFDirIndex = 1;
  95.     fi.fileParam.ioCompletion = nil;
  96.     fi.fileParam.ioNamePtr = &filename;
  97.     fi.fileParam.ioVRefNum = vRefNum;
  98.     fi.fileParam.ioDirID = dirID;
  99.     
  100.     while (PBHGetFInfoSync(&fi) == noErr) {
  101.         /* scan system folder for driver resource files of specific type & creator */
  102.         if (fi.fileParam.ioFlFndrInfo.fdType == 'cdev' &&
  103. /* HACK BY CPS */
  104.  
  105. //            fi.fileParam.ioFlFndrInfo.fdCreator == 'ztcp') {
  106.  
  107.  
  108.             (fi.fileParam.ioFlFndrInfo.fdCreator == 'ztcp' ||
  109.             fi.fileParam.ioFlFndrInfo.fdCreator == 'mtcp')) {
  110.             spec.vRefNum = fi.fileParam.ioVRefNum;
  111.             spec.parID = dirID;
  112.             BlockMove(&filename, &spec.name, 63);
  113.             ResolveAliasFile(&spec, true, &targetIsFolder, &wasAliased);
  114.             /* found the MacTCP driver file */
  115.             return(FSpOpenResFile(&spec, fsRdPerm));
  116. /* END HACK */
  117.             }
  118.         /* check next file in system folder */
  119.         fi.fileParam.ioDirID = dirID; // trashed
  120.         fi.fileParam.ioCompletion = nil;
  121.         fi.fileParam.ioNamePtr = &filename;
  122.         fi.fileParam.ioVRefNum = vRefNum;
  123.         fi.fileParam.ioFDirIndex++;
  124.         }
  125.  
  126.     FindFolder(kOnSystemDisk, kSystemFolderType, kCreateFolder, &vRefNum, &dirID);
  127.     fi.fileParam.ioFDirIndex = 1;
  128.     fi.fileParam.ioCompletion = nil;
  129.     fi.fileParam.ioNamePtr = &filename;
  130.     fi.fileParam.ioVRefNum = vRefNum;
  131.     fi.fileParam.ioDirID = dirID;
  132.     
  133.     while (PBHGetFInfoSync(&fi) == noErr) {
  134.         /* scan system folder for driver resource files of specific type & creator */
  135.         if (fi.fileParam.ioFlFndrInfo.fdType == 'cdev' &&
  136. /* HACK BY CPS */
  137.  
  138. //            fi.fileParam.ioFlFndrInfo.fdCreator == 'ztcp') {
  139.  
  140.  
  141.             (fi.fileParam.ioFlFndrInfo.fdCreator == 'ztcp' ||
  142.             fi.fileParam.ioFlFndrInfo.fdCreator == 'mtcp')) {
  143.             spec.vRefNum = fi.fileParam.ioVRefNum;
  144.             spec.parID = dirID;
  145.             BlockMove(&filename, &spec.name, 63);
  146.             ResolveAliasFile(&spec, true, &targetIsFolder, &wasAliased);
  147.             /* found the MacTCP driver file */
  148.             return(FSpOpenResFile(&spec, fsRdPerm));
  149. /* END HACK */
  150.             }
  151.         /* check next file in system folder */
  152.         fi.fileParam.ioDirID = dirID; // trashed
  153.         fi.fileParam.ioCompletion = nil;
  154.         fi.fileParam.ioNamePtr = &filename;
  155.         fi.fileParam.ioVRefNum = vRefNum;
  156.         fi.fileParam.ioFDirIndex++;
  157.         }
  158.  
  159.     return(-1);
  160.     }    
  161.  
  162.  
  163. OSErr OpenResolver(fileName)
  164. char *fileName;
  165. {
  166.     short refnum;
  167.     OSErr rc;
  168.     
  169.     if (dnr != nil)
  170.         /* resolver already loaded in */
  171.         return(noErr);
  172.         
  173.     /* open the MacTCP driver to get DNR resources. Search for it based on
  174.        creator & type rather than simply file name */    
  175.     refnum = OpenOurRF();
  176.  
  177.     /* ignore failures since the resource may have been installed in the 
  178.        System file if running on a Mac 512Ke */
  179.        
  180.     /* load in the DNR resource package */
  181.     codeHndl = GetIndResource('dnrp', 1);
  182.     if (codeHndl == nil) {
  183.         /* can't open DNR */
  184.         return(ResError());
  185.         }
  186.     
  187.     DetachResource(codeHndl);
  188.     if (refnum != -1) {
  189. //        CloseWD(refnum);
  190.         CloseResFile(refnum);
  191.         }
  192.         
  193.     /* lock the DNR resource since it cannot be reloated while opened */
  194.     HLock(codeHndl);
  195.     dnr = (OSErrProcPtr) *codeHndl;
  196.     
  197.     /* call open resolver */
  198.     rc = (*dnr)(OPENRESOLVER, fileName);
  199.     if (rc != noErr) {
  200.         /* problem with open resolver, flush it */
  201.         HUnlock(codeHndl);
  202.         DisposHandle(codeHndl);
  203.         dnr = nil;
  204.         }
  205.     return(rc);
  206.     }
  207.  
  208.  
  209. OSErr CloseResolver()
  210. {
  211.     if (dnr == nil)
  212.         /* resolver not loaded error */
  213.         return(notOpenErr);
  214.         
  215.     /* call close resolver */
  216.     (void) (*dnr)(CLOSERESOLVER);
  217.  
  218.     /* release the DNR resource package */
  219.     HUnlock(codeHndl);
  220.     DisposHandle(codeHndl);
  221.     dnr = nil;
  222.     return(noErr);
  223.     }
  224.  
  225. OSErr StrToAddr(hostName, rtnStruct, resultproc, userDataPtr)
  226. char *hostName;
  227. struct hostInfo *rtnStruct;
  228. long resultproc;
  229. char *userDataPtr;
  230. {
  231.     if (dnr == nil)
  232.         /* resolver not loaded error */
  233.         return(notOpenErr);
  234.         
  235.     return((*dnr)(STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
  236.     }
  237.     
  238. OSErr AddrToStr(addr, addrStr)
  239. unsigned long addr;
  240. char *addrStr;                                    
  241. {
  242.     if (dnr == nil)
  243.         /* resolver not loaded error */
  244.         return(notOpenErr);
  245.         
  246.     (*dnr)(ADDRTOSTR, addr, addrStr);
  247.     return(noErr);
  248.     }
  249.     
  250. OSErr EnumCache(resultproc, userDataPtr)
  251. long resultproc;
  252. char *userDataPtr;
  253. {
  254.     if (dnr == nil)
  255.         /* resolver not loaded error */
  256.         return(notOpenErr);
  257.         
  258.     return((*dnr)(ENUMCACHE, resultproc, userDataPtr));
  259.     }
  260.     
  261.     
  262. OSErr AddrToName(addr, rtnStruct, resultproc, userDataPtr)
  263. unsigned long addr;
  264. struct hostInfo *rtnStruct;
  265. long resultproc;
  266. char *userDataPtr;                                    
  267. {
  268.     if (dnr == nil)
  269.         /* resolver not loaded error */
  270.         return(notOpenErr);
  271.         
  272.     return((*dnr)(ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
  273.     }
  274.  
  275.  
  276. extern OSErr HInfo(hostName, returnRecPtr, resultProc, userDataPtr)
  277. char *hostName;
  278. struct returnRec *returnRecPtr;
  279. long resultProc;
  280. char *userDataPtr;
  281. {
  282.     if (dnr == nil)
  283.         /* resolver not loaded error */
  284.         return(notOpenErr);
  285.         
  286.     return((*dnr)(HINFO, hostName, returnRecPtr, resultProc, userDataPtr));
  287.  
  288.     }
  289.     
  290. extern OSErr MXInfo(hostName, returnRecPtr, resultProc, userDataPtr)
  291. char *hostName;
  292. struct returnRec *returnRecPtr;
  293. long resultProc;
  294. char *userDataPtr;
  295. {
  296.     if (dnr == nil)
  297.         /* resolver not loaded error */
  298.         return(notOpenErr);
  299.         
  300.     return((*dnr)(MXINFO, hostName, returnRecPtr, resultProc, userDataPtr));
  301.  
  302.     }
  303.