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.
36 #include "bootstrap.h"
38 INTERP_DEFINE("simp");
44 setenv("script.lang", "simple", 1);
45 /* Read our default configuration. */
46 interp_include("/boot/loader.rc");
50 interp_run(const char *input)
55 if (parse(&argc, &argv, input)) {
56 printf("parse error\n");
60 if (interp_builtin_cmd(argc, argv)) {
61 printf("%s: %s\n", argv[0], command_errmsg);
70 * Header prepended to each line. The text immediately follows the header.
71 * We try to make this short in order to save memory -- the loader has
72 * limited memory available, and some of the forth files are very long.
76 struct includeline *next;
79 #define SL_QUIET (1<<0)
80 #define SL_IGNOREERR (1<<1)
85 interp_include(const char *filename)
87 struct includeline *script, *se, *sp;
88 char input[256]; /* big enough? */
93 if (((fd = open(filename, O_RDONLY)) == -1)) {
94 snprintf(command_errbuf, sizeof(command_errbuf),
95 "can't open '%s': %s", filename, strerror(errno));
100 * Read the script into memory.
105 while (fgetstr(input, sizeof(input), fd) >= 0) {
108 /* Discard comments */
109 if (strncmp(input+strspn(input, " "), "\\ ", 2) == 0)
113 if (input[0] == '@') {
118 if (input[0] == '-') {
120 flags |= SL_IGNOREERR;
123 /* Allocate script line structure and copy line, flags */
125 continue; /* ignore empty line, save memory */
126 sp = malloc(sizeof(struct includeline) + strlen(cp) + 1);
127 /* On malloc failure (it happens!), free as much as possible and exit */
129 while (script != NULL) {
131 script = script->next;
134 snprintf(command_errbuf, sizeof(command_errbuf),
135 "file '%s' line %d: memory allocation failure - aborting",
140 strcpy(sp->text, cp);
145 if (script == NULL) {
159 for (sp = script; sp != NULL; sp = sp->next) {
161 /* print if not being quiet */
162 if (!(sp->flags & SL_QUIET)) {
163 interp_emit_prompt();
164 printf("%s\n", sp->text);
167 /* Parse the command */
168 if (!parse(&argc, &argv, sp->text)) {
169 if ((argc > 0) && (interp_builtin_cmd(argc, argv) != 0)) {
171 printf("%s: %s\n", argv[0], command_errmsg);
172 if (!(sp->flags & SL_IGNOREERR)) {
180 printf("%s line %d: parse error\n", filename, sp->line);
188 while (script != NULL) {
190 script = script->next;