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