interp.c revision 1.1.2.4 1 1.1.2.4 yamt /* $NetBSD: interp.c,v 1.1.2.4 2006/08/11 15:42:00 yamt Exp $ */
2 1.1.2.2 yamt
3 1.1.2.2 yamt /*-
4 1.1.2.2 yamt * Copyright (c) 1998 Michael Smith <msmith (at) freebsd.org>
5 1.1.2.2 yamt * All rights reserved.
6 1.1.2.2 yamt *
7 1.1.2.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 yamt * modification, are permitted provided that the following conditions
9 1.1.2.2 yamt * are met:
10 1.1.2.2 yamt * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 yamt * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 yamt * documentation and/or other materials provided with the distribution.
15 1.1.2.2 yamt *
16 1.1.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1.2.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1.2.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1.2.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1.2.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1.2.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1.2.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1.2.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1.2.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.2.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.2.2 yamt * SUCH DAMAGE.
27 1.1.2.2 yamt */
28 1.1.2.2 yamt
29 1.1.2.2 yamt #include <sys/cdefs.h>
30 1.1.2.3 yamt /* __FBSDID("$FreeBSD: src/sys/boot/common/interp.c,v 1.29 2003/08/25 23:30:41 obrien Exp $"); */
31 1.1.2.2 yamt
32 1.1.2.2 yamt /*
33 1.1.2.2 yamt * Simple commandline interpreter, toplevel and misc.
34 1.1.2.2 yamt *
35 1.1.2.2 yamt * XXX may be obsoleted by BootFORTH or some other, better, interpreter.
36 1.1.2.2 yamt */
37 1.1.2.2 yamt
38 1.1.2.2 yamt #include <lib/libsa/stand.h>
39 1.1.2.2 yamt
40 1.1.2.2 yamt #include "bootstrap.h"
41 1.1.2.2 yamt
42 1.1.2.2 yamt #ifdef BOOT_FORTH
43 1.1.2.2 yamt #include "ficl.h"
44 1.1.2.2 yamt #define RETURN(x) stackPushINT(bf_vm->pStack,!x); return(x)
45 1.1.2.2 yamt
46 1.1.2.2 yamt extern FICL_VM *bf_vm;
47 1.1.2.2 yamt #else
48 1.1.2.2 yamt #define RETURN(x) return(x)
49 1.1.2.2 yamt #endif
50 1.1.2.2 yamt
51 1.1.2.2 yamt #define MAXARGS 20 /* maximum number of arguments allowed */
52 1.1.2.2 yamt
53 1.1.2.2 yamt static void prompt(void);
54 1.1.2.2 yamt
55 1.1.2.2 yamt #ifndef BOOT_FORTH
56 1.1.2.2 yamt static int perform(int argc, char *argv[]);
57 1.1.2.2 yamt
58 1.1.2.2 yamt /*
59 1.1.2.2 yamt * Perform the command
60 1.1.2.2 yamt */
61 1.1.2.2 yamt int
62 1.1.2.2 yamt perform(int argc, char *argv[])
63 1.1.2.2 yamt {
64 1.1.2.2 yamt int result;
65 1.1.2.2 yamt struct bootblk_command *cmdp;
66 1.1.2.2 yamt bootblk_cmd_t *cmd;
67 1.1.2.2 yamt
68 1.1.2.2 yamt int i;
69 1.1.2.2 yamt
70 1.1.2.2 yamt if (argc < 1)
71 1.1.2.2 yamt return(CMD_OK);
72 1.1.2.2 yamt
73 1.1.2.2 yamt /* set return defaults; a successful command will override these */
74 1.1.2.2 yamt command_errmsg = command_errbuf;
75 1.1.2.2 yamt strcpy(command_errbuf, "no error message");
76 1.1.2.2 yamt cmd = NULL;
77 1.1.2.2 yamt result = CMD_ERROR;
78 1.1.2.2 yamt
79 1.1.2.2 yamt /* search the command set for the command */
80 1.1.2.2 yamt for (i = 0, cmdp = commands; (cmdp->c_name != NULL) && (cmdp->c_desc != NULL ); i++, cmdp = commands + i) {
81 1.1.2.2 yamt if ((cmdp->c_name != NULL) && !strcmp(argv[0], cmdp->c_name))
82 1.1.2.2 yamt cmd = cmdp->c_fn;
83 1.1.2.2 yamt }
84 1.1.2.2 yamt if (cmd != NULL) {
85 1.1.2.2 yamt result = (cmd)(argc, argv);
86 1.1.2.2 yamt } else {
87 1.1.2.2 yamt command_errmsg = "unknown command";
88 1.1.2.2 yamt }
89 1.1.2.2 yamt RETURN(result);
90 1.1.2.2 yamt }
91 1.1.2.2 yamt #endif /* ! BOOT_FORTH */
92 1.1.2.2 yamt
93 1.1.2.2 yamt /*
94 1.1.2.2 yamt * Interactive mode
95 1.1.2.2 yamt */
96 1.1.2.2 yamt void
97 1.1.2.2 yamt interact(void)
98 1.1.2.2 yamt {
99 1.1.2.2 yamt char input[256]; /* big enough? */
100 1.1.2.2 yamt #ifndef BOOT_FORTH
101 1.1.2.2 yamt int argc;
102 1.1.2.2 yamt char **argv;
103 1.1.2.2 yamt #endif
104 1.1.2.2 yamt
105 1.1.2.2 yamt #ifdef BOOT_FORTH
106 1.1.2.2 yamt bf_init();
107 1.1.2.2 yamt #endif
108 1.1.2.2 yamt
109 1.1.2.2 yamt /*
110 1.1.2.2 yamt * Read our default configuration
111 1.1.2.2 yamt */
112 1.1.2.2 yamt if(include("/boot/loader.rc")!=CMD_OK)
113 1.1.2.2 yamt include("/boot/boot.conf");
114 1.1.2.2 yamt printf("\n");
115 1.1.2.4 yamt
116 1.1.2.2 yamt /*
117 1.1.2.4 yamt * XXX: Before interacting, we might want to autoboot.
118 1.1.2.2 yamt */
119 1.1.2.4 yamt
120 1.1.2.2 yamt
121 1.1.2.2 yamt /*
122 1.1.2.2 yamt * Not autobooting, go manual
123 1.1.2.2 yamt */
124 1.1.2.2 yamt printf("\nType '?' for a list of commands, 'help' for more detailed help.\n");
125 1.1.2.2 yamt if (getenv("prompt") == NULL)
126 1.1.2.2 yamt setenv("prompt", "${interpret}", 1);
127 1.1.2.2 yamt if (getenv("interpret") == NULL)
128 1.1.2.2 yamt setenv("interpret", "OK", 1);
129 1.1.2.2 yamt
130 1.1.2.2 yamt
131 1.1.2.2 yamt for (;;) {
132 1.1.2.2 yamt input[0] = '\0';
133 1.1.2.2 yamt prompt();
134 1.1.2.2 yamt ngets(input, sizeof(input));
135 1.1.2.2 yamt #ifdef BOOT_FORTH
136 1.1.2.2 yamt bf_vm->sourceID.i = 0;
137 1.1.2.2 yamt bf_run(input);
138 1.1.2.2 yamt #else
139 1.1.2.2 yamt if (!parse(&argc, &argv, input)) {
140 1.1.2.2 yamt if (perform(argc, argv))
141 1.1.2.2 yamt printf("%s: %s\n", argv[0], command_errmsg);
142 1.1.2.2 yamt free(argv);
143 1.1.2.2 yamt } else {
144 1.1.2.2 yamt printf("parse error\n");
145 1.1.2.2 yamt }
146 1.1.2.2 yamt #endif
147 1.1.2.2 yamt }
148 1.1.2.2 yamt }
149 1.1.2.2 yamt
150 1.1.2.2 yamt /*
151 1.1.2.2 yamt * Read commands from a file, then execute them.
152 1.1.2.2 yamt *
153 1.1.2.2 yamt * We store the commands in memory and close the source file so that the media
154 1.1.2.2 yamt * holding it can safely go away while we are executing.
155 1.1.2.2 yamt *
156 1.1.2.2 yamt * Commands may be prefixed with '@' (so they aren't displayed) or '-' (so
157 1.1.2.2 yamt * that the script won't stop if they fail).
158 1.1.2.2 yamt */
159 1.1.2.2 yamt
160 1.1.2.2 yamt int
161 1.1.2.2 yamt command_include(int argc, char *argv[])
162 1.1.2.2 yamt {
163 1.1.2.2 yamt int i;
164 1.1.2.2 yamt int res;
165 1.1.2.2 yamt char **argvbuf;
166 1.1.2.2 yamt
167 1.1.2.2 yamt /*
168 1.1.2.2 yamt * Since argv is static, we need to save it here.
169 1.1.2.2 yamt */
170 1.1.2.2 yamt argvbuf = (char**) calloc((u_int)argc, sizeof(char*));
171 1.1.2.2 yamt for (i = 0; i < argc; i++)
172 1.1.2.2 yamt argvbuf[i] = strdup(argv[i]);
173 1.1.2.2 yamt
174 1.1.2.2 yamt res=CMD_OK;
175 1.1.2.2 yamt for (i = 1; (i < argc) && (res == CMD_OK); i++)
176 1.1.2.2 yamt res = include(argvbuf[i]);
177 1.1.2.2 yamt
178 1.1.2.2 yamt for (i = 0; i < argc; i++)
179 1.1.2.2 yamt free(argvbuf[i]);
180 1.1.2.2 yamt free(argvbuf);
181 1.1.2.2 yamt
182 1.1.2.2 yamt return(res);
183 1.1.2.2 yamt }
184 1.1.2.2 yamt
185 1.1.2.2 yamt struct includeline
186 1.1.2.2 yamt {
187 1.1.2.2 yamt char *text;
188 1.1.2.2 yamt int flags;
189 1.1.2.2 yamt int line;
190 1.1.2.2 yamt #define SL_QUIET (1<<0)
191 1.1.2.2 yamt #define SL_IGNOREERR (1<<1)
192 1.1.2.2 yamt struct includeline *next;
193 1.1.2.2 yamt };
194 1.1.2.2 yamt
195 1.1.2.2 yamt int
196 1.1.2.2 yamt include(const char *filename)
197 1.1.2.2 yamt {
198 1.1.2.2 yamt struct includeline *script, *se, *sp;
199 1.1.2.2 yamt char input[256]; /* big enough? */
200 1.1.2.2 yamt #ifdef BOOT_FORTH
201 1.1.2.2 yamt int res;
202 1.1.2.2 yamt char *cp;
203 1.1.2.2 yamt int prevsrcid, fd, line;
204 1.1.2.2 yamt #else
205 1.1.2.2 yamt int argc,res;
206 1.1.2.2 yamt char **argv, *cp;
207 1.1.2.2 yamt int fd, flags, line;
208 1.1.2.2 yamt #endif
209 1.1.2.2 yamt
210 1.1.2.2 yamt if (((fd = open(filename, O_RDONLY)) == -1)) {
211 1.1.2.2 yamt sprintf(command_errbuf,"can't open '%s': %s\n", filename, strerror(errno));
212 1.1.2.2 yamt return(CMD_ERROR);
213 1.1.2.2 yamt }
214 1.1.2.2 yamt
215 1.1.2.2 yamt /*
216 1.1.2.2 yamt * Read the script into memory.
217 1.1.2.2 yamt */
218 1.1.2.2 yamt script = se = NULL;
219 1.1.2.2 yamt line = 0;
220 1.1.2.2 yamt
221 1.1.2.2 yamt while (fgetstr(input, sizeof(input), fd) >= 0) {
222 1.1.2.2 yamt line++;
223 1.1.2.2 yamt #ifdef BOOT_FORTH
224 1.1.2.2 yamt cp = input;
225 1.1.2.2 yamt #else
226 1.1.2.2 yamt flags = 0;
227 1.1.2.2 yamt /* Discard comments */
228 1.1.2.2 yamt if (strncmp(input+strspn(input, " "), "\\ ", 2) == 0)
229 1.1.2.2 yamt continue;
230 1.1.2.2 yamt cp = input;
231 1.1.2.2 yamt /* Echo? */
232 1.1.2.2 yamt if (input[0] == '@') {
233 1.1.2.2 yamt cp++;
234 1.1.2.2 yamt flags |= SL_QUIET;
235 1.1.2.2 yamt }
236 1.1.2.2 yamt /* Error OK? */
237 1.1.2.2 yamt if (input[0] == '-') {
238 1.1.2.2 yamt cp++;
239 1.1.2.2 yamt flags |= SL_IGNOREERR;
240 1.1.2.2 yamt }
241 1.1.2.2 yamt #endif
242 1.1.2.2 yamt /* Allocate script line structure and copy line, flags */
243 1.1.2.2 yamt sp = alloc(sizeof(struct includeline) + strlen(cp) + 1);
244 1.1.2.2 yamt sp->text = (char *)sp + sizeof(struct includeline);
245 1.1.2.2 yamt strcpy(sp->text, cp);
246 1.1.2.2 yamt #ifndef BOOT_FORTH
247 1.1.2.2 yamt sp->flags = flags;
248 1.1.2.2 yamt #endif
249 1.1.2.2 yamt sp->line = line;
250 1.1.2.2 yamt sp->next = NULL;
251 1.1.2.2 yamt
252 1.1.2.2 yamt if (script == NULL) {
253 1.1.2.2 yamt script = sp;
254 1.1.2.2 yamt } else {
255 1.1.2.2 yamt se->next = sp;
256 1.1.2.2 yamt }
257 1.1.2.2 yamt se = sp;
258 1.1.2.2 yamt }
259 1.1.2.2 yamt close(fd);
260 1.1.2.2 yamt
261 1.1.2.2 yamt /*
262 1.1.2.2 yamt * Execute the script
263 1.1.2.2 yamt */
264 1.1.2.2 yamt #ifndef BOOT_FORTH
265 1.1.2.2 yamt argv = NULL;
266 1.1.2.2 yamt #else
267 1.1.2.2 yamt prevsrcid = bf_vm->sourceID.i;
268 1.1.2.2 yamt bf_vm->sourceID.i = fd;
269 1.1.2.2 yamt #endif
270 1.1.2.2 yamt res = CMD_OK;
271 1.1.2.2 yamt for (sp = script; sp != NULL; sp = sp->next) {
272 1.1.2.2 yamt
273 1.1.2.2 yamt #ifdef BOOT_FORTH
274 1.1.2.2 yamt res = bf_run(sp->text);
275 1.1.2.2 yamt if (res != VM_OUTOFTEXT) {
276 1.1.2.2 yamt sprintf(command_errbuf, "Error while including %s, in the line:\n%s", filename, sp->text);
277 1.1.2.2 yamt res = CMD_ERROR;
278 1.1.2.2 yamt break;
279 1.1.2.2 yamt } else
280 1.1.2.2 yamt res = CMD_OK;
281 1.1.2.2 yamt #else
282 1.1.2.2 yamt /* print if not being quiet */
283 1.1.2.2 yamt if (!(sp->flags & SL_QUIET)) {
284 1.1.2.2 yamt prompt();
285 1.1.2.2 yamt printf("%s\n", sp->text);
286 1.1.2.2 yamt }
287 1.1.2.2 yamt
288 1.1.2.2 yamt /* Parse the command */
289 1.1.2.2 yamt if (!parse(&argc, &argv, sp->text)) {
290 1.1.2.2 yamt if ((argc > 0) && (perform(argc, argv) != 0)) {
291 1.1.2.2 yamt /* normal command */
292 1.1.2.2 yamt printf("%s: %s\n", argv[0], command_errmsg);
293 1.1.2.2 yamt if (!(sp->flags & SL_IGNOREERR)) {
294 1.1.2.2 yamt res=CMD_ERROR;
295 1.1.2.2 yamt break;
296 1.1.2.2 yamt }
297 1.1.2.2 yamt }
298 1.1.2.2 yamt free(argv);
299 1.1.2.2 yamt argv = NULL;
300 1.1.2.2 yamt } else {
301 1.1.2.2 yamt printf("%s line %d: parse error\n", filename, sp->line);
302 1.1.2.2 yamt res=CMD_ERROR;
303 1.1.2.2 yamt break;
304 1.1.2.2 yamt }
305 1.1.2.2 yamt #endif
306 1.1.2.2 yamt }
307 1.1.2.2 yamt #ifndef BOOT_FORTH
308 1.1.2.2 yamt if (argv != NULL)
309 1.1.2.2 yamt free(argv);
310 1.1.2.2 yamt #else
311 1.1.2.2 yamt bf_vm->sourceID.i = prevsrcid;
312 1.1.2.2 yamt #endif
313 1.1.2.2 yamt while(script != NULL) {
314 1.1.2.2 yamt se = script;
315 1.1.2.2 yamt script = script->next;
316 1.1.2.2 yamt free(se);
317 1.1.2.2 yamt }
318 1.1.2.2 yamt return(res);
319 1.1.2.2 yamt }
320 1.1.2.2 yamt
321 1.1.2.2 yamt /*
322 1.1.2.2 yamt * Emit the current prompt; use the same syntax as the parser
323 1.1.2.2 yamt * for embedding environment variables.
324 1.1.2.2 yamt */
325 1.1.2.2 yamt static void
326 1.1.2.2 yamt prompt(void)
327 1.1.2.2 yamt {
328 1.1.2.2 yamt char *pr, *p, *cp, *ev;
329 1.1.2.2 yamt
330 1.1.2.2 yamt if ((cp = getenv("prompt")) == NULL)
331 1.1.2.2 yamt cp = ">";
332 1.1.2.2 yamt pr = p = strdup(cp);
333 1.1.2.2 yamt
334 1.1.2.2 yamt while (*p != 0) {
335 1.1.2.2 yamt if ((*p == '$') && (*(p+1) == '{')) {
336 1.1.2.2 yamt for (cp = p + 2; (*cp != 0) && (*cp != '}'); cp++)
337 1.1.2.2 yamt ;
338 1.1.2.2 yamt *cp = 0;
339 1.1.2.2 yamt ev = getenv(p + 2);
340 1.1.2.2 yamt
341 1.1.2.2 yamt if (ev != NULL)
342 1.1.2.2 yamt printf("%s", ev);
343 1.1.2.2 yamt p = cp + 1;
344 1.1.2.2 yamt continue;
345 1.1.2.2 yamt }
346 1.1.2.2 yamt putchar(*p++);
347 1.1.2.2 yamt }
348 1.1.2.2 yamt putchar(' ');
349 1.1.2.2 yamt free(pr);
350 1.1.2.2 yamt }
351