;       DETACH --  DETACH TERMINAL FROM JOB
;
;       This program detachs a terminal from whatever job
;       it is connected to.  The program is rentrant.
;
;       Copyright (C) 1978 by
;       Khalsa Computer Systems Incorporated
;
;       detach.m68
;       original source detach.mac by Khalsa Computer Systems Inc.
;       converted to MC680x0 code from detach.mac 04/28/88
;
;       Gregory J. Schraiber
;
;
;
       SEARCH  SYS                     ; system definitions
       SEARCH  TRM                     ; terminal definitions
       SEARCH  SYSSYM                  ;

       OBJNAM  .LIT

       .OFINI
       .OFDEF  F.NAM,10                ; trmdef name packed RAD50
       .OFDEF  F.BUF,10                ; scratch area
       .OFSIZ  F.SIZ

       VMAJOR=1
       VMINOR=1
       VEDIT=100.

DETACH: PHDR    -2,PV$RSM,PH$REU!PH$REE ; Setup program header

       LIN                             ; no trmdef name supplied
       BNE     SEARCH                  ;
       TYPECR  <USAGE:  DETACH  trmdef-name>
       EXIT

       GETIMP  F.SIZ,A5,NOMEM          ; get some memory

SEARCH:
       BYP                             ; skip white noise charactors
       LEA     A1,F.NAM(A5)            ; trmdef name buffer
       PACK                            ;
       PACK                            ;

       MOV     TRMDFC,D7               ; index the terminal definition chain
10$:    MOV     D7,A3
       CMM     F.NAM(A5),4(A3)         ;
       BEQ     FOUND                   ;
20$:    MOV     @A3,D7                  ; get next terminal in chain
       BNE     10$                     ; unless end of chain
       TYPECR  <DETACH: trmdef-name not found>
       EXIT                            ;


FOUND:  LEA     A1,F.NAM(A5)            ; PRINT TERMINAL NAME
       CALL    TPRINT                  ;
       LEA     A3,10(A3)               ; index to terminal
       TST     T.JLK(A3)               ; is new terminal attached?
       BEQ     10$                     ; no
       MOV     T.JLK(A3),A0            ; yes - get JCB addres in A0
       CLR     JOBTRM(A0)              ; clear JCB link to TCB
       CLR     T.JLK(A3)               ; clear TCB link to JCB
       TYPE    < detached from >       ;
       LEA     A1,JOBNAM(A0)           ; display job name
       CALL    TPRINT                  ;
       CRLF                            ;
       EXIT                            ;
10$:    TYPECR  < not connected>
       EXIT


TPRINT: LEA     A2,F.BUF(A5)    ; scratch buffer
       UNPACK
       UNPACK
10$:    CMPB    -(A2),#32.      ; remove trailing spaces
       BEQ     10$
       CLRB    1(A2)           ; mark end-of-name
       TTYL    F.BUF(A5)
       RTN

NOMEM:  TYPECR  <insufficiant memory>
EXIT:   EXIT

       RAD50   /COPYRIGHT 1978 KHALSA COMPUTER SYSTEMS/
       END