; 29.8.00
;
;       @=$100                  ; normal com programm
        @=$200                  ; batch binary

        move.w  #$82,r5         ; start after space
        movu.bw  -2.b(r5.w),r6  ; cmdline length
        dec.w   r6              ; char after space

_10:    dec.w   r6              ; char left?
        bmi.b   _100            ; no
        move.b  (r5.w),r0       ; next char from cmdline
        inc.w   r5
        cmp.b   #' ',r0         ; space ?
        beq.b   _10             ; yes, ignore
        cmp.b   #'e',r0         ; echo command?
        beq.b   echo            ; yes
        cmp.b   #'+',r0         ; incr command
        beq.b   inc             ; yes
_100:   rts.w                   ; no other commands

inc:    move.w  r6,r2           ; length of number
        dec.w   r6              ; at least 1
        bmi.b   _100            ; no
        move.w  r5,r1           ; adr of first digit
        add.w   r6,r5           ; adr of last digit
_20:    inc.b   (r5.w)          ; inc last digit
        cmp.b   #'9',(r5.w)     ; now > 9?
        bls.b   _10             ; no: ok
        move.b  #'0',(r5.w)     ; set 0
        dec.w   r5              ; go to preceding digit
        dec.w   r6              ; digits left?
        bpl.b   _20             ; yes
_10:    bsr.w   out1            ; all done
_100:   rts.w
        
        
echo:   move.w  #buf,r1         ; write buffer
        move.w  #1,r2           ; write 1 char
_00:    dec.w   r6              ; chars left?
        bmi.b   _100            ; no
        move.b  (r5.w),r0       ; next char from cmdline
        inc.w   r5
        cmp.b   #'l',r0         ; lower case subcommand
        bne.b   _10             ; no
        inc.b   _200            ; yes: set flag >0
_10:    cmp.b   #'u',r0         ; upper case subcommand
        bne.b   _20             ; no
        dec.b   _200            ; yes: set flag <0
_20:    cmp.b   #'_',r0         ; space subcommand?
        bne.b   _21             ; no
        inc.b   _201            ; yes: set flag >0
_21:    cmp.b   #'.',r0         ; skip after . subcommand
        bne.b   _30             ; no
        inc.b   _202            ; remember number of . to skip
_30:    cmp.b   #':',r0         ; skip before . subcommand
        bne.b   _40             ; no
        dec.b   _202            ; remember number of . to skip
_40:    cmp.b   #' ',r0         ; space
        bne.b   _00             ; no: read more subcommands

_50:    dec.w   r6              ; chars left?
        bmi.b   _100            ; no
        move.b  (r5.w),r0       ; get next char
        inc.w   r5
        cmp.b   #'$',r0         ; start of hex value or date command
        bne.b   _60             ; no
        cmp.b   #'t',(r5.w)     ; date command?
        bne.b   _51             ; no
        dec.w   r6              ; number of chars left
        inc.w   r5              ; adr of next char
        move.b  #$2a,m0         ; get date function
        trap    #$21            ; call dos
        move.w  r2,r0           ; year
        divu.b  c100,m0|r0      ; last 2 digits -> m0
        lsr.w   #8,r0           ; last 2 digits -> r0
        move.w  #buf,r3         ; adr of write buffer
        bsr.w   _500            ; output in write buffer
        move.b  m1,r0           ; month
        bsr.w   _500            ; output in write buffer
        move.b  r1,r0           ; day
        bsr.w   _500            ; output in write buffer
        move.w  #buf,r1         ; adr of write buffer
        move.w  #6,r2           ; write 6 char: yymmdd
        bsr.w   out1            ; write it
        move.w  #1,r2           ; restore 1
        br.b    _50             ; process next char


_51:    bsr.w   hex2bin         ; convert hex to bin
        bsr.w   hex2bin         ; convert hex to bin
_60:    bsr.w   out             ; write it
        br.b    _50             ; process next char

_100:   bsr.w   in              ; read char from stdin
        bcs.b   _199            ; if error
        or.w    r0,r0           ; 1 char read
        beq.b   _199            ; no
        move.b  buf,r0          ; read char -> r0
        cmp.b   #10,r0          ; LF
        beq.b   _100            ; yes: ignore
        cmp.b   #13,r0          ; CR
        beq.b   _198            ; read LF and exit
        tst.b   #$ff,_200       ; lower/upper case flag
        beq.b   _103            ; not set
        bpl.b   _104            ; convert to lower
        cmp.b   #'a',r0         ; >= a ?
        blo.b   _103            ; no: nothing to convert
        cmp.b   #'z',r0         ; <= z ?
        bhi.b   _103            ; no: nothing to convert
        br.b    _105            ; convert
_104:   cmp.b   #'A',r0         ; >= A ?
        blo.b   _103            ; no: nothing to convert
        cmp.b   #'Z',r0         ; <= Z ?
        bhi.b   _103            ; no: nothing to convert
_105:   eor.b   #$20,r0         ; convert
_103:   tst.b   #$ff,_201       ; space flag
        beq.b   _106            ; not set
        cmp.b   #' ',r0         ; space ?
        bne.b   _106            ; no: nothing to do
        move.b  #'_',r0         ; replace by _
_106:   tst.b   #$ff,_202       ; skip after/before . flag
        beq.b   _110            ; not set
        bpl.b   _111            ; skip after .
        cmp.b   #'.',r0         ; is this char a . ?
        bne.b   _100            ; no: nothing to do
        inc.b   _202            ; yes: decr number of . to skip
        br.b    _100            ; next char
_111:   cmp.b   #'.',r0         ; is this char a . ?
        bne.b   _110            ; no: write char
        dec.b   _202            ; yes: dec number of . to output
        bne.b   _110            ; if not 0: write .
        move.b  #$80,_202       ; will skip the next 128 . 
        br.b    _100            ; next char

_110:   bsr.w   out             ; write char to stdout
        br.b    _100            ; next char

_198:   bsr.w   in              ; read LF
_199:   rts.w                   ; exit

_200:   dc.b    0               ; upper/lower flag
_201:   dc.b    0               ; space flag
_202:   dc.b    0               ; . flag

_500:   divu.b  c10,m0|r0       ; r0=high m0=low digit
        add.w   #'00',r0        ; convert to ascii
        move.w  r0,(r3.w)       ; copy in write buffer
        inc.w   r3              ; inc write pointer
        inc.w   r3              ; inc write pointer
        eor.w   r0,r0           ; clear r0
        rts.w

c100:   dc.b    100             ; constant 100
c10:    dc.b    10              ; constant  10

in:     move.b  #$3f,m0         ; read function
        eor.w   r3,r3           ; stdin
        trap    #$21            ; call dos
        rts.w

out:    move.b  r0,buf          ; copy char in write buffer
out1:   move.b  #$40,m0         ; write function
        move.w  #1,r3           ; stdout
        trap    #$21            ; call dos
        rts.w

hex2bin:dec.w   r6              ; decr. number of chars left
        move.b  (r5.w),m0       ; get next char
        inc.w   r5              ; incr pointer to next char
        or.b    #$20,m0         ; convert to lower case (A-F)
        sub.b   #'a'-10,m0      ; convert ascii to bin
        bpl.b   _10             ; all done if a-f
        add.b   #'a'-10-'0',m0  ; correct if 0-9
_10:    lsl.b   #4,r0           ; shift last nibble
        add.b   m0,r0           ; add new nibble
        rts.w
buf:                            ; write buffer