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
26 * $FreeBSD: src/sys/boot/common/interp.c,v 1.22.2.5 2001/03/04 04:46:18 obrien Exp $
27 * $DragonFly: src/sys/boot/common/interp.c,v 1.2 2003/06/17 04:28:16 dillon Exp $
30 * Simple commandline interpreter, toplevel and misc.
32 * XXX may be obsoleted by BootFORTH or some other, better, interpreter.
37 #include "bootstrap.h"
41 #define RETURN(x) stackPushINT(bf_vm->pStack,!x); return(x)
43 extern FICL_VM *bf_vm;
45 #define RETURN(x) return(x)
48 #define MAXARGS 20 /* maximum number of arguments allowed */
50 static void prompt(void);
53 static int perform(int argc, char *argv[]);
59 perform(int argc, char *argv[])
62 struct bootblk_command **cmdp;
68 /* set return defaults; a successful command will override these */
69 command_errmsg = command_errbuf;
70 strcpy(command_errbuf, "no error message");
74 /* search the command set for the command */
75 SET_FOREACH(cmdp, Xcommand_set) {
76 if (((*cmdp)->c_name != NULL) && !strcmp(argv[0], (*cmdp)->c_name))
80 result = (cmd)(argc, argv);
82 command_errmsg = "unknown command";
86 #endif /* ! BOOT_FORTH */
94 char input[256]; /* big enough? */
105 * Read our default configuration
107 if(include("/boot/loader.rc")!=CMD_OK)
108 include("/boot/boot.conf");
111 * Before interacting, we might want to autoboot.
116 * Not autobooting, go manual
118 printf("\nType '?' for a list of commands, 'help' for more detailed help.\n");
119 if (getenv("prompt") == NULL)
120 setenv("prompt", "${interpret}", 1);
121 if (getenv("interpret") == NULL)
122 setenv("interpret", "ok", 1);
128 ngets(input, sizeof(input));
130 bf_vm->sourceID.i = 0;
133 if (!parse(&argc, &argv, input)) {
134 if (perform(argc, argv))
135 printf("%s: %s\n", argv[0], command_errmsg);
138 printf("parse error\n");
145 * Read commands from a file, then execute them.
147 * We store the commands in memory and close the source file so that the media
148 * holding it can safely go away while we are executing.
150 * Commands may be prefixed with '@' (so they aren't displayed) or '-' (so
151 * that the script won't stop if they fail).
153 COMMAND_SET(include, "include", "read commands from a file", command_include);
156 command_include(int argc, char *argv[])
163 * Since argv is static, we need to save it here.
165 argvbuf = (char**) calloc((u_int)argc, sizeof(char*));
166 for (i = 0; i < argc; i++)
167 argvbuf[i] = strdup(argv[i]);
170 for (i = 1; (i < argc) && (res == CMD_OK); i++)
171 res = include(argvbuf[i]);
173 for (i = 0; i < argc; i++)
185 #define SL_QUIET (1<<0)
186 #define SL_IGNOREERR (1<<1)
187 struct includeline *next;
191 include(const char *filename)
193 struct includeline *script, *se, *sp;
194 char input[256]; /* big enough? */
198 int prevsrcid, fd, line;
205 if (((fd = open(filename, O_RDONLY)) == -1)) {
206 sprintf(command_errbuf,"can't open '%s': %s\n", filename, strerror(errno));
211 * Read the script into memory.
216 while (fgetstr(input, sizeof(input), fd) >= 0) {
222 /* Discard comments */
223 if (strncmp(input+strspn(input, " "), "\\ ", 2) == 0)
227 if (input[0] == '@') {
232 if (input[0] == '-') {
234 flags |= SL_IGNOREERR;
237 /* Allocate script line structure and copy line, flags */
238 sp = malloc(sizeof(struct includeline) + strlen(cp) + 1);
239 sp->text = (char *)sp + sizeof(struct includeline);
240 strcpy(sp->text, cp);
247 if (script == NULL) {
262 prevsrcid = bf_vm->sourceID.i;
263 bf_vm->sourceID.i = fd;
266 for (sp = script; sp != NULL; sp = sp->next) {
269 res = bf_run(sp->text);
270 if (res != VM_OUTOFTEXT) {
271 sprintf(command_errbuf, "Error while including %s, in the line:\n%s", filename, sp->text);
277 /* print if not being quiet */
278 if (!(sp->flags & SL_QUIET)) {
280 printf("%s\n", sp->text);
283 /* Parse the command */
284 if (!parse(&argc, &argv, sp->text)) {
285 if ((argc > 0) && (perform(argc, argv) != 0)) {
287 printf("%s: %s\n", argv[0], command_errmsg);
288 if (!(sp->flags & SL_IGNOREERR)) {
296 printf("%s line %d: parse error\n", filename, sp->line);
306 bf_vm->sourceID.i = prevsrcid;
308 while(script != NULL) {
310 script = script->next;
317 * Emit the current prompt; use the same syntax as the parser
318 * for embedding environment variables.
323 char *pr, *p, *cp, *ev;
325 if ((cp = getenv("prompt")) == NULL)
330 if ((*p == '$') && (*(p+1) == '{')) {
331 for (cp = p + 2; (*cp != 0) && (*cp != '}'); cp++)