[comp.sys.mac.programmer] Patching a trap address

mikeoro@hubcap.clemson.edu (Michael K O'Rourke) (05/23/89)

I am new to the world of inits, so i have a question.  I want to create an init
that will add certain functionality to the standard save/load dialogs.  I 
could patch the trap call, but then the old trap wouldn't get called.  What i
want to do is have everything work normally until the user pushs a special
key combination while in the save/load dialog.  If they press that combination
i want my routine to get called to do something.

What is the proper way to do this?  For instance, the init Quickfolder does
the same sort of rout-around that i want to be able to do (if anyone knows the
author and he/she can send code...).

Thanx,
Michael O'Rourke

thecloud@dhw68k.cts.com (Ken McLeod) (05/24/89)

In article <5563@hubcap.clemson.edu> mikeoro@hubcap.clemson.edu (Michael K O'Rourke) writes:
>I am new to the world of inits, so i have a question.  I want to create an init
>that will add certain functionality to the standard save/load dialogs.  I 
>could patch the trap call, but then the old trap wouldn't get called.  What i
>want to do is have everything work normally until the user pushs a special
>key combination while in the save/load dialog.  If they press that combination
>i want my routine to get called to do something.

  The general strategy for patching traps is to first call GetTrapAddress,
which will give you the (current) address of the trap you're patching.
You save this address off somewhere in your code, and then call SetTrapAddress
to the entry point of your code. Thereafter, your code does whatever it does
(i.e. examine the KeyMap to see if certain combinations of keys are down, or
whatever), then it "exits" via a jmp to the saved address, and the original
trap gets called. The alternative method, "tail patching", involves calling
the original trap yourself (via a 'jsr'), and is NOT A GOOD IDEA AT ALL (no
matter how careful you are, something will break it.)  Also, when you patch
a trap, you'll need to make doubly sure that you save and restore the
correct registers (if the call is register-based), and that the stack pointer
is at the same place you found it on entry. (You need to know exactly what
your code does to the stack, especially if it's not written in assembly.)

-- 
==========     .......     =============================================
Ken McLeod    :.     .:    UUCP: ...{spsd,zardoz,felix}!dhw68k!thecloud
==========   :::.. ..:::   INTERNET: thecloud@dhw68k.cts.com
                ////       =============================================

han@Apple.COM (Byron Han, wyl E. coyote ) (05/25/89)

In article <23164@dhw68k.cts.com> thecloud@dhw68k.cts.com (Ken McLeod) writes:
>
>  The general strategy for patching traps is to first call GetTrapAddress,
>which will give you the (current) address of the trap you're patching.
>You save this address off somewhere in your code, and then call SetTrapAddress

Actually NGetTrapAddress and NSetTrapAddress defined in IM-IV are the
recommended calls.
+-----------------------------------------------------------------------------+
| Disclaimer: Apple has no connection with my postings.                       |
+-----------------------------------------------------------------------------+ 
Byron Han, Communications Scapegoat   At Apple, we change the world everyday.
Apple Computer, Inc.                  -----------------------------------------
20525 Mariani Ave, MS27Y              Internet: han@apple.COM
Cupertino, CA 95014                   UUCP:{sun,voder,nsc,decwrl}!apple!han
------------------------------------  GENIE:BYRONHAN   CompuServe:72167,1664
ATTnet: 408-974-6450                  Applelink:HAN1   HAN1@applelink.apple.COM
-------------------------------------------------------------------------------

mnkonar@bigbootee.SRC.Honeywell.COM (Murat N. Konar) (05/27/89)

In article <31640@apple.Apple.COM> han@Apple.COM (Byron Han, wyl E. coyote ) writes:
(stuff about using GetTrapAddress and SetTrapAddress to patch traps deleted)
>Actually NGetTrapAddress and NSetTrapAddress defined in IM-IV are the
>recommended calls.


What is the difference?  Is there some advantage to using NGet/SetTrapAddress?
Or is it to ensure future compatibility?


____________________________________________________________________
Have a day. :^|
Murat N. Konar        Honeywell Systems & Research Center, Camden, MN
mnkonar@SRC.honeywell.com (internet) {umn-cs,ems,bthpyd}!srcsip!mnkonar(UUCP)

han@Apple.COM (Byron Han, wyl E. coyote ) (05/28/89)

article <22635@srcsip.UUCP> mnkonar@src.honeywell.com (Murat N. Konar) writes:
>In article <31640@apple.Apple.COM> han@Apple.COM (Byron Han, wyl E. coyote ) writes:
>(stuff about using GetTrapAddress and SetTrapAddress to patch traps deleted)
>>Actually NGetTrapAddress and NSetTrapAddress defined in IM-IV are the
>>recommended calls.
>
>What is the difference?  Is there some advantage to using NGet/SetTrapAddress?
>Or is it to ensure future compatibility?

GetTrapAddress and SetTrapAddress were used with the old 64K ROM's.  The new
routines NGetTrapAddress and NSetTrapAddress allow developers to specify
whether or not they are mucking with OS or Toolbox traps.

You do need to make sure that you are running with 128K ROM's or later.  See
Inside Macintosh Volume IV (233-234)
+-----------------------------------------------------------------------------+
| Disclaimer: Apple has no connection with my postings.                       |
+-----------------------------------------------------------------------------+ 
Byron Han, Communications Scapegoat   At Apple, we change the world everyday.
Apple Computer, Inc.                  -----------------------------------------
20525 Mariani Ave, MS27Y              Internet: han@apple.COM
Cupertino, CA 95014                   UUCP:{sun,voder,nsc,decwrl}!apple!han
------------------------------------  GENIE:BYRONHAN   CompuServe:72167,1664
ATTnet: 408-974-6450                  Applelink:HAN1   HAN1@applelink.apple.COM
-------------------------------------------------------------------------------