commit 6e30417c3d2292e337439137e1cb4577ba95c78e
parent 92deb8bb98e88adf900387837561c18684d5dd53
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Fri, 1 Jul 2022 14:59:40 -0400
Isolate zstring handling to fs/linux
The rest of the system can now safely assume "fstrings" everywhere.
Diffstat:
4 files changed, 9 insertions(+), 13 deletions(-)
diff --git a/fs/fs/linux.fs b/fs/fs/linux.fs
@@ -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 ;
diff --git a/fs/lib/file.fs b/fs/lib/file.fs
@@ -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 +
diff --git a/fs/sys/scratch.fs b/fs/sys/scratch.fs
@@ -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 ;
diff --git a/fs/xcomp/boothi.fs b/fs/xcomp/boothi.fs
@@ -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 ;