******************************************************************
*                                                                *
*                     RENAME Command Handler                     *
*                                                                *
*----------------------------------------------------------------*
*                                                                *
*      The RENAME command changes the name of a file on the disk.*
* Both the old name and a new name must be issued with the       *
* command.  Volume, drive and slot parameters are optional.  A   *
* locked file cannot be RENAMEd.                                 *
*      Note that no check is made to insure that a new name does *
* not duplicate that of a file already on the disk.  Therefore,  *
* misuse of the RENAME command can result in a catalog display   *
* similar to the following:  HELLO                               *
*                            MY FIRST PRGM                       *
*                            FREDS PRGM                          *
*                            MY FIRST PRGM                       *
* If a command containing a duplicate file name is used, the     *
* computer will always try to utilize the first version listed.  *
* Therefore, the second version will appear to be unusable.      *
* Fortunately, the  RENAME command can be used to repair damage  *
* caused by its previous misuse.  For example, if you want to    *
* change the above catalog to read:  HELLO                       *
*                                    MY FIRST PRGM               *
*                                    FREDS PRGM                  *
*                                    MY 2ND PRGM                 *
* then, use the following sequence of commands:                  *
*                       RENAME MY FIRST PRGM, TEMP NAME          *
*                       RENAME MY FIRST PRGM, MY 2ND PRGM        *
*                       RENAME TEMP NAME, MY FIRST PRGM          *
*      If you RENAME the greeting program, the disk won't boot   *
* correctly unless you put a new program with the same greeting  *
* name on the disk.  Failure to boot results from the fact that  *
* the boot looks for the greeting name in the primary file name  *
* buffer that was "etched" on the disk during an INIT.  The      *
* image of the original primary file name buffer is located on   *
* the disk at trk$01/sec$09/offset$75 to trk$01/sec$09/offset$92.*
* Therefore if you feel compelled to change the name of the      *
* greeting program, you must zap this area of the disk with the  *
* new file name after you use the RENAME command.  If necessary, *
* be sure to add trailing spaces to the new file name to fill    *
* the entire 30-byte buffer (or else residual characters from    *
* the original name may be tacked on to the end of the new name).*
*                                                                *
* Execution pattern:                                             *
*      The RENAME command handler first copies the address of    *
* the secondary file name buffer (SCNDFNBF, $AA93) from ADRSFNBF *
* ($9D08) to RENAMBUF ($B5BD) in the FM parameter list.  The     *
* file is then closed if it is already open.  A DOS buffer is    *
* (re-)assigned to the file and the file manager is called to    *
* execute the rename function (FNRENAME, $AC3A).  FNRENAME calls *
* the COMNOPEN routine ($AB28) to configure the FM work area and *
* search for the original file name.  The directory sectors are  *
* then read in one at a time until the appropriate file          *
* description entry is found.  If the old file name cannot be    *
* located, a file-not-found message is generated.  Once the old  *
* file name is located, the new name is copied from the secondary*
* file name buffer (SCNFNBF, $AA93) to the name field of the     *
* description entry in the directory sector buffer.  Finally,    *
* the file is closed via the close command handler (CMDCLOSE,    *
* $A2EA).                                                        *
*                                                                *
******************************************************************
   
   
* On entry - CUMLOPTN ($AA65) has been updated
*            to reflect parsed option words (V, D ,S).
*          - the validity of the options issued
*            with the command (and their numeric
*            values) have been checked.
*          - legal file names have been parsed and
*            stored in the primary (PRIMFNBF, $AA75) &
*            secondary (SCNDFNBF, $AA93) file name bufs.


(A281)
CMDRENAM LDA ADRSFNBF ;Copy addr of secondary filename buffer
         STA RENAMBUF ;to the rename buffer in the FM parm list.
         LDA ADRSFNBF+1
         STA RENAMBUF+1
         LDA #9       ;Put RENAME opcode in (a) and save it in
         STA TEMPBYT  ;temporary buffer.
