collapseos/apps/ed/main.asm

113 lines
3.1 KiB
NASM
Raw Normal View History

2019-07-13 23:57:37 +10:00
; ed - line editor
;
; A text editor modeled after UNIX's ed, but simpler. The goal is to stay tight
; on resources and to avoid having to implement screen management code (that is,
; develop the machinery to have ncurses-like apps in Collapse OS).
;
; ed has a mechanism to avoid having to move a lot of memory around at each
; edit. Each line is an element in an doubly-linked list and each element point
; to an offset in the "scratchpad". The scratchpad starts with the file
; contents and every time we change or add a line, that line goes to the end of
; the scratch pad and linked lists are reorganized whenever lines are changed.
; Contents itself is always appended to the scratchpad.
;
; That's on a resourceful UNIX system.
;
; That doubly linked list on the z80 would use 7 bytes per line (prev, next,
; offset, len), which is a bit much. Moreover, there's that whole "scratchpad
; being loaded in memory" thing that's a bit iffy. We sacrifice speed for
; memory usage.
;
; So here's what we do. First, we have two scratchpads. The first one is the
2019-07-14 01:29:06 +10:00
; file being read itself. The second one is memory, for modifications we
2019-07-13 23:57:37 +10:00
; make to the file. When reading the file, we note the offset at which it ends.
; All offsets under this limit refer to the first scratchpad. Other offsets
; refer to the second.
;
2019-07-14 01:29:06 +10:00
; Then, our line list is just an array of 16-bit offsets. This means that we
2019-07-13 23:57:37 +10:00
; don't have an easy access to line length and we have to move a lot of memory
; around whenever we add or delete lines. Hopefully, "LDIR" will be our friend
; here...
;
2019-07-14 01:29:06 +10:00
; *** Usage ***
;
; ed takes no argument. It reads from the currently selected blkdev and writes
; to it. It repeatedly presents a prompt, waits for a command, execute the
; command. 'q' to quit.
;
; Enter a number to print this line's number. For ed, we break with Collapse
; OS's tradition of using hex representation. It would be needlessly confusing
; when combined with commands (p, c, d, a, i). All numbers in ed are
; represented in decimals.
;
2019-07-14 04:01:20 +10:00
; Like in ed, line indexing is one-based. This is only in the interface,
; however. In the code, line indexes are zero-based.
;
2019-07-13 23:57:37 +10:00
; *** Requirements ***
2019-07-14 01:29:06 +10:00
; BLOCKDEV_SIZE
2019-07-14 04:01:20 +10:00
; addHL
2019-07-14 01:29:06 +10:00
; blkGetC
; blkSeek
2019-07-14 04:01:20 +10:00
; blkTell
; cpHLDE
; intoHL
2019-07-13 23:57:37 +10:00
; printstr
; printcrlf
; stdioGetLine
2019-07-14 04:01:20 +10:00
; stdioPutC
; stdioReadC
; unsetZ
2019-07-13 23:57:37 +10:00
edMain:
2019-07-14 04:01:20 +10:00
; Fill line buffer
.loop:
call blkTell ; --> HL
call blkGetC
jr nz, edLoop
call bufAddLine
2019-07-14 01:29:06 +10:00
call ioGetLine
2019-07-14 04:01:20 +10:00
jr .loop
2019-07-14 01:29:06 +10:00
; Continue to loop
2019-07-13 23:57:37 +10:00
edLoop:
2019-07-14 04:01:20 +10:00
ld a, ':'
call stdioPutC
2019-07-13 23:57:37 +10:00
.inner:
call stdioReadC
jr nz, .inner ; not done? loop
; We're done. Process line.
call printcrlf
call stdioGetLine
2019-07-14 01:29:06 +10:00
call .processLine
2019-07-13 23:57:37 +10:00
ret z
jr edLoop
; Sets Z if we need to quit
2019-07-14 01:29:06 +10:00
.processLine:
2019-07-13 23:57:37 +10:00
ld a, (hl)
cp 'q'
ret z
call parseDecimal
jr z, .processNumber
2019-07-14 04:01:20 +10:00
jr .error
.processNumber:
; number is in IX
; Because we don't have a line buffer yet, let's simply print seek
; offsets.
push ix \ pop hl
2019-07-14 04:01:20 +10:00
dec hl ; from 1-based to zero-based
call bufGetLine
jr nz, .error
call printstr
call printcrlf
; continue to end
.processEnd:
2019-07-13 23:57:37 +10:00
call printcrlf
jp unsetZ
2019-07-14 04:01:20 +10:00
.error:
ld a, '?'
call stdioPutC
call printcrlf
jp unsetZ