;************************************************************************
; CLIP.M68      -       "CLIP"
;
; by Dave Heyliger - AMUS Staff
;
; CLIP will "clip" the current screen of a JOB's terminal and place this
; picture in a file in your current PPN. The JOB should be in a non-active
; state, i.e., waiting for input or at the dot. The file is JOBNAM.PIC. You
; may even CLIP yourself - this is not a problem.
;
; Usage: .CLIP jobnam
;
; Example: .CLIP JOB2
;           This will create JOB2.PIC in your PPN
;
; SPECIAL NOTE: this seems to only work on terminals that support a TCRT
;               calls of (-1,19) and (-1,20).
;
; Re-write of program 12-30-87 by Dave Heyliger
;************************************************************************

       SEARCH SYS                              ;search normals
       SEARCH SYSSYM
       SEARCH TRM

       VMAJOR = 1                              ;version number
       VMINOR = 0

       .OFINI                                  ;define some variables
       .OFDEF  CURSOR,2                        ;"their" current cursor pos.
       .OFDEF  PICFIL,D.DDB                    ;picture file DDB
       .OFDEF  OURTRM,4                        ;"our" terminal pointer
       .OFDEF  THRTRM,4                        ;"their" terminal pointer
       .OFDEF  STSWRD,2                        ;"their" status word
       .OFDEF  IDX,4                           ;JOBIDX of JOB on input line
       .OFSIZ  IMPSIZ

       PHDR    -1,0,PH$REE!PH$REU              ;reentrant, reusable

       GETIMP  IMPSIZ,A3                       ;A3 will point to vars

       ;process input line
       LIN                                     ;just a CR?
       BNE     10$                             ;nope
       TYPECR  <Usage:   .CLIP JOBNAM>         ;yup, give instructions
       TYPECR  <Output file will be JOBNAM.PIC in your current PPN>
       EXIT

10$:    BYP                                     ;bypass whitespace
       LEA     A0,PICFIL(A3)                   ;point to picture file DDB
       FSPEC   @A0,PIC                         ;create filename (and JOBNAM)

       ;find the JOB on the input line
       MOV     JOBTBL,A2                       ;JOB TABLE address to A2
       LEA     A1,D.FIL(A0)                    ;point A1 to JOBNAM in DDB
30$:    MOV     (A2)+,A4                        ;A4 now points to each JCB per SCAN
       MOV     A4,D1                           ;Set the status register
       JMI     NONAME                          ;didn't find JOB
       BEQ     30$                             ;if a "0" then look at next JCB
       CMM     JOBNAM(A4),@A1                  ;see if name matches
       BNE     30$                             ;no match

       ;found JOB, initialize picture file, save IDX value
       MOV     A4,IDX(A3)                      ;save IDX value
       INIT    @A0                             ;initialize DDB
       LOOKUP  @A0                             ;there?
       BNE     50$                             ;nope
       DSKDEL  @A0                             ;yup, erase old pic file

       ;make .PIC file
50$:    OPENO   @A0                             ;get ready to create file

       ;output what is going on...
       TYPECR  <Found JOB, creating picture file.   Please wait for the dot....>

       ;if YOURSELF, don't do any detaching, attaching etc..
       JOBIDX  A6                              ;get YOU
       CMP     A6,A4                           ;YOU?
       JEQ     SETTSW                          ;yup, bypass below

       ;attach "other" terminal to "this" terminal
       JOBIDX  A6                              ;our JCB
       MOV     JOBTRM(A6),A2                   ;get what's inside into A2
       MOV     A2,OURTRM(A3)                   ;and save it

       MOV     JOBTRM(A4),A5                   ;get what's inside into A5
       MOV     A5,THRTRM(A3)                   ;and save it too
       MOVW    @A5,STSWRD(A3)                  ;and save their status word

       LEA     A5,JOBTRM(A4)                   ;address of "their" TB^
       LEA     A1,JOBTRM(A6)                   ;A1 points to "our" TB^

       JLOCK   ;----------------------------------------------------------
       CLR     @A5                             ;"detach" their terminal
       MOV     THRTRM(A3),@A1                  ;"attach" their terminal to us
       JUNLOK  ;----------------------------------------------------------

       ;set the terminal status word.
