From 99eb96536f47b2598d368c515c46309f06ffa5df Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Sun, 3 Jan 2021 22:10:09 -0800 Subject: [PATCH] Argc, Argv --- ueforth/common/boot.fs | 2 ++ ueforth/common/core.h | 7 ++++++- ueforth/posix/posix.fs | 10 +++++++++- ueforth/posix/posix_main.c | 2 +- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/ueforth/common/boot.fs b/ueforth/common/boot.fs index 3a24751..0d41808 100644 --- a/ueforth/common/boot.fs +++ b/ueforth/common/boot.fs @@ -38,6 +38,8 @@ : 'heap ( -- a ) 'sys 5 cells + ; : last ( -- a ) 'sys 6 cells + ; : 'notfound ( -- a ) 'sys 7 cells + ; +: 'argc ( -- a ) 'sys 8 cells + ; +: 'argv ( -- a ) 'sys 9 cells + ; ( Dictionary ) : here ( -- a ) 'heap @ ; diff --git a/ueforth/common/core.h b/ueforth/common/core.h index 98f8e45..d4c0c34 100644 --- a/ueforth/common/core.h +++ b/ueforth/common/core.h @@ -13,6 +13,8 @@ static struct { const char *tib; cell_t ntib, tin, state, base; cell_t *heap, *last, notfound; + int argc; + char **argv; cell_t DOLIT_XT, DOEXIT_XT; } g_sys; @@ -114,7 +116,8 @@ static cell_t *evaluate1(cell_t *sp, cell_t *call) { return sp; } -static void ueforth(void *heap, const char *src, cell_t src_len) { +static void ueforth(int argc, char *argv[], void *heap, + const char *src, cell_t src_len) { g_sys.heap = (cell_t *) heap; register cell_t *sp = g_sys.heap; g_sys.heap += STACK_SIZE; register cell_t *rp = g_sys.heap; g_sys.heap += STACK_SIZE; @@ -134,6 +137,8 @@ static void ueforth(void *heap, const char *src, cell_t src_len) { *g_sys.heap++ = FIND("EVALUATE1"); *g_sys.heap++ = FIND("BRANCH"); *g_sys.heap++ = (cell_t) ip; + g_sys.argc = argc; + g_sys.argv = argv; g_sys.base = 10; g_sys.tib = src; g_sys.ntib = src_len; diff --git a/ueforth/posix/posix.fs b/ueforth/posix/posix.fs index dbeb009..f565dda 100644 --- a/ueforth/posix/posix.fs +++ b/ueforth/posix/posix.fs @@ -27,6 +27,7 @@ z" mmap" 6 sysfunc mmap z" munmap" 2 sysfunc munmap z" unlink" 1 sysfunc unlink z" rename" 2 sysfunc rename +z" strlen" 1 sysfunc strlen ( Errno ) z" __errno_location" 0 sysfunc __errno_location @@ -94,12 +95,19 @@ z" realloc" 2 sysfunc realloc : free ( a -- ior ) sysfree 0 ; : resize ( a n -- a ior ) realloc 0ior ; +( String Handling ) +: s>z ( a n -- z ) here >r $place r> ; +: z>s ( z -- a n ) dup strlen ; + +( Arguments ) +: argc ( -- n ) 'argc @ ; +: argv ( n -- a n ) cells 'argv @ + @ z>s ; + ( Generic Files ) O_RDONLY constant r/o O_WRONLY constant w/o O_RDWR constant r/w octal 777 constant 0777 decimal -: s>z ( a n -- z ) here >r $place r> ; : open-file ( a n fam -- fh ior ) >r s>z r> 0777 open 0r s>z r> O_CREAT or 0777 open 0