2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * Simple commandline interpreter, toplevel and misc.
33 * XXX may be obsoleted by BootFORTH or some other, better, interpreter.
38 #include "bootstrap.h"
42 #define RETURN(x) stackPushINT(bf_vm->pStack,!x); return(x)
44 extern FICL_VM *bf_vm;
46 #define RETURN(x) return(x)
49 #define MAXARGS 20 /* maximum number of arguments allowed */
51 static void prompt(void);
54 static int perform(int argc, char *argv[]);
60 perform(int argc, char *argv[])
63 struct bootblk_command **cmdp;
69 /* set return defaults; a successful command will override these */
70 command_errmsg = command_errbuf;
71 strcpy(command_errbuf, "no error message");
75 /* search the command set for the command */
76 SET_FOREACH(cmdp, Xcommand_set) {
77 if (((*cmdp)->c_name != NULL) && !strcmp(argv[0], (*cmdp)->c_name))
81 result = (cmd)(argc, argv);
83 command_errmsg = "unknown command";
87 #endif /* ! BOOT_FORTH */
93 interact(const char *rc)
95 static char input[256]; /* big enough? */
102 bf_init((rc) ? "" : NULL);
106 /* Read our default configuration. */
107 if (include("/boot/loader.rc") != CMD_OK)
108 include("/boot/boot.conf");
109 } else if (*rc != '\0')
115 * Before interacting, we might want to autoboot.
120 * Not autobooting, go manual
122 printf("\nType '?' for a list of commands, 'help' for more detailed help.\n");
123 if (getenv("prompt") == NULL)
124 setenv("prompt", "${interpret}", 1);
125 if (getenv("interpret") == NULL)
126 setenv("interpret", "OK", 1);
132 ngets(input, sizeof(input));
134 bf_vm->sourceID.i = 0;
137 if (!parse(&argc, &argv, input)) {
138 if (perform(argc, argv))
139 printf("%s: %s\n", argv[0], command_errmsg);
142 printf("parse error\n");
149 * Read commands from a file, then execute them.
151 * We store the commands in memory and close the source file so that the media
152 * holding it can safely go away while we are executing.
154 * Commands may be prefixed with '@' (so they aren't displayed) or '-' (so
155 * that the script won't stop if they fail).
157 COMMAND_SET(include, "include", "read commands from a file", command_include);
160 command_include(int argc, char *argv[])
167 * Since argv is static, we need to save it here.
169 argvbuf = (char**) calloc((u_int)argc, sizeof(char*));
170 for (i = 0; i < argc; i++)
171 argvbuf[i] = strdup(argv[i]);
174 for (i = 1; (i < argc) && (res == CMD_OK); i++)
175 res = include(argvbuf[i]);
177 for (i = 0; i < argc; i++)
185 * Header prepended to each line. The text immediately follows the header.
186 * We try to make this short in order to save memory -- the loader has
187 * limited memory available, and some of the forth files are very long.
191 struct includeline *next;
195 #define SL_QUIET (1<<0)
196 #define SL_IGNOREERR (1<<1)
202 include(const char *filename)
204 struct includeline *script, *se, *sp;
205 char input[256]; /* big enough? */
209 int prevsrcid, fd, line;
216 if (((fd = open(filename, O_RDONLY)) == -1)) {
217 snprintf(command_errbuf, sizeof(command_errbuf),
218 "can't open '%s': %s", filename, strerror(errno));
223 * Read the script into memory.
228 while (fgetstr(input, sizeof(input), fd) >= 0) {
234 /* Discard comments */
235 if (strncmp(input+strspn(input, " "), "\\ ", 2) == 0)
239 if (input[0] == '@') {
244 if (input[0] == '-') {
246 flags |= SL_IGNOREERR;
249 /* Allocate script line structure and copy line, flags */
251 continue; /* ignore empty line, save memory */
252 sp = malloc(sizeof(struct includeline) + strlen(cp) + 1);
253 /* On malloc failure (it happens!), free as much as possible and exit */
255 while (script != NULL) {
257 script = script->next;
260 snprintf(command_errbuf, sizeof(command_errbuf),
261 "file '%s' line %d: memory allocation failure - aborting",
265 strcpy(sp->text, cp);
272 if (script == NULL) {
287 prevsrcid = bf_vm->sourceID.i;
288 bf_vm->sourceID.i = fd;
291 for (sp = script; sp != NULL; sp = sp->next) {
294 res = bf_run(sp->text);
295 if (res != VM_OUTOFTEXT) {
296 snprintf(command_errbuf, sizeof(command_errbuf),
297 "Error while including %s, in the line:\n%s",
304 /* print if not being quiet */
305 if (!(sp->flags & SL_QUIET)) {
307 printf("%s\n", sp->text);
310 /* Parse the command */
311 if (!parse(&argc, &argv, sp->text)) {
312 if ((argc > 0) && (perform(argc, argv) != 0)) {
314 printf("%s: %s\n", argv[0], command_errmsg);
315 if (!(sp->flags & SL_IGNOREERR)) {
323 printf("%s line %d: parse error\n", filename, sp->line);
333 bf_vm->sourceID.i = prevsrcid;
335 while(script != NULL) {
337 script = script->next;
344 * Emit the current prompt; use the same syntax as the parser
345 * for embedding environment variables.
350 char *pr, *p, *cp, *ev;
352 if ((cp = getenv("prompt")) == NULL)
357 if ((*p == '$') && (*(p+1) == '{')) {
358 for (cp = p + 2; (*cp != 0) && (*cp != '}'); cp++)