home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / utils / dirutl / xren.lbr / XREN.AZM / XREN.ASM
Encoding:
Assembly Source File  |  1987-01-15  |  9.0 KB  |  334 lines

  1.  
  2.  
  3. ;**********************************************
  4. ;              XREN.ASM     2/18/84   7:45 P.M.
  5. ;
  6. ;     File renaming with wild cards *,?
  7. ;     allowed in both the old and new file names
  8. ;
  9. ;    USAGE:  XREN (d:)fileOld fileNew
  10. ;
  11. ;    Program: By Miguel Vasquez based on
  12. ;             program by George M Gallen
  13. ;
  14. ;        needs 8080   and CP/M
  15. ;***********************************************
  16. ;
  17. ;--------------------EQUATES--------------------
  18. ;
  19. BDOS    EQU    5    ;Entry point to CP/M functions
  20. PRINTS    EQU    9    ;Func. # to Print String 
  21. OPEN    EQU    15    ;Func. # to Open existing File 
  22. CLOSE    EQU    16    ;Func. # to Close File 
  23. REN    EQU    23    ;Func. # to Rename File 
  24. FCB0    EQU    5CH    ;CP/M's FCB
  25. NOFIND    EQU    0FFH    ;Error code for file not found
  26. ;------------------------------------------------
  27. ;*************** MAIN PROGRAM START **************
  28.     ORG    0100H        ;Start of TPA
  29. MAIN:    CALL    INIT        ;Initialize our Stack.
  30.     LDA    FCB0+1        ;Check if source file is
  31.     CPI    ' '        ;blank. If so give BAD
  32.     JZ    BF        ;FORMAT messg. and exit.
  33.     LDA    FCB0+17        ;Check second file
  34.     CPI    ' '        ;also.
  35.     JZ    BF        ;
  36. ;
  37.     LXI    H,FCB0+1    ;Move original file name
  38.     LXI    D,FCB1+1    ;to its place in
  39.     LXI    B,11        ;FCB1 and FCB3.
  40.     CALL    LDIR        ;
  41.     LXI    H,FCB0+1    ;
  42.     LXI    D,FCB3+1
  43.     LXI    B,11        ;
  44.     CALL    LDIR        ;
  45. ;
  46.     LXI    H,FCB0+17    ;Move new file name
  47.     LXI    D,FCB5+1    ;to FCB5.
  48.     LXI    B,11        ;
  49.     CALL    LDIR        ;
  50. ;
  51.     CALL    DRIVE        ;Save Drive specs
  52. ;
  53. RNLOOP:    LXI    H,FCB3        ;Try to open source file.
  54.     CALL    FOPEN        ; 
  55.     CPI    NOFIND        ;If unsuccessful, then no more
  56.     JZ    ENDIT        ;files, so end it.
  57. ;
  58.     LXI    H,FCB0+1    ;Put full file name (now without ??)
  59.     LXI    D,FCB1+1    ;(with the extensions) to
  60.     LXI    B,11        ;the appropriate spots in
  61.     CALL    LDIR        ;FCB1.
  62.     CALL    NEWNAM        ;Make new full file name in FCB2.
  63. ;
  64.     MVI    C,CLOSE        ;Close the original file
  65.     LXI    D,FCB0        ;
  66.     CALL    BDOS        ;
  67. ;
  68.     LXI    H,FCB2+1    ;Set up new full file name
  69.     LXI    D,FCB4+1    ;(with extension)
  70.     LXI    B,11        ;in FCB4.
  71.     CALL    LDIR        ;
  72.     LXI    H,FCB4        ;Try to open the new file
  73.     CALL    FOPEN        ;
  74.     CPI    NOFIND        ;Could open it?  Horrors!!
  75.     JNZ    CANNT        ;Exit without renaming.
  76. ;
  77.     MVI    C,REN        ;Rename the old file
  78.     LXI    D,FCB1        ;to the new name.
  79.     CALL    BDOS        ;
  80. ;
  81.     CALL    INFORM        ;Inform of files renamed
  82.     JMP    RNLOOP        ;Process next one.
  83. ;
  84. CANNT:    MVI    C,CLOSE        ;Found a file with that name!!
  85.     CALL    BDOS        ;close it !
  86. ;
  87.     LXI    H,FCB2+1    ;Move the Bad file 
  88.     LXI    D,MSG7        ;name and extension
  89.     LXI    B,8        ;into Message #7
  90.     CALL    LDIR        ;
  91.     INX    D        ;
  92.     LXI    B,3        ;
  93.     CALL    LDIR        ;
  94.     LXI    D,MSG8        ;display message and
  95.     JMP    STROUT        ;exit.
  96.  
  97. ENDIT:    LXI    D,MSG5        ;We're done, prepare message
  98.     JMP    STROUT        ;Print it and return to CP/M
  99. ;
  100. BF:    LXI    D,MSG6        ;Prepare BAD FORMAT message
  101.     JMP    STROUT        ;go printit and exit
  102. ;
  103. ;=============== SUBROUTINES ======================
  104. ;
  105. ;********************************************************
  106. ;                INFORM
  107. ;
  108. ;      to print message with files being renamed
  109. ;********************************************************
  110. INFORM:    LXI    H,FCB1+1    ;Move old file name
  111.     LXI    D,MSG3        ;and extension into
  112.     LXI    B,8        ;MSG3.
  113.     CALL    LDIR        ;
  114.     INX    D        ;
  115.     LXI    B,3        ;
  116.     CALL    LDIR        ;
  117.     LXI    H,FCB2+1    ;Move new file name
  118.     LXI    D,MSG4        ;and extension into
  119.     LXI    B,8        ;MSG4.
  120.     CALL    LDIR        ;
  121.     INX    D        ;Then display message
  122.     LXI    B,3        ;with the two names
  123.     CALL    LDIR        ;to let us know what's
  124.     LXI    D,MSG1        ;going on. (Drop down
  125. ;    JMP    STROUT        ;into STROUT to do it)
  126. ;********************************************************
  127. ;                STROUT  to send a string to the console
  128. ;
  129. ;   Arguments : DE has the addr. of the string to be sent
  130. ;*******************************************************
  131. STROUT:    MVI    C,PRINTS    ;Set function code
  132.     JMP    BDOS        ;go doit
  133. ;        
  134. ;*******************************************************
  135. ;                 DRIVE   to save drives
  136. ;
  137. ;    Arguments:  The first byte of FCB0 contains
  138. ;                the source drive:
  139. ;                0  for the logged drive
  140. ;         1  for drive A
  141. ;                2  for drive B, etc.
  142. ;*******************************************************
  143. DRIVE:    LDA    FCB0        ;Get the drive # and store
  144.     STA    FCB1        ;it in its place in FCB1,
  145.     STA    FCB3        ;FCB3, and FCB4.
  146.     STA    FCB4        ;
  147.     ORA    A        ;Is it the Logged drive?
  148.     RZ            ;Yes, leave drive blank.
  149.     ADI    64        ;No, convert to letter,
  150.     STA    DRV1        ;Save it and 
  151.     STA    DRV2        ;add a colon.
  152.     MVI    A,':'        ;
  153.     STA    DRV1+1        ;
  154.     STA    DRV2+1        ;
  155.     RET
  156. ;
  157. ;********************************************************
  158. ;                NEWNAM  to make new name
  159. ;
  160. ;     Arguments:    FCB0 has a regular full filename 
  161. ;              FCB5 hs a file name with wild cards (?)
  162. ;              On exit FCB2 will have FCB5 but
  163. ;                     with ?'s replaced with the corresponding
  164. ;                     character from FCB0.
  165. ;********************************************************
  166. NEWNAM:    MVI    B,11        ;Counter (bytes in file name).
  167.     LXI    D,FCB2+1    ;Destination addr. in DE.
  168.     LXI    H,FCB0+1    ;Use Stack as an extra
  169.     PUSH    H        ;index register for FBC0.(Source)
  170.     LXI    H,FCB5+1    ;Mask addr. in HL
  171. LBA:    MOV    A,M        ;Move mask byte to A.
  172.     CPI    '?'        ;MasK='?' ?
  173.     JNZ    LBB        ;no, output A.
  174.     XTHL            ;yes, Get FCB0 index into HL.
  175.     MOV    A,M        ;Get source byte. 
  176.     XTHL            ;Put FCB0 index back in stack.
  177.     CPI    ' '        ;Is source a blank?
  178.     JNZ    LBB        ;No, then output it.
  179.     MVI    C,1        ;Yes, set C to number of
  180.     MOV    A,B        ;bytes neccessary to
  181.     CPI    4        ;fill the rest of
  182.     JM    LBC        ;the name or extension
  183.     MVI    C,4        ;with blanks. That is:
  184. LBC:    MOV    A,B        ;if B<4 then C=1 else C=4
  185.     SUB    C        ;C=B-C
  186.     MOV    C,A        ;
  187. LBD:    MOV    A,C        ;If C=0 then we are
  188.     ORA    A        ;at the last blank byte,
  189.     MVI    A,' '        ;Go and output it.
  190.     JZ    LBB        ;
  191.     STAX    D        ;Put blank in FCB5.
  192.     INX    H        ;Increment the three
  193.     INX    D        ;indexes.
  194.     XTHL            ;
  195.     INX    H        ;
  196.     XTHL            ;
  197.     DCR    B        ;Decrement the two
  198.     DCR    C        ;counters.
  199.     JMP    LBD        ;
  200. LBB:    STAX    D        ;Put char. in FCB5.
  201.     INX    H        ;Increment the three
  202.     INX    D        ;indexes.
  203.     XTHL            ;
  204.     INX    H        ;
  205.     XTHL            ;
  206.     DCR    B        ;Process next byte if
  207.     JNZ    LBA        ;not thru yet.
  208.     POP    H        ;If thru, restore stack
  209.     RET            ;and return
  210. ;    
  211. ;********************************************************
  212. ;             FOPEN  to try to open an existing file
  213. ;
  214. ;      Arguments:  HL has addr. of FCB with file to be opened
  215. ;********************************************************
  216. FOPEN:    LXI    D,FCB0        ;Move FCB to CP/M's
  217.     LXI    B,35        ;FCB.
  218.     CALL    LDIR        ;
  219.     MVI    C,OPEN        ;Try to open the file.
  220.     LXI    D,FCB0        ;
  221.     CALL    BDOS        ;
  222.         RET
  223. ;
  224. ;********************************************************
  225. ;              LDIR     to simulate Z-80's LDIR
  226. ;
  227. ;  used to move a string from a location to another
  228. ;  
  229. ;  Arguments:   HL  addr. of source
  230. ;               DE  addr. of destination
  231. ;               BC  Number of bytes to be moved
  232. ;
  233. ; All the registers are modified
  234. ;
  235. ; Any Z-80 based computer (such as Osborne 1's)
  236. ; can replace all the CALL LDIR's
  237. ; with:     DB    0EDH,0B0H 
  238. ; or replace this subroutine with
  239. ; LDIR:    DB    0EDH,0B0H
  240. ;     RET
  241. ;*************************************************** 
  242. LDIR:    MOV    A,M        ;Move next byte
  243.     STAX    D        ;to its destination.
  244.     INX    H        ;Increment the pointers
  245.     INX    D        ;to source and destination.
  246.     DCX    B        ;Decrement byte count.
  247.     MOV    A,B        ;Test if BC
  248.     ORA    C        ;is zero.
  249.     RZ                 ; Yes, then return.
  250.     JMP    LDIR        ; No, process another byte
  251. ;
  252. ;********************************************************
  253. ;        INIT to initialize the stack
  254. ;        and save CP/M's stack
  255. ;********************************************************
  256. INIT:    POP    B        ;get return addr.
  257.     LXI    H,0        ;load HL with
  258.     DAD    SP        ;the stack pointer.
  259.     SHLD    CPMSTK        ;save it in CPMSTK.
  260.     LXI    H,MYSTAK    ;Change to my stack.
  261.     SPHL            ;
  262.     LXI    D,FINISH    ;Put finish routine
  263.     PUSH    D        ;in stack.
  264.     PUSH    B        ;put return addr.in stack.
  265.     RET            ;return 
  266. FINISH    LHLD    CPMSTK        ;reinstall CP/M's stack
  267.     SPHL            ;
  268.     RET            ;and return to CP/M.
  269. ;
  270. ;========================= DATA AREAS ==================
  271. FCB1:    DB    0        ;Drive for the Original FCB, 
  272.     DB    '????????'    ;Name
  273.     DB    '???'        ;Extension
  274.     DB    0,0,0,0        ;Filler
  275. ;
  276. FCB2:    DB    0        ;New FCB, (drive)
  277.     DB    '????????'    ;Name
  278.     DB    '???'        ;Extension
  279.     DB    0,0,0,0        ;Filler
  280. ;
  281. FCB3:    DB    0        ;Full original FCB 
  282.     DB    '????????'
  283.     DB    '???'
  284.     DB    0,0,0,0,0
  285.     DB    0,0,0,0,0
  286.     DB    0,0,0,0,0
  287.     DB    0,0,0,0,0
  288.     DB    0,0,0
  289. ;
  290. FCB4:    DB    0        ;Full new FCB
  291.     DB    '????????'
  292.     DB    '???'
  293.     DB    0,0,0,0,0
  294.     DB    0,0,0,0,0
  295.     DB    0,0,0,0,0
  296.     DB    0,0,0,0,0
  297.     DB    0,0,0
  298. ;
  299. FCB5:    DB    0        ;Mask for New FCB
  300.     DB    '????????'    ;Name
  301.     DB    '???'        ;Extension
  302. ;
  303. ; misc messages to let you know what's going on
  304. ;
  305. MSG1:    DB    'RENAMING '
  306. DRV1:    DB    '  '        ;2 spaces
  307. MSG3:    DB    '        '     ;8 spaces
  308.     DB    '.'
  309.     DB    '   '        ;3 spaces
  310.     DB    ' TO '
  311. MSG4:    DB    '        '     ;8 spaces
  312.     DB    '.'
  313.     DB    '   '        ;3 spaces
  314.     DB    13,10,'$'
  315. MSG5:    DB    10,'No more matches',13,10
  316.     DB    'I AM DONE......',13,10,'$'
  317. MSG6:    DB    '* INVALID FORMAT *',13,10,10
  318.     DB    'It should be :  '
  319.     DB    'XREN [d:]fileOld fileNew',13,10,10,7,'$'
  320. MSG8:    DB    10,'THE FILE '
  321. DRV2:    DB    '  '        ;2 spaces
  322. MSG7:    DB    '        '     ;8 spaces
  323.     DB    '.'
  324.     DB    '   '        ;3 spaces
  325.     DB    ' ALREADY EXISTS. ',13,10
  326.         DB    'PROCESSING HALTED. CANNOT RENAME ALL FILES'
  327.         DB    13,10,10,7,'$'
  328. ;
  329. CPMSTK    DS    2        ;CP/M's stack pointer
  330.     DS    40        ;
  331. MYSTAK    EQU    $        ;My stack.
  332.     END
  333.