f0a17f5a21a77d6cf277244619fcafad413308ad
[dragonfly.git] / sys / boot / common / do_dloader.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 #include "dloader.h"
40
41 static void     prompt(void);
42
43 /*
44  * Perform the command
45  */
46 int
47 perform(int argc, char *argv[])
48 {
49     int                         result;
50     struct bootblk_command      **cmdp;
51     bootblk_cmd_t               *cmd;
52     const char *av0;
53     char *ptr;
54
55     if (argc < 1)
56         return(CMD_OK);
57
58     av0 = argv[0];
59     if ((ptr = strchr(av0, '=')) != NULL)
60         av0 = "local";
61
62     /* set return defaults; a successful command will override these */
63     command_errmsg = command_errbuf;
64     strcpy(command_errbuf, "no error message");
65     cmd = NULL;
66     result = CMD_ERROR;
67
68     /* search the command set for the command */
69     SET_FOREACH(cmdp, Xcommand_set) {
70         if (((*cmdp)->c_name != NULL) && !strcmp(av0, (*cmdp)->c_name))
71             cmd = (*cmdp)->c_fn;
72     }
73     if (cmd != NULL) {
74         result = (cmd)(argc, argv);
75     } else {
76         command_errmsg = "unknown command";
77     }
78     return(result);
79 }
80
81 /*
82  * Interactive mode
83  */
84 void
85 interact(void)
86 {
87     char        input[256];                     /* big enough? */
88     int         argc;
89     char        **argv;
90
91     /*
92      * We may be booting from the boot partition, or we may be booting
93      * from the root partition with a /boot sub-directory.  If the latter
94      * chdir into /boot.  Ignore any error.  Only rel_open() uses the chdir
95      * info.
96      */
97     chdir("/boot");
98     setenv("base", DirBase, 1);
99
100     /*
101      * Read our default configuration
102      */
103     if (include("dloader.rc")!=CMD_OK)
104         include("boot.conf");
105     printf("\n");
106     /*
107      * Before interacting, we might want to autoboot.
108      */
109     autoboot_maybe();
110
111     dloader_init_cmds();
112
113     /*
114      * Not autobooting, go manual
115      */
116     printf("\nType '?' for a list of commands, 'help' for more detailed help.\n");
117     if (getenv("prompt") == NULL)
118         setenv("prompt", "${interpret}", 1);
119     if (getenv("interpret") == NULL)
120         setenv("interpret", "OK", 1);
121
122
123     for (;;) {
124         input[0] = '\0';
125         prompt();
126         ngets(input, sizeof(input));
127         if (!parse(&argc, &argv, input)) {
128             if (perform(argc, argv))
129                 printf("%s: %s\n", argv[0], command_errmsg);
130             free(argv);
131         } else {
132             printf("parse error\n");
133         }
134     }
135 }
136
137 /*
138  * Read commands from a file, then execute them.
139  *
140  * We store the commands in memory and close the source file so that the media
141  * holding it can safely go away while we are executing.
142  *
143  * Commands may be prefixed with '@' (so they aren't displayed) or '-' (so
144  * that the script won't stop if they fail).
145  */
146 COMMAND_SET(include, "include", "run commands from file", command_include);
147
148 static int
149 command_include(int argc, char *argv[])
150 {
151     int         i;
152     int         res;
153     char        **argvbuf;
154
155     /*
156      * Since argv is static, we need to save it here.
157      */
158     argvbuf = (char**) calloc((u_int)argc, sizeof(char*));
159     for (i = 0; i < argc; i++)
160         argvbuf[i] = strdup(argv[i]);
161
162     res=CMD_OK;
163     for (i = 1; (i < argc) && (res == CMD_OK); i++)
164         res = include(argvbuf[i]);
165
166     for (i = 0; i < argc; i++)
167         free(argvbuf[i]);
168     free(argvbuf);
169
170     return(res);
171 }
172 COMMAND_SET(optinclude, "optinclude", "run commands from file",
173             command_optinclude);
174
175 static int
176 command_optinclude(int argc, char *argv[])
177 {
178     int         i;
179     int         res;
180     char        **argvbuf;
181
182     /*
183      * Since argv is static, we need to save it here.
184      */
185     argvbuf = (char**) calloc((u_int)argc, sizeof(char*));
186     for (i = 0; i < argc; i++)
187         argvbuf[i] = strdup(argv[i]);
188
189     res=CMD_OK;
190     for (i = 1; (i < argc) && (res == CMD_OK); i++)
191         include(argvbuf[i]);
192
193     for (i = 0; i < argc; i++)
194         free(argvbuf[i]);
195     free(argvbuf);
196
197     return(res);
198 }
199
200 struct includeline
201 {
202     char                *text;
203     int                 flags;
204     int                 line;
205 #define SL_QUIET        (1<<0)
206 #define SL_IGNOREERR    (1<<1)
207     struct includeline  *next;
208 };
209
210 int
211 include(const char *filename)
212 {
213     struct includeline  *script, *se, *sp;
214     char                input[256];                     /* big enough? */
215     int                 argc,res;
216     char                **argv, *cp;
217     int                 fd, flags, line;
218
219     if (((fd = rel_open(filename, NULL, O_RDONLY)) == -1)) {
220         command_errmsg = command_errbuf;
221         snprintf(command_errbuf, 256, "cannot find \"%s\"", filename);
222         return(CMD_ERROR);
223     }
224
225     /*
226      * Read the script into memory.
227      */
228     script = se = NULL;
229     line = 0;
230
231     while (fgetstr(input, sizeof(input), fd) >= 0) {
232         line++;
233         flags = 0;
234         /* Discard comments */
235         if (strncmp(input+strspn(input, " "), "\\ ", 2) == 0)
236             continue;
237         cp = input;
238         /* Echo? */
239         if (input[0] == '@') {
240             cp++;
241             flags |= SL_QUIET;
242         }
243         /* Error OK? */
244         if (input[0] == '-') {
245             cp++;
246             flags |= SL_IGNOREERR;
247         }
248         /* Allocate script line structure and copy line, flags */
249         sp = malloc(sizeof(struct includeline) + strlen(cp) + 1);
250         sp->text = (char *)sp + sizeof(struct includeline);
251         strcpy(sp->text, cp);
252         sp->flags = flags;
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     argv = NULL;
269     res = CMD_OK;
270     for (sp = script; sp != NULL; sp = sp->next) {
271
272 #if 0
273         /* print if not being quiet */
274         if (!(sp->flags & SL_QUIET)) {
275             prompt();
276             printf("%s\n", sp->text);
277         }
278 #endif
279
280         /* Parse the command */
281         if (!parse(&argc, &argv, sp->text)) {
282             if ((argc > 0) && (perform(argc, argv) != 0)) {
283                 /* normal command */
284                 printf("%s: %s\n", argv[0], command_errmsg);
285                 if (!(sp->flags & SL_IGNOREERR)) {
286                     res=CMD_ERROR;
287                     break;
288                 }
289             }
290             free(argv);
291             argv = NULL;
292         } else {
293             printf("%s line %d: parse error\n", filename, sp->line);
294             res=CMD_ERROR;
295             break;
296         }
297     }
298     if (argv != NULL)
299         free(argv);
300     while(script != NULL) {
301         se = script;
302         script = script->next;
303         free(se);
304     }
305     return(res);
306 }
307
308 /*
309  * Emit the current prompt; use the same syntax as the parser
310  * for embedding environment variables.
311  */
312 static void
313 prompt(void)
314 {
315     char        *pr, *p, *cp, *ev;
316
317     if ((cp = getenv("prompt")) == NULL)
318         cp = ">";
319     pr = p = strdup(cp);
320
321     while (*p != 0) {
322         if ((*p == '$') && (*(p+1) == '{')) {
323             for (cp = p + 2; (*cp != 0) && (*cp != '}'); cp++)
324                 ;
325             *cp = 0;
326             ev = getenv(p + 2);
327
328             if (ev != NULL)
329                 printf("%s", ev);
330             p = cp + 1;
331             continue;
332         }
333         putchar(*p++);
334     }
335     putchar(' ');
336     free(pr);
337 }