SETTSW: JOBIDX  A6                              ;get you
       MOV     JOBTRM(A6),A6                   ;get your TSW
       ORW     #T$IMI!T$ECS,@A6                ;tweek "MY" status word

       CALL    CLRBUF                          ;clear out input buffer

       ;now remember the cursor position of the terminal
       MOVW    #<-1_8.>+19.,D1                 ;create PRINT TAB (-1,19)
       TCRT                                    ;puts cursor add in input buf
       CALL    INBUF                           ;wait for input value...
       LEA     A2,CURSOR(A3)                   ;point A2 to cursor area
       KBD                                     ;get row
       SUBB    #37,D1                          ;create row number
       MOVB    D1,(A2)+                        ;save row
       KBD                                     ;get column
       SUBB    #37,D1                          ;convert to binary number
       MOVB    D1,@A2                          ;save column
       CALL    CLRBUF                          ;clear input buffer

       ;now scan the entire screen and get the picture
       MOV     #1,D2                           ;start row
       CLR     D3                              ;clear D3
LOOP:   INC     D3                              ;inc column
       MOVB    D2,D1                           ;TCRT to row,column
       LSLW    D1,#8.
       MOVB    D3,D1
       TCRT
       CALL    GETCHR                          ;get the character at cursor
       CMP     D3,#79.                         ;end of column?
       BNE     LOOP                            ;nope
       MOV     #15,D1                          ;yup, get CRLF out to file
       FILOTB  @A0
       MOV     #12,D1
       FILOTB  @A0
       CLR     D3                              ;clear column count
10$:    CMP     D2,#24.                         ;end of rows?
       BEQ     20$                             ;yup
       INC     D2                              ;nope, get next row
       BR      LOOP                            ;and get next character

       ;picture all done, reset the cursor
20$:    LEA     A6,CURSOR(A3)                   ;point to "their" cursor pos
       MOVB    (A6)+,D1                        ;get row
       LSLW    D1,#8.                          ;shift over
       MOVB    @A6,D1                          ;get column number
       TCRT                                    ;return their cursor


       JOBIDX  A6                              ;get yourself
       MOV     IDX(A3),A4                      ;get IDX value of JOB
       CMP     A4,A6                           ;if "other = YOU"
       JEQ     OUT                             ;bypass below

       ;re-attach everybody
       LEA     A5,JOBTRM(A4)
       JLOCK   ;----------------------------------------------------------
       CLR     @A1                     ;detach "their" termianl from us
       MOV     THRTRM(A3),@A5          ;attach it back to original owner
       MOV     @A5,A5                  ;get thier TSW
       MOVW    STSWRD(A3),@A5          ;reset their status word
       MOV     OURTRM(A3),JOBTRM(A6)   ;attach OUR original terminal back
       JUNLOK  ;-----------------------------------------------------------

OUT:    CLOSE   @A0                             ;close the file
       EXIT                                    ;and quit

       ;SUBROUTINE to get the character under the cursor
GETCHR: MOVW    #<-1_8.>+20.,D1                 ;create PRINT TAB (-1,20)
       TCRT                                    ;puts character in input buf
       CALL    INBUF                           ;wait for input to be returned
       KBD                                     ;get key under cursor
       FILOTB  @A0                             ;and output it to file
       RTN                                     ;return

       ;SUBROUTINE to clear out input buffer
CLRBUF: TCKI                                    ;input?
       BNE     10$                             ;nope, all clear
       KBD                                     ;yup, get it out of buffer
       BR      CLRBUF                          ;and look for more input
10$:    RTN                                     ;input buffer cleared

       ;SUBROUTINE to wait for input
INBUF:  TCKI                                    ;input?
       BNE     INBUF                           ;nope
       RTN                                     ;yup!


       ;error message
NONAME: TYPECR  <?Like, can't find the JOB, dude.>
       EXIT

       END