Improving I/O adding terminal.

This commit is contained in:
Brad Nelson
2021-01-29 17:59:42 -08:00
parent 74ae2b8e18
commit c58b10fe0b
11 changed files with 65 additions and 21 deletions

View File

@ -115,21 +115,21 @@ core_test: $(POSIX)/ueforth common/core_test.fs \
$(GEN):
mkdir -p $@
POSIX_BOOT = common/boot.fs common/terminal.fs \
POSIX_BOOT = common/boot.fs common/ansi.fs \
posix/posix.fs posix/posix_highlevel.fs \
common/filetools.fs posix/posix_desktop.fs \
common/highlevel.fs common/filetools.fs posix/posix_desktop.fs \
common/tasks.fs common/streams.fs common/blocks.fs
$(GEN)/posix_boot.h: common/source_to_string.js $(POSIX_BOOT) | $(GEN)
echo "ok" | cat $(POSIX_BOOT) - | $< boot >$@
WINDOWS_BOOT = common/boot.fs common/terminal.fs \
windows/windows.fs \
WINDOWS_BOOT = common/boot.fs common/ansi.fs \
windows/windows.fs windows/windows_highlevel.fs common/highlevel.fs \
common/tasks.fs common/streams.fs common/blocks.fs
$(GEN)/windows_boot.h: common/source_to_string.js $(WINDOWS_BOOT) | $(GEN)
echo "ok" | cat $(WINDOWS_BOOT) - | $< boot >$@
ARDUINO_BOOT = common/boot.fs arduino/arduino.fs \
posix/posix_highlevel.fs common/filetools.fs \
posix/posix_highlevel.fs common/highlevel.fs common/filetools.fs \
common/tasks.fs common/streams.fs arduino/arduino_server.fs \
arduino/esp_camera.fs common/blocks.fs \
arduino/autoboot.fs

View File

@ -1,4 +1,4 @@
( Terminal handling )
( ANSI Codes )
: n. ( n -- ) base @ swap decimal <# #s #> type base ! ;
: esc 27 emit ; : bel 7 emit ;
: at-xy ( x y -- ) esc ." [" 1+ n. ." ;" 1+ n. ." H" ;

View File

@ -205,8 +205,15 @@ variable echo
: ?echo ( n -- ) echo @ if emit else drop then ;
: ?echo-prompt echo @ if ." --> " then ;
: accept ( a n -- n ) ?echo-prompt 0 swap begin 2dup < while
key dup ?echo dup nl = if 2drop nip exit then
>r rot r> over c! 1+ -rot swap 1+ swap repeat drop nip ;
key
dup nl = if ?echo drop nip exit then
dup 8 = over 127 = or if
drop over if rot 1- rot 1- rot 8 ?echo bl ?echo 8 ?echo then
else
dup ?echo
>r rot r> over c! 1+ -rot swap 1+ swap
then
repeat drop nip ;
200 constant input-limit
: tib ( -- a ) 'tib @ ;
create input-buffer input-limit allot

View File

@ -0,0 +1,11 @@
( Including Files )
: included ( a n -- )
r/o open-file dup if nip throw else drop then
dup file-size throw
dup allocate throw
swap 2dup >r >r
rot dup >r read-file throw drop
r> close-file throw
r> r> over >r evaluate
r> free throw ;
: include ( "name" -- ) bl parse included ;

View File

@ -66,6 +66,7 @@ octal
100 constant O_CREAT
200 constant O_TRUNC
2000 constant O_APPEND
4000 constant O_NONBLOCK
decimal
( Hookup I/O )
@ -103,3 +104,6 @@ octal 777 constant 0777 decimal
( Other Utils )
: ms ( n -- ) 1000 * usleep drop ;
( errno.h )
11 constant EAGAIN

View File

@ -3,14 +3,3 @@
: free ( a -- ior ) sysfree drop 0 ;
: resize ( a n -- a ior ) realloc dup 0= ;
( Including Files )
: included ( a n -- )
r/o open-file dup if nip throw else drop then
dup file-size throw
dup allocate throw
swap 2dup >r >r
rot dup >r read-file throw drop
r> close-file throw
r> r> over >r evaluate
r> free throw ;
: include ( "name" -- ) bl parse included ;

