ed: check addr bounds

This commit is contained in:
Virgil Dupras 2019-07-14 09:04:51 -04:00
parent c811d5330c
commit 50d0dc982c
2 changed files with 31 additions and 27 deletions

View File

@ -153,17 +153,3 @@ cmdParse:
pop bc pop bc
ret ret
; Make (IX) point to addr 1
cmdAddr1:
ld ix, CMD_ADDR1
ret
; Make (IX) point to addr 2
cmdAddr2:
ld ix, CMD_ADDR2
ret
; Set A to CMD_TYPE
cmdType:
ld a, (CMD_TYPE)
ret

View File

@ -88,22 +88,17 @@ edMain:
call stdioGetLine call stdioGetLine
call cmdParse call cmdParse
jr nz, .error jr nz, .error
call cmdType ld a, (CMD_TYPE)
cp 'q' cp 'q'
ret z jr z, .doQuit
jr .doPrint jr .doPrint
.doQuit:
xor a
ret
.doPrint: .doPrint:
call cmdAddr1 call edReadAddrs
call edResolveAddr jr nz, .error
ex de, hl ; DE: addr1
call cmdAddr2
call edResolveAddr
ld (ED_CURLINE), hl
ex de, hl ; HL: addr1, DE: addr2
call cpHLDE
jr z, .doPrintLoop ; DE == HL, ok
jr nc, .error ; DE < HL, not good
.doPrintLoop: .doPrintLoop:
push hl push hl
call bufGetLine call bufGetLine
@ -112,9 +107,12 @@ edMain:
call printcrlf call printcrlf
pop hl pop hl
call cpHLDE call cpHLDE
jr nc, .mainLoop jr z, .doPrintEnd
inc hl inc hl
jr .doPrintLoop jr .doPrintLoop
.doPrintEnd:
ld (ED_CURLINE), hl
jr .mainLoop
.error: .error:
ld a, '?' ld a, '?'
call stdioPutC call stdioPutC
@ -140,3 +138,23 @@ edResolveAddr:
pop de pop de
ret ret
; Read absolute addr1 in HL and addr2 in DE. Also, check bounds and set Z if
; both addresses are within bounds, unset if not.
edReadAddrs:
ld ix, CMD_ADDR2
call edResolveAddr
ex de, hl
ld hl, (BUF_LINECNT)
ex de, hl ; HL: addr2 DE: cnt
call cpHLDE
jp nc, unsetZ ; HL (addr2) >= DE (cnt). no good
ex de, hl ; DE: addr2
ld ix, CMD_ADDR1
call edResolveAddr
ex de, hl ; HL: addr2, DE: addr1
call cpHLDE
jp c, unsetZ ; HL (addr2) < DE (addr1). no good
ex de, hl ; HL: addr1, DE: addr2
cp a ; ensure Z
ret