;
; FileNote.asm
;
; Written: Jan/10/1987 by SDB
; Copyright (c) 1987 by Scott Ballantyne.
;
;       This program may be freely distributed.  Improvements are welcome,
;       but any code based on this program can never become proprietary.
;       You are welcome to use, improve, and distribute this program.
;       You are forbidden to prevent anyone else from using, improving,
;       or distributing this program. 
;       
;       Another contribution to Cheath's replace AmigaDOS commands.  Be the
;       first on YOUR block to own the entire set.
;
;       With the manx assembler, this comes in at 388 bytes.  Metacompost's
;       assembler should bring this in about 72 bytes less.  The BCPL version
;       is 700 bytes...
;
;       Syntax is the essentially the same as the BCPL version, but quotes
;       are optional, unless, of course, the filename has spaces.
;
;       filenote ["]<filename>["] ["]<comment>["]
;
;       As in the BCPL version, filename by itself deletes the comment, if
;       any, as does Filenote filename "".  Actually, I don't know if that
;       works in the BCPL one.
;
;       Tried to be a little more informative in usage and error messages.
;
LIBRARY_VERSION         equ     31

_LVOOpenLibrary         equ     -552
_LVOCloseLibrary        equ     -414
_AbsExecBase            equ     4
_LVOOutput              equ     -60
_LVOWrite               equ     -48
_LVOSetComment          equ     -180

        entry   Start
        rts
Start:
        move.l  a0,a2           ; save command ptr
        move.l  _AbsExecBase,a6
        lea     DOSName(pc),a1
        moveq.l #LIBRARY_VERSION,d0
        jsr     _LVOOpenLibrary(a6)
        bne.s   okok
        move.l  #20,d0          ; openlibrary didn't...
        rts
okok:
        move.l  d0,a6           ; doslib ptr in a6
        move.b  #' ',d7         ; initial terminator character for filename
        bsr.s   getcmd          ; get filename in a3
        tst.l   d0              ; returns length in d0
        bne.s   okokok
        lea     usage(pc),a0
        bra.s   error
okokok: move.l  a0,d1           ; Setup for setcomment
        lea     null(pc),a0     ; potential NULL comment
        cmp.b   #$0a,d6
        beq.s   setcom          ; use null comment
getcom: moveq   #$0a,d7         ; Scan to the bitter EOL for this one.
        bsr.s   getcmd          ; get comment in a4
        cmp.l   #80,d0          ; we allow a zero length string here.
        bls.s   setcom
        lea     toolong(pc),a0
        bra.s   error
setcom: move.l  a0,d2           ; ptr to comment in a4
        ;
        ; Ok, we have the parameters, now let's set the comment.
        ;
        jsr     _LVOSetComment(a6)
        tst.l   d0
        bne.s   okokokok
        lea     nocando(pc),a0
        bra.s   error
okokokok:
        moveq.l #0,d4
        bra.s   exit

        ; Get command - return ptr to start of string in a0
        ; This searches for quotes, etc.
        ; Enter with d7.b = default terminator character for command
        ;
        ; Returns a2 pointing to next unread character
        ; a0 points to start of arg
        ; d0 contains character count of arg
        ; d6 contains last character read
getcmd: 
        moveq.l #0,d0           ; zero command line count
skipbl:
        move.b  (a2)+,d6
        cmp.b   #' ',d6         ; skip leading blanks
        beq.s   skipbl
        cmp.b   #$0a,d6         ; empty string?
        bne.s   chkquote        ; null string
        lea.l   null(pc),a0     ; return ptr to null
        rts
chkquote:
        move.l  a2,a0           ; setup return pointer
        cmp.b   #'"',d6         ; check for quoted string
        bne.s   notquoted
        move.b  d6,d7           ; change default terminator
        bra.s   scan
notquoted:
        subq.l  #1,a0
scan:
        addq    #1,d0
        move.b  (a2)+,d6
        cmp.b   #$0a,d6         ; exit on LF
        beq.s   zapch
        cmp.b   d6,d7
        bne.s   scan
zapch:  clr.b   -1(a2)          ; null terminate
        rts
        
;
; Error Routines - enter with a0 pointing to error message.
;
error:
        moveq.l #0,d3           ; clear upper bytes
        move.l  d3,d4
        move.b  (a0)+,d4        ; get return code
        move.b  (a0)+,d3        ; get length
        move.l  a0,d2           ; address
        jsr     _LVOOutput(a6)
        move.l  d0,d1
        jsr     _LVOWrite(a6)
exit:   move.l  a6,a1
        move.l  _AbsExecBase,a6
        jsr     _LVOCloseLibrary(a6)
        move.l  d4,d0
        rts

DOSName:        dc.b    "dos.library"
null:           dc.b    0
        ;
        ;
        ; Format of error numbers here is
        ; Return code, length, string.
        ;
        ;
usage:          dc.b    0,37,"Usage: Filenote <filename> <comment>",$0a
toolong:        dc.b    20,18,"Comment too long.",$0a
nocando:        dc.b    20,20,"Can't set filenote.",$0a
