boot system and buildkernel - Remove the thrice damned forth interpreter
[dragonfly.git] / sys / boot / common / interp.c
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  * $FreeBSD: src/sys/boot/common/interp.c,v 1.29 2003/08/25 23:30:41 obrien Exp $
27  * $DragonFly: src/sys/boot/common/interp.c,v 1.4 2008/09/02 17:21:12 dillon Exp $
28  */
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
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
93 interact(void)
94 {
95     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();
103 #endif
104
105     /*
106      * We may be booting from the boot partition, or we may be booting
107      * from the root partition with a /boot sub-directory.  If the latter
108      * chdir into /boot.  Ignore any error.  Only rel_open() uses the chdir
109      * info.
110      */
111     chdir("/boot");
112
113     /*
114      * Read our default configuration
115      */
116     if(include("loader.rc")!=CMD_OK)
117         include("boot.conf");
118     printf("\n");
119     /*
120      * Before interacting, we might want to autoboot.
121      */
122     autoboot_maybe();
123     
124     /*
125      * Not autobooting, go manual
126      */
127     printf("\nType '?' for a list of commands, 'help' for more detailed help.\n");
128     if (getenv("prompt") == NULL)
129         setenv("prompt", "${interpret}", 1);
130     if (getenv("interpret") == NULL)
131         setenv("interpret", "OK", 1);
132     
133
134     for (;;) {
135         input[0] = '\0';
136         prompt();
137         ngets(input, sizeof(input));
138 #ifdef BOOT_FORTH
139         bf_vm->sourceID.i = 0;
140         bf_run(input);
141 #else
142         if (!parse(&argc, &argv, input)) {
143             if (perform(argc, argv))
144                 printf("%s: %s\n", argv[0], command_errmsg);
145             free(argv);
146         } else {
147             printf("parse error\n");
148         }
149 #endif
150     }
151 }
152
153 /*
154  * Read commands from a file, then execute them.
155  *
156  * We store the commands in memory and close the source file so that the media
157  * holding it can safely go away while we are executing.
158  *
159  * Commands may be prefixed with '@' (so they aren't displayed) or '-' (so
160  * that the script won't stop if they fail).
161  */
162 COMMAND_SET(include, "include", "read commands from a file", command_include);
163
164 static int
165 command_include(int argc, char *argv[])
166 {
167     int         i;
168     int         res;
169     char        **argvbuf;
170
171     /* 
172      * Since argv is static, we need to save it here.
173      */
174     argvbuf = (char**) calloc((u_int)argc, sizeof(char*));
175     for (i = 0; i < argc; i++)
176         argvbuf[i] = strdup(argv[i]);
177
178     res=CMD_OK;
179     for (i = 1; (i < argc) && (res == CMD_OK); i++)
180         res = include(argvbuf[i]);
181
182     for (i = 0; i < argc; i++)
183         free(argvbuf[i]);
184     free(argvbuf);
185
186     return(res);
187 }
188
189 struct includeline 
190 {
191     char                *text;
192     int                 flags;
193     int                 line;
194 #define SL_QUIET        (1<<0)
195 #define SL_IGNOREERR    (1<<1)
196     struct includeline  *next;
197 };
198
199 int
200 include(const char *filename)
201 {
202     struct includeline  *script, *se, *sp;
203     char                input[256];                     /* big enough? */
204 #ifdef BOOT_FORTH
205     int                 res;
206     char                *cp;
207     int                 prevsrcid, fd, line;
208 #else
209     int                 argc,res;
210     char                **argv, *cp;
211     int                 fd, flags, line;
212 #endif
213
214     if (((fd = rel_open(filename, O_RDONLY)) == -1)) {
215         sprintf(command_errbuf,"can't open '%s': %s\n", filename, strerror(errno));
216         return(CMD_ERROR);
217     }
218
219     /*
220      * Read the script into memory.
221      */
222     script = se = NULL;
223     line = 0;
224         
225     while (fgetstr(input, sizeof(input), fd) >= 0) {
226         line++;
227 #ifdef BOOT_FORTH
228         cp = input;
229 #else
230         flags = 0;
231         /* Discard comments */
232         if (strncmp(input+strspn(input, " "), "\\ ", 2) == 0)
233             continue;
234         cp = input;
235         /* Echo? */
236         if (input[0] == '@') {
237             cp++;
238             flags |= SL_QUIET;
239         }
240         /* Error OK? */
241         if (input[0] == '-') {
242             cp++;
243             flags |= SL_IGNOREERR;
244         }
245 #endif
246         /* Allocate script line structure and copy line, flags */
247         sp = malloc(sizeof(struct includeline) + strlen(cp) + 1);
248         sp->text = (char *)sp + sizeof(struct includeline);
249         strcpy(sp->text, cp);
250 #ifndef BOOT_FORTH
251         sp->flags = flags;
252 #endif
253         sp->line = line;
254         sp->next = NULL;
255             
256         if (script == NULL) {
257             script = sp;
258         } else {
259             se->next = sp;
260         }
261         se = sp;
262     }
263     close(fd);
264     
265     /*
266      * Execute the script
267      */
268 #ifndef BOOT_FORTH
269     argv = NULL;
270 #else
271     prevsrcid = bf_vm->sourceID.i;
272     bf_vm->sourceID.i = fd;
273 #endif
274     res = CMD_OK;
275     for (sp = script; sp != NULL; sp = sp->next) {
276         
277 #ifdef BOOT_FORTH
278         res = bf_run(sp->text);
279         if (res != VM_OUTOFTEXT) {
280                 sprintf(command_errbuf, "Error while including %s, in the line:\n%s", filename, sp->text);
281                 res = CMD_ERROR;
282                 break;
283         } else
284                 res = CMD_OK;
285 #else
286         /* print if not being quiet */
287         if (!(sp->flags & SL_QUIET)) {
288             prompt();
289             printf("%s\n", sp->text);
290         }
291
292         /* Parse the command */
293         if (!parse(&argc, &argv, sp->text)) {
294             if ((argc > 0) && (perform(argc, argv) != 0)) {
295                 /* normal command */
296                 printf("%s: %s\n", argv[0], command_errmsg);
297                 if (!(sp->flags & SL_IGNOREERR)) {
298                     res=CMD_ERROR;
299                     break;
300                 }
301             }
302             free(argv);
303             argv = NULL;
304         } else {
305             printf("%s line %d: parse error\n", filename, sp->line);
306             res=CMD_ERROR;
307             break;
308         }
309 #endif
310     }
311 #ifndef BOOT_FORTH
312     if (argv != NULL)
313         free(argv);
314 #else
315     bf_vm->sourceID.i = prevsrcid;
316 #endif
317     while(script != NULL) {
318         se = script;
319         script = script->next;
320         free(se);
321     }
322     return(res);
323 }
324
325 /*
326  * Emit the current prompt; use the same syntax as the parser
327  * for embedding environment variables.
328  */
329 static void
330 prompt(void) 
331 {
332     char        *pr, *p, *cp, *ev;
333     
334     if ((cp = getenv("prompt")) == NULL)
335         cp = ">";
336     pr = p = strdup(cp);
337
338     while (*p != 0) {
339         if ((*p == '$') && (*(p+1) == '{')) {
340             for (cp = p + 2; (*cp != 0) && (*cp != '}'); cp++)
341                 ;
342             *cp = 0;
343             ev = getenv(p + 2);
344             
345             if (ev != NULL)
346                 printf("%s", ev);
347             p = cp + 1;
348             continue;
349         }
350         putchar(*p++);
351     }
352     putchar(' ');
353     free(pr);
354 }