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