Break out the interpreters (simple and forth) w/o ifdefs. This is

akin to what Pedro Souza and Wojciech Koszek did in the lua GSoC with
interp.h, interp_simple.c and changes to interp.c and interp_forth.c,
but completely redone from scratch.

This effectively restores the spirit of r326712 (my first attempt to
bring in Pedro's and Wojciech's work) updated for new requirements
that had silently broke their original work.  This change also differs
by using fixed function names instead of function pointers to simply
things. Only one interpreter at a time may be compiled in.

Also of note: we take a mutable string, pass it in via a const char *
pointer into intrp_forth's interp_run(). We then cast away the const
to pass into ficlExec since ficl would require extensive changes to
properly const-poison. See Sections 6.5.2.5 and 6.7.3 of C11 standard
noting it's only UB if you modify a const object through a non-const
pointer, but not char [] -> const char * -> char * as here.
This commit is contained in:
Warner Losh 2018-01-31 22:46:05 +00:00
parent 839bbb1c21
commit 79a6a17abd
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=328642
6 changed files with 366 additions and 247 deletions

View file

@ -46,7 +46,11 @@ extern char command_errbuf[COMMAND_ERRBUFSZ];
/* interp.c */
void interact(void);
int include(const char *filename);
void interp_emit_prompt(void);
/* Called by interp.c for interp_*.c embedded interpreters */
int interp_include(const char *filename); /* Execute commands from filename */
void interp_init(void); /* Initialize interpreater */
int interp_run(const char *line); /* Run a single command */
/* interp_backslash.c */
char *backslash(const char *str);
@ -54,10 +58,6 @@ char *backslash(const char *str);
/* interp_parse.c */
int parse(int *argc, char ***argv, const char *str);
/* interp_forth.c */
void bf_init(void);
int bf_run(char *line);
/* boot.c */
int autoboot(int timeout, char *prompt);
void autoboot_maybe(void);

View file

@ -286,7 +286,7 @@ install(char *pkgname)
fd = open(s, O_RDONLY);
if (fd != -1) {
close(fd);
error = include(s);
error = inter_include(s);
if (error == CMD_ERROR)
goto fail;
}

View file