10
ueforth/posix/terminal.fs Normal file
View File

@ -0,0 +1,10 @@
include posix/termios.fs
s" /dev/ttyS3" r/w O_NONBLOCK or open-file throw constant remote
: remote-type ( a n -- ) remote write-file throw ;
: remote-emit ( ch -- ) >r rp@ 1 remote-type rdrop ;
: remote-key ( -- ch|0 )
0 >r rp@ 1 remote read-file dup EAGAIN = if rdrop 2drop 0 exit then
throw if r> else rdrop 0 then ;
: terminal nodelay-mode begin key dup if remote-emit else drop then
remote-key dup if emit else drop then again ;

View File

@ -1,4 +1,4 @@
<!DOCTYPE html>
<script src="ueforth.js"></script>
<script type="forth">
needs terminal.fs
needs ansi.fs

View File

@ -0,0 +1,7 @@
s" \\.\COM3" r/w open-file throw constant remote
: remote-type ( a n -- ) remote write-file throw ;
: remote-emit ( ch -- ) >r rp@ 1 remote-type rdrop ;
: remote-key ( -- ch|0 ) 0 >r rp@ 1 remote read-file throw 0= throw r> ;
: remote-key? remote 0 WaitForSingleObject 0= ;
: terminal begin remote-key? if remote-key emit then
key? if key remote-emit then again ;

View File

@ -46,7 +46,9 @@ z" ExitProcess" 1 Kernel32 ExitProcess
z" GetStdHandle" 1 Kernel32 GetStdHandle
z" GetConsoleMode" 2 Kernel32 GetConsoleMode
z" SetConsoleMode" 2 Kernel32 SetConsoleMode
z" FlushConsoleInputBuffer" 1 Kernel32 FlushConsoleInputBuffer
z" Sleep" 1 Kernel32 Sleep
z" WaitForSingleObject" 2 Kernel32 WaitForSingleObject
z" GetLastError" 0 Kernel32 GetLastError
z" CreateFileA" 7 Kernel32 CreateFileA
@ -65,16 +67,21 @@ STD_INPUT_HANDLE GetStdHandle constant stdin
STD_OUTPUT_HANDLE GetStdHandle constant stdout
STD_ERROR_HANDLE GetStdHandle constant stderr
variable console-mode
stdin console-mode GetConsoleMode drop
stdin console-mode @ ENABLE_LINE_INPUT ENABLE_MOUSE_INPUT or
ENABLE_WINDOW_INPUT or invert and SetConsoleMode drop
stdout console-mode GetConsoleMode drop
stdout console-mode @ ENABLE_VIRTUAL_TERMINAL_PROCESSING or SetConsoleMode drop
: win-type ( a n -- ) stdout -rot NULL NULL WriteFile drop ;
' win-type is type
: raw-key ( -- n ) 0 >r stdin rp@ 1 NULL NULL ReadFile drop r> ;
: win-key ( -- n ) begin raw-key dup 13 = while drop repeat ;
: key? ( -- f ) stdin 0 WaitForSingleObject 0= ;
: win-key ( -- n ) raw-key dup 13 = if drop nl then ;
' win-key is key
: win-bye ( -- ) 0 ExitProcess drop ;
' win-bye is bye
-1 echo !
: set-title ( a n -- ) esc ." ]0;" type bel ;
s" uEforth" set-title

View File

@ -0,0 +1,9 @@
( Words with OS assist )
z" GetProcessHeap" 0 Kernel32 GetProcessHeap
z" HeapAlloc" 3 Kernel32 HeapAlloc
z" HeapFree" 3 Kernel32 HeapFree
z" HeapReAlloc" 4 Kernel32 HeapReAlloc
GetProcessHeap constant process-heap
: allocate ( n -- a ior ) process-heap 0 rot HeapAlloc dup 0= ;
: free ( a -- ior ) process-heap 0 rot HeapFree drop 0 ;
: resize ( a n -- a ior ) process-heap -rot 0 -rot HeapReAlloc dup 0= ;