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