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