;*****************************************************************************
;*                                                                           *
;* SONOTRAP.ASP                              by Ed Paquette, Rochester NY    *
;*                                                                           *
;* A modified version of TRAP.ASP by Chuck Spohr and Markus Pope of          *
;* DATASTORM TECHNOLOGIES, INC.  This version has been tailored and tuned    *
;* to support color 3161 emulation via a 3174 AEA connection.                *
;*                                                                           *
;* Note: With a PS2 model 30, 8086, with 640k memory, a 2400 baud modem and  *
;* a 10k RAM disk to which all .ASX scripts were located the following       *
;* timings were recorded;                                                    *
;*                                                                           *
;* 1. To paint a 3270 screen with no script - 5.34 secs.                     *
;* 2. To paint a 3270 screen with the original TRAP script and the same      *
;*    Trapx strings as this script - 47 secs.                                *
;* 3. To paint a 3270 screen with this script - 27.00 secs.                  *
;*                                                                           *
;* On a 386/20 PC with a 512k disk cache, the difference between using this  *
;* script and no script is not as dramatic, time-wise.                       *
;*                                                                           *
;/////////////////////////////////////////////////////////////////////////////
;/                                                                           /
;/ I wish to thank Chuck and Markus for developing this script.  I've been   /
;/ wanting something like this every since I got Procomm.  Due to the color  /
;/ attributes of our mainframe's 3174 AEA connection Procomm Plus was        /
;/ unuseable.  We have to use FTTERM or Relay Gold to connect to the AEA.    /
;/ Again, thank you Chuck and Markus, this does work!                        /
;/                                                                           /
;/ P.S. Recommend the Function keys be remapped to support 3270 PF key       /
;/ functions.  The KB3708.TXT module on the Datastorm BBS lists the proper   /
;/ values to be associated with each key.                                    /
;/                                                                           /
;/ ==== Ed Paquette = Rochester NY = October 1992 ====                       /
;/                                                                           /
;/////////////////////////////////////////////////////////////////////////////
;*****************************************************************************

;*****************************************************************************
;* GLOBAL VARIABLES   Define sequences to trap here!                         *
;*****************************************************************************

string Trap0="Y"         ; tab       ; Define sequences to trap here -
string Trap1="[22m"      ; green     ;
string Trap2="[34m"      ; lt blue   ; Traps have been biased to the
string Trap3="[37m"      ; br white  ; frequency in which they occur.        
string Trap4="[21m"      ; red       
 
;*****************************************************************************
;*                                                                           *
;*   User-defined service routines for emulation sequences.                  *
;*   Prepare custom emulation sequence support routines here!                *
;*                                                                           *
;*****************************************************************************

proc Service0
endproc

proc Service1
endproc

proc Service2
endproc

proc Service3
endproc

proc Service4
endproc

;*****************************************************************************
;*                                                                           *
;*   Supporting routines - do not alter code beyond this point!              *
;*                                                                           *
;*****************************************************************************

;*****************************************************************************
;*   GLOBAL VARIABLES                                                        *
;*****************************************************************************

integer TrapCSI0                     ; Declare variables for sequence
integer TrapCSI1                     ; initiators
integer TrapCSI2
integer TrapCSI3
integer TrapCSI4

integer TrapCSIx                     ; Extra CSI

;*****************************************************************************
;*                                                                           *
;* MAIN                                                                      *
;* The main procedure calls SetTrap to initialize the CSI (control           *
;* sequence initiator) variables,then enters a loop to process each          *
;* character.  If 2 CSIs are found, Trap is called to process the            *
;* sequence.                                                                 *
;*                                                                           *
;* Calls: SetTrap, Trap                                                      *
;* Modifies globals: N0                                                      *
;*                                                                           *
;*****************************************************************************

proc main
   integer Chr,ChrX                  ; Declare variable for current chars.

   set wrap off                      ; Required for consistancy 
   set statline off                  ; Required for 3270 support

   set softflow on
   set rxdata on                     ; Script to process all incoming data
   call SetTrap                      ; Initialize trap variables

   call AEA                          ; Respond to AEA prompts

   while 1                           ; Begin infinite loop
      while COMDATA                  ; Is there data in the receive buffer?
         comgetcd Chr                ; Grab character; is it an initator?

         n0=(Chr==TrapCSI1)
         if n0                       ; If so,
            comgetcd ChrX            ; Grab 2nd character; is it an initator?
            n0=(ChrX==TrapCSIx)
            if n0                    ; If so,
               pushback              ; Put 2nd initiator back into buffer
               pushback              ; Put 1st initiator back into buffer
               call Trap             ; Process the sequence
            else                     ; Otherwise,
               pushback              ; Put 2nd initiator back into buffer
               writec Chr            ; Pass 1st initiator to the emulation
            endif
         else                        ; Otherwise,
            writec Chr               ; Pass it to the emulation
         endif
      endwhile
   endwhile
endproc

;*****************************************************************************
;*                                                                           *
;* SETTRAP                                                                   *
;* The SetTrap procedure sets initiator variables based on  target           *
;* strings.                                                                  *
;*                                                                           *
;* Calls:                                                                    *
;* Modifies globals: S0, S1, S2, S3, S4, S5, TrapCSI0, TrapCSI1,             *
;*                   TrapCSI2, TrapCSI3, TrapCSI4                            *
;*****************************************************************************

