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