@ -37,49 +37,8 @@ __FBSDID("$FreeBSD$");
#include <string.h>
#include "bootstrap.h"
#ifdef BOOT_FORTH
#include "ficl.h"
extern FICL_VM *bf_vm;
#endif
#define MAXARGS 20 /* maximum number of arguments allowed */
static void prompt(void);
#ifndef BOOT_FORTH
/*
* Perform the command
*/
static int
perform(int argc, char *argv[])
{
int result;
struct bootblk_command **cmdp;
bootblk_cmd_t *cmd;
if (argc < 1)
return(CMD_OK);
/* set return defaults; a successful command will override these */
command_errmsg = command_errbuf;
strcpy(command_errbuf, "no error message");
cmd = NULL;
result = CMD_ERROR;
/* search the command set for the command */
SET_FOREACH(cmdp, Xcommand_set) {
if (((*cmdp)->c_name != NULL) && !strcmp(argv[0], (*cmdp)->c_name))
cmd = (*cmdp)->c_fn;
}
if (cmd != NULL) {
result = (cmd)(argc, argv);
} else {
command_errmsg = "unknown command";
}
return(result);
}
#endif /* ! BOOT_FORTH */
/*
* Interactive mode
*/
@ -87,17 +46,8 @@ void
interact(void)
{
static char input[256]; /* big enough? */
#ifndef BOOT_FORTH
int argc;
char **argv;
#endif
#ifdef BOOT_FORTH
bf_init();
#endif
/* Read our default configuration. */
include("/boot/loader.rc");
interp_init();
printf("\n");
@ -105,7 +55,7 @@ interact(void)
* Before interacting, we might want to autoboot.
*/
autoboot_maybe();
/*
* Not autobooting, go manual
*/
@ -114,24 +64,12 @@ interact(void)
setenv("prompt", "${interpret}", 1);
if (getenv("interpret") == NULL)
setenv("interpret", "OK", 1);
for (;;) {
input[0] = '\0';
prompt();
interp_emit_prompt();
ngets(input, sizeof(input));
#ifdef BOOT_FORTH
bf_vm->sourceID.i = 0;
bf_run(input);
#else
if (!parse(&argc, &argv, input)) {
if (perform(argc, argv))
printf("%s: %s\n", argv[0], command_errmsg);
free(argv);
} else {
printf("parse error\n");
}
#endif
interp_run(input);
}
}
@ -153,7 +91,7 @@ command_include(int argc, char *argv[])
int res;
char **argvbuf;
/*
/*
* Since argv is static, we need to save it here.
*/
argvbuf = (char**) calloc((u_int)argc, sizeof(char*));
@ -162,7 +100,7 @@ command_include(int argc, char *argv[])
res=CMD_OK;
for (i = 1; (i < argc) && (res == CMD_OK); i++)
res = include(argvbuf[i]);
res = interp_include(argvbuf[i]);
for (i = 0; i < argc; i++)
free(argvbuf[i]);
@ -171,175 +109,15 @@ command_include(int argc, char *argv[])
return(res);
}
/*
* Header prepended to each line. The text immediately follows the header.
* We try to make this short in order to save memory -- the loader has
* limited memory available, and some of the forth files are very long.
*/
struct includeline
{
struct includeline *next;
#ifndef BOOT_FORTH
int flags;
int line;
#define SL_QUIET (1<<0)
#define SL_IGNOREERR (1<<1)
#endif
char text[0];
};
int
include(const char *filename)
{
struct includeline *script, *se, *sp;
char input[256]; /* big enough? */
#ifdef BOOT_FORTH
int res;
char *cp;
int prevsrcid, fd, line;
#else
int argc,res;
char **argv, *cp;
int fd, flags, line;
#endif
if (((fd = open(filename, O_RDONLY)) == -1)) {
snprintf(command_errbuf, sizeof(command_errbuf),
"can't open '%s': %s", filename, strerror(errno));
return(CMD_ERROR);
}
/*
* Read the script into memory.
*/
script = se = NULL;
line = 0;
while (fgetstr(input, sizeof(input), fd) >= 0) {
line++;
#ifdef BOOT_FORTH
cp = input;
#else
flags = 0;
/* Discard comments */
if (strncmp(input+strspn(input, " "), "\\ ", 2) == 0)
continue;
cp = input;
/* Echo? */
if (input[0] == '@') {
cp++;
flags |= SL_QUIET;
}
/* Error OK? */
if (input[0] == '-') {
cp++;
flags |= SL_IGNOREERR;
}
#endif
/* Allocate script line structure and copy line, flags */
if (*cp == '\0')
continue; /* ignore empty line, save memory */
sp = malloc(sizeof(struct includeline) + strlen(cp) + 1);
/* On malloc failure (it happens!), free as much as possible and exit */
if (sp == NULL) {
while (script != NULL) {
se = script;
script = script->next;
free(se);
}
snprintf(command_errbuf, sizeof(command_errbuf),
"file '%s' line %d: memory allocation failure - aborting",
filename, line);
close(fd);
return (CMD_ERROR);
}
strcpy(sp->text, cp);
#ifndef BOOT_FORTH
sp->flags = flags;
sp->line = line;
#endif
sp->next = NULL;
if (script == NULL) {
script = sp;
} else {
se->next = sp;
}
se = sp;
}
close(fd);
/*
* Execute the script
*/
#ifndef BOOT_FORTH
argv = NULL;
#else
prevsrcid = bf_vm->sourceID.i;
bf_vm->sourceID.i = fd;
#endif
res = CMD_OK;
for (sp = script; sp != NULL; sp = sp->next) {
#ifdef BOOT_FORTH
res = bf_run(sp->text);
if (res != VM_OUTOFTEXT) {
snprintf(command_errbuf, sizeof(command_errbuf),
"Error while including %s, in the line:\n%s",
filename, sp->text);
res = CMD_ERROR;
break;
} else
res = CMD_OK;
#else
/* print if not being quiet */
if (!(sp->flags & SL_QUIET)) {
prompt();
printf("%s\n", sp->text);
}
/* Parse the command */
if (!parse(&argc, &argv, sp->text)) {
if ((argc > 0) && (perform(argc, argv) != 0)) {
/* normal command */
printf("%s: %s\n", argv[0], command_errmsg);
if (!(sp->flags & SL_IGNOREERR)) {
res=CMD_ERROR;
break;
}
}
free(argv);
argv = NULL;
} else {
printf("%s line %d: parse error\n", filename, sp->line);
res=CMD_ERROR;
break;
}
#endif
}
#ifndef BOOT_FORTH
if (argv != NULL)
free(argv);
#else
bf_vm->sourceID.i = prevsrcid;
#endif
while(script != NULL) {
se = script;
script = script->next;
free(se);
}
return(res);
}
/*
* Emit the current prompt; use the same syntax as the parser
* for embedding environment variables.
* for embedding environment variables. Does not accept input.
*/
static void
prompt(void)
void
interp_emit_prompt(void)
{
char *pr, *p, *cp, *ev;
if ((cp = getenv("prompt")) == NULL)
cp = ">";
pr = p = strdup(cp);
@ -350,7 +128,7 @@ prompt(void)
;
*cp = 0;
ev = getenv(p + 2);
if (ev != NULL)
printf("%s", ev);
p = cp + 1;

View file

@ -81,7 +81,7 @@ bf_command(FICL_VM *vm)
/* Get the name of the current word */
name = vm->runningWord->name;
/* Find our command structure */
cmd = NULL;
SET_FOREACH(cmdp, Xcommand_set) {
@ -90,7 +90,7 @@ bf_command(FICL_VM *vm)
}
if (cmd == NULL)
panic("callout for unknown command '%s'", name);
/* Check whether we have been compiled or are being interpreted */
if (stackPopINT(vm->pStack)) {
/*
@ -118,7 +118,7 @@ bf_command(FICL_VM *vm)
tail = vmGetInBuf(vm);
for (cp = tail, len = 0; cp != vm->tib.end && *cp != 0 && *cp != '\n'; cp++, len++)
;
line = malloc(strlen(name) + len + 2);
strcpy(line, name);
if (len > 0) {
@ -128,7 +128,7 @@ bf_command(FICL_VM *vm)
}
}
DEBUG("cmd '%s'", line);
command_errmsg = command_errbuf;
command_errbuf[0] = 0;
if (!parse(&argc, &argv, line)) {
@ -289,12 +289,19 @@ bf_init(void)
/*
* Feed a line of user input to the Forth interpreter
*/
int
bf_run(char *line)
static int
bf_run(const char *line)
{
int result;
result = ficlExec(bf_vm, line);
/*
* ficl would require extensive changes to accept a const char *
* interface. Instead, cast it away here and hope for the best.
* We know at the present time the caller for us in the boot
* forth loader can tolerate the string being modified because
* the string is passed in here and then not touched again.
*/
result = ficlExec(bf_vm, __DECONST(char *, line));
DEBUG("ficlExec '%s' = %d", line, result);
switch (result) {
@ -318,10 +325,119 @@ bf_run(char *line)
command_errmsg = NULL;
}
}
if (result == VM_USEREXIT)
panic("interpreter exit");
setenv("interpret", bf_vm->state ? "" : "OK", 1);
return (result);
}
void
interp_init(void)
{
bf_init();
/* Read our default configuration. */
interp_include("/boot/loader.rc");
}
int
interp_run(const char *input)
{
bf_vm->sourceID.i = 0;
return bf_run(input);
}
/*
* Header prepended to each line. The text immediately follows the header.
* We try to make this short in order to save memory -- the loader has
* limited memory available, and some of the forth files are very long.
*/
struct includeline
{
struct includeline *next;
char text[0];
};
int
interp_include(const char *filename)
{
struct includeline *script, *se, *sp;
char input[256]; /* big enough? */
int res;
char *cp;
int prevsrcid, fd, line;
if (((fd = open(filename, O_RDONLY)) == -1)) {
snprintf(command_errbuf, sizeof(command_errbuf),
"can't open '%s': %s", filename, strerror(errno));
return(CMD_ERROR);
}
/*
* Read the script into memory.
*/
script = se = NULL;
line = 0;
while (fgetstr(input, sizeof(input), fd) >= 0) {
line++;
cp = input;
/* Allocate script line structure and copy line, flags */
if (*cp == '\0')
continue; /* ignore empty line, save memory */
sp = malloc(sizeof(struct includeline) + strlen(cp) + 1);
/* On malloc failure (it happens!), free as much as possible and exit */
if (sp == NULL) {
while (script != NULL) {
se = script;
script = script->next;
free(se);
}
snprintf(command_errbuf, sizeof(command_errbuf),
"file '%s' line %d: memory allocation failure - aborting",
filename, line);
close(fd);
return (CMD_ERROR);
}
strcpy(sp->text, cp);
sp->next = NULL;
if (script == NULL) {
script = sp;
} else {
se->next = sp;
}
se = sp;
}
close(fd);
/*
* Execute the script
*/
prevsrcid = bf_vm->sourceID.i;
bf_vm->sourceID.i = fd;
res = CMD_OK;
for (sp = script; sp != NULL; sp = sp->next) {
res = bf_run(sp->text);
if (res != VM_OUTOFTEXT) {
snprintf(command_errbuf, sizeof(command_errbuf),
"Error while including %s, in the line:\n%s",
filename, sp->text);
res = CMD_ERROR;
break;
} else
res = CMD_OK;
}
bf_vm->sourceID.i = prevsrcid;
while (script != NULL) {
se = script;
script = script->next;
free(se);
}
return(res);
}

View file

@ -0,0 +1,223 @@
/*-
* Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
/*
* Simple commandline interpreter, toplevel and misc.
*/
#include <stand.h>
#include <string.h>
#include "bootstrap.h"
/*
* Perform the command
*/
static int
perform(int argc, char *argv[])
{
int result;
struct bootblk_command **cmdp;
bootblk_cmd_t *cmd;
if (argc < 1)
return(CMD_OK);
/* set return defaults; a successful command will override these */
command_errmsg = command_errbuf;
strcpy(command_errbuf, "no error message");
cmd = NULL;
result = CMD_ERROR;
/* search the command set for the command */
SET_FOREACH(cmdp, Xcommand_set) {
if (((*cmdp)->c_name != NULL) && !strcmp(argv[0], (*cmdp)->c_name))
cmd = (*cmdp)->c_fn;
}
if (cmd != NULL) {
result = (cmd)(argc, argv);
} else {
command_errmsg = "unknown command";
}
return(result);
}
void
interp_init(void)
{
/* Read our default configuration. */
interp_include("/boot/loader.rc");
}
int
interp_run(const char *input)
{
int argc;
char **argv;
if (parse(&argc, &argv, input)) {
printf("parse error\n");
return CMD_ERROR;
}
if (perform(argc, argv)) {
printf("%s: %s\n", argv[0], command_errmsg);
free(argv);
return CMD_ERROR;
}
free(argv);
return CMD_OK;
}
/*
* Header prepended to each line. The text immediately follows the header.
* We try to make this short in order to save memory -- the loader has
* limited memory available, and some of the forth files are very long.
*/
struct includeline
{
struct includeline *next;
int flags;
int line;
#define SL_QUIET (1<<0)
#define SL_IGNOREERR (1<<1)
char text[0];
};
int
interp_include(const char *filename)
{
struct includeline *script, *se, *sp;
char input[256]; /* big enough? */
int argc,res;
char **argv, *cp;
int fd, flags, line;
if (((fd = open(filename, O_RDONLY)) == -1)) {
snprintf(command_errbuf, sizeof(command_errbuf),
"can't open '%s': %s", filename, strerror(errno));
return(CMD_ERROR);
}
/*
* Read the script into memory.
*/
script = se = NULL;
line = 0;
while (fgetstr(input, sizeof(input), fd) >= 0) {
line++;
flags = 0;
/* Discard comments */
if (strncmp(input+strspn(input, " "), "\\ ", 2) == 0)
continue;
cp = input;
/* Echo? */
if (input[0] == '@') {
cp++;
flags |= SL_QUIET;
}
/* Error OK? */
if (input[0] == '-') {
cp++;
flags |= SL_IGNOREERR;
}
/* Allocate script line structure and copy line, flags */
if (*cp == '\0')
continue; /* ignore empty line, save memory */
sp = malloc(sizeof(struct includeline) + strlen(cp) + 1);
/* On malloc failure (it happens!), free as much as possible and exit */
if (sp == NULL) {
while (script != NULL) {
se = script;
script = script->next;
free(se);
}
snprintf(command_errbuf, sizeof(command_errbuf),
"file '%s' line %d: memory allocation failure - aborting",
filename, line);
close(fd);
return (CMD_ERROR);
}
strcpy(sp->text, cp);
sp->flags = flags;
sp->line = line;
sp->next = NULL;
if (script == NULL) {
script = sp;
} else {
se->next = sp;
}
se = sp;
}
close(fd);
/*
* Execute the script
*/
argv = NULL;
res = CMD_OK;
for (sp = script; sp != NULL; sp = sp->next) {
/* print if not being quiet */
if (!(sp->flags & SL_QUIET)) {
interp_emit_prompt();
printf("%s\n", sp->text);
}
/* Parse the command */
if (!parse(&argc, &argv, sp->text)) {
if ((argc > 0) && (perform(argc, argv) != 0)) {
/* normal command */
printf("%s: %s\n", argv[0], command_errmsg);
if (!(sp->flags & SL_IGNOREERR)) {
res=CMD_ERROR;
break;
}
}
free(argv);
argv = NULL;
} else {
printf("%s line %d: parse error\n", filename, sp->line);
res=CMD_ERROR;
break;
}
}
if (argv != NULL)
free(argv);
while (script != NULL) {
se = script;
script = script->next;
free(se);
}
return(res);
}

View file

@ -59,6 +59,8 @@ SRCS+= pnp.c
.if ${MK_FORTH} != "no"
SRCS+= interp_forth.c
.include "${BOOTSRC}/ficl.mk"
.else
SRCS+= interp_simple.c
.endif
.if defined(BOOT_PROMPT_123)