Add mkdir, cd, and more, refactor.

This commit is contained in:
Brad Nelson
2022-09-05 21:25:46 -07:00
parent 0dbe262abf
commit 234d85bf71
6 changed files with 95 additions and 33 deletions

View File

@ -248,9 +248,9 @@ $(GEN)/windows_boot.h: tools/source_to_string.js $(WINDOWS_BOOT) | $(GEN)
$< -win boot $(VERSION) $(REVISION) $(WINDOWS_BOOT) >$@
ESP32_BOOT = $(COMMON_PHASE1) \
esp32/allocation.fs \
esp32/allocation.fs esp32/bindings.fs \
$(COMMON_PHASE2) $(COMMON_FILETOOLS) \
esp32/bindings.fs esp32/platform.fs \
esp32/platform.fs \
posix/httpd.fs posix/web_interface.fs esp32/web_interface.fs \
esp32/registers.fs esp32/timers.fs \
esp32/bterm.fs posix/telnetd.fs \

View File

@ -53,6 +53,16 @@ forth definitions internals
again
;
DEFINED? read-dir [IF]
: ls ( "path" -- )
bl parse open-dir throw { dh } begin
dh read-dir dup 0= if
2drop dh close-dir throw exit
then type cr
again
;
[THEN]
internals definitions
( Leave some room for growth of starting system. )
0 value saving-base

View File

@ -307,6 +307,17 @@ e: check-float-opcodes
out: FSQRT
;e
e: check-files-dir
out: READ-DIR
out: CLOSE-DIR
out: OPEN-DIR
;e
e: check-files-dir-reverse
out: OPEN-DIR
out: CLOSE-DIR
;e
e: check-files
out: NON-BLOCK
out: FILE-SIZE
@ -409,6 +420,12 @@ e: check-snapshots
out: remember
out: restore
out: save
;e
e: check-fileops
DEFINED? open-dir [IF]
out: ls
[THEN]
out: cat
out: touch
out: rm
@ -479,6 +496,7 @@ e: check-filetools
check-blocks
check-imports
check-snapshots
check-fileops
out: streams
out: ms
check-tasks
@ -568,8 +586,13 @@ e: test-posix-forth-namespace
out: termios
check-allocation
out: ok
out: pwd
out: rmdir
out: mkdir
out: cd
out: ms-ticks
out: ms
check-files-dir
check-files
out: default-key
out: default-type
@ -586,6 +609,10 @@ e: test-esp32-forth-voclist
internals ' ansi voclist-from
out: ansi
out: registers
out: ansi
out: editor
out: streams
out: tasks
out: oled
out: bluetooth
out: rtos
@ -601,10 +628,6 @@ e: test-esp32-forth-voclist
out: WiFi
out: Wire
out: ESP
out: ansi
out: editor
out: streams
out: tasks
out: structures
out: internalized
out: internals
@ -637,10 +660,30 @@ e: check-esp32-builtins
out: MS-TICKS
out: TERMINATE
check-files-reverse
check-files-dir-reverse
out: dacWrite
out: MDNS.begin
;e
e: check-esp32-bindings
out: oled
out: bluetooth
out: rtos
out: rmt
out: interrupts
out: sockets
out: Serial
out: ledc
out: SPIFFS
out: spi_flash
out: SD_MMC
out: SD
out: WiFi
out: Wire
out: ESP
out: read-dir
;e
e: test-esp32-forth-namespace
' forth list-from
out: FORTH
@ -657,23 +700,9 @@ e: test-esp32-forth-namespace
out: web-interface
out: httpd
check-esp32-platform
out: oled
out: bluetooth
out: rtos
out: rmt
out: interrupts
out: sockets
out: Serial
out: ledc
out: SPIFFS
out: spi_flash
out: SD_MMC
out: SD
out: WiFi
out: Wire
out: ESP
check-filetools
check-phase2
check-esp32-bindings
check-allocation
check-phase1
check-esp32-builtins

View File

@ -14,12 +14,14 @@
( Migrate various words to separate vocabularies, and constants )
forth definitions internals
: read-dir ( dh -- a n ) dup if errno else 0 then ;
forth definitions
vocabulary ESP ESP definitions
transfer ESP-builtins
only forth definitions
forth definitions
vocabulary Wire Wire definitions
transfer wire-builtins
forth definitions

View File

