;
; Program: ALIAS0
; Author: Richard Conn
; Version: 1.1
; Date: 10 Jun 84
; Previous Versions:  1.0 (5 Mar 84)
;
version equ     11

;
;       The purpose of ALIAS0 is to create aliases using the ALIAS1
; program.
;

;
; Environment Definition
;
       MACLIB  Z3BASE.LIB

;
; Basic Equates
;
buffer  equ     4000h   ;1K (approx) buffer
tbuff   equ     80h
fcb     equ     5ch
cr      equ     0dh
lf      equ     0ah

;
; External References
;
       ext     $memry
       ext     z3init,qprint,getwhl
       ext     print,sksp,bbline,capine,crlf,pfn1,retud,logud,padc
       ext     hmovb,sfa,cout,ccout,pstr,zfname
       ext     f$open,f$read,f$close,f$make,f$delete,f$write,f$exist,initfcb

;
; Key Equates for Address Definition of Environment
;
envdefn equ     103H    ;Beginning of Environment Definition
envclas equ     108H    ;Address of Environment Descriptor Class

;
; 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  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      'ALIAS, Version '
       db      (version/10)+'0','.',(version mod 10)+'0',0
;
; Check for File
;
       xra     a
       sta     flflag  ;clear file loaded flag
       lxi     h,fcb+1 ;pt to file name
       mov     a,m     ;get first char
       cpi     ' '     ;no name if space
       jz      create  ;create an alias if no name
       cpi     '/'     ;help?
       jnz     start0
;
; Help Message
;
       call    print
       db      cr,lf,'ALIAS -- Create Alias Commands'
       db      cr,lf,'Syntax:'
       db      cr,lf,' ALIAS                   <-- Define New Command'
       db      cr,lf,'or'
       db      cr,lf,' ALIAS dir:cmndname      <-- Redefine Old Command'
       db      0
       ret

;
; Load ALIAS File
;
start0:
       call    setcom  ;set file type of file in FCB to COM if not already
       call    retud   ;get current DU in BC
       dcx     h       ;pt to disk
       mov     a,m     ;get disk
       ora     a       ;no current disk?
       jz      start1  ;use current disk
       dcr     a       ;set disk (A=0)
       mov     b,a
start1:
       mvi     m,0     ;set current disk
       lxi     d,13    ;pt to user number
       push    h       ;save ptr to FCB
       dad     d       ;pt to user
       mov     c,m     ;get user
       pop     d       ;pt to FCB
       call    logud   ;log into dir in BC
       lxi     h,buffer        ;pt to file storage buffer
       call    f$open  ;open alias file for input
       jz      load    ;load file
       call    print
       db      cr,lf,' Alias File ',0
       inx     d       ;pt to file name
       call    pfn1    ;print it
       call    print
       db      ' Not Found',0
       ret
;
; Load Alias File
;
load:
       lxi     h,buffer        ;pt to load buffer
       mvi     c,9     ;set block count
load1:
       lxi     d,fcb   ;pt to FCB
       call    f$read  ;read next block
       jnz     load2
       lxi     d,tbuff ;pt to buffer
       xchg
       mvi     b,128   ;128 bytes
       call    hmovb
       xchg            ;DE pts to next block
       dcr     c       ;count down
       jnz     load1
;
; Error - Not an Alias File
;
noalias:
       call    print
       db      cr,lf,' File ',0
       lxi     d,fcb+1
       call    pfn1
       call    print
       db      ' is Not an Alias',0
       ret
;
; Load Complete - Check for Alias File Structure
;
load2:
       call    f$close ;close input file
       lhld    buffer+1        ;get address of START
       lxi     d,buffer-100H
       dad     d       ;pt to actual address
       lxi     d,9     ;skip to ALIAS ID
       dad     d
       lxi     d,aliasid       ;compare
       mvi     b,8     ;8 chars
acheck:
       ldax    d       ;get char
       cmp     m       ;compare
       jnz     noalias
       inx     h       ;pt to next
       inx     d
       dcr     b       ;count down
       jnz     acheck
       shld    oldaline        ;save ptr to old alias command line
       mvi     a,0ffh  ;set file loaded flag
       sta     flflag
;
; Create New Alias
;
create:
       lda     flflag  ;check for file loaded
       ora     a       ;0 if not
       jz      cr1
;
; Display Data on Old Alias
;
       call    print
       db      cr,lf,' Alias Name: ',0
       lxi     h,fcb+1 ;pt to name
       mvi     b,8     ;8 chars
cr0:
       mov     a,m     ;print name
       call    cout
       inx     h       ;pt to next char
       dcr     b       ;count down
       jnz     cr0
       call    print
       db      cr,lf,' Old Alias Command Line:',0
       lhld    oldaline
       mvi     b,1     ;set line count
cr0a:
       mov     a,m     ;check for done
       ora     a
       jz      cr1
       call    crlf    ;new line
       mov     a,b     ;print line number
       call    padc
       inr     b       ;increment count
       call    print   ;print prompt
       db      ' --> ',0
cr0b:
       mov     a,m     ;get char
       ora     a
       jz      cr1
       call    ccout   ;print char with control char processing
       inx     h       ;pt to next
       cpi     ';'     ;next logical line?
       jz      cr0a
       jmp     cr0b

