From 0889b9be414989d292712d60a1e2fcd837eaf504 Mon Sep 17 00:00:00 2001 From: John Baldwin Date: Tue, 11 Dec 2001 00:49:34 +0000 Subject: [PATCH] - Add 'fwrite' and 'fseek' words for writing to and seeking on files. - Change the 'fopen' keyword to accept a mode parameter. Note that this will break existing 4th scripts that use fopen. Thus, the loader version has been bumped and loader.4th has been changed to check for a sufficient version on i386 and alpha. Be sure that you either do a full world build or install or full build and install of sys/boot after this since loader.old won't work with the new 4th files and vice versa. PR: kern/32389 Submitted by: Jonathan Mini Sponsored by: ClickArray, Inc. --- share/examples/bootforth/boot.4th | 6 +-- share/examples/bootforth/loader.rc | 6 +-- sys/boot/alpha/cdboot/version | 1 + sys/boot/alpha/loader/version | 1 + sys/boot/common/loader.8 | 14 +++++- sys/boot/ficl/loader.c | 73 ++++++++++++++++++++++++++---- sys/boot/forth/loader.4th | 8 ++-- sys/boot/forth/pnp.4th | 2 +- sys/boot/forth/support.4th | 23 +++++----- sys/boot/i386/loader/version | 1 + 10 files changed, 102 insertions(+), 33 deletions(-) diff --git a/share/examples/bootforth/boot.4th b/share/examples/bootforth/boot.4th index 631775b304f7..8f26e0d0a38d 100644 --- a/share/examples/bootforth/boot.4th +++ b/share/examples/bootforth/boot.4th @@ -7,15 +7,15 @@ cr .( Loading Forth extensions:) cr .( - screen.4th...) -s" /boot/screen.4th" fopen dup fload fclose +s" /boot/screen.4th" O_RDONLY fopen dup fload fclose \ Load frame support cr .( - frames.4th...) -s" /boot/frames.4th" fopen dup fload fclose +s" /boot/frames.4th" O_RDONLY fopen dup fload fclose \ Load our little menu cr .( - menu.4th...) -s" /boot/menu.4th" fopen dup fload fclose +s" /boot/menu.4th" O_RDONLY fopen dup fload fclose \ Show it cr diff --git a/share/examples/bootforth/loader.rc b/share/examples/bootforth/loader.rc index d216a460416f..617bc3db4104 100644 --- a/share/examples/bootforth/loader.rc +++ b/share/examples/bootforth/loader.rc @@ -13,15 +13,15 @@ include /boot/loader.4th \ Load the screen manipulation words cr .( - screen.4th...) -s" /boot/screen.4th" fopen dup fload fclose +s" /boot/screen.4th" O_RDONLY fopen dup fload fclose \ Load frame support cr .( - frames.4th...) -s" /boot/frames.4th" fopen dup fload fclose +s" /boot/frames.4th" O_RDONLY fopen dup fload fclose \ Load our little menu cr .( - menuconf.4th...) -s" /boot/menuconf.4th" fopen dup fload fclose +s" /boot/menuconf.4th" O_RDONLY fopen dup fload fclose \ Initialize loader.4th stuff diff --git a/sys/boot/alpha/cdboot/version b/sys/boot/alpha/cdboot/version index a544d7fc798c..028be8b5295c 100644 --- a/sys/boot/alpha/cdboot/version +++ b/sys/boot/alpha/cdboot/version @@ -3,6 +3,7 @@ $FreeBSD$ NOTE ANY CHANGES YOU MAKE TO THE BOOTBLOCKS HERE. The format of this file is important. Make sure the current version number is on line 6. +1.2: New calling conventions for fopen. 1.1: New semantics for finding the kernel, new boot. 1.0: Released working DEC Alpha version. 0.1: Initial i386 version, germinated from the NetBSD i386 diff --git a/sys/boot/alpha/loader/version b/sys/boot/alpha/loader/version index c4eced4ae61c..d16c21538a7a 100644 --- a/sys/boot/alpha/loader/version +++ b/sys/boot/alpha/loader/version @@ -3,6 +3,7 @@ $FreeBSD$ NOTE ANY CHANGES YOU MAKE TO THE BOOTBLOCKS HERE. The format of this file is important. Make sure the current version number is on line 6. +1.2: New calling conventions for fopen. 1.1: New semantics for finding the kernel, new boot. 1.0: Released working DEC Alpha version. 0.3: Set/getenv&cia, copyin/out. diff --git a/sys/boot/common/loader.8 b/sys/boot/common/loader.8 index 1d70aca5135c..85c214f65367 100644 --- a/sys/boot/common/loader.8 +++ b/sys/boot/common/loader.8 @@ -642,9 +642,19 @@ Reads a single character from a file. .It Ic fload Pq Ar fd -- Process file .Em fd . -.It Ic fopen Pq Ar addr len -- fd +.It Ic fopen Pq Ar addr len mode Li -- Ar fd Open a file. -Returns a file descriptor, or -1 in case of failure. +Returns a file descriptor, or \-1 in case of failure. The +.Ar mode +parameter selects whether the file is to be opened for read access, write +access, or both. +The constants +.Dv O_RDONLY , O_WRONLY , +and +.Dv O_RDWR +are defined in +.Pa /boot/support.4th , +indicating read only, write only, and read-write access, respectively. .It Xo .Ic fread .Pq Ar fd addr len -- len' diff --git a/sys/boot/ficl/loader.c b/sys/boot/ficl/loader.c index 43db67fa1c59..54380b21e1cf 100644 --- a/sys/boot/ficl/loader.c +++ b/sys/boot/ficl/loader.c @@ -371,22 +371,37 @@ static void displayCellNoPad(FICL_VM *pVM) /* fopen - open a file and return new fd on stack. * - * fopen ( count ptr -- fd ) + * fopen ( ptr count mode -- fd ) */ static void pfopen(FICL_VM *pVM) { - int fd; - char *p; + int mode, fd, count; + char *ptr, *name; #if FICL_ROBUST > 1 - vmCheckStack(pVM, 2, 1); + vmCheckStack(pVM, 3, 1); #endif - (void)stackPopINT(pVM->pStack); /* don't need count value */ - p = stackPopPtr(pVM->pStack); - fd = open(p, O_RDONLY); + + mode = stackPopINT(pVM->pStack); /* get mode */ + count = stackPopINT(pVM->pStack); /* get count */ + ptr = stackPopPtr(pVM->pStack); /* get ptr */ + + if ((count < 0) || (ptr == NULL)) { + stackPushINT(pVM->pStack, -1); + return; + } + + /* ensure that the string is null terminated */ + name = (char *)malloc(count+1); + bcopy(ptr,name,count); + name[count] = 0; + + /* open the file */ + fd = open(name, mode); + free(name); stackPushINT(pVM->pStack, fd); return; - } +} /* fclose - close a file who's fd is on stack. * @@ -444,6 +459,46 @@ static void pfload(FICL_VM *pVM) return; } +/* fwrite - write file contents + * + * fwrite ( fd buf nbytes -- nwritten ) + */ +static void pfwrite(FICL_VM *pVM) +{ + int fd, len; + char *buf; + +#if FICL_ROBUST > 1 + vmCheckStack(pVM, 3, 1); +#endif + len = stackPopINT(pVM->pStack); /* get number of bytes to read */ + buf = stackPopPtr(pVM->pStack); /* get buffer */ + fd = stackPopINT(pVM->pStack); /* get fd */ + if (len > 0 && buf && fd != -1) + stackPushINT(pVM->pStack, write(fd, buf, len)); + else + stackPushINT(pVM->pStack, -1); + return; +} + +/* fseek - seek to a new position in a file + * + * fseek ( fd ofs whence -- pos ) + */ +static void pfseek(FICL_VM *pVM) +{ + int fd, pos, whence; + +#if FICL_ROBUST > 1 + vmCheckStack(pVM, 3, 1); +#endif + whence = stackPopINT(pVM->pStack); + pos = stackPopINT(pVM->pStack); + fd = stackPopINT(pVM->pStack); + stackPushINT(pVM->pStack, lseek(fd, pos, whence)); + return; +} + /* key - get a character from stdin * * key ( -- char ) @@ -568,6 +623,8 @@ void ficlCompilePlatform(FICL_SYSTEM *pSys) dictAppendWord(dp, "fread", pfread, FW_DEFAULT); dictAppendWord(dp, "fload", pfload, FW_DEFAULT); dictAppendWord(dp, "fkey", fkey, FW_DEFAULT); + dictAppendWord(dp, "fseek", pfseek, FW_DEFAULT); + dictAppendWord(dp, "fwrite", pfwrite, FW_DEFAULT); dictAppendWord(dp, "key", key, FW_DEFAULT); dictAppendWord(dp, "key?", keyQuestion, FW_DEFAULT); dictAppendWord(dp, "ms", ms, FW_DEFAULT); diff --git a/sys/boot/forth/loader.4th b/sys/boot/forth/loader.4th index f4d7666d2c24..3349c1f78ae1 100644 --- a/sys/boot/forth/loader.4th +++ b/sys/boot/forth/loader.4th @@ -26,8 +26,8 @@ s" arch-alpha" environment? [if] [if] s" loader_version" environment? [if] - 11 < [if] - .( Loader version 1.1+ required) cr + 12 < [if] + .( Loader version 1.2+ required) cr abort [then] [else] @@ -38,8 +38,8 @@ s" arch-alpha" environment? [if] [if] s" arch-i386" environment? [if] [if] s" loader_version" environment? [if] - 10 < [if] - .( Loader version 1.0+ required) cr + 11 < [if] + .( Loader version 1.1+ required) cr abort [then] [else] diff --git a/sys/boot/forth/pnp.4th b/sys/boot/forth/pnp.4th index a9f28995ed5b..395164deadfc 100644 --- a/sys/boot/forth/pnp.4th +++ b/sys/boot/forth/pnp.4th @@ -158,7 +158,7 @@ only forth also support-functions : load-pnp 0 to end_of_file? reset_line_reading - s" /boot/pnpid.conf" fopen fd ! + s" /boot/pnpid.conf" O_RDONLY fopen fd ! fd @ -1 <> if begin end_of_file? 0= diff --git a/sys/boot/forth/support.4th b/sys/boot/forth/support.4th index 79880125f49d..d711094c87e8 100644 --- a/sys/boot/forth/support.4th +++ b/sys/boot/forth/support.4th @@ -80,6 +80,16 @@ 8 constant before_load_error 9 constant after_load_error +\ I/O constants + +0 constant SEEK_SET +1 constant SEEK_CUR +2 constant SEEK_END + +0 constant O_RDONLY +1 constant O_WRONLY +2 constant O_RDWR + \ Crude structure support : structure: @@ -931,23 +941,12 @@ support-functions definitions only forth also support-functions definitions -: create_null_terminated_string { addr len -- addr' len } - len char+ allocate if out_of_memory throw then - >r - addr r@ len move - 0 r@ len + c! - r> len -; - \ Interface to loading conf files : load_conf ( addr len -- ) 0 to end_of_file? reset_line_reading - create_null_terminated_string - over >r - fopen fd ! - r> free-memory + O_RDONLY fopen fd ! fd @ -1 = if open_error throw then ['] process_conf catch fd @ fclose diff --git a/sys/boot/i386/loader/version b/sys/boot/i386/loader/version index 44d3b963a534..7a2acaf1859a 100644 --- a/sys/boot/i386/loader/version +++ b/sys/boot/i386/loader/version @@ -3,6 +3,7 @@ $FreeBSD$ NOTE ANY CHANGES YOU MAKE TO THE BOOTBLOCKS HERE. The format of this file is important. Make sure the current version number is on line 6. +1.1: New calling conventions for fopen. 1.0: New semantics for finding the kernel, new boot. 0.8: Set/getenv & cia, copyin/out. 0.7: Supports large KVM