interp.c revision 1.2.10.3 1 1.2.10.3 yamt /* $NetBSD: interp.c,v 1.2.10.3 2006/12/30 20:46:20 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.3 yamt
116 1.2.10.2 yamt /*
117 1.2.10.3 yamt * XXX: Before interacting, we might want to autoboot.
118 1.2.10.2 yamt */
119 1.2.10.3 yamt
120 1.2.10.2 yamt
121 1.2.10.2 yamt /*
122 1.2.10.2 yamt * Not autobooting, go manual
123 1.2.10.2 yamt */
124 1.2.10.2 yamt printf("\nType '?' for a list of commands, 'help' for more detailed help.\n");
125 1.2.10.2 yamt if (getenv("prompt") == NULL)
126 1.2.10.2 yamt setenv("prompt", "${interpret}", 1);
127 1.2.10.2 yamt if (getenv("interpret") == NULL)
128 1.2.10.2 yamt setenv("interpret", "OK", 1);
129 1.2.10.2 yamt
130 1.2.10.2 yamt
131 1.2.10.2 yamt for (;;) {
132 1.2.10.2 yamt input[0] = '\0';
133 1.2.10.2 yamt prompt();
134 1.2.10.2 yamt ngets(input, sizeof(input));
135 1.2.10.2 yamt #ifdef BOOT_FORTH
136 1.2.10.2 yamt bf_vm->sourceID.i = 0;
137 1.2.10.2 yamt bf_run(input);
138 1.2.10.2 yamt #else
139 1.2.10.2 yamt if (!parse(&argc, &argv, input)) {
140 1.2.10.2 yamt if (perform(argc, argv))
141 1.2.10.2 yamt printf("%s: %s\n", argv[0], command_errmsg);
142 1.2.10.2 yamt free(argv);
143 1.2.10.2 yamt } else {
144 1.2.10.2 yamt printf("parse error\n");
145 1.2.10.2 yamt }
146 1.2.10.2 yamt #endif
147 1.2.10.2 yamt }
148 1.2.10.2 yamt }
149 1.2.10.2 yamt
150 1.2.10.2 yamt /*
151 1.2.10.2 yamt * Read commands from a file, then execute them.
152 1.2.10.2 yamt *
153 1.2.10.2 yamt * We store the commands in memory and close the source file so that the media
154 1.2.10.2 yamt * holding it can safely go away while we are executing.
155 1.2.10.2 yamt *
156 1.2.10.2 yamt * Commands may be prefixed with '@' (so they aren't displayed) or '-' (so
157 1.2.10.2 yamt * that the script won't stop if they fail).
158 1.2.10.2 yamt */
159 1.2.10.2 yamt
160 1.2.10.2 yamt int
161 1.2.10.2 yamt command_include(int argc, char *argv[])
162 1.2.10.2 yamt {
163 1.2.10.2 yamt int i;
164 1.2.10.2 yamt int res;
165 1.2.10.2 yamt char **argvbuf;
166 1.2.10.2 yamt
167 1.2.10.2 yamt /*
168 1.2.10.2 yamt * Since argv is static, we need to save it here.
169 1.2.10.2 yamt */
170 1.2.10.2 yamt argvbuf = (char**) calloc((u_int)argc, sizeof(char*));
171 1.2.10.2 yamt for (i = 0; i < argc; i++)
172 1.2.10.2 yamt argvbuf[i] = strdup(argv[i]);
173 1.2.10.2 yamt
174 1.2.10.2 yamt res=CMD_OK;
175 1.2.10.2 yamt for (i = 1; (i < argc) && (res == CMD_OK); i++)
176 1.2.10.2 yamt res = include(argvbuf[i]);
177 1.2.10.2 yamt
178 1.2.10.2 yamt for (i = 0; i < argc; i++)
179 1.2.10.2 yamt free(argvbuf[i]);
180 1.2.10.2 yamt free(argvbuf);
181 1.2.10.2 yamt
182 1.2.10.2 yamt return(res);
183 1.2.10.2 yamt }
184 1.2.10.2 yamt
185 1.2.10.2 yamt struct includeline
186 1.2.10.2 yamt {
187 1.2.10.2 yamt char *text;
188 1.2.10.2 yamt int flags;
189 1.2.10.2 yamt int line;
190 1.2.10.2 yamt #define SL_QUIET (1<<0)
191 1.2.10.2 yamt #define SL_IGNOREERR (1<<1)
192 1.2.10.2 yamt struct includeline *next;
193 1.2.10.2 yamt };
194 1.2.10.2 yamt
195 1.2.10.2 yamt int
196 1.2.10.2 yamt include(const char *filename)
197 1.2.10.2 yamt {
198 1.2.10.2 yamt struct includeline *script, *se, *sp;
199 1.2.10.2 yamt char input[256]; /* big enough? */
200 1.2.10.2 yamt #ifdef BOOT_FORTH
201 1.2.10.2 yamt int res;
202 1.2.10.2 yamt char *cp;
203 1.2.10.2 yamt int prevsrcid, fd, line;
204 1.2.10.2 yamt #else
205 1.2.10.2 yamt int argc,res;
206 1.2.10.2 yamt char **argv, *cp;
207 1.2.10.2 yamt int fd, flags, line;
208 1.2.10.2 yamt #endif
209 1.2.10.2 yamt
210 1.2.10.2 yamt if (((fd = open(filename, O_RDONLY)) == -1)) {
211 1.2.10.2 yamt sprintf(command_errbuf,"can't open '%s': %s\n", filename, strerror(errno));
212 1.2.10.2 yamt return(CMD_ERROR);
213 1.2.10.2 yamt }
214 1.2.10.2 yamt
215 1.2.10.2 yamt /*
216 1.2.10.2 yamt * Read the script into memory.
217 1.2.10.2 yamt */
218 1.2.10.2 yamt script = se = NULL;
219 1.2.10.2 yamt line = 0;
220 1.2.10.2 yamt
221 1.2.10.2 yamt while (fgetstr(input, sizeof(input), fd) >= 0) {
222 1.2.10.2 yamt line++;
223 1.2.10.2 yamt #ifdef BOOT_FORTH
224 1.2.10.2 yamt cp = input;
225 1.2.10.2 yamt #else
226 1.2.10.2 yamt flags = 0;
227 1.2.10.2 yamt /* Discard comments */
228 1.2.10.2 yamt if (strncmp(input+strspn(input, " "), "\\ ", 2) == 0)
229 1.2.10.2 yamt continue;
230 1.2.10.2 yamt cp = input;
231 1.2.10.2 yamt /* Echo? */
232 1.2.10.2 yamt if (input[0] == '@') {
233 1.2.10.2 yamt cp++;
234 1.2.10.2 yamt flags |= SL_QUIET;
235 1.2.10.2 yamt }
236 1.2.10.2 yamt /* Error OK? */
237 1.2.10.2 yamt if (input[0] == '-') {
238 1.2.10.2 yamt cp++;
239 1.2.10.2 yamt flags |= SL_IGNOREERR;
240 1.2.10.2 yamt }
241 1.2.10.2 yamt #endif
242 1.2.10.2 yamt /* Allocate script line structure and copy line, flags */
243 1.2.10.2 yamt sp = alloc(sizeof(struct includeline) + strlen(cp) + 1);
244 1.2.10.2 yamt sp->text = (char *)sp + sizeof(struct includeline);
245 1.2.10.2 yamt strcpy(sp->text, cp);
246 1.2.10.2 yamt #ifndef BOOT_FORTH
247 1.2.10.2 yamt sp->flags = flags;
248 1.2.10.2 yamt #endif
249 1.2.10.2 yamt sp->line = line;
250 1.2.10.2 yamt sp->next = NULL;
251 1.2.10.2 yamt
252 1.2.10.2 yamt if (script == NULL) {
253 1.2.10.2 yamt script = sp;
254 1.2.10.2 yamt } else {
255 1.2.10.2 yamt se->next = sp;
256 1.2.10.2 yamt }
257 1.2.10.2 yamt se = sp;
258 1.2.10.2 yamt }
259 1.2.10.2 yamt close(fd);
260 1.2.10.2 yamt
261 1.2.10.2 yamt /*
262 1.2.10.2 yamt * Execute the script
263 1.2.10.2 yamt */
264 1.2.10.2 yamt #ifndef BOOT_FORTH
265 1.2.10.2 yamt argv = NULL;
266 1.2.10.2 yamt #else
267 1.2.10.2 yamt prevsrcid = bf_vm->sourceID.i;
268 1.2.10.2 yamt bf_vm->sourceID.i = fd;
269 1.2.10.2 yamt #endif
270 1.2.10.2 yamt res = CMD_OK;
271 1.2.10.2 yamt for (sp = script; sp != NULL; sp = sp->next) {
272 1.2.10.2 yamt
273 1.2.10.2 yamt #ifdef BOOT_FORTH
274 1.2.10.2 yamt res = bf_run(sp->text);
275 1.2.10.2 yamt if (res != VM_OUTOFTEXT) {
276 1.2.10.2 yamt sprintf(command_errbuf, "Error while including %s, in the line:\n%s", filename, sp->text);
277 1.2.10.2 yamt res = CMD_ERROR;
278 1.2.10.2 yamt break;
279 1.2.10.2 yamt } else
280 1.2.10.2 yamt res = CMD_OK;
281 1.2.10.2 yamt #else
282 1.2.10.2 yamt /* print if not being quiet */
283 1.2.10.2 yamt if (!(sp->flags & SL_QUIET)) {
284 1.2.10.2 yamt prompt();
285 1.2.10.2 yamt printf("%s\n", sp->text);
286 1.2.10.2 yamt }
287 1.2.10.2 yamt
288 1.2.10.2 yamt /* Parse the command */
289 1.2.10.2 yamt if (!parse(&argc, &argv, sp->text)) {
290 1.2.10.2 yamt if ((argc > 0) && (perform(argc, argv) != 0)) {
291 1.2.10.2 yamt /* normal command */
292 1.2.10.2 yamt printf("%s: %s\n", argv[0], command_errmsg);
293 1.2.10.2 yamt if (!(sp->flags & SL_IGNOREERR)) {
294 1.2.10.2 yamt res=CMD_ERROR;
295 1.2.10.2 yamt break;
296 1.2.10.2 yamt }
297 1.2.10.2 yamt }
298 1.2.10.2 yamt free(argv);
299 1.2.10.2 yamt argv = NULL;
300 1.2.10.2 yamt } else {
301 1.2.10.2 yamt printf("%s line %d: parse error\n", filename, sp->line);
302 1.2.10.2 yamt res=CMD_ERROR;
303 1.2.10.2 yamt break;
304 1.2.10.2 yamt }
305 1.2.10.2 yamt #endif
306 1.2.10.2 yamt }
307 1.2.10.2 yamt #ifndef BOOT_FORTH
308 1.2.10.2 yamt if (argv != NULL)
309 1.2.10.2 yamt free(argv);
310 1.2.10.2 yamt #else
311 1.2.10.2 yamt bf_vm->sourceID.i = prevsrcid;
312 1.2.10.2 yamt #endif
313 1.2.10.2 yamt while(script != NULL) {
314 1.2.10.2 yamt se = script;
315 1.2.10.2 yamt script = script->next;
316 1.2.10.2 yamt free(se);
317 1.2.10.2 yamt }
318 1.2.10.2 yamt return(res);
319 1.2.10.2 yamt }
320 1.2.10.2 yamt
321 1.2.10.2 yamt /*
322 1.2.10.2 yamt * Emit the current prompt; use the same syntax as the parser
323 1.2.10.2 yamt * for embedding environment variables.
324 1.2.10.2 yamt */
325 1.2.10.2 yamt static void
326 1.2.10.2 yamt prompt(void)
327 1.2.10.2 yamt {
328 1.2.10.2 yamt char *pr, *p, *cp, *ev;
329 1.2.10.2 yamt
330 1.2.10.2 yamt if ((cp = getenv("prompt")) == NULL)
331 1.2.10.2 yamt cp = ">";
332 1.2.10.2 yamt pr = p = strdup(cp);
333 1.2.10.2 yamt
334 1.2.10.2 yamt while (*p != 0) {
335 1.2.10.2 yamt if ((*p == '$') && (*(p+1) == '{')) {
336 1.2.10.2 yamt for (cp = p + 2; (*cp != 0) && (*cp != '}'); cp++)
337 1.2.10.2 yamt ;
338 1.2.10.2 yamt *cp = 0;
339 1.2.10.2 yamt ev = getenv(p + 2);
340 1.2.10.2 yamt
341 1.2.10.2 yamt if (ev != NULL)
342 1.2.10.2 yamt printf("%s", ev);
343 1.2.10.2 yamt p = cp + 1;
344 1.2.10.2 yamt continue;
345 1.2.10.2 yamt }
346 1.2.10.2 yamt putchar(*p++);
347 1.2.10.2 yamt }
348 1.2.10.2 yamt putchar(' ');
349 1.2.10.2 yamt free(pr);
350 1.2.10.2 yamt }
351