@ -14,6 +14,7 @@
#ifndef SIM_PRINT_ONLY
# include <dirent.h>
# include <errno.h>
# include <unistd.h>
# include <fcntl.h>
@ -128,7 +129,7 @@ static cell_t ResizeFile(cell_t fd, cell_t size);
X("RENAME-FILE", RENAME_FILE, \
cell_t len = n0; DROP; memcpy(filename, a0, len); filename[len] = 0; DROP; \
cell_t len2 = n0; DROP; memcpy(filename2, a0, len2); filename2[len2] = 0; \
n0 = rename(filename, filename2); n0 = n0 ? errno : 0) \
n0 = rename(filename2, filename); n0 = n0 ? errno : 0) \
X("WRITE-FILE", WRITE_FILE, cell_t fd = n0; DROP; cell_t len = n0; DROP; \
n0 = write(fd, a0, len); n0 = n0 != len ? errno : 0) \
X("READ-FILE", READ_FILE, cell_t fd = n0; DROP; cell_t len = n0; DROP; \
@ -141,7 +142,11 @@ static cell_t ResizeFile(cell_t fd, cell_t size);
X("FILE-SIZE", FILE_SIZE, struct stat st; w = fstat(n0, &st); \
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)
n0 = n0 < 0 ? errno : 0) \
X("OPEN-DIR", OPEN_DIR, memcpy(filename, a1, n0); filename[n0] = 0; \
n1 = (cell_t) opendir(filename); n0 = n1 ? 0 : errno) \
X("CLOSE-DIR", CLOSE_DIR, n0 = closedir((DIR *) n0); n0 = n0 ? errno : 0) \
YV(internals, READDIR, SET readdir((DIR *) n0)->d_name)
#ifndef ENABLE_LEDC_SUPPORT
# define OPTIONAL_LEDC_SUPPORT

View File

@ -58,8 +58,10 @@ z" usleep" 1 sysfunc usleep
z" signal" 2 sysfunc signal
( Directories )
z" chdir" 1 sysfunc chdir
z" mkdir" 2 sysfunc mkdir
z" rmdir" 1 sysfunc rmdir
z" getwd" 1 sysfunc getwd
z" opendir" 1 sysfunc opendir
z" closedir" 1 sysfunc closedir
z" readdir" 1 sysfunc readdir
@ -114,7 +116,10 @@ only posix definitions
' posix-bye is bye
( I/O Error Helpers )
: d0<ior ( n -- n ior ) dup 0< if errno else 0 then ;
: 0<ior ( n -- ior ) 0< if errno else 0 then ;
: 0=ior ( n -- ior ) 0= if errno else 0 then ;
: d0<ior ( n -- n ior ) dup 0<ior ;
: d0=ior ( n -- n ior ) dup 0=ior ;
( errno.h )
11 constant EAGAIN
@ -164,18 +169,18 @@ O_WRONLY constant W/O
O_RDWR constant R/W
: BIN ( fh -- fh ) ;
: CLOSE-FILE ( fh -- ior ) close sign-extend ;
: FLUSH-FILE ( fh -- ior ) fsync sign-extend ;
: CLOSE-FILE ( fh -- ior ) close 0<ior ;
: FLUSH-FILE ( fh -- ior ) fsync 0<ior ;
: OPEN-FILE ( a n fam -- fh ior ) >r s>z r> 0777 open sign-extend d0<ior ;
: CREATE-FILE ( a n fam -- fh ior )
>r s>z r> O_CREAT or 0777 open sign-extend d0<ior ;
: DELETE-FILE ( a n -- ior ) s>z unlink sign-extend ;
: RENAME-FILE ( a n a n -- ior ) s>z -rot s>z swap rename sign-extend ;
: WRITE-FILE ( a n fh -- ior ) -rot dup >r write r> = 0= ;
: DELETE-FILE ( a n -- ior ) s>z unlink 0<ior ;
: RENAME-FILE ( a n a n -- ior ) s>z -rot s>z swap rename 0<ior ;
: WRITE-FILE ( a n fh -- ior ) -rot dup >r write r> = 0=ior ;
: READ-FILE ( a n fh -- n ior ) -rot read d0<ior ;
: FILE-POSITION ( fh -- n ior ) 0 SEEK_CUR lseek d0<ior ;
: REPOSITION-FILE ( n fh -- ior ) swap SEEK_SET lseek 0< ;
: RESIZE-FILE ( n fh -- ior ) swap ftruncate 0< ;
: REPOSITION-FILE ( n fh -- ior ) swap SEEK_SET lseek 0<ior ;
: RESIZE-FILE ( n fh -- ior ) swap ftruncate 0<ior ;
: FILE-SIZE ( fh -- n ior )
dup 0 SEEK_CUR lseek >r
dup 0 SEEK_END lseek r> swap >r
@ -183,12 +188,23 @@ O_RDWR constant R/W
( Non-standard )
: NON-BLOCK ( fh -- ior ) F_SETFL FNDELAY fcntl ;
( Directories )
: OPEN-DIR ( a n -- dh ior ) s>z opendir d0=ior ;
: CLOSE-DIR ( dh -- ior ) closedir 0<ior ;
: READ-DIR ( dh -- a n ) readdir dup if .d_name z>s else 0 then ;
( Other Utils )
: ms ( n -- ) 1000 * usleep drop ;
: ms-ticks ( -- n )
0 >r 0 >r CLOCK_MONOTONIC_RAW rp@ cell - clock_gettime throw
r> 1000000 / r> 1000 * + ;
( Shell ops )
: cd ( "path" -- ) bl parse s>z chdir throw ;
: mkdir ( "path" -- ) bl parse s>z 0777 mkdir throw ;
: rmdir ( "path" -- ) bl parse s>z rmdir throw ;
: pwd here getwd z>s type cr ;
forth
( Setup entry )