~vdupras/duskos

6e30417c3d2292e337439137e1cb4577ba95c78e — Virgil Dupras a month ago 92deb8b
Isolate zstring handling to fs/linux

The rest of the system can now safely assume "fstrings" everywhere.
4 files changed, 9 insertions(+), 13 deletions(-)

M fs/fs/linux.fs
M fs/lib/file.fs
M fs/sys/scratch.fs
M fs/xcomp/boothi.fs
M fs/fs/linux.fs => fs/fs/linux.fs +7 -2
@@ 2,9 2,14 @@
\ Requires a "lnxcall" word in the kernel

: fclose ( fd -- ) 6 ( close ) swap 0 0 ( close fd 0 0 ) lnxcall drop ;

create _buf $100 allot
: _tozstr ( s -- zs )
  c@+ >r _buf r@ move 0 _buf r> + c! _buf ;

create _ 'C' c, 'a' c, 'n' c, ''' c, 't' c, $20 c, 'o' c, 'p' c, 'e' c, 'n' c,
: zfopen ( zfname -- fd )
  5 ( open ) swap 0 0 ( open cstr noflag O_RDONLY ) lnxcall
: fopen ( fname -- fd )
  _tozstr 5 ( open ) swap 0 0 ( open cstr noflag O_RDONLY ) lnxcall
  dup 0< if _ 10 rtype abort then ;
create _ 1 allot
: fread ( fd -- c-or-0 ) 3 ( read ) swap _ 1 lnxcall 1 = if _ c@ else 0 then ;

M fs/lib/file.fs => fs/lib/file.fs +0 -5
@@ 1,16 1,11 @@
\ File I/O
\ requires sys/scratch
: fopen ( fname -- fd ) str>zstr zfopen ;

\ This creates a "f<" reader with the file descriptor embedded in it. This
\ allows for a straightforward override of input/output words.
: [f<] ( curfd -- word )
  scratch[ litn compile fread exit, ]scratch ;

\ Autoloading
\ entries in the floaded list have both a length byte and
\ a nul termination byte. the nul termination is used only
\ by the linux syscall, and may be removed in the future.

: floaded? ( str -- f )
  floaded begin dup while 2dup 4 +

M fs/sys/scratch.fs => fs/sys/scratch.fs +0 -4
@@ 16,13 16,9 @@ scratch( value scratch>
: scratchallot ( n -- a )
  scratch> over + scratch) >= if scratch( to scratch> then
  scratch> swap to+ scratch> ( a ) ;
: scratchallot0 ( n -- a ) dup scratchallot dup >r swap 0 fill r> ;
\ push a range to the scratchpad as a string
: []>str ( a u -- str )
  dup 1+ scratchallot ( src u dst-1 ) >r dup r@ c!+ swap ( src dst u ) move r> ;
\ transforms str into a null-terminated string
: str>zstr ( str -- zstr )
  c@+ dup 1+ scratchallot0 ( str+1 u a ) dup >r swap move r> ;

\ Open a scratch area for writing
: scratch[ ( -- ) here to _here scratch> to here ;

M fs/xcomp/boothi.fs => fs/xcomp/boothi.fs +2 -2
@@ 4,9 4,9 @@
: f< ( -- c ) curfd fread fecho if dup emit then ;
: fload ( fname -- )
  floaded here to floaded , ( fname )
  dup c@ 1+ move, 0 c, ( )
  dup c@ 1+ move, ( )
  curfd >r
  floaded 5 + zfopen to curfd
  floaded 4 + fopen to curfd
  to' in< @ >r ['] f< to in<
  begin maybeword ?dup if runword 0 else 1 then until
  r> to in< curfd fclose r> to curfd ;