- 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 <mini@haikugeek.com>
Sponsored by:	ClickArray, Inc.
This commit is contained in:
John Baldwin 2001-12-11 00:49:34 +00:00
parent 4fd13c1ba2
commit 0889b9be41
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=87636
10 changed files with 102 additions and 33 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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.

View File

@ -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'

View File

@ -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);

View File

@ -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]

View File

@ -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=

View File

@ -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

View File

@ -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