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