;
;   Dumper - file dump - Jim Butterfield.  March 15/88.

; Exec library calls
            xref _LVOOpenLibrary       ;-$228
            xref _LVOCloseLibrary      ;-$19E
            xref _LVOSetSignal         ;-$132
; DOS library calls
            xref _LVOOpen              ;-$1E
            xref _LVOClose             ;-$24
            xref _LVOOutput            ;-$3C
            xref _LVOWrite             ;-$30
            xref _LVORead              ;-$2A
;
;-- Initial setup:
Startup     move.l a0,a4             ; Remember ptr to argument line.
            move.l d0,d4             ; Remember len of argument line.
            lea    dosname(pc),a1    ;-Specify name 'dos.library'.
            clr    d0                ;   Any version (0)
            move.l $4,a6             ;   Then using Exec library,
            jsr    _LVOOpenLibrary(a6)  ;   Open Dos library.
            move.l d0,a6             ; Remember dosbase ptr.
            beq    StartupQuit       ; Check for error (d0=0 means
            bsr    DOSinit
            move.l a6,a1             ;-Specify Dos library in a1;
            move.l $4,a6             ;   then using Exec library,
            jsr    _LVOCloseLibrary(a6)    ;   close Dos library.
StartupQuit rts                      ; End of Program
;
;-- Get CLI outhandle:
DOSinit     jsr    _LVOOutput(a6)    ;   get CLI outhandle,
            move.l d0,a5             ;   & then remember it.
;
;-- Change arg file name to 'C string':
checklen    move.b #0,-$1(a4,d4.W)   ; binary zero at end
skipspc     cmp.b  #$20,(a4)+
            beq    skipspc
            subq   #1,a4
            move.l a4,d1             ; filename pointer
            move.l #1005,d2          ; MODE_OLDFILE (for reading)
            jsr    _LVOOpen(A6)
            move.l d0,d6             ; file inhandle
            beq    DOSquit           ; no good, quit
            bsr    DumpFile          ; the main job
            move.l d6,d1             ; use the handle..
            jsr    _LVOClose(A6)     ; to close the file
DOSquit     rts                      ;   exit program.
;
; DOS is open and we have our in/out handles.  Let's go!
DumpFile    moveq  #0,d7             ; zero column count
ReadBlock   move.l d6,d1             ; input file handle
            move.l #BufAdr,d2        ; input buffer address
            move.l #BufSize,d3       ; input buffer size
            jsr    _LVORead(A6)      ; so read it
            move.l d0,d5             ; size of data
; Here's where we scan and output
            moveq  #0,d4             ; start of buffer pointer
            lea    BufAdr,a4
; Character loop; are we at end of buffer?
NextChar    cmp.l  d5,d4
            bge   EndBuf
; We have data!  Let's crunch it.
            move.b $0(a4,d4),d0
            addq   #1,d4
            cmp.b  #$0a,d0
            beq    NuLiner
            cmp.b  #$20,d0
            bge    NotDot
            move.b #$2e,d0
NotDot      move.b d0,OutBuf
            move.l a5,d1             ;file handle
            move.l #OutBuf,d2        ;buffer
            moveq  #1,d3             ;length
            jsr    _LVOWrite(A6)
; count and send NewLine if needed
            addq   #1,d7             ;bump column count
            cmp.b  #64,d7            ; at limit?
            blt    NextChar
NuLiner     move.b #$0a,OutBuf
            move.l a5,d1             ;file handle
            move.l #OutBuf,d2        ;buffer
            moveq  #1,d3             ;length
            jsr    _LVOWrite(A6)
            move.l a6,d7             ;stash DOSbase

            moveq  #0,d0
            move.l #$1000,d1
            movea.l $4,a6
            jsr    _LVOSetSignal(A6) ;test CTRL-C
            move.l d7,a6             ;restore DOSBase
            andi.l #$1000,d0
            tst.l  d0
            beq    NotCtrlC
            move.l a5,d1             ;file handle
            move.l #CtrlCMess,d2     ;buffer
            moveq  #3,d3             ;length
            jsr    _LVOWrite(A6)
            moveq  #0,d5             ;zap buffer causing exit
NotCtrlC    moveq  #0,d7             ;colcount = 0
            bra    NextChar
; The buffer is bare.  If twas full, refill it.
EndBuf      cmp.w  #BufSize,d5       ; was it a full buffer?
            beq    ReadBlock         ; yes, get more
            rts                      ; no, we're done
dosname     dc.b 'dos.library',0
CtrlCMess   dc.b '^C',$0a
OutBuf      dc.b  0
BufSize     EQU  10
BufAdr      dc.b  0,0,0,0,0,0,0,0,0,0


   end
