1 /*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 /*
31 * Simple commandline interpreter, toplevel and misc.
32 *
33 * XXX may be obsoleted by BootFORTH or some other, better, interpreter.
34 */
35
36 #include <stand.h>
37 #include <string.h>
38 #include "bootstrap.h"
39
40 #ifdef BOOT_FORTH
41 #include "ficl.h"
42 #define RETURN(x) stackPushINT(bf_vm->pStack,!x); return(x)
43
44 extern FICL_VM *bf_vm;
45 #else
46 #define RETURN(x) return(x)
47 #endif
48
49 #define MAXARGS 20 /* maximum number of arguments allowed */
50
51 static void prompt(void);
52
53 #ifndef BOOT_FORTH
54 static int perform(int argc, char *argv[]);
55
56 /*
57 * Perform the command
58 */
59 int
perform(int argc,char * argv[])60 perform(int argc, char *argv[])
61 {
62 int result;
63 struct bootblk_command **cmdp;
64 bootblk_cmd_t *cmd;
65
66 if (argc < 1)
67 return(CMD_OK);
68
69 /* set return defaults; a successful command will override these */
70 command_errmsg = command_errbuf;
71 strcpy(command_errbuf, "no error message");
72 cmd = NULL;
73 result = CMD_ERROR;
74
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))
78 cmd = (*cmdp)->c_fn;
79 }
80 if (cmd != NULL) {
81 result = (cmd)(argc, argv);
82 } else {
83 command_errmsg = "unknown command";
84 }
85 RETURN(result);
86 }
87 #endif /* ! BOOT_FORTH */
88
89 /*
90 * Interactive mode
91 */
92 void
interact(const char * rc)93 interact(const char *rc)
94 {
95 static char input[256]; /* big enough? */
96 #ifndef BOOT_FORTH
97 int argc;
98 char **argv;
99 #endif
100
101 #ifdef BOOT_FORTH
102 bf_init((rc) ? "" : NULL);
103 #endif
104
105 if (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')
110 include(rc);
111
112 printf("\n");
113
114 /*
115 * Before interacting, we might want to autoboot.
116 */
117 autoboot_maybe();
118
119 /*
120 * Not autobooting, go manual
121 */
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);
127
128
129 for (;;) {
130 input[0] = '\0';
131 prompt();
132 ngets(input, sizeof(input));
133 #ifdef BOOT_FORTH
134 bf_vm->sourceID.i = 0;
135 bf_run(input);
136 #else
137 if (!parse(&argc, &argv, input)) {
138 if (perform(argc, argv))
139 printf("%s: %s\n", argv[0], command_errmsg);
140 free(argv);
141 } else {
142 printf("parse error\n");
143 }
144 #endif
145 }
146 }
147
148 /*
149 * Read commands from a file, then execute them.
150 *
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.
153 *
154 * Commands may be prefixed with '@' (so they aren't displayed) or '-' (so
155 * that the script won't stop if they fail).
156 */
157 COMMAND_SET(include, "include", "read commands from a file", command_include);
158
159 static int
command_include(int argc,char * argv[])160 command_include(int argc, char *argv[])
161 {
162 int i;
163 int res;
164 char **argvbuf;
165
166 /*
167 * Since argv is static, we need to save it here.
168 */
169 argvbuf = (char**) calloc((u_int)argc, sizeof(char*));
170 for (i = 0; i < argc; i++)
171 argvbuf[i] = strdup(argv[i]);
172
173 res=CMD_OK;
174 for (i = 1; (i < argc) && (res == CMD_OK); i++)
175 res = include(argvbuf[i]);
176
177 for (i = 0; i < argc; i++)
178 free(argvbuf[i]);
179 free(argvbuf);
180
181 return(res);
182 }
183
184 /*
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.
188 */
189 struct includeline
190 {
191 struct includeline *next;
192 #ifndef BOOT_FORTH
193 int flags;
194 int line;
195 #define SL_QUIET (1<<0)
196 #define SL_IGNOREERR (1<<1)
197 #endif
198 char text[0];
199 };
200
201 int
include(const char * filename)202 include(const char *filename)
203 {
204 struct includeline *script, *se, *sp;
205 char input[256]; /* big enough? */
206 #ifdef BOOT_FORTH
207 int res;
208 char *cp;
209 int prevsrcid, fd, line;
210 #else
211 int argc,res;
212 char **argv, *cp;
213 int fd, flags, line;
214 #endif
215
216 if (((fd = open(filename, O_RDONLY)) == -1)) {
217 sprintf(command_errbuf,"can't open '%s': %s", filename, strerror(errno));
218 return(CMD_ERROR);
219 }
220
221 /*
222 * Read the script into memory.
223 */
224 script = se = NULL;
225 line = 0;
226
227 while (fgetstr(input, sizeof(input), fd) >= 0) {
228 line++;
229 #ifdef BOOT_FORTH
230 cp = input;
231 #else
232 flags = 0;
233 /* Discard comments */
234 if (strncmp(input+strspn(input, " "), "\\ ", 2) == 0)
235 continue;
236 cp = input;
237 /* Echo? */
238 if (input[0] == '@') {
239 cp++;
240 flags |= SL_QUIET;
241 }
242 /* Error OK? */
243 if (input[0] == '-') {
244 cp++;
245 flags |= SL_IGNOREERR;
246 }
247 #endif
248 /* Allocate script line structure and copy line, flags */
249 if (*cp == '\0')
250 continue; /* ignore empty line, save memory */
251 sp = malloc(sizeof(struct includeline) + strlen(cp) + 1);
252 /* On malloc failure (it happens!), free as much as possible and exit */
253 if (sp == NULL) {
254 while (script != NULL) {
255 se = script;
256 script = script->next;
257 free(se);
258 }
259 sprintf(command_errbuf, "file '%s' line %d: memory allocation "
260 "failure - aborting", filename, line);
261 return (CMD_ERROR);
262 }
263 strcpy(sp->text, cp);
264 #ifndef BOOT_FORTH
265 sp->flags = flags;
266 sp->line = line;
267 #endif
268 sp->next = NULL;
269
270 if (script == NULL) {
271 script = sp;
272 } else {
273 se->next = sp;
274 }
275 se = sp;
276 }
277 close(fd);
278
279 /*
280 * Execute the script
281 */
282 #ifndef BOOT_FORTH
283 argv = NULL;
284 #else
285 prevsrcid = bf_vm->sourceID.i;
286 bf_vm->sourceID.i = fd;
287 #endif
288 res = CMD_OK;
289 for (sp = script; sp != NULL; sp = sp->next) {
290
291 #ifdef BOOT_FORTH
292 res = bf_run(sp->text);
293 if (res != VM_OUTOFTEXT) {
294 sprintf(command_errbuf, "Error while including %s, in the line:\n%s", filename, sp->text);
295 res = CMD_ERROR;
296 break;
297 } else
298 res = CMD_OK;
299 #else
300 /* print if not being quiet */
301 if (!(sp->flags & SL_QUIET)) {
302 prompt();
303 printf("%s\n", sp->text);
304 }
305
306 /* Parse the command */
307 if (!parse(&argc, &argv, sp->text)) {
308 if ((argc > 0) && (perform(argc, argv) != 0)) {
309 /* normal command */
310 printf("%s: %s\n", argv[0], command_errmsg);
311 if (!(sp->flags & SL_IGNOREERR)) {
312 res=CMD_ERROR;
313 break;
314 }
315 }
316 free(argv);
317 argv = NULL;
318 } else {
319 printf("%s line %d: parse error\n", filename, sp->line);
320 res=CMD_ERROR;
321 break;
322 }
323 #endif
324 }
325 #ifndef BOOT_FORTH
326 if (argv != NULL)
327 free(argv);
328 #else
329 bf_vm->sourceID.i = prevsrcid;
330 #endif
331 while(script != NULL) {
332 se = script;
333 script = script->next;
334 free(se);
335 }
336 return(res);
337 }
338
339 /*
340 * Emit the current prompt; use the same syntax as the parser
341 * for embedding environment variables.
342 */
343 static void
prompt(void)344 prompt(void)
345 {
346 char *pr, *p, *cp, *ev;
347
348 if ((cp = getenv("prompt")) == NULL)
349 cp = ">";
350 pr = p = strdup(cp);
351
352 while (*p != 0) {
353 if ((*p == '$') && (*(p+1) == '{')) {
354 for (cp = p + 2; (*cp != 0) && (*cp != '}'); cp++)
355 ;
356 *cp = 0;
357 ev = getenv(p + 2);
358
359 if (ev != NULL)
360 printf("%s", ev);
361 p = cp + 1;
362 continue;
363 }
364 putchar(*p++);
365 }
366 putchar(' ');
367 free(pr);
368 }
369