;
; Input Alias Command from User
;
cr1:
       call    getwhl  ;get wheel byte
       jnz     cr1a
       call    print
       db      cr,lf,' Modification of Alias Not Permitted',0
       ret
;
; Input Alias from User
;
cr1a:
       call    print
       db      cr,lf
       db      cr,lf,' Input Alias (RETURN to Abort)',cr,lf,' --> ',0
       mvi     a,0ffh  ;input line from user
       call    bbline
       call    sksp    ;skip to non-blank
       mov     a,m     ;get char
       ora     a       ;any input?
       rz
       push    h       ;save ptr
;
; Set Buffers in New Alias to Reflect the ALIAS0 Buffers
;
       call    codend  ;pt to new alias
       inx     h       ;pt to start of buffers
       inx     h
       inx     h
       lxi     d,envdefn       ;pt to environment definition
       mvi     b,8     ;prepare to copy 8 bytes for class 1
                       ; bytes are:    DB      'Z3ENV',1
                       ;               DW      envaddr
       lda     envclas ;get code for class of alias
       cpi     1       ;class 1 is address of Env Desc only
       jz      cr1b
       mvi     b,100H-3        ;prepare to copy an entire env desc
;
; Copy the Buffers Over
;
cr1b:
       ldax    d       ;copy environment definition into new alias
       mov     m,a
       inx     h       ;advance
       inx     d
       dcr     b       ;count down
       jnz     cr1b
;
; Prep to Copy New Command Into Alias
;
       call    codend  ;pt to new alias command buffer
       inx     h
       mov     e,m     ;get address of START
       inx     h
       mov     d,m
       lxi     h,17    ;pt to buffer at START+17
       dad     d
       lxi     d,-100H ;compute offset
       dad     d       ;HL contains offset
       xchg
       call    codend
       dad     d       ;HL pts to absolute address
       mvi     b,255   ;check for overflow (max number of chars)
       pop     d       ;pt to new command
;
; Copy New Command into ALIAS File
;
cr2:
       ldax    d       ;get char
       mov     m,a     ;put char
       ora     a       ;done?
       jz      cr3
       inx     h       ;pt to next
       inx     d
       dcr     b       ;count down
       jnz     cr2
       call    print
       db      cr,lf,' Command Too Long for Buffer - Reenter',0
       jmp     create
;
; ALIAS Command Line Copied
;
cr3:
       lda     flflag  ;file loaded?
       ora     a       ;0=no
       jnz     write
       call    print
       db      cr,lf,' Name of ALIAS Command (RETURN to Abort)? ',0
       mvi     a,0ffh  ;capitalize
       call    bbline
       call    sksp    ;skip to non-blank
       mov     a,m
       ora     a
       jz      create
       lxi     d,fcb   ;pt to FCB
       call    zfname  ;convert and place in FCB
       call    setcom  ;set file type to COM if not already
       call    retud   ;get current DU
       lda     fcb     ;get disk
       ora     a       ;default?
       jz      cr4
       dcr     a       ;disk A = 0
       mov     b,a
cr4:
       lda     fcb+13  ;get user
       mov     c,a
       call    logud
;
; Write File whose Name is in FCB
;
write:
       lxi     d,fcb   ;check for file
       call    initfcb ;clear FCB
       call    f$exist ;check for existence
       jz      write1
       call    print
       db      cr,lf,' File ',0
       lxi     d,fcb+1
       call    pfn1
       call    print
       db      ' Exists - Overwrite (Y/N)? ',0
       call    capine
       call    crlf
       cpi     'Y'
       rnz             ;abort if not Y
       lxi     d,fcb   ;clear file attributes
       xra     a
       call    sfa     ;set file attributes to R/W DIR
       call    f$delete        ;delete file
;
; Create New File
;
write1:
       lxi     d,fcb   ;pt to FCB
       call    initfcb ;init it
       call    f$make  ;create file
       mvi     c,8     ;8 sectors
       call    codend  ;pt to file
write2:
       lxi     d,tbuff ;copy sector into buffer
       mvi     b,128   ;128 bytes
       call    hmovb
       lxi     d,fcb   ;write block
       call    f$write
       jnz     werr
       dcr     c       ;count down
       jnz     write2
       call    f$close ;close file
       call    print
       db      cr,lf,' Alias Created',0
       ret
werr:
       call    print
       db      cr,lf,' Error in Creating File',0
       ret

;
; Determine End of Code to Nearest 128 bytes
;
codend:
       push    d
       lhld    $memry  ;get next available byte
       mov     a,l     ;get low
       ani     80h     ;set MSB
       mov     l,a
       lxi     d,128   ;next 128 bytes
       dad     d
       pop     d
       ret

;
; Set File Type of File in FCB to COM if Not Already Set
;
setcom:
       lda     fcb+9   ;get first char of file type
       cpi     ' '
       rnz
       push    h
       push    d
       push    b
       lxi     h,comtyp        ;set COM type
       lxi     d,fcb+9         ;copy into file type
       mvi     b,3     ;3 bytes
       call    hmovb
       pop     b
       pop     d
       pop     h
       ret

;
; Buffers
;
comtyp:
       db      'COM'           ;COM File Type
aliasid:
       db      'Z3 ALIAS'      ;ALIAS File ID
flflag:
       ds      1       ;file loaded flag
oldaline:
       ds      2       ;ptr to old alias line

       end