diff --git a/ueforth/esp32/template.ino b/ueforth/esp32/template.ino index 6e7c50d..074444b 100644 --- a/ueforth/esp32/template.ino +++ b/ueforth/esp32/template.ino @@ -174,7 +174,9 @@ n0 = (cell_t) lseek(fd, tos, SEEK_SET); n0 = n0 < 0 ? errno : 0) \ X("RESIZE-FILE", RESIZE_FILE, cell_t fd = n0; DROP; n0 = ResizeFile(fd, tos)) \ X("FILE-SIZE", FILE_SIZE, struct stat st; w = fstat(n0, &st); \ - n0 = (cell_t) st.st_size; PUSH w < 0 ? errno : 0) + n0 = (cell_t) st.st_size; PUSH w < 0 ? errno : 0) \ + X("NON-BLOCK", NON_BLOCK, n0 = fcntl(n0, F_SETFL, O_NONBLOCK); \ + n0 = n0 < 0 ? errno : 0) #ifndef ENABLE_LEDC_SUPPORT # define OPTIONAL_LEDC_SUPPORT diff --git a/ueforth/posix/httpd.fs b/ueforth/posix/httpd.fs index fad3359..a2335ae 100644 --- a/ueforth/posix/httpd.fs +++ b/ueforth/posix/httpd.fs @@ -30,16 +30,20 @@ sockaddr httpd-port sockaddr client variable client-len : handleClient clientfd close-file drop + -1 to clientfd sockfd client client-len sockaccept - dup 0< if drop exit then to clientfd + dup 0< if drop 0 exit then + to clientfd chunk chunk-size 0 fill chunk chunk-size clientfd read-file throw to chunk-filled + -1 ; : server ( port -- ) httpd-port ->port! ." Listening on port " httpd-port ->port@ . cr AF_INET SOCK_STREAM 0 socket to sockfd ( sockfd SOL_SOCKET SO_REUSEADDR 1 >r rp@ 4 setsockopt rdrop throw ) + sockfd non-block throw sockfd httpd-port sizeof(sockaddr_in) bind throw sockfd max-connections listen throw ; diff --git a/ueforth/posix/posix.fs b/ueforth/posix/posix.fs index 706a703..5f08721 100644 --- a/ueforth/posix/posix.fs +++ b/ueforth/posix/posix.fs @@ -148,6 +148,11 @@ z" clock_gettime" 2 sysfunc clock_gettime 8 constant CLOCK_REALTIME_ALARM 9 constant CLOCK_BOOTTIME_ALARM +( File control ) +z" fcntl" 3 sysfunc fcntl +4 constant F_SETFL +2048 constant FNDELAY ( 04000 ) + forth definitions posix ( Generic Files ) @@ -172,6 +177,9 @@ O_RDWR constant r/w dup 0 SEEK_END lseek r> swap >r SEEK_SET lseek drop r> d0port@ ( a -- n ) 2 + >r r@ c@ 256 * r> 1+ c@ + ; : ->port! ( n a -- ) 2 + >r dup 256 / r@ c! r> 1+ c! ; +( Fixup return ) +: sockaccept sockaccept sign-extend ; + forth definitions diff --git a/ueforth/posix/telnetd.fs b/ueforth/posix/telnetd.fs index e69cf00..b68b25c 100644 --- a/ueforth/posix/telnetd.fs +++ b/ueforth/posix/telnetd.fs @@ -32,13 +32,20 @@ defer broker ['] telnet-key is key ['] telnet-type is type quit ; +: wait-for-connection + begin + sockfd client client-len sockaccept + dup 0 >= if exit else drop then + again +; + : broker-connection rp0 rp! sp0 sp! begin ['] default-key is key ['] default-type is type -1 echo ! ." Listening on port " telnet-port ->port@ . cr - sockfd client client-len sockaccept + wait-for-connection ." Connected: " dup . cr connection again ; ' broker-connection is broker @@ -46,6 +53,7 @@ defer broker : server ( port -- ) telnet-port ->port! AF_INET SOCK_STREAM 0 socket to sockfd + sockfd non-block throw sockfd telnet-port sizeof(sockaddr_in) bind throw sockfd 1 listen throw broker ; diff --git a/ueforth/posix/termios.fs b/ueforth/posix/termios.fs index 5376a77..f4e42d7 100644 --- a/ueforth/posix/termios.fs +++ b/ueforth/posix/termios.fs @@ -18,12 +18,9 @@ vocabulary termios termios definitions also posix z" tcgetattr" 2 sysfunc tcgetattr z" tcsetattr" 3 sysfunc tcsetattr -z" fcntl" 3 sysfunc fcntl z" ioctl" 3 sysfunc ioctl ( Blocking ) -4 constant F_SETFL -2048 constant FNDELAY : nodelay-mode stdin F_SETFL FNDELAY fcntl throw ; : delay-mode stdin F_SETFL 0 fcntl throw ; diff --git a/ueforth/posix/web_interface.fs b/ueforth/posix/web_interface.fs index b5f4574..97cf064 100644 --- a/ueforth/posix/web_interface.fs +++ b/ueforth/posix/web_interface.fs @@ -47,11 +47,11 @@ body {

ESP32forth v7

Upload File:
- - - - - + + + + +

@@ -73,8 +73,9 @@ function httpPost(url, data, callback) { r.open('POST', url); r.send(data); } +setInterval(function() { ask(''); }, 300); function ask(cmd, callback) { - httpPost('/input', cmd + '\n', function(data) { + httpPost('/input', cmd, function(data) { if (data !== null) { output.value += data; } output.scrollTop = output.scrollHeight; // Scroll to the bottom if (callback !== undefined) { callback(); } @@ -83,7 +84,7 @@ function ask(cmd, callback) { prompt.onkeyup = function(event) { if (event.keyCode === 13) { event.preventDefault(); - ask(prompt.value); + ask(prompt.value + '\n'); prompt.value = ''; } }; @@ -102,7 +103,7 @@ filepick.onchange = function(event) { } }; window.onload = function() { - ask(''); + ask('\n'); prompt.focus(); }; @@ -130,10 +131,11 @@ create out-string out-size 1+ allot align : serve-key ( -- n ) input-stream stream>ch ; : handle1 - handleClient - s" /" path str= if handle-index exit then - s" /input" path str= if handle-input exit then - notfound-response + handleClient if + s" /" path str= if handle-index exit then + s" /input" path str= if handle-input exit then + notfound-response + then ; : do-serve begin handle1 pause again ; @@ -141,8 +143,8 @@ create out-string out-size 1+ allot align : server ( port -- ) server - ['] serve-type is type ['] serve-key is key + ['] serve-type is type webserver-task start-task ;