home *** CD-ROM | disk | FTP | other *** search
/ Carousel / CAROUSEL.cdr / mactosh / init / rwatcher.sit / RWatcher.a < prev    next >
Text File  |  1988-11-14  |  9KB  |  395 lines

  1.     title    'RWatcher'
  2. ;_______________________________________________________________________
  3. ;
  4. ; RWatcher - Resource Watching INIT.
  5. ;
  6. ; Version 1.0.  November, 1988.
  7. ;
  8. ; John Norstad
  9. ; Academic Computing and Network Services
  10. ; Northwestern University
  11. ; 2129 Sheridan Road
  12. ; Evanston, IL 60208
  13. ;
  14. ; Bitnet: jln@nuacc
  15. ; Internet: jln@nuacc.acns.nwu.edu
  16. ;
  17. ; Copyright ⌐ 1988, John Norstad.  Permission is granted to make and
  18. ; distribute copies of this software, its source code, and documentation, 
  19. ; provided this copyright notice is preserved on all copies.  
  20. ; The software cannot, however, be sold or distributed for profit.
  21. ; The software has no warranty, express or implied.  Use it at your 
  22. ; own risk.
  23. ;_______________________________________________________________________
  24.  
  25.  
  26. ;_______________________________________________________________________
  27. ;
  28. ; RWatcher is a very simple INIT that patches the AddResource and
  29. ; ChangedResource system traps.  The patches watch for attempts to
  30. ; create or modify any of a list of resources.  If such an attempt
  31. ; is made the patches beep 10 times and exit to shell (quit the
  32. ; offending application).
  33. ;
  34. ; The list of resources to be monitored is specified in the RLIS 128
  35. ; resource on the INIT file.  This list can be easily modified with
  36. ; ResEdit.
  37. ;_______________________________________________________________________
  38.  
  39.  
  40. ;_______________________________________________________________________
  41. ;
  42. ; RWatcher is written in MPW Assembler.
  43. ;
  44. ; Use the following MPW commands to build the INIT resource:
  45. ;
  46. ; asm -wb -l -pagesize 73,105 RWatcher.a
  47. ; link RWatcher.a.o ::ShowInit:ShowInit.a.o ╢
  48. ;     -t INIT -rt INIT=0 -sg RWatcher -ra RWatcher=$10 -o RWatcher
  49. ;_______________________________________________________________________
  50.  
  51.  
  52.     main
  53.     
  54.     
  55. ; Imports.
  56.  
  57.     import    ShowInit
  58.     
  59. ; Include Files.
  60.  
  61.     print   off
  62.     Include    'Traps.A'
  63.     Include    'SysEqu.A'
  64.     Include    'ToolEqu.A'
  65.     print    on
  66.  
  67.     eject
  68.     title    'Constants and Assembly Options'
  69. ;_______________________________________________________________________
  70. ;
  71. ; Constants and Assembly Options.
  72. ;_______________________________________________________________________
  73.  
  74.     
  75. iconID        equ    128    ; resource id of ICN#
  76.  
  77. addResTrap    equ    $1ab    ; AddResource trap number
  78. chaResTrap    equ    $1aa    ; ChangedResource trap number
  79.  
  80. rListType    equ    'RLIS'    ; resource type of resource list
  81. rListID        equ    128    ; resource id of resource list
  82.  
  83. numBeeps    equ    10    ; number of beeps if bad rsrc found
  84. beepDuration    equ    10    ; beep duration
  85.  
  86. ; The RLIS 128 resource is a zero byte terminated list of resource
  87. ; descriptions.  The following constants describe the contents of
  88. ; each element of the list.  Two "wild card" flags permit matching
  89. ; any resource id and/or size for a given resource type.
  90.  
  91. rLisType    equ    0    ; offset of resource type
  92. rLisID        equ    4    ; offset of resource ID
  93. rLisSize    equ    6    ; offset of resource size
  94. rLisFlags    equ    10    ; offset of resource flags
  95. rLisAnyID    equ    7    ; bit number of any ID flag
  96. rLisAnySize    equ    6    ; bit number of any size flag
  97. rLisLen        equ    12    ; length of resource info
  98.     eject
  99.     title    'Global Variables'
  100. ;_______________________________________________________________________
  101. ;
  102. ; Global Variables.
  103. ;_______________________________________________________________________
  104.     
  105.  
  106. ; The following instruction is the first one executed when the INIT runs
  107. ; at boot time.  It branches around the patch code to the initialization
  108. ; code.
  109.  
  110. Header:
  111.  
  112.     bra    Init
  113.     
  114.     
  115. ; The INIT installs the patches as a single non-relocatable block in
  116. ; the system heap.  The contents of this block begin here.
  117.  
  118. BlockBegin:
  119.  
  120.  
  121. ; The following three global variables are initialized by the INIT.
  122.  
  123. AddResOldAddr:
  124.  
  125.     ds.l    1        ; old AddResource trap address
  126.     
  127. ChaResOldAddr:
  128.  
  129.     ds.l    1        ; old ChangedResource trap address
  130.     
  131. RListHandle:
  132.  
  133.     ds.l    1        ; handle to RLIS 128 resource list
  134.     eject
  135.     title     'AddResPatch - AddResource Patch'
  136. ;_______________________________________________________________________
  137. ;
  138. ; AddResPatch - AddResource Patch
  139. ;_______________________________________________________________________
  140.     
  141.     
  142. ; Stack frame record template.    
  143.     
  144. AddResFrame    record    {oldA6},decrement
  145. theData        ds.l    1    ; handle to data
  146. theType        ds.l    1    ; type
  147. theID        ds.w    1    ; id
  148. name        ds.l    1    ; ptr to name
  149. retAddr        ds.l    1    ; return address
  150. oldA6        ds.l    1    ; old A6
  151. size        equ    *
  152.         endr
  153.         
  154.         
  155. ; Patch entry point.
  156.     
  157.     with    AddResFrame
  158.         
  159. AddResPatch:
  160.  
  161. ; Create stack frame and save registers.
  162.  
  163.     link    a6,#size
  164.     movem.l    d3-d4/a2,-(sp)
  165.  
  166. ; Load registers for CheckRes:
  167. ;
  168. ; d3 = resource type (from param list).
  169. ; d4 = resource id (from param list).
  170. ; a2 = handle to data (from param list).
  171.  
  172.     move.l    theType(a6),d3        ; type
  173.     move.w    theID(a6),d4        ; id
  174.     move.l    theData(a6),a2        ; handle
  175.     
  176. ; Call CheckRes to compare against resource list.
  177.     
  178.     bsr.s    CheckRes
  179.     
  180. ; Exit.  Restore registers, unlink, and call old trap.
  181.  
  182.     movem.l    (sp)+,d3-d4/a2
  183.     unlk    a6
  184.     move.l    AddResOldAddr,a0    ; old trap address
  185.     jmp    (a0)
  186.     
  187.     endwith
  188.     
  189.     eject
  190.     title     'ChaResPatch - ChangedResource Patch'
  191. ;_______________________________________________________________________
  192. ;
  193. ; ChaResPatch - ChangedResource Patch
  194. ;_______________________________________________________________________
  195.     
  196.     
  197. ; Stack frame record template.    
  198.  
  199. ChaResFrame    record    {oldA6},decrement
  200. theResource    ds.l    1    ; handle to resource
  201. retAddr        ds.l    1    ; return address
  202. oldA6        ds.l    1    ; old A6
  203. theID        ds.w    1    ; id
  204. theType        ds.l    1    ; type
  205. name        ds.b    256    ; name
  206. size        equ    *
  207.         endr
  208.         
  209.         
  210. ; Patch entry point.
  211.  
  212.     with    ChaResFrame
  213.         
  214. ChaResPatch:
  215.         
  216. ; Create stack frame and save registers.
  217.  
  218.     link    a6,#size
  219.     movem.l    d3-d4/a2,-(sp)
  220.  
  221. ; Call GetResInfo to get resource type and id.
  222.  
  223.     move.l    theResource(a6),-(sp)
  224.     pea    theID(a6)
  225.     pea    theType(a6)
  226.     pea    name(a6)
  227.     _GetResInfo
  228.     
  229. ; Load registers for CheckRes:
  230. ;
  231. ; d3 = resource type (from GetResInfo).
  232. ; d4 = resource id (from GetResInfo).
  233. ; a2 = handle to data (from param list).
  234.     
  235.     move.l    theType(a6),d3
  236.     move.w    theID(a6),d4
  237.     move.l    theResource(a6),a2
  238.  
  239. ; Call CheckRes to compare against resource list.
  240.     
  241.     bsr.s    CheckRes
  242.     
  243. ; Exit.  Restore registers, unlink, and call old trap.
  244.  
  245.     movem.l    (sp)+,d3-d4/a2
  246.     unlk    a6
  247.     move.l    ChaResOldAddr,a0    ; old trap address
  248.     jmp    (a0)
  249.     
  250.     endwith
  251.     eject
  252.     title    'CheckRes - Check Resource Procedure'
  253. ;_______________________________________________________________________
  254. ;
  255. ; CheckRes - Check Resource Procedure.
  256. ;
  257. ; This procedure is called by both patches.  It compares a resource
  258. ; to the RLIS 128 list.
  259. ;
  260. ; Entry:    (d3) = resource type.
  261. ;        (d4) = resource id.
  262. ;        (a2) = handle to resource data.
  263. ;
  264. ; Exit:        If match found:  Beep numBeep times and exit to shell.
  265. ;        If no match found:  Return to caller.
  266. ;_______________________________________________________________________
  267.  
  268.  
  269. CheckRes:
  270.  
  271. ;    Call GetHandleSize to get (d0) = handle size.
  272.         
  273.     move.l    a2,a0
  274.     _GetHandleSize
  275.         
  276. ;    Walk RLIS 128 resource, comparing type, id, and size.
  277.  
  278.     move.l    RListHandle,a2
  279.     move.l    (a2),a2            ; (a2) = ptr to RLIS 128 resource
  280.     
  281. Loop:    
  282.  
  283.     tst.b    (a2)
  284.     bnz.s    CheckType        ; if not end of list
  285.     rts                ; no match - return to caller
  286.     
  287. CheckType:
  288.  
  289.     cmp.l    rLisType(a2),d3
  290.     bne.s    NextRsrc        ; if different types
  291.     
  292. CheckID:
  293.  
  294.     btst    #rLisAnyID,rLisFlags(a2)
  295.     bnz.s    CheckSize        ; if any id specified
  296.     cmp.w    rLisID(a2),d4
  297.     bne.s    NextRsrc        ; if different ids
  298.     
  299. CheckSize:
  300.  
  301.     btst    #rLisAnySize,rLisFlags(a2)
  302.     bnz.s    BadRsrc            ; if any size specified
  303.     cmp.l    rLisSize(a2),d0
  304.     beq.s    BadRsrc            ; if same sizes
  305.     
  306. NextRsrc:
  307.  
  308.     lea    rLisLen(a2),a2        ; advance to next rsrc in list
  309.     bra    Loop
  310.     
  311. ; Bad resource found.  Beep numBeeps times and exit to shell.
  312.     
  313. BadRsrc:
  314.  
  315.     move.w    #numBeeps-1,d3
  316.     
  317. BeepLoop:
  318.  
  319.     move.w    #beepDuration,-(sp)
  320.     _SysBeep
  321.     dbra    d3,BeepLoop
  322.     _ExitToShell
  323.  
  324. blockSize    equ    *-BlockBegin
  325.     eject
  326.     title    'Init - Initialization'
  327. ;_______________________________________________________________________
  328. ;
  329. ; Init - Initialization.
  330. ;_______________________________________________________________________
  331.     
  332.     
  333. Init:
  334.  
  335. ; Show the icon.
  336.  
  337.     move.w    #iconID,-(sp)
  338.     jsr    ShowInit
  339.     
  340. ; Load the RLIS 128 resource and detach it.  The RLIS 128 resource must
  341. ; have the system heap attribute set.  It should be unlocked but
  342. ; non-purgeable.
  343.  
  344.     sub.l    #4,sp
  345.     move.l    #rListType,-(sp)
  346.     move.w    #rListID,-(sp)
  347.     _GetResource            ; load RLIS 128
  348.     move.l    (sp)+,d0
  349.     bz.s    Exit            ; if error
  350.     move.l    d0,a3            ; (a3) = handle to RLIS 128
  351.     move.l    a3,-(sp)
  352.     _DetachResource            ; detach rsrc
  353.  
  354. ; Allocate new non-relocatable system heap block to hold data and patches.
  355.  
  356.     move.l    #blockSize,d0
  357.     _NewPtr    ,sys
  358.     move.l    a0,a2            ; (a2) = ptr to block
  359.     
  360. ; Copy data and patches to the block.
  361.  
  362.     lea    BlockBegin,a0
  363.     move.l    a2,a1
  364.     move.l    #blockSize,d0
  365.     _BlockMove
  366.     
  367. ; Save handle to resource list in data area.
  368.  
  369.     move.l    a3,RListHandle-BlockBegin(a2)
  370.  
  371. ; Get and save old AddResource and ChangedResource trap addresses.
  372.  
  373.     move.w    #addResTrap,d0
  374.     _GetTrapAddress
  375.     move.l    a0,AddResOldAddr-BlockBegin(a2)
  376.     move.w    #chaResTrap,d0
  377.     _GetTrapAddress
  378.     move.l    a0,ChaResOldAddr-BlockBegin(a2)
  379.  
  380. ; Patch AddResource and ChangedResource.
  381.  
  382.     lea    AddResPatch-BlockBegin(a2),a0
  383.     move.w    #addResTrap,d0
  384.     _SetTrapAddress
  385.     lea    ChaResPatch-BlockBegin(a2),a0
  386.     move.w    #chaResTrap,d0
  387.     _SetTrapAddress
  388.     
  389. ; Return.
  390.  
  391. Exit:
  392.  
  393.     rts
  394.  
  395.     end