Initial import of binutils 2.22 on the new vendor branch
[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
34 #include <stand.h>
35 #include <string.h>
36 #include "bootstrap.h"
37 #include "dloader.h"
38
39 static void     prompt(void);
40 static int      iseol(char c);
41 static void     skipeol(int fd);
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             break;
73         }
74     }
75     if (cmd != NULL) {
76         if ((*cmdp)->c_cond || CurrentCondition != 0)
77                 result = (cmd)(argc, argv);
78         else
79                 result = CMD_OK;
80     } else {
81         command_errmsg = "unknown command";
82     }
83     return(result);
84 }
85
86 /*
87  * Interactive mode
88  */
89 void
90 interact(void)
91 {
92     char        input[256];                     /* big enough? */
93     int         argc;
94     char        **argv;
95
96     /*
97      * We may be booting from the boot partition, or we may be booting
98      * from the root partition with a /boot sub-directory.  If the latter
99      * chdir into /boot.  Ignore any error.  Only rel_open() uses the chdir
100      * info.
101      */
102     chdir("/boot");
103     setenv("base", DirBase, 1);
104
105     /*
106      * Read our default configuration
107      */
108     if (include("dloader.rc") != CMD_OK)
109         include("boot.conf");
110     printf("\n");
111     /*
112      * Before interacting, we might want to autoboot.
113      */
114     autoboot_maybe();
115
116     dloader_init_cmds();
117
118     /*
119      * Not autobooting, go manual
120      */
121     printf("\nType '?' for a list of commands, 'help' for more detailed help.\n");
122     if (getenv("prompt") == NULL)
123         setenv("prompt", "OK", 1);
124
125     for (;;) {
126         input[0] = '\0';
127         prompt();
128         ngets(input, sizeof(input));
129         if (!parse(&argc, &argv, input)) {
130             if (perform(argc, argv))
131                 printf("%s: %s\n", argv[0], command_errmsg);
132             free(argv);
133         } else {
134             printf("parse error\n");
135         }
136     }
137 }
138
139 /*
140  * Read commands from a file, then execute them.
141  *
142  * We store the commands in memory and close the source file so that the media
143  * holding it can safely go away while we are executing.
144  *
145  * Commands may be prefixed with '@' (so they aren't displayed) or '-' (so
146  * that the script won't stop if they fail).
147  */
148 COMMAND_SET(include, "include", "run commands from file", command_include);
149
150 static int
151 command_include(int argc, char *argv[])
152 {
153     int         i;
154     int         res;
155     char        **argvbuf;
156
157     /*
158      * Since argv is static, we need to save it here.
159      */
160     argvbuf = (char**) calloc((u_int)argc, sizeof(char*));
161     for (i = 0; i < argc; i++)
162         argvbuf[i] = strdup(argv[i]);
163
164     res=CMD_OK;
165     for (i = 1; (i < argc) && (res == CMD_OK); i++)
166         res = include(argvbuf[i]);
167
168     for (i = 0; i < argc; i++)
169         free(argvbuf[i]);
170     free(argvbuf);
171
172     return(res);
173 }
174
175 COMMAND_SET(optinclude, "optinclude",
176             "run commands from file; ignore exit status",
177             command_optinclude);
178
179 static int
180 command_optinclude(int argc, char *argv[])
181 {
182     int         i;
183     char        **argvbuf;
184
185     /*
186      * Since argv is static, we need to save it here.
187      */
188     argvbuf = (char**) calloc((u_int)argc, sizeof(char*));
189     for (i = 0; i < argc; i++)
190         argvbuf[i] = strdup(argv[i]);
191
192     for (i = 1; (i < argc); i++)
193         include(argvbuf[i]);
194
195     for (i = 0; i < argc; i++)
196         free(argvbuf[i]);
197     free(argvbuf);
198
199     return(CMD_OK);
200 }
201
202 struct includeline
203 {
204     char                *text;
205     int                 flags;
206     int                 line;
207 #define SL_QUIET        (1<<0)
208 #define SL_IGNOREERR    (1<<1)
209     struct includeline  *next;
210 };
211
212 int
213 include(const char *filename)
214 {
215     struct includeline  *script, *se, *sp;
216     char                input[256];                     /* big enough? */
217     int                 argc,res;
218     char                **argv, *cp;
219     int                 fd, flags, line;
220
221     if (((fd = rel_open(filename, NULL, O_RDONLY)) == -1)) {
222         command_errmsg = command_errbuf;
223         snprintf(command_errbuf, 256, "cannot find \"%s\"", filename);
224         return(CMD_ERROR);
225     }
226
227     /*
228      * Read the script into memory.
229      */
230     script = se = NULL;
231     line = 0;
232
233     while (fgets(input, sizeof(input), fd) != NULL) {
234         line++;
235         flags = 0;
236         if(strlen(input) == sizeof(input) - 1 &&
237             !iseol(input[sizeof(input) - 2])) {
238             printf("WARNING: %s: %s: Line too long: truncating; have:\n",
239                 __func__, filename);
240             printf("%s\n", input);
241             skipeol(fd);
242         }
243         /* Discard comments */
244         if (strncmp(input+strspn(input, " "), "\\ ", 2) == 0)
245             continue;
246         cp = input;
247         /* Echo? */
248         if (input[0] == '@') {
249             cp++;
250             flags |= SL_QUIET;
251         }
252         /* Error OK? */
253         if (input[0] == '-') {
254             cp++;
255             flags |= SL_IGNOREERR;
256         }
257         /* Allocate script line structure and copy line, flags */
258         sp = malloc(sizeof(struct includeline) + strlen(cp) + 1);
259         sp->text = (char *)sp + sizeof(struct includeline);
260         strcpy(sp->text, cp);
261         sp->flags = flags;
262         sp->line = line;
263         sp->next = NULL;
264
265         if (script == NULL) {
266             script = sp;
267         } else {
268             se->next = sp;
269         }
270         se = sp;
271     }
272     close(fd);
273
274     /*
275      * Execute the script
276      */
277     argv = NULL;
278     res = CMD_OK;
279     for (sp = script; sp != NULL; sp = sp->next) {
280
281 #if 0
282         /* print if not being quiet */
283         if (!(sp->flags & SL_QUIET)) {
284             prompt();
285             printf("%s\n", sp->text);
286         }
287 #endif
288
289         /* Parse the command */
290         if (!parse(&argc, &argv, sp->text)) {
291             if ((argc > 0) && (perform(argc, argv) != 0)) {
292                 /* normal command */
293                 printf("%s: %s\n", argv[0], command_errmsg);
294                 if (!(sp->flags & SL_IGNOREERR)) {
295                     res=CMD_ERROR;
296                     break;
297                 }
298             }
299             free(argv);
300             argv = NULL;
301         } else {
302             printf("%s line %d: parse error\n", filename, sp->line);
303             res=CMD_ERROR;
304             break;
305         }
306     }
307     if (argv != NULL)
308         free(argv);
309     while(script != NULL) {
310         se = script;
311         script = script->next;
312         free(se);
313     }
314     return(res);
315 }
316
317 /*
318  * Emit the current prompt; use the same syntax as the parser
319  * for embedding environment variables.
320  */
321 static void
322 prompt(void)
323 {
324     char        *pr, *p, *cp, *ev;
325
326     if ((cp = getenv("prompt")) == NULL)
327         cp = ">";
328     pr = p = strdup(cp);
329
330     while (*p != 0) {
331         if ((*p == '$') && (*(p+1) == '{')) {
332             for (cp = p + 2; (*cp != 0) && (*cp != '}'); cp++)
333                 ;
334             *cp = 0;
335             ev = getenv(p + 2);
336
337             if (ev != NULL)
338                 printf("%s", ev);
339             p = cp + 1;
340             continue;
341         }
342         putchar(*p++);
343     }
344     putchar(' ');
345     free(pr);
346 }
347
348 static int
349 iseol(char c)
350 {
351     return(c == '\n' || c == '\r');
352 }
353
354 static void
355 skipeol(int fd)
356 {
357     char c;
358
359     while (read(fd, &c, 1) == 1) {
360         if (iseol(c))
361             break;
362     }
363 }