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