diff options
author | jkh <jkh@FreeBSD.org> | 1998-11-07 06:18:06 +0000 |
---|---|---|
committer | jkh <jkh@FreeBSD.org> | 1998-11-07 06:18:06 +0000 |
commit | 9c7d73f4f1fefca88980fd2bcf95f473fcd70b9a (patch) | |
tree | b23c04f2423937bdf7331bd1e633e88e3bfa2b4f /sys/boot | |
parent | 0ba901f49f8ddba427868dcec9f2ab5d46dabf79 (diff) | |
download | FreeBSD-src-9c7d73f4f1fefca88980fd2bcf95f473fcd70b9a.zip FreeBSD-src-9c7d73f4f1fefca88980fd2bcf95f473fcd70b9a.tar.gz |
Eliminate much code cruft by extending simple file I/O API to include
fopen and fclose.
Diffstat (limited to 'sys/boot')
-rw-r--r-- | sys/boot/ficl/ficl.c | 50 | ||||
-rw-r--r-- | sys/boot/ficl/ficl.h | 8 | ||||
-rw-r--r-- | sys/boot/ficl/words.c | 98 |
3 files changed, 101 insertions, 55 deletions
diff --git a/sys/boot/ficl/ficl.c b/sys/boot/ficl/ficl.c index a69500f..3b5885f 100644 --- a/sys/boot/ficl/ficl.c +++ b/sys/boot/ficl/ficl.c @@ -255,6 +255,54 @@ int ficlExec(FICL_VM *pVM, char *pText) return (except); } +/************************************************************************** + f i c l E x e c F D +** reads in text from file fd and passes it to ficlExec() + * returns VM_OUTOFTEXT on success or the ficlExec() error code on + * failure. + */ +#define nLINEBUF 256 +int ficlExecFD(FICL_VM *pVM, int fd) +{ + char cp[nLINEBUF]; + int i, nLine = 0, rval = VM_OUTOFTEXT; + char ch; + CELL id; + + id = pVM->sourceID; + pVM->sourceID.i = fd; + + /* feed each line to ficlExec */ + while (1) { + int status, i; + + i = 0; + while ((status = read(fd, &ch, 1)) > 0 && ch != '\n') + cp[i++] = ch; + nLine++; + if (!i) { + if (status < 1) + break; + continue; + } + cp[i] = '\0'; + if ((rval = ficlExec(pVM, cp)) >= VM_ERREXIT) + { + pVM->sourceID = id; + vmThrowErr(pVM, "ficlExecFD: Error at line %d", nLine); + break; + } + } + /* + ** Pass an empty line with SOURCE-ID == 0 to flush + ** any pending REFILLs (as required by FILE wordset) + */ + pVM->sourceID.i = -1; + ficlExec(pVM, ""); + + pVM->sourceID = id; + return rval; +} /************************************************************************** f i c l L o o k u p @@ -382,5 +430,3 @@ void ficlTermSystem(void) return; } - - diff --git a/sys/boot/ficl/ficl.h b/sys/boot/ficl/ficl.h index b961d51..2836c9d 100644 --- a/sys/boot/ficl/ficl.h +++ b/sys/boot/ficl/ficl.h @@ -695,6 +695,14 @@ void ficlTermSystem(void); int ficlExec(FICL_VM *pVM, char *pText); /* +** ficlExecFD(FICL_VM *pVM, int fd); + * Evaluates text from file passed in via fd. + * Execution returns when all of file has been executed or an + * error occurs. + */ +int ficlExecFD(FICL_VM *pVM, int fd); + +/* ** Create a new VM from the heap, and link it into the system VM list. ** Initializes the VM and binds default sized stacks to it. Returns the ** address of the VM, or NULL if an error occurs. diff --git a/sys/boot/ficl/words.c b/sys/boot/ficl/words.c index 42a210e..4af3f0e 100644 --- a/sys/boot/ficl/words.c +++ b/sys/boot/ficl/words.c @@ -4021,68 +4021,55 @@ static void forget(FICL_VM *pVM) /************************* freebsd added I/O words **************************/ -/* fload - load a file and interpret it +/* fopen - open a file and return new fd on stack. * - * grabbed out of testmain.c example and frobbed to fit. - * - * fload ( addr count -- ) + * fopen ( count ptr -- fd ) */ -#define nLINEBUF 256 -static void fload(FICL_VM *pVM) +static void pfopen(FICL_VM *pVM) { - char cp[nLINEBUF], *p; - int i, fd, nLine = 0; - char ch; - CELL id; + int fd; + char *p; (void)stackPopINT32(pVM->pStack); /* don't need count value */ p = stackPopPtr(pVM->pStack); fd = open(p, O_RDONLY); - if (fd == -1) - { - vmTextOut(pVM, "fload: Unable to open file: ", 0); - vmTextOut(pVM, p, 1); - vmThrow(pVM, VM_QUIT); - } + stackPushINT32(pVM->pStack, fd); + return; +} - id = pVM->sourceID; - pVM->sourceID.i = fd; - - /* feed each line to ficlExec */ - while (1) { - int status, i; - - i = 0; - while ((status = read(fd, &ch, 1)) > 0 && ch != '\n') - cp[i++] = ch; - nLine++; - if (!i) { - if (status < 1) - break; - continue; - } - cp[i] = '\0'; - if (ficlExec(pVM, cp) >= VM_ERREXIT) - { - pVM->sourceID = id; - close(fd); - vmThrowErr(pVM, "fload: Error in file %s, line %d", p, nLine); - break; - } - } - /* - ** Pass an empty line with SOURCE-ID == 0 to flush - ** any pending REFILLs (as required by FILE wordset) - */ - pVM->sourceID.i = -1; - ficlExec(pVM, ""); +/* fclose - close a file who's fd is on stack. + * + * fclose ( fd -- ) + */ +static void pfclose(FICL_VM *pVM) +{ + int fd; - pVM->sourceID = id; - close(fd); + fd = stackPopINT32(pVM->pStack); /* get fd */ + if (fd != -1) + close(fd); return; } -static void fexists(FICL_VM *pVM) +/* fload - interpret file contents + * + * fload ( fd -- ) + */ +static void pfload(FICL_VM *pVM) +{ + int fd; + + fd = stackPopINT32(pVM->pStack); /* get fd */ + if (fd != -1) + ficlExecFD(pVM, fd); + return; +} + +/* fexists - check to see if file exists, returning TRUE or FALSE + * + * fexists ( count ptr -- bool ) + */ +static void pfexists(FICL_VM *pVM) { char *p; int fd; @@ -4099,7 +4086,10 @@ static void fexists(FICL_VM *pVM) return; } -/* Get a character from stdin */ +/* key - get a character from stdin + * + * key ( -- char ) + */ static void key(FICL_VM *pVM) { stackPushINT32(pVM->pStack, getchar()); @@ -4281,8 +4271,10 @@ void ficlCompileCore(FICL_DICT *dp) dictAppendWord(dp, "\\", commentLine, FW_IMMEDIATE); /* FreeBSD extention words */ - dictAppendWord(dp, "fload", fload, FW_DEFAULT); - dictAppendWord(dp, "fexists", fexists, FW_DEFAULT); + dictAppendWord(dp, "fexists", pfexists, FW_DEFAULT); + dictAppendWord(dp, "fopen", pfopen, FW_DEFAULT); + dictAppendWord(dp, "fclose", pfclose, FW_DEFAULT); + dictAppendWord(dp, "fload", pfload, FW_DEFAULT); dictAppendWord(dp, "key", key, FW_DEFAULT); /* |