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