; ZCPR3 TCAP Facility (Z3TCAP)
; Program Name:  TCCHECK
; Author:  Richard Conn
; Version:  1.0
; Date:  10 Mar 84
version equ     10

;
;       TCCHECK checks the indicated (or implied) Z3TCAP.TCP file
; and displays statistics about it.
;

;
;  Basic Equates
;
z3env   SET     0f400h  ;ZCPR3 Environment Descriptor
fcb     equ     5ch
tbuff   equ     80h
ctrlc   equ     'C'-'@'
cr      equ     0dh
lf      equ     0ah

;
;  ZCPR3 and SYSLIB References
;
       ext     z3init,qprint,z3log
       ext     codend,moveb
       ext     print,pfn1,cout,phldc
       ext     initfcb,pfind,f$open,f$read,f$close

;
; Environment Definition
;
       if      z3env ne 0
;
; External ZCPR3 Environment Descriptor
;
       jmp     start
       db      'Z3ENV' ;This is a ZCPR3 Utility
       db      1       ;External Environment Descriptor
z3eadr:
       dw      z3env
start:
       lhld    z3eadr  ;pt to ZCPR3 environment
;
       else
;
; Internal ZCPR3 Environment Descriptor
;
       MACLIB  Z3BASE.LIB
       MACLIB  SYSENV.LIB
z3eadr:
       jmp     start
       SYSENV
start:
       lxi     h,z3eadr        ;pt to ZCPR3 environment
       endif

;
; Start of Program -- Initialize ZCPR3 Environment
;
       call    z3init  ;initialize the ZCPR3 Env and the VLIB Env

;
; Print Banner
;
       call    qprint
       db      'TCCHECK, Version '
       db      (version/10)+'0','.',(version mod 10)+'0'
       db      0

;
; Check for Entry in FCB
;
       lda     fcb+1   ;get first char
       cpi     '/'     ;none if slash
       jnz     start1
;
; Print Help Info
;
help:
       call    print
       db      'TCCHECK - Select Entry from Z3TCAP.TCP'
       db      cr,lf,'Syntax:'
       db      cr,lf,' TCCHECK infile  -or-  TCCHECK infile.typ'
       db      cr,lf
       db      cr,lf,'where "infile" is the file to be checked by'
       db      cr,lf,'the execution of TCCHECK.  If no file type is'
       db      cr,lf,'given, a file type of TCP is the default.'
       db      cr,lf
       db      cr,lf,'Syntax:'
       db      cr,lf,' TCCHECK'
       db      cr,lf
       db      cr,lf,'where this alternate form may be used to check'
       db      cr,lf,'the Z3TCAP.TCP file.'
       db      0
       ret
;
; Resume Processing
;
start1:
;
; Set Default File if None
;
       lxi     d,fcb+1         ;pt to file name
       lxi     h,deffile       ;pt to default file
       mvi     b,11            ;11 bytes
       ldax    d               ;get first char
       cpi     ' '             ;none if space
       cz      moveb           ;set default file
;
; Set Default File Type if None
;
       lxi     d,fcb+9         ;pt to file type
       lxi     h,deftyp        ;pt to default file type
       mvi     b,3             ;3 bytes
       ldax    d               ;get first char
       cpi     ' '             ;none if space
       cz      moveb           ;set default file type
;
; Begin Reading Z3TCAP.TCP
;
       lxi     d,fcb           ;try to open Z3TCAP.TCP
       call    initfcb         ;init FCB
       mvi     a,0ffh          ;search current also
       call    pfind           ;look for file
       jnz     start2          ;file found
;
; File Z3TCAP.TCP Not Found
;
fnferr:
       call    print
       db      '  File ',0
       lxi     d,fcb+1         ;print file name
       call    pfn1
       call    print
       db      ' Not Found - Aborting',0
       ret
;
; Extract Z3TCAP Index
;
start2:
       lxi     d,fcb           ;pt to FCB
       call    f$open          ;open file
       jnz     fnferr
       call    codend          ;read file into buffer
;
; Load Z3TCAP Index
;
loadi:
       lxi     d,fcb           ;pt to FCB
       call    f$read          ;read next block
       jnz     rerr1           ;read error
       lxi     d,tbuff         ;copy from TBUFF
       push    h               ;save ptr to this block
       xchg
       mvi     b,128           ;128 bytes
       call    moveb
       pop     h               ;pt to this block
       lxi     d,16            ;every 16
       mvi     b,8             ;8 entries possible
;
; Check for End of Index
;
loadi1:
       mov     a,m             ;end of index if space
       cpi     ' '
       jz      loadi2
       dad     d               ;pt to next
       dcr     b               ;count down
       jnz     loadi1
       jmp     loadi           ;HL pts to next block to load
;
; Error in Reading File
;
rerr1:
       call    print
       db      cr,lf,'File Read Error During Read of Index',0
       ret
;
; Reached End of Index
;
loadi2:
       shld    z3tcver         ;save ptr to version number
;
; Read Each Block of File
;
       lxi     h,0             ;set count
       shld    tcount
       call    codend          ;pt to first element
check:
       mov     a,m             ;see if done
       cpi     ' '             ;space if so
       jz      done
       lxi     d,fcb           ;pt to FCB
       call    f$read          ;read next record
       jnz     rerr
       push    h               ;save ptr
       lhld    tcount          ;increment count
       inx     h
       shld    tcount
       pop     h               ;get ptr
       lxi     d,tbuff         ;pt to name in buffer
       mvi     b,16            ;check 16 chars
check1:
       ldax    d               ;compare
       cmp     m
       jnz     tnerror
       inx     h               ;pt to next
       inx     d
       dcr     b               ;count down
       jnz     check1
       jmp     check
;
; Read Error
;
rerr:
       call    print
       db      cr,lf,'Premature End of File after ',0
       lhld    tcount          ;get count
       call    phldc
       call    print
       db      ' Terminals Checked',0
       ret
;
; Terminal Name Error
;
tnerror:
       call    print
       db      cr,lf,'Terminal Names are Inconsistent at Terminal ',0
       push    h               ;save ptr
       lhld    tcount          ;get count
       call    phldc
       pop     h               ;get ptr
tne1:
       inx     h       ;advance to next
       dcr     b
       jnz     tne1
       jmp     check

;
; Check Complete
;
done:
       lxi     d,fcb           ;close file
       call    f$close
       call    print
       db      cr,lf,'Z3TCAP File Check of ',0
       lxi     d,fcb+1
       call    pfn1
       call    print
       db      ' Version ',0
       lhld    z3tcver
       inx     h               ;pt to version number
done1:
       mov     a,m             ;print it
       call    cout
       inx     h
       cpi     ' '
       jnz     done1
       call    print
       db      cr,lf,' File Checks with ',0
       lhld    tcount          ;print entry count
       call    phldc
       call    print
       db      ' Terminals Defined',0
       ret
;
; Buffers
;
deffile:
       db      'Z3TCAP  '      ;default file name
deftyp:
       db      'TCP'           ;default file type
tcount:
       ds      2       ;terminal count
z3tcver:
       ds      2       ;ptr to ZCPR3 TCAP Version Number

       end