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"
42 perform(int argc, char *argv[])
45 struct bootblk_command **cmdp;
51 /* set return defaults; a successful command will override these */
52 command_errmsg = command_errbuf;
53 strcpy(command_errbuf, "no error message");
57 /* search the command set for the command */
58 SET_FOREACH(cmdp, Xcommand_set) {
59 if (((*cmdp)->c_name != NULL) && !strcmp(argv[0], (*cmdp)->c_name))
63 result = (cmd)(argc, argv);
65 command_errmsg = "unknown command";
74 /* Read our default configuration. */
75 interp_include("/boot/loader.rc");
79 interp_run(const char *input)
84 if (parse(&argc, &argv, input)) {
85 printf("parse error\n");
89 if (perform(argc, argv)) {
90 printf("%s: %s\n", argv[0], command_errmsg);
99 * Header prepended to each line. The text immediately follows the header.
100 * We try to make this short in order to save memory -- the loader has
101 * limited memory available, and some of the forth files are very long.
105 struct includeline *next;
108 #define SL_QUIET (1<<0)
109 #define SL_IGNOREERR (1<<1)
114 interp_include(const char *filename)
116 struct includeline *script, *se, *sp;
117 char input[256]; /* big enough? */
122 if (((fd = open(filename, O_RDONLY)) == -1)) {
123 snprintf(command_errbuf, sizeof(command_errbuf),
124 "can't open '%s': %s", filename, strerror(errno));
129 * Read the script into memory.
134 while (fgetstr(input, sizeof(input), fd) >= 0) {
137 /* Discard comments */
138 if (strncmp(input+strspn(input, " "), "\\ ", 2) == 0)
142 if (input[0] == '@') {
147 if (input[0] == '-') {
149 flags |= SL_IGNOREERR;
152 /* Allocate script line structure and copy line, flags */
154 continue; /* ignore empty line, save memory */
155 sp = malloc(sizeof(struct includeline) + strlen(cp) + 1);
156 /* On malloc failure (it happens!), free as much as possible and exit */
158 while (script != NULL) {
160 script = script->next;
163 snprintf(command_errbuf, sizeof(command_errbuf),
164 "file '%s' line %d: memory allocation failure - aborting",
169 strcpy(sp->text, cp);
174 if (script == NULL) {
188 for (sp = script; sp != NULL; sp = sp->next) {
190 /* print if not being quiet */
191 if (!(sp->flags & SL_QUIET)) {
192 interp_emit_prompt();
193 printf("%s\n", sp->text);
196 /* Parse the command */
197 if (!parse(&argc, &argv, sp->text)) {
198 if ((argc > 0) && (perform(argc, argv) != 0)) {
200 printf("%s: %s\n", argv[0], command_errmsg);
201 if (!(sp->flags & SL_IGNOREERR)) {
209 printf("%s line %d: parse error\n", filename, sp->line);
217 while (script != NULL) {
219 script = script->next;