;
;   Load and Analyze file - Jim Butterfield.  March 23/89.

; Exec library calls
            xref _LVOAllocMem          ;-$C6
            xref _LVOFreeMem           ;-$D2
            xref _LVOSetSignal         ;-$132
            xref _LVOCloseLibrary      ;-$19E
            xref _LVOOpenLibrary       ;-$228
; DOS library calls
            xref _LVOOpen              ;-$1E
            xref _LVOClose             ;-$24
            xref _LVORead              ;-$2A
            xref _LVOWrite             ;-$30
            xref _LVOOutput            ;-$3C
            xref _LVOLock              ;-$54
            xref _LVOUnLock            ;-$5A
            xref _LVOExamine           ;-$66
;
ACCESS_READ            equ -2
MODE_OLDFILE           equ 1005
INFOSIZE               equ 260
SIZEPOINTER            equ 124
;
BufSize EQU 10
;-- Initial setup:
Startup     link   a4,#-$20
            move.l a0,-$4(a4)        ; Remember ptr to argument line.
            move.l d0,-$8(a4)        ; Remember len of argument line.
            lea    dosname(pc),a1    ; Name 'dos.library'.
            clr.l  d0                ;   Any version (0)
            move.l $4,a6             ;   Using Exec library
            jsr    _LVOOpenLibrary(a6)  ;   Open Dos library.
            move.l d0,-$C(a4)        ; Remember DosBase ptr.
            beq.s  StartupQuit       ;      dos not opened
            bsr.s  DOSinit
            move.l -$C(a4),a1        ; DosBase to a1;
            move.l $4,a6             ;   then using Exec library,
            jsr    _LVOCloseLibrary(a6)    ;   close Dos library.
StartupQuit unlk   a4
            rts                      ; End of Program
;
;-- Get CLI outhandle:
DOSinit     move.l -$C(a4),a6        ; set Dos library
            jsr    _LVOOutput(a6)    ;   get CLI outhandle,
            move.l d0,-$10(a4)       ;   & then remember it.
;
; -- Change arg file name to C type of string:
checklen    move.l -$4(a4),a3        ; command tail address
            move.l -$8(a4),d3        ; command tail length
            move.b #0,-$1(a3,d3.W)   ; binary zero at end
skipspc     cmp.b  #$20,(a3)+
            beq.s  skipspc
            subq.l #1,a3
            move.l a3,-$4(a4)        ; save corrected address
; get a 'lock' on the file, if possible.
            move.l a3,d1
            moveq  #ACCESS_READ,D2   ; get a read lock
            jsr _LVOLock(a6)
            move.l d0,-$14(a4)       ; store the lock pointer
            beq    DosQuit           ; couldn't get lock
; ask for memory to hold a FileInfoBlock.
            move.l $4,a6             ; this calls for Exec
            moveq  #0,d1             ; any type of memory
            move.l #INFOSIZE,d0      ; size of block needed
            jsr    _LVOAllocMem(a6)  ; go get it
            move.l d0,-$18(a4)       ; save the block address
            beq.s  DropLock          ; quit if no memory
; Now get information on the file
            move.l -$C(a4),a6        ; set Dos library
            move.l -$14(a4),d1       ; lock
            move.l d0,d2             ; file info block address
            jsr _LVOExamine(a6)      ; go for the data
; Assume Examine never fails.  Go for the size.
            move.l d2,a2             ; file info block address
            move.l SIZEPOINTER(a2),-$1C(a4)  ;file size
            beq.s  FreeInfo
; OK, we know the size.  Ask for a block that big
            move.l $4,a6             ; this calls for Exec
            moveq  #0,d1             ; any type of memory
            move.l -$1C(a4),d0       ; size of block needed
            jsr    _LVOAllocMem(a6)  ; go get it
            move.l d0,-$20(a4)       ; save the block address
            beq.s  FreeInfo          ; quit if no memory
; Whew!  Now ready to read the file.
            move.l -$C(a4),a6        ; set Dos library
            move.l -$4(a4),d1        ; filename pointer
            move.l #1005,d2          ; MODE_OLDFILE (for reading)
            jsr    _LVOOpen(A6)
            move.l d0,d6             ; file inhandle
            beq.s  FreeFileMem       ; no good, quit
; Read whole file in one shot.
            move.l d6,d1             ; input file handle
            move.l -$20(a4),d2       ; input buffer address
            move.l -$1C(a4),d3       ; input buffer size
            jsr    _LVORead(A6)      ; so read it
            move.l d6,d1             ; use the handle..
            jsr    _LVOClose(A6)     ; to close the file
            bsr.s  MainJob           ; the main job
FreeFileMem move.l $4,a6             ; set Exec library
            move.l -$20(a4),a1       ; memory block address
            move.l -$1C(a4),d0       ; size of block
            jsr    _LVOFreeMem(a6)   ; release this memory
FreeInfo    move.l $4,a6             ; set Exec library
            move.l -$18(a4),a1       ; memory block address
            move.l #INFOSIZE,d0      ; size of block
            jsr    _LVOFreeMem(a6)   ; release this memory
DropLock    move.l -$C(a4),a6        ; set Dos Library
            move.l -$14(a4),d1       ; take off the lock
            jsr    _LVOUnLock(a6)
DosQuit     rts                      ;   exit program.
;
; The file is in the buffer.  Write it in one shot!
; Register A6 contains DOSBase
MainJob     move.l -$10(a4),d1       ;file handle
            move.l -$20(a4),d2       ;outbuffer
            move.l -$1C(a4),d3       ;length
            jsr    _LVOWrite(A6)
            rts
dosname     dc.b   'dos.library',0
   end
