home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / PatchLib 1.0d3 / Source / PatchLib / PatchLib.c next >
Encoding:
C/C++ Source or Header  |  1994-11-11  |  2.6 KB  |  110 lines  |  [TEXT/KAHL]

  1. /* See the file Distribution for distribution terms.
  2.     (c) Copyright 1994 Ari Halberstadt */
  3.  
  4. /*    §Patch Library
  5.  
  6.     Patch Library is used to manage patches to traps. Installing and removing
  7.     patches is simpler than using the Toolbox routines NSetTrapAddress and
  8.     NGetTrapAddress. In addition, macros are provided that setup and restore
  9.     the environment for the patch routine. C source code is provided. */
  10.  
  11. #include <Memory.h>
  12. #include "PatchLib.h"
  13.  
  14. /* list of patches */
  15. static PatchType gPatches;
  16.  
  17. /*    GetTrapType returns the type of the trap (ToolBox or Operating System). */
  18. static TrapType GetTrapType(short trap)
  19. {
  20.     return((trap & 0x0800) > 0 ? ToolTrap : OSTrap);
  21. }
  22.  
  23. /*    PatchInsert inserts the patch into the list of patches, and returns the
  24.     new head of the list. */
  25. static PatchType PatchInsert(PatchType list, PatchType patch)
  26. {
  27.     patch->next = list;
  28.     return(patch);
  29. }
  30.  
  31. /*    PatchDelete removes the patch from the list of patches, and returns the
  32.     new head of the list. */
  33. static PatchType PatchDelete(PatchType list, PatchType patch)
  34. {
  35.     PatchType p, prev;
  36.  
  37.     prev = NULL;    
  38.     for (p = list; p && p != patch; p = p->next)
  39.         prev = p;
  40.     if (p) {
  41.         if (prev)
  42.             prev->next = p->next;
  43.         else
  44.             list = p->next;
  45.     }
  46.     return(list);
  47. }
  48.  
  49. /*    ƒPatchInstall installs the patch. */
  50. void PatchInstall(PatchType patch)
  51. {
  52.     if (! patch->installed) {
  53.         NSetTrapAddress(patch->addr, patch->number, patch->type);
  54.         patch->installed = true;
  55.     }
  56. }
  57.  
  58. /*    ƒPatchRemove removes the patch. */
  59. void PatchRemove(PatchType patch)
  60. {
  61.     if (patch->installed) {
  62.         NSetTrapAddress(patch->trap, patch->number, patch->type);
  63.         patch->installed = false;
  64.     }
  65. }
  66.  
  67. /*    ƒPatchRemoveAll removes all patches. */
  68. void PatchRemoveAll(void)
  69. {
  70.     PatchType patch;
  71.     
  72.     for (patch = gPatches; patch; patch = patch->next)
  73.         PatchRemove(patch);
  74. }
  75.  
  76. /*    ƒPatchBegin creates and installs a patch, and returns a pointer to the
  77.     patch structure, or NULL if the patch couldn't be created. */
  78. PatchType PatchBegin(UniversalProcPtr addr, short number)
  79. {
  80.     PatchType patch;
  81.     
  82.     patch = (PatchType) NewPtrClear(sizeof(PatchStructure));
  83.     if (patch) {
  84.         patch->number = number;
  85.         patch->type = GetTrapType(patch->number);
  86.         patch->addr = addr;
  87.         patch->trap = NGetTrapAddress(patch->number, patch->type);
  88.         gPatches = PatchInsert(gPatches, patch);
  89.         PatchInstall(patch);
  90.     }
  91.     return(patch);
  92. }
  93.  
  94. /*    ƒPatchEnd removes and disposes of the patch. */
  95. void PatchEnd(PatchType patch)
  96. {
  97.     if (patch) {
  98.         gPatches = PatchDelete(gPatches, patch);
  99.         PatchRemove(patch);
  100.         DisposePtr((Ptr) patch);
  101.     }
  102. }
  103.  
  104. /*    ƒPatchEndAll removes and disposes of all patches. */
  105. void PatchEndAll(void)
  106. {
  107.     while (gPatches)
  108.         PatchEnd(gPatches);
  109. }
  110.