xref: /freebsd/stand/common/interp_forth.c (revision fa9012aef1ad596a99950f986cf1fb31111c034a)
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>		/* to pick up __FreeBSD_version */
31 #include <string.h>
32 #include <stand.h>
33 #include "bootstrap.h"
34 #include "ficl.h"
35 
36 extern unsigned bootprog_rev;
37 INTERP_DEFINE("4th");
38 
39 /* #define BFORTH_DEBUG */
40 
41 #ifdef BFORTH_DEBUG
42 #define	DPRINTF(fmt, args...)	printf("%s: " fmt "\n" , __func__ , ## args)
43 #else
44 #define	DPRINTF(fmt, args...)	((void)0)
45 #endif
46 
47 /*
48  * Eventually, all builtin commands throw codes must be defined
49  * elsewhere, possibly bootstrap.h. For now, just this code, used
50  * just in this file, it is getting defined.
51  */
52 #define BF_PARSE 100
53 
54 /*
55  * FreeBSD loader default dictionary cells
56  */
57 #ifndef	BF_DICTSIZE
58 #define	BF_DICTSIZE	10000
59 #endif
60 
61 /*
62  * BootForth   Interface to Ficl Forth interpreter.
63  */
64 
65 FICL_SYSTEM *bf_sys;
66 FICL_VM	*bf_vm;
67 
68 /*
69  * Shim for taking commands from BF and passing them out to 'standard'
70  * argv/argc command functions.
71  */
72 static void
73 bf_command(FICL_VM *vm)
74 {
75 	char			*name, *line, *tail, *cp;
76 	size_t			len;
77 	struct bootblk_command	**cmdp;
78 	bootblk_cmd_t		*cmd;
79 	int			nstrings, i;
80 	int			argc, result;
81 	char			**argv;
82 
83 	/* Get the name of the current word */
84 	name = vm->runningWord->name;
85 
86 	/* Find our command structure */
87 	cmd = NULL;
88 	SET_FOREACH(cmdp, Xcommand_set) {
89 		if (((*cmdp)->c_name != NULL) && !strcmp(name, (*cmdp)->c_name))
90 			cmd = (*cmdp)->c_fn;
91 	}
92 	if (cmd == NULL)
93 		panic("callout for unknown command '%s'", name);
94 
95 	/* Check whether we have been compiled or are being interpreted */
96 	if (stackPopINT(vm->pStack)) {
97 		/*
98 		 * Get parameters from stack, in the format:
99 		 * an un ... a2 u2 a1 u1 n --
100 		 * Where n is the number of strings, a/u are pairs of
101 		 * address/size for strings, and they will be concatenated
102 		 * in LIFO order.
103 		 */
104 		nstrings = stackPopINT(vm->pStack);
105 		for (i = 0, len = 0; i < nstrings; i++)
106 			len += stackFetch(vm->pStack, i * 2).i + 1;
107 		line = malloc(strlen(name) + len + 1);
108 		strcpy(line, name);
109 
110 		if (nstrings)
111 			for (i = 0; i < nstrings; i++) {
112 				len = stackPopINT(vm->pStack);
113 				cp = stackPopPtr(vm->pStack);
114 				strcat(line, " ");
115 				strncat(line, cp, len);
116 			}
117 	} else {
118 		/* Get remainder of invocation */
119 		tail = vmGetInBuf(vm);
120 		for (cp = tail, len = 0; cp != vm->tib.end && *cp != 0 && *cp != '\n'; cp++, len++)
121 			;
122 
123 		line = malloc(strlen(name) + len + 2);
124 		strcpy(line, name);
125 		if (len > 0) {
126 			strcat(line, " ");
127 			strncat(line, tail, len);
128 			vmUpdateTib(vm, tail + len);
129 		}
130 	}
131 	DPRINTF("cmd '%s'", line);
132 
133 	command_errmsg = command_errbuf;
134 	command_errbuf[0] = 0;
135 	if (!parse(&argc, &argv, line)) {
136 		result = (cmd)(argc, argv);
137 		free(argv);
138 	} else {
139 		result=BF_PARSE;
140 	}
141 
142 	switch (result) {
143 	case CMD_CRIT:
144 		printf("%s\n", command_errmsg);
145 		command_errmsg = NULL;
146 		break;
147 	case CMD_FATAL:
148 		panic("%s", command_errmsg);
149 	}
150 
151 	free(line);
152 	/*
153 	 * If there was error during nested ficlExec(), we may no longer have
154 	 * valid environment to return.  Throw all exceptions from here.
155 	 */
156 	if (result != CMD_OK)
157 		vmThrow(vm, result);
158 
159 	/* This is going to be thrown!!! */
160 	stackPushINT(vm->pStack,result);
161 }
162 
163 /*
164  * Replace a word definition (a builtin command) with another
165  * one that:
166  *
167  *        - Throw error results instead of returning them on the stack
168  *        - Pass a flag indicating whether the word was compiled or is
169  *          being interpreted.
170  *
171  * There is one major problem with builtins that cannot be overcome
172  * in anyway, except by outlawing it. We want builtins to behave
173  * differently depending on whether they have been compiled or they
174  * are being interpreted. Notice that this is *not* the interpreter's
175  * current state. For example:
176  *
177  * : example ls ; immediate
178  * : problem example ;		\ "ls" gets executed while compiling
179  * example			\ "ls" gets executed while interpreting
180  *
181  * Notice that, though the current state is different in the two
182  * invocations of "example", in both cases "ls" has been
183  * *compiled in*, which is what we really want.
184  *
185  * The problem arises when you tick the builtin. For example:
186  *
187  * : example-1 ['] ls postpone literal ; immediate
188  * : example-2 example-1 execute ; immediate
189  * : problem example-2 ;
190  * example-2
191  *
192  * We have no way, when we get EXECUTEd, of knowing what our behavior
193  * should be. Thus, our only alternative is to "outlaw" this. See RFI
194  * 0007, and ANS Forth Standard's appendix D, item 6.7 for a related
195  * problem, concerning compile semantics.
196  *
197  * The problem is compounded by the fact that "' builtin CATCH" is valid
198  * and desirable. The only solution is to create an intermediary word.
199  * For example:
200  *
201  * : my-ls ls ;
202  * : example ['] my-ls catch ;
203  *
204  * So, with the below implementation, here is a summary of the behavior
205  * of builtins:
206  *
207  * ls -l				\ "interpret" behavior, ie,
208  *					\ takes parameters from TIB
209  * : ex-1 s" -l" 1 ls ;			\ "compile" behavior, ie,
210  *					\ takes parameters from the stack
211  * : ex-2 ['] ls catch ; immediate	\ undefined behavior
212  * : ex-3 ['] ls catch ;		\ undefined behavior
213  * ex-2 ex-3				\ "interpret" behavior,
214  *					\ catch works
215  * : ex-4 ex-2 ;			\ "compile" behavior,
216  *					\ catch does not work
217  * : ex-5 ex-3 ; immediate		\ same as ex-2
218  * : ex-6 ex-3 ;			\ same as ex-3
219  * : ex-7 ['] ex-1 catch ;		\ "compile" behavior,
220  *					\ catch works
221  * : ex-8 postpone ls ;	immediate	\ same as ex-2
222  * : ex-9 postpone ls ;			\ same as ex-3
223  *
224  * As the definition below is particularly tricky, and it's side effects
225  * must be well understood by those playing with it, I'll be heavy on
226  * the comments.
227  *
228  * (if you edit this definition, pay attention to trailing spaces after
229  *  each word -- I warned you! :-) )
230  */
231 #define BUILTIN_CONSTRUCTOR						\
232 	": builtin: "							\
233 	">in @ "		/* save the tib index pointer */	\
234 	"' "			/* get next word's xt */		\
235 	"swap >in ! "		/* point again to next word */		\
236 	"create "		/* create a new definition of the next word */ \
237 	", "			/* save previous definition's xt */	\
238 	"immediate "		/* make the new definition an immediate word */ \
239 									\
240 	"does> "		/* Now, the *new* definition will: */	\
241 	"state @ if "		/* if in compiling state: */		\
242 	"1 postpone literal "	/* pass 1 flag to indicate compile */	\
243 	"@ compile, "		/* compile in previous definition */	\
244 	"postpone throw "		/* throw stack-returned result */ \
245 	"else "		/* if in interpreting state: */			\
246 	"0 swap "			/* pass 0 flag to indicate interpret */ \
247 	"@ execute "		/* call previous definition */		\
248 	"throw "			/* throw stack-returned result */ \
249 	"then ; "
250 
251 /*
252  * Initialise the Forth interpreter, create all our commands as words.
253  */
254 void
255 bf_init(void)
256 {
257 	struct bootblk_command	**cmdp;
258 	char create_buf[41];	/* 31 characters-long builtins */
259 	int fd;
260 
261 	bf_sys = ficlInitSystem(BF_DICTSIZE);
262 	bf_vm = ficlNewVM(bf_sys);
263 
264 	/* Put all private definitions in a "builtins" vocabulary */
265 	ficlExec(bf_vm, "vocabulary builtins also builtins definitions");
266 
267 	/* Builtin constructor word  */
268 	ficlExec(bf_vm, BUILTIN_CONSTRUCTOR);
269 
270 	/* make all commands appear as Forth words */
271 	SET_FOREACH(cmdp, Xcommand_set) {
272 		ficlBuild(bf_sys, (char *)(*cmdp)->c_name, bf_command, FW_DEFAULT);
273 		ficlExec(bf_vm, "forth definitions builtins");
274 		sprintf(create_buf, "builtin: %s", (*cmdp)->c_name);
275 		ficlExec(bf_vm, create_buf);
276 		ficlExec(bf_vm, "builtins definitions");
277 	}
278 	ficlExec(bf_vm, "only forth definitions");
279 
280 	/* Export some version numbers so that code can detect the loader/host version */
281 	ficlSetEnv(bf_sys, "FreeBSD_version", __FreeBSD_version);
282 	ficlSetEnv(bf_sys, "loader_version", bootprog_rev);
283 
284 	/* try to load and run init file if present */
285 	if ((fd = open("/boot/boot.4th", O_RDONLY)) != -1) {
286 #ifdef LOADER_VERIEXEC
287 		if (verify_file(fd, "/boot/boot.4th", 0, VE_GUESS, __func__) < 0) {
288 			close(fd);
289 			return;
290 		}
291 #endif
292 		(void)ficlExecFD(bf_vm, fd);
293 		close(fd);
294 	}
295 }
296 
297 /*
298  * Feed a line of user input to the Forth interpreter
299  */
300 static int
301 bf_run(const char *line)
302 {
303 	int		result;
304 
305 	/*
306 	 * ficl would require extensive changes to accept a const char *
307 	 * interface. Instead, cast it away here and hope for the best.
308 	 * We know at the present time the caller for us in the boot
309 	 * forth loader can tolerate the string being modified because
310 	 * the string is passed in here and then not touched again.
311 	 */
312 	result = ficlExec(bf_vm, __DECONST(char *, line));
313 
314 	DPRINTF("ficlExec '%s' = %d", line, result);
315 	switch (result) {
316 	case VM_OUTOFTEXT:
317 	case VM_ABORTQ:
318 	case VM_QUIT:
319 	case VM_ERREXIT:
320 		break;
321 	case VM_USEREXIT:
322 		printf("No where to leave to!\n");
323 		break;
324 	case VM_ABORT:
325 		printf("Aborted!\n");
326 		break;
327 	case BF_PARSE:
328 		printf("Parse error!\n");
329 		break;
330 	default:
331 		if (command_errmsg != NULL) {
332 			printf("%s\n", command_errmsg);
333 			command_errmsg = NULL;
334 		}
335 	}
336 
337 	if (result == VM_USEREXIT)
338 		panic("interpreter exit");
339 	setenv("interpret", bf_vm->state ? "" : "OK", 1);
340 
341 	return (result);
342 }
343 
344 void
345 interp_init(void)
346 {
347 
348 	setenv("script.lang", "forth", 1);
349 	bf_init();
350 	/* Read our default configuration. */
351 	interp_include("/boot/loader.rc");
352 }
353 
354 int
355 interp_run(const char *input)
356 {
357 
358 	bf_vm->sourceID.i = 0;
359 	return bf_run(input);
360 }
361 
362 /*
363  * Header prepended to each line. The text immediately follows the header.
364  * We try to make this short in order to save memory -- the loader has
365  * limited memory available, and some of the forth files are very long.
366  */
367 struct includeline
368 {
369 	struct includeline	*next;
370 	char			text[0];
371 };
372 
373 int
374 interp_include(const char *filename)
375 {
376 	struct includeline	*script, *se, *sp;
377 	char			input[256];			/* big enough? */
378 	int			res;
379 	char			*cp;
380 	int			prevsrcid, fd, line;
381 
382 	if (((fd = open(filename, O_RDONLY)) == -1)) {
383 		snprintf(command_errbuf, sizeof(command_errbuf),
384 		    "can't open '%s': %s", filename, strerror(errno));
385 		return(CMD_ERROR);
386 	}
387 
388 #ifdef LOADER_VERIEXEC
389 	if (verify_file(fd, filename, 0, VE_GUESS, __func__) < 0) {
390 		close(fd);
391 		sprintf(command_errbuf,"can't verify '%s'", filename);
392 		return(CMD_ERROR);
393 	}
394 #endif
395 	/*
396 	 * Read the script into memory.
397 	 */
398 	script = se = NULL;
399 	line = 0;
400 
401 	while (fgetstr(input, sizeof(input), fd) >= 0) {
402 		line++;
403 		cp = input;
404 		/* Allocate script line structure and copy line, flags */
405 		if (*cp == '\0')
406 			continue;	/* ignore empty line, save memory */
407 		sp = malloc(sizeof(struct includeline) + strlen(cp) + 1);
408 		/* On malloc failure (it happens!), free as much as possible and exit */
409 		if (sp == NULL) {
410 			while (script != NULL) {
411 				se = script;
412 				script = script->next;
413 				free(se);
414 			}
415 			snprintf(command_errbuf, sizeof(command_errbuf),
416 			    "file '%s' line %d: memory allocation failure - aborting",
417 			    filename, line);
418 			close(fd);
419 			return (CMD_ERROR);
420 		}
421 		strcpy(sp->text, cp);
422 		sp->next = NULL;
423 
424 		if (script == NULL) {
425 			script = sp;
426 		} else {
427 			se->next = sp;
428 		}
429 		se = sp;
430 	}
431 	close(fd);
432 
433 	/*
434 	 * Execute the script
435 	 */
436 	prevsrcid = bf_vm->sourceID.i;
437 	bf_vm->sourceID.i = fd;
438 	res = CMD_OK;
439 	for (sp = script; sp != NULL; sp = sp->next) {
440 		res = bf_run(sp->text);
441 		if (res != VM_OUTOFTEXT) {
442 			snprintf(command_errbuf, sizeof(command_errbuf),
443 			    "Error while including %s, in the line:\n%s",
444 			    filename, sp->text);
445 			res = CMD_ERROR;
446 			break;
447 		} else
448 			res = CMD_OK;
449 	}
450 	bf_vm->sourceID.i = prevsrcid;
451 
452 	while (script != NULL) {
453 		se = script;
454 		script = script->next;
455 		free(se);
456 	}
457 	return(res);
458 }
459