(A292)   JSR CLSLOCBF ;Close file if it is already open and then
                      ;do the RENAME function:
                      ; -copy new file name to directory sector
                      ;  buffer & then write the updated directory
                      ;  sector back to the disk.

                      (A2C8)
                      CLSLOCBF JSR CMDCLOSE ;Close file if it's already open.

                                            (A2EA)
                                            CMDCLOSE .
                                                     .
                                            (See dis'mbly of CMDCLOSE given below.)
                                                     .
                                                     .
                                            - Note that execution flows thru CMDCLOSE twice if the
                                              file is already open.
                                            - The first time thru, the matching DOS filename buffer is
                                              located & then CLOSEONE is used to close the file.
                                            - Execution then jumps back to the start of CMDCLOSE.
                                            - On this second pass, a matching filename is not found
                                              because the DOS filename buffer was released on the
                                              first pass.  Therefore, A5L/H is left pointing at the
                                              highest numbered (lowest in memory) FREE DOS buffer
                                              when CMCLOSE is exited via EVENTXIT and CLOSERTS.
                                            - If the file is not already open on the first entry to
                                              CMDCLOSE, only one pass is made.  This single pass
                                              resembles the second pass mentioned above.
                                                     .
                                                     .
                                            - If necessary, the CLOSE FUNCTION updates the data
                                              sector, T/S list sector & the VTOC.  It also fixes
                                              up links in the directory sectors and updates the
                                              file size if needed.
                                                     .
                                                     .
                                                     (RTS)

                      (A2CB)   LDA A5L+1    ;Hi byte of A5L/H pointer which points at the highest
                                            ;numbered (lowest in memory) free DOS name buffer (in chain).
                      (A2CD)   BNE SAVFNPTR ;Branch if found a free buffer.
                      (A2CF)   JMP NOBUFERR ;Go issue an out-of-buffers message.
                               ------------ ;(See dis'mbly of errors.)
                      (A2D2)
                      SAVFNPTR STA A3L+1    ;Reset A3L/H to point at DOS buffer that we
                               LDA A5L      ;will use for file name field buffer (chain).
                               STA A3L
                      (A2D8)   JSR CPYPFN

                                            * NOTE:  This (re)assigns a DOS buffer to the
                                            * file we want to rename.  The buffer may or
                                            * may not be the same one that was just released
                                            * by the CLOSE cmd above.  The highest numbered
                                            * (lowest in memory) free DOS buffer is used.
                                            (A743)
                                            CPYPFN   LDY #29      ;30 bytes to copy (0 to 29).
                                            CPYPRIM  LDA PRIMFNBF,Y ;Copy the name of the file wanted from
                                                     STA (A3L),Y  ;the primary filename buffer into the
                                                     DEY          ;filename field buffer (in DOS chain).
                                                     BPL CPYRIM   ;More chars to get.
                                            (A74D)   RTS
                                            
                      (A2DB)   JSR BUFS2PRM

                                            * Get addresses of the various DOS buffers from the
                                            * chain buffer & put them in the FM parameter list.
                                            (A74E)
                                            BUFS2PRM LDY #30      ;Get addr of FM work buf, T/S list
                                            ADRINPRM LDA (A3L),Y  ;buf, data sector buf & next DOS
                                                     STA WRKBUFFM-30,Y ;filename buf from chain
                                                     INY          ;pointer buffer & put them in FM parm list.
                                                     CPY #38      ;(P.S.  Adr of next DOS file name buf is
                                                     BNE ADRINPRM ;not used by DOS.)
                                            (A75A)   RTS

                      (A2DE)   JSR CPY2PARM

                                            * Put volume, drive, & slot values plus the
                                            * address of the primary filename buffer
                                            * in the FM parameter list.
                                            (A71A)
                                            CPY2PARM LDA VOLPRSD  ;From parsed table.
                                                     STA VOLFM
                                                     LDA DRVPRSD  ;From parsed table.
                                                     STA DRVFM
                                                     LDA SLOTPRSD ;From parsed table.
                                                     STA SLOTFM
                                                     LDA ADRPFNBF ;Get the adr of the primary file
                                                     STA FNAMBUFM ;name buf from the constants tbl
                                                     LDA ADRPFNBF+1 ;and put it in the FM parm list.
                                                     STA FNAMBUFM+1
                                                     LDA A3L      ;Save adr of current DOS file name
                                                     STA CURFNADR ;buf in table of DOS variables.
                                                     LDA A3L+1
                                                     STA CURFNADR+1
                                            (A742)   RTS

                      (A2E1)   LDA TEMPBYT  ;Get RENAME opcode back from temporary buffer
                               STA OPCODEFM ;and put it in the FM parameter list.
                      (A2E7)   JMP FMDRIVER
                               ------------

                      * Use the file manager driver
                      * to do the RENAME FUNCTION.
                      (A6A8)
                      FMDRIVER JSR FILEMGR  ;Call the file manager to do the function.

                                            * File manager proper.
                                            (AB06)
                                            FILEMGR  TSX          ;Save stk pointer so can later rtn to caller of FM.
                                                     STX STKSAV
                                            (AB0A)   JSR RSTRFMWA

                                                                  * Copy FM work buf (in DOS chain) to
                                                                  * FM work area (not in DOS chain).
                                                                  (AE6A)
                                                                  RSTRFMWA JSR SELWKBUF ;Point A4L/H at FM work buf.

                                                                                        * Get addr of FM work buff from
                                                                                        * the FM parm list & put it in
                                                                                        * the A4L/H pointer.
                                                                                        (AF08)
                                                                                        SELWKBUF LDX #0       ;Offset to select
                                                                                                              ;work buffer.
                                                                                        (AF0A)   BEQ PT2FMBUF ;ALWAYS.

                                                                                        (AF12)
                                                                                        PT2FMBUF LDA WRKBUFFM,X
                                                                                                 STA A4L
                                                                                                 LDA WRKBUFFM+1,X
                                                                                                 STA A4L+1
                                                                                        (AF1C)   RTS

                                                                  (AE6D)   LDY #0       ;Zero out return code in FM parm list to
                                                                           STY RTNCODFM ;signal no errors as default condition.
                                                                  STORFMWK LDA (A4L),Y  ;Copy FM work buf to FM work area.
                                                                           STA FMWKAREA,Y
                                                                           INY
                                                                           CPY #45      ;45 bytes to copy (0 to 44).
                                                                           BNE STORFMWK
                                                                           CLC          ;WHY?????
                                                                  (AE7D)   RTS

                                            (AB0D)   LDA OPCODEFM ;Check if opcode is legal.
                                                     CMP #13      ;(Must be less than 13.)
                                                     BCS TOERROP  ;Opcode too large so got range error.
                                                     ASL          ;Double val of opcode & put it in (x)
                                                     TAX          ;so it indexes tables of adrs.
                                                     LDA FMFUNCTB+1,X ;Stick adr of appropriate function
                                                     PHA          ;handler on stack (hi byte first).
                                                     LDA FMFUNCTB,X
                                                     PHA
                                            (AB1E)   RTS          ;DO STACK JUMP TO FUNCTION ENTRY POINT.

                                                     .
                                                     .
                                            (AC3A)
                                            FNRENAME .
                                                     .
                                            (See dis'mbly of RENAME function.)
                                                     .
                                                     .
                                            - uses part of COMNOPEN routine.
                                            - reads in VTOC to get link to 1rst directory.
                                            - reads in 1rst directory sec & searches
                                              for matching primary filename.  If not found,
                                              reads in next directory sec to determine
                                              if file is locked or not.
                                            - if file locked, then get addr of secondary
                                              filename buf from FM parm list & copy secondary
                                              filename into filename field of file
                                              description entry in directory sector.
                                            - writes updated directory sector to disk.
                                                     .
                                                     .
                                                     (RTS)
                                                     ============

                                            TOERROP  JMP RNGERROP ;Go handle range error.
                                            (AB1F)   ------------ ;(See dis'mbly of errors.)

                      * Return here after doing the RENAME function.
                      * (Cause after @ function is done, use stack
                      * to get back to the original caller.)
                      (A6AB)
                      AFTRFUNC BCC FMDRVRTN ;(c) = 0 = no errors.
                               LDA RTNCODFM ;Get error code from FM parameter list.
                               CMP #$5      ;End-of-data error?
                      (A6B2)   BEQ TOAPPTCH ;Yes - encountered a zeroed-out T/S link or 
                                            ;a zeroed-out data pair (trk/sec vals) in a
                                            ;T/S list.  (Not applicable to rename function.)
                      (A6B4)   JMP OTHRERR  ;No.  See dis'mbly of errors.
                               ------------

                      (A6C3)
                      FMDRVRTN RTS


(A295)   JMP CMDCLOSE ;Exit RENAME cmd via CLOSE cmd.

* Because the file is already open, execution flows
* thru the CLOSE cmd twice.  The first time thru, the matching
* DOS filename buffer is located & then CLOSEONE is used to
* close the file via the RENAME FUNCTION.  The 2nd time thru, a
* matching filename buffer is not found because the DOS
* buffer was released on the first pass.  Therefore, A5L/H is
* left pointing at the highest numbered (lowest in memory)
* FREE DOS buffer when the CLOSE command is exited via EVENTXIT
* and CLOSERTS.
* (P.S.  NOT ALL RAMIFICATIONS OF THE CLOSE COMMAND ARE SHOWN
* BELOW.)

(A2EA)
CMDCLOSE LDA PRIMFNBF ;Get 1rst char from primary name buffer.
         CMP #" "     ;Don't allow leading <spc> in name.
(A2EF)   BEQ CLOSEALL ;Leading <spc> = signal to close all files.
                      ;(A CLOSE cmd was issued with no 
                      ;accompanying file name.)
(A2F1)   JSR GETBUFF  ;Locate a DOS file buffer.

                      * Locate buffer with same name.
                      * If that fails, locate a free buffer.
                      (A764)
                      GETBUFF  LDA #0       ;Default hi-byte of pointer to 0
                               STA A5L+1    ;(ie. assume no free buff available).
                      (A768)   JSR GETFNBF1 ;Get pointer to 1rst filename buffer in chain.
                                            (A792)
                                            GETFNBF1 LDA ADOSFNB1 ;First link to chain of DOS buffers
                                                     LDX ADOSFNB1+1 ;(ie. pt 2 1rst name buf in chain).
                                            (A798)   BNE SETNXPTR ;ALWAYS.

                                            (A7A4)
                                            SETNXPTR STX A3L+1    ;Put addr of 1rst filename buffer in ptr
                                                     STA A3L      ;(ie. highest name buffer in chain).
                                                     TXA          ;Get hi-byte of addr in back in (a).
                                            GETNXRTN RTS
                                            (A7A9)
     
                      (A76B)   JMP FNCHAR1  ;Go get first byte in DOS name buf.
                               ------------

                      (A76E)
                      GETFNLNK JSR GETNXBUF

                                            * Get addr of next filename buffer in chain
                                            * from chain pointers buffer offset 37 & 36
                                            * bytes from 1rst char of present filename
                                            * buffer.
                                            (A79A)
                                            GETNXBUF LDY #37      ;Point the pointer at the chain buffer &
                                                     LDA (A3L),Y  ;get addr of the next filename buffer.
                                                     BEQ GETNXRTN ;If hi-byte is 0, then link zeroed out.
                                                     TAX          ;Save hi-byte in (x).
                                                     DEY          ;Pick up low-byte.
                                                     LDA (A3L),Y
                                            SETNXPTR STX A3L+1    ;Stick addr of filename buffer in ptr.
                                                     STA A3L
                                                     TXA          ;Get hi-byte back in (a).
                                            GETNXRTN RTS
                                            (A7A9)

                      (A771)   BEQ NOFNMTCH ;Link zeroed out, end of chain.
                      FNCHAR1  JSR GETFNBY1 ;Get the 1rst char of filename from buf in chain.
                      (A773)

                                            * Get first byte from DOS name buffer.
                                            (A7AA)
                                            GETFNBY1 LDY #0       ;Buffer is free if 1rst byte = $00.
                                                     LDA (A3L),Y  ;If buf occuppied, the 1rst byte = 1rst
                                            (A7AE)   RTS          ;char of filename which owns buffer.
 
                      (A776)   BNE NXFNBUF  ;Take branch if buffer wasn't free.
                               LDA A3L      ;Buffer was free, there4 point the A5L/H
                               STA A5L      ;pointer at the free buffer.
                               LDA A3L+1
                               STA A5L+1
                      (A780)   BNE GETFNLNK ;ALWAYS.

                      (A782)
                      NXFNBUF  LDY #29      ;Buffer not free there4 compare name
                      CMPFNCHR LDA (A3L),Y  ;of owner with name of file in primary
                               CMP PRIMFNBF,Y ;name buffer.  (Start with last char first.)
                      (A789)   BNE GETFNLNK ;Char doesn't match, there4 look for another
                                            ;buffer that might have same name.
                      (A78B)   DEY          ;That char matched, how bout rest of name?
                               BPL CMPFNCHR ;30 chars in name (ie. 0 to 29).
                               CLC          ;Clr carry to signal names matched.
                      (A78F)   RTS
                               ============

                      (A790)
                      NOFNMTCH SEC          ;Link zeroed out.
                      (A791)   RTS
                               ============
 
(A2F4)
EVENTXIT BCS CLOSERTS ;EVENTUALLY exit via this route.
(A2F6)   JSR CLOSEONE ;Matching file name was found, so close that file.

                      * Close a specific file.
                      (A2FC)
                      CLOSEONE JSR CKEXCBUF

                                            * Check if current filename buffer
                                            * belongs to an EXEC file.  After all,
                                            * don't want to close buffer if we are
                                            * using it to exec (ie. would be like
                                            * burying ourselves alive).
                                            (A7AF)
                                            CKEXCBUF LDA EXECFLAG ;Check to see if EXECing.
                                                     BEQ NOTEXCBF ;No sweat - not running exec file.
                                                     LDA EXECBUFF ;We are EXECing, there4 chk if addr of
                                                     CMP A3L      ;current filename buffer same as that
                                                     BNE CKEXCRTN ;for buffer belonging to EXEC.
                                                     LDA EXECBUFF+1 ;Maybe - low-bytes matched,
                                                     CMP A3L+1    ;so now check hi bytes of addr.
                                                     BEQ CKEXCRTN ;Exec buffer = current buffer.
                                            NOTEXCBF DEX          ;Not EXECing, DEX to make sure z-flag off.
                                            (A7C2)                ;(P.S. (x) was orig set to large non-zero
                                                                  ;val on entry to GETFNBF1.  There4, if now
                                                                  ;DEX, can be sure z-flag will be off.)
                                            CKEXCRTN RTS          ;Exit with: z-flag = 1 if execing.
                                            (A7C3)                ;                  = 0 if not execing.

                      (A2FF)   BNE FREEBUFF ;Not EXECing from this particular file.
                               LDA #0       ;Closing an exec file so shut
                               STA EXECFLAG ;off the exec flag.
                      FREEBUFF LDY #0       ;Free up the DOS buffer by poking a $00 in
                               TYA          ;1rst byte of filename.
                               STA (A3L),Y
                      (A30B)   JSR BUFS2PRM

                                            * GET addresses of the DOS buffers from chain
                                            * buffer & put them in FM parameter list.
                                            (A74E)
                                            BUFS2PRM LDY #30      ;Get addresses of FM Work buffer,
                                            ADRINPRM LDA (A3L),Y  ;T/S list buffer, Data sec buffer and 
                                                     STA WRKBUFFM-30,Y ;NEXT filename buf from the chain ptr
                                                     INY          ;buf & put them in the FM parameter list.
                                                     CPY #38      ;(P.S. Addr of NEXT filename buffer is not
                                                     BNE ADRINPRM ;used by DOS.)
                                            (A75A)   RTS

                      (A30E)   LDA #2       ;Stick opcode for CLOSE function
                               STA OPCODEFM ;in the FM parameter list.
                      (A313)   JMP FMDRIVER ;Get ready to do the function.
                               ------------

                      (A6A8)
                      FMDRIVER JSR FILEMGR  ;Call the file manager to do the function.
         
                                            (AB06)
                                            FILEMGR  TSX          ;Save stk ptr so we can rtn to caller.
                                                     STX STKSAV
                                            (AB0A)   JSR RSTRFMWA ;Copy contents of FM work buffer (in DOS
                                                                  ;chain) to FM work area (not in chain).

                                                                  (AE6A)
                                                                  RSTRFMWA JSR SELWKBUF ;Find FM work buffer.

                                                                                        * Get address of FM work buf from FM parm
                                                                                        * list & stick it in the A4L/H pointer.
                                                                                        (AF08)
                                                                                        SELWKBUF LDA #0
                                                                                        (AF0A)   BEQ PT2FMBUF ;ALWAYS.

                                                                                        (AF12)
                                                                                        PT2FMBUF LDA WRKBUFFM,X 
                                                                                                 STA A4L
                                                                                                 LDA WRKBUFFM+1,X
                                                                                                 STA A4L+1
                                                                                        (AF1C)   RTS

                                                                  (AE6D)   LDY #0       ;Zero-out return code in FM parm list
                                                                           STY RTNCODFM ;to signal no errors.
                                                                  STORFMWK LDA (A4L),Y  ;Copy FM work buff (in chain) to FM work
                                                                           STA FMWKAREA,Y ;area (not in DOS buffer chain).
                                                                           INY
                                                                           CPY #45
                                                                           BNE STORFMWK
                                                                           CLC          ;Why?????
                                                                  (AE7D)   RTS
 
                                            (AB0D)   LDA OPCODEFM ;Chk if opcode is legal.
                                                     CMP #13      ;(Must be less than 13.)
                                                     BCS TOERROP  ;Opcode too large, got range error.
                                                     ASL          ;Double val of opcode & get addr of
                                                     TAX          ;appropriate function handler from tbl.
                                                     LDA FMFUNCTB+1,X ;Put the adr on stack (hi byte first)
                                                     PHA          ;& then do a "stack jump" to the appropriate
                                                     LDA FMFUNCTB,X ;function handler.
                                                     PHA
                                            (AB1E)   RTS

                                                     .
                                                     .
                                            (AC06)   .
                                            FNCLOSE  .
                                                     .
                                                     .
                                            (See dis'mbly of CLOSE function.)
                                                     .
                                                     .
                                            - if necessary, free up any sectors that
                                              were previously allocated to the file
                                              but not needed.
                                            - also updates the file size, fixes up
                                              links & updates data, VTOC, T/S list
                                              & directory sectors if necessary.
                                                     .
                                                     .
                                                     (RTS)
                                                     ============

                                            TOERROP  JMP RNGERROP ;(To $B363 - see dis'mbly of errors.)
                                            (AB1F)   ------------

                      * Return here after doing the CLOSE function
                      * (Cause after @ function is done, use stack
                      * to get back to original caller.)
                      (A6AB)
                      AFTRFUNC BCC FMDRVRTN ;(c) = 0 = no errors.
                               LDA RTNCODFM ;Get error code from FM parameter list.
                               CMP #$5      ;End-of-data error?
                      (A6B2)   BEQ TOAPPTCH ;Yes - encountered a zeroed-out T/S link or 
                                            ;a zeroed-out data pair (trk/sec vals) in a
                                            ;T/S list.  (Not applicable to close function.)
                      (A6B4)   JMP OTHRERR  ;No.  See dis'mbly of errors.
                               ------------

                      (A6C3)
                      FMDRVRTN RTS
         
(A2F9)   JMP CMDCLOSE ;Go back into CMDCLOSE to point A5L/H pointer
         ------------ ;at the highest numbered (lowest in
                      ;memory) free filename buffer in chain of DOS
                      ;buffers & then exit CMDCLOSE via EVENTXIT & CLOSERTS.

(A330)
CLOSERTS RTS          ;Exit to the caller of the RENAME command.
         ============ ;(Normally returns to AFTRCMD ($A17D) in the
                      ;command parsing and processing routines.)
