commit dd2c554111461575aaa24c8c17538d9cdb7c37d1
parent 5c062d154cbed8c0623679d58a020cce270fda51
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Tue, 19 Jul 2022 17:13:12 -0400
More I/O restructuring
Diffstat:
4 files changed, 8 insertions(+), 17 deletions(-)
diff --git a/fs/doc/file.txt b/fs/doc/file.txt
@@ -34,13 +34,3 @@ fwritebuf ( a n fcursor -- written-n )
proceed as is best for its implementation, as long as it writes at least 1
byte.
-## File API
-
-On top of that protocol above, the File subsystem implements those words:
-
-fgetc ( fcursor -- c )
- Read a single character from fcursor. If the end of file (EOF) is reached,
- returns -1.
-
-fputc ( c fcursor -- )
- Write a single character to fcursor, growing the file if needed.
diff --git a/fs/lib/io.fs b/fs/lib/io.fs
@@ -1,10 +1,12 @@
-\ I/O aliases
+\ Input/Output. See doc/io
-\ Defines stdin (and soon stdout) which is used by many programs and words as
-\ their main input output. In addition to those words, this subsystem also
-\ implements some convenience words to manage where they point to.
?f<< /lib/str.fs
+: readbuf ( n hdl -- a? n ) dup @ execute ;
+: writebuf ( a n hdl -- n ) dup 4 + @ execute ;
+: flush ( hdl -- ) dup 8 + @ execute ;
+: getc ( fcursor -- c ) 1 swap readbuf if c@ else -1 ( EOF ) then ;
+
alias in< stdin ( -- c )
create _buf( STR_MAXSZ allot
diff --git a/fs/sys/file.fs b/fs/sys/file.fs
@@ -1,18 +1,18 @@
\ File subsystem, see doc/file
?f<< /lib/scratch.fs
?f<< /lib/with.fs
+?f<< /lib/io.fs
\ We need a private scratchpad here because some cursors can be quite
\ long-lived. If we use the system scratchpad, short-lived data will overwrite
\ our cursors.
$200 scratchpad$ filespad
-: fgetc ( fcursor -- c ) 1 swap freadbuf if c@ else -1 ( EOF ) then ;
\ 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 )
filespad to' Scratchpad with[
- scratch[ litn compile fgetc exit, ]scratch ]with ;
+ scratch[ litn compile getc exit, ]scratch ]with ;
: .floaded floaded begin dup while dup 4 + stype nl> @ repeat drop ;
: require word dup floaded? not if stype abort" required" else drop then ;
diff --git a/fs/xcomp/glue2.fs b/fs/xcomp/glue2.fs
@@ -1,5 +1,4 @@
bootfile xcomp/glue2.fs
\ Glue code that goes between the filesystem part and boothi
alias fatopenlo fopen
-alias fatreadbuf freadbuf
alias fatclose fclose