;
; Program:  CD
; Version:  3.0
; Author:  Richard Conn
; Date: 12 Apr 84
; Previous Versions: None
; Derivation: In Concept from CD 2.4 for ZCPR2
;
version equ     30
z3env   set     0f400h

;
;       CD is used to log into a new directory by name or DU (DIR or DU forms)
; and to automatically run ST once there if it is available.
;
;       Syntax:
;               CD or CD //     <-- Print Help
;               CD dir:         <-- Log In and Run ST.COM
;

;
; OS Equates et al
;
cpm     equ     0
udbyte  equ     4
bdos    equ     5
fcb     equ     5ch
tbuff   equ     80h
tpa     equ     100h
cr      equ     0dh
lf      equ     0ah

;
; SYSLIB and Z3LIB Functions
;
       ext     z3init,z3log
       ext     retud,moveb,initfcb,putcl,eprint,pafdc,cout,dutdir

;
; 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
       lda     fcb+1   ;check for help
       cpi     '/'     ;help?
       jnz     cd
       call    eprint
       db      'CD, Version '
       db      (version/10)+'0','.',(version mod 10)+'0'
       db      cr,lf,'Syntax:'
       db      cr,lf,'  CD dir:  or  CD du:  <-- Change Directory'
       db      0
       ret
;
; Log into DU converted by ZCPR3
;
cd:
       lxi     d,fcb   ;pt to FCB
       call    z3log   ;login to DU
       call    retud   ;set DU in the UD byte
       mov     a,c     ;set user
       rlc             ;rotate right 4 bits
       rlc
       rlc
       rlc
       ani     0f0h    ;mask
       mov     c,a     ;save for now
       mov     a,b     ;get disk
       ani     0fh     ;mask
       ora     c       ;mask in user
       sta     udbyte  ;save value in UD byte
;
; Print New Directory
;
       call    eprint
       db      ' Logging Into ',0
       call    retud   ;get DU in BC
       mov     a,b     ;get disk
       adi     'A'
       call    cout
       mov     a,c     ;get user
       call    pafdc   ;print number
       mvi     a,':'   ;print colon
       call    cout
       call    dutdir  ;convert to name
       jz      runfile ;run file if no name
       mvi     b,8     ;8 chars max to name (pted to by HL)
prtname:
       mov     a,m     ;get name char
       cpi     ' '     ;done?
       jz      runfile
       inx     h       ;pt to next
       call    cout
       dcr     b       ;count down
       jnz     prtname
;
; Look for File
;
runfile:
       lxi     h,stfile        ;pt to FCB
       lxi     d,fcb           ;copy into FCB
       mvi     b,12            ;12 bytes
       call    moveb
       call    initfcb         ;init FCB
       mvi     c,15            ;try to open file
       call    bdos            ;use BDOS
       cpi     0ffh            ;not found?
       jz      cpm             ;done - no ST.COM
       lxi     h,stcl          ;pt to default command line
       call    putcl           ;store it in ZCPR3 CL Buffer
       jnz     cpm             ;abort to OS if done
       call    eprint          ;print error message
       db      ' Command Line Overflow',0
       jmp     cpm

;
; Buffers
;
stfile:
       db      0,'ST      COM' ;default command
stcl:
       db      'ST',0          ;default command line

       end