interp.c revision 43614
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 *	$Id: interp.c,v 1.13 1999/01/22 23:50:13 msmith Exp $
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    setenv("prompt", "${currdev}>", 1);
115
116
117    for (;;) {
118	input[0] = '\0';
119	prompt();
120	ngets(input, sizeof(input));
121#ifdef BOOT_FORTH
122	bf_run(input);
123#else
124	if (!parse(&argc, &argv, input)) {
125	    if (perform(argc, argv))
126		printf("%s: %s\n", argv[0], command_errmsg);
127	    free(argv);
128	} else {
129	    printf("parse error\n");
130	}
131#endif
132    }
133}
134
135/*
136 * Read commands from a file, then execute them.
137 *
138 * We store the commands in memory and close the source file so that the media
139 * holding it can safely go away while we are executing.
140 *
141 * Commands may be prefixed with '@' (so they aren't displayed) or '-' (so
142 * that the script won't stop if they fail).
143 */
144COMMAND_SET(include, "include", "read commands from a file", command_include);
145
146static int
147command_include(int argc, char *argv[])
148{
149    int		i;
150    int		res;
151
152    res=CMD_OK;
153    for (i = 1; (i < argc) && (res == CMD_OK); i++)
154	res = include(argv[i]);
155    return(res);
156}
157
158struct includeline
159{
160    char		*text;
161    int			flags;
162    int			line;
163#define SL_QUIET	(1<<0)
164#define SL_IGNOREERR	(1<<1)
165    struct includeline	*next;
166};
167
168int
169include(char *filename)
170{
171    struct includeline	*script, *se, *sp;
172    char		input[256];			/* big enough? */
173#ifdef BOOT_FORTH
174    int			res;
175    char		*cp;
176    int			fd, line;
177#else
178    int			argc,res;
179    char		**argv, *cp;
180    int			fd, flags, line;
181#endif
182
183    if (((fd = open(filename, O_RDONLY)) == -1)) {
184	sprintf(command_errbuf,"can't open '%s': %s\n", filename, strerror(errno));
185	return(CMD_ERROR);
186    }
187
188    /*
189     * Read the script into memory.
190     */
191    script = se = NULL;
192    line = 0;
193
194    while (fgetstr(input, sizeof(input), fd) > 0) {
195	line++;
196#ifdef BOOT_FORTH
197	cp = input;
198#else
199	flags = 0;
200	/* Discard comments */
201	if (input[0] == '#')
202	    continue;
203	cp = input;
204	/* Echo? */
205	if (input[0] == '@') {
206	    cp++;
207	    flags |= SL_QUIET;
208	}
209	/* Error OK? */
210	if (input[0] == '-') {
211	    cp++;
212	    flags |= SL_IGNOREERR;
213	}
214#endif
215	/* Allocate script line structure and copy line, flags */
216	sp = malloc(sizeof(struct includeline) + strlen(cp) + 1);
217	sp->text = (char *)sp + sizeof(struct includeline);
218	strcpy(sp->text, cp);
219#ifndef BOOT_FORTH
220	sp->flags = flags;
221#endif
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#ifndef BOOT_FORTH
238    argv = NULL;
239#endif
240    res = CMD_OK;
241    for (sp = script; sp != NULL; sp = sp->next) {
242
243#ifdef BOOT_FORTH
244	res = bf_run(sp->text);
245	if (res != VM_OUTOFTEXT) {
246		sprintf(command_errbuf, "Error while including %s:\n%s", filename, sp->text);
247		res = CMD_ERROR;
248		break;
249	} else
250		res = CMD_OK;
251#else
252	/* print if not being quiet */
253	if (!(sp->flags & SL_QUIET)) {
254	    prompt();
255	    printf("%s\n", sp->text);
256	}
257
258	/* Parse the command */
259	if (!parse(&argc, &argv, sp->text)) {
260	    if ((argc > 0) && (perform(argc, argv) != 0)) {
261		/* normal command */
262		printf("%s: %s\n", argv[0], command_errmsg);
263		if (!(sp->flags & SL_IGNOREERR)) {
264		    res=CMD_ERROR;
265		    break;
266		}
267	    }
268	    free(argv);
269	    argv = NULL;
270	} else {
271	    printf("%s line %d: parse error\n", filename, sp->line);
272	    res=CMD_ERROR;
273	    break;
274	}
275#endif
276    }
277#ifndef BOOT_FORTH
278    if (argv != NULL)
279	free(argv);
280#endif
281    while(script != NULL) {
282	se = script;
283	script = script->next;
284	free(se);
285    }
286    return(res);
287}
288
289/*
290 * Emit the current prompt; use the same syntax as the parser
291 * for embedding environment variables.
292 */
293static void
294prompt(void)
295{
296    char	*p, *cp, *ev;
297
298    if ((cp = getenv("prompt")) == NULL)
299	cp = ">";
300    p = strdup(cp);
301
302    while (*p != 0) {
303	if ((*p == '$') && (*(p+1) == '{')) {
304	    for (cp = p + 2; (*cp != 0) && (*cp != '}'); cp++)
305		;
306	    *cp = 0;
307	    ev = getenv(p + 2);
308
309	    if (ev != NULL)
310		printf(ev);
311	    p = cp + 1;
312	    continue;
313	}
314	putchar(*p++);
315    }
316    putchar(' ');
317}
318