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
28 * Simple commandline interpreter, toplevel and misc.
33 #include "bootstrap.h"
35 INTERP_DEFINE("simp");
41 setenv("script.lang", "simple", 1);
42 /* Read our default configuration. */
43 interp_include("/boot/loader.rc");
47 interp_run(const char *input)
52 if (parse(&argc, &argv, input)) {
53 printf("parse error\n");
57 if (interp_builtin_cmd(argc, argv)) {
58 printf("%s: %s\n", argv[0], command_errmsg);
67 * Header prepended to each line. The text immediately follows the header.
68 * We try to make this short in order to save memory -- the loader has
69 * limited memory available, and some of the forth files are very long.
73 struct includeline *next;
76 #define SL_QUIET (1<<0)
77 #define SL_IGNOREERR (1<<1)
82 interp_include(const char *filename)
84 struct includeline *script, *se, *sp;
85 char input[256]; /* big enough? */
90 if (((fd = open(filename, O_RDONLY)) == -1)) {
91 snprintf(command_errbuf, sizeof(command_errbuf),
92 "can't open '%s': %s", filename, strerror(errno));
96 #ifdef LOADER_VERIEXEC
97 if (verify_file(fd, filename, 0, VE_GUESS, __func__) < 0) {
99 sprintf(command_errbuf,"can't verify '%s'", filename);
105 * Read the script into memory.
110 while (fgetstr(input, sizeof(input), fd) >= 0) {
113 /* Discard comments */
114 if (strncmp(input+strspn(input, " "), "\\", 1) == 0)
118 if (input[0] == '@') {
123 if (input[0] == '-') {
125 flags |= SL_IGNOREERR;
128 /* Allocate script line structure and copy line, flags */
130 continue; /* ignore empty line, save memory */
131 sp = malloc(sizeof(struct includeline) + strlen(cp) + 1);
132 /* On malloc failure (it happens!), free as much as possible and exit */
134 while (script != NULL) {
136 script = script->next;
139 snprintf(command_errbuf, sizeof(command_errbuf),
140 "file '%s' line %d: memory allocation failure - aborting",
145 strcpy(sp->text, cp);
150 if (script == NULL) {
164 for (sp = script; sp != NULL; sp = sp->next) {
166 /* print if not being quiet */
167 if (!(sp->flags & SL_QUIET)) {
168 interp_emit_prompt();
169 printf("%s\n", sp->text);
172 /* Parse the command */
173 if (!parse(&argc, &argv, sp->text)) {
174 if ((argc > 0) && (interp_builtin_cmd(argc, argv) != 0)) {
176 printf("%s: %s\n", argv[0], command_errmsg);
177 if (!(sp->flags & SL_IGNOREERR)) {
185 printf("%s line %d: parse error\n", filename, sp->line);
193 while (script != NULL) {
195 script = script->next;
202 * There's no graphics commands for the simple interpreter.