Smart Cards and the Open Terminal Architecture 
by Edward K. Conklin


Example 1:

: INSERT-RECORD ( S: -- )     ( G: Insert a record before the current )   
   WRITABLE #RECORD 0 MAX          \ From the current record
   DUP >R AVAILABLE  DB-INSERT     \ To last record, insert record
   AVAILABLE 1+ TO AVAILABLE       \ Increment available
   R> TO #RECORD                   \ Reset current record
   UPDATE-RECORD                   \ Save file update
;
: DBADDREC ( S: -- )   ( G: Add a record to the end of the database )
   KIND ORDERED AND              \ Check that file is not ordered
   DB-INVALID-FUNCTION ?THROW    \ Exception
   AVAILABLE TO #RECORD          \ Set record number to end of database
   INSERT-RECORD                 \ Insert a new record
;


Example 2:

: INIT-PURCHASE   ( S: -- )
   ( Initialise the purchase transaction. Perform all common steps for 
       online and offline transactions. )
   NEEDS-UPLOADING? ERR-DENIED ?THROW     \ Transaction log full ?
   LOAD-APPLICATION-DATA                  \ Load application data
   5 TRS-TRANSACTION-AMOUNT               \ Determine transaction amount
   EMV-INITIATE-APPLICATION               \ Initiate application on card
   EMV-PROCESS-COMPLETE?                  \ $9000 returned ?
   GET-CURRENT-BALANCE                    \ Get current balance from card
   MSG-BALANCE TRS-CONSTRUCT-CH-MESSAGE   \ Display balance for cardholder
   [ FLG-SECONDARY-DISPLAY ] [IF]
      MSG-PLEASE-WAIT TRS-CONSTRUCT-MRCH-MESSAGE \ Ask merchant to wait
   [THEN]
   EMV-READ-APPLICATION-DATA              \ Read data from the card
   EMV-PROCESS-COMPLETE?                  \ $9000 returned ?
   ICC-PUBLIC-KEY-AUTH                    \ Authenticate ICC public key
   EMV-PROCESSING-RESTRICTIONS            \ Check restrictions
   TRS-CHECK-STATUS                       \ Check transaction has not 
                                          \       been canceled
;