proc SetTrap
   substr S0 Trap0 0 1               ; Find first character of each target
   substr S1 Trap1 0 1
   substr S2 Trap2 0 1
   substr S3 Trap3 0 1
   substr S4 Trap4 0 1

   strpeek S0 0 TrapCSI0             ; Convert them to integer values
   strpeek S1 0 TrapCSI1
   strpeek S2 0 TrapCSI2
   strpeek S3 0 TrapCSI3
   strpeek S4 0 TrapCSI4

   substr S9 Trap1 1 1               ; Find 2ndary character of each target
   strpeek S9 0 TrapCSIx             ; Convert it to integer value
endproc

;*****************************************************************************
;*                                                                           *
;* TRAP                                                                      *
;* The Trap procedure calls GrabSeq and if match is found, calls             *
;* appropriate service routine.  If no match is found, characters are        *
;* passed to emulation.                                                      *
;*                                                                           *
;* Calls: GrabSeq, Service0, Service1, Service2, Service3, Service4          *
;* Modifies globals:                                                         *
;*                                                                           *
;*****************************************************************************

proc trap
   integer Chr, Match=0
;  Colors are determined by Procomm Plus setup for Terminal Colors 
      call GrabSeq with Trap1 &Match          ; Check for next seq. ...etc.
      if Match                                ; Input - 3270 green
         normon
      else
         call GrabSeq with Trap2 &Match
         if Match                             ; Protected - 3270 blue 
            dimon
         else
            call GrabSeq with Trap3 &Match
            if Match                          ; Intensified Protected - 3270 wht
               boldon
            else
               call GrabSeq with Trap4 &Match
               if Match                       ; Intensified Input - 3270 red
                  revon
               else                           ; No Matches found?
                  comgetcd Chr                ; Write current char
                  writec Chr                  ; to emulation
               endif
            endif
         endif
      endif
endproc

;*****************************************************************************
;*                                                                           *
;* GRABSEQ                                                                   *
;* The GrabSeq grabs current sequence from buffer and compares it to         *
;* the current target.  If they match, 'Match' is set to 1.  If not,         *
;* 'Match' is set to 0 and the current sequence is 'pushed back' into        *
;* the buffer, ready for the next comparison.                                *
;*                                                                           *
;* Calls:                                                                    *
;* Modifies Globals: N0, N1                                                  *
;*****************************************************************************

proc GrabSeq
   strparm Seq                       ; Target parameter
   intparm Match                     ; 'Match' flag

   integer Len,Chr,Val ;Mask=63      ; Declare local variables & mask (?)
   Match=1                           ; Init 'Match' flag

   strlen Seq Len                    ; Find length of target
   Len--                             ; Decrement length value

   if Len>=0
      for n0=0 upto Len              ; Loop to grab incoming sequence
         comgetcd Chr                ; Get next character from port

         if Chr==-1                  ; If 'comgetcd' timed out
            n0--                     ; decrement character counter
         endif
         strpeek Seq n0 Val          ; Get the next char value from target
;        if Val!=Mask                ; Is it not the mask char "?"?
         if Val!=Chr                 ; Is it not a match?
            Match=0                  ; If not, unset 'Match' flag
            for n1=0 upto n0         ; Put characters back in the buffer
               pushback              ; for next check
            endfor
            exitfor                  ; Jump out of current checking loop
;        endif
         endif                       ; If match found, continue loop
      endfor
   else
      Match=0
   endif
endproc

;include "aea.inc"                  ; AEA signon processing
;  call AEA                        ; Respond to AEA prompts

;*****************************************************************************
;*                                                                           *
;* AEA                                                                       *
;* This procedure responds to the AEA's prompts.                             *
;*                                                                           *
;*****************************************************************************

proc AEA 
   string AEA_pswd="...........^M" ; password assigned to AEA unit
   string kb_map="0^M" 
   integer X=0,Y=0

   if not fromddir                 ; if not from the Dialing Directory
      return                       ; exit this procedure
   endif

   waitfor "USER NAME:"      10    ; Call back security signon prompt
   if waitfor
      when 0 "INVALID LOGIN" call Exit_script
      when 1 "TIME OUT"      call Exit_script
      when 2 "NO CARRIER"    call Alt_Y     
      init x 60                    ; x = nbr of secs to wait for AEA prompt
   endif
   while not zero X
      waitfor "WORD =======>"     1
      if waitfor
         cwhen 0
         cwhen 1
         cwhen 2
         transmit AEA_pswd
         init X 1
         init Y 5                  ; y = nbr of secs to wait for keyboard prompt
         clear
      endif
      dec X                        ; reduce counter by 1 and loopback 
   endwhile  
   while not zero Y
      waitfor "0=NO)? ======>"    1
      if waitfor
         transmit kb_map
         init Y 1
         clear
      endif
      dec Y        
   endwhile
   call Colorset                   ; Initialize terminal colors to std 3270
endproc

proc Exit_script
   hangup          
   exit
endproc

proc Alt_Y
   termkey 0x1500                  ; alt-y => initiate auto-answer
endproc

proc Colorset
   set termnorm 10        ; green
   set termdim  11        ; blue
   set termbold 15        ; white
   set termrev  12        ; red
endproc
