xref: /freebsd/stand/common/interp_forth.c (revision 68344c9c6c69fc604b402eee420285356f7a677f)
1ca987d46SWarner Losh /*-
2ca987d46SWarner Losh  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3ca987d46SWarner Losh  * All rights reserved.
4ca987d46SWarner Losh  *
5ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
6ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
7ca987d46SWarner Losh  * are met:
8ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
9ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
10ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
12ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
13ca987d46SWarner Losh  *
14ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15ca987d46SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16ca987d46SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17ca987d46SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18ca987d46SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19ca987d46SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20ca987d46SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21ca987d46SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22ca987d46SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23ca987d46SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24ca987d46SWarner Losh  * SUCH DAMAGE.
25ca987d46SWarner Losh  */
26ca987d46SWarner Losh 
27ca987d46SWarner Losh #include <sys/param.h>		/* to pick up __FreeBSD_version */
28ca987d46SWarner Losh #include <string.h>
29ca987d46SWarner Losh #include <stand.h>
30ca987d46SWarner Losh #include "bootstrap.h"
31ca987d46SWarner Losh #include "ficl.h"
32ca987d46SWarner Losh 
33d3d381b2SKyle Evans INTERP_DEFINE("4th");
34ca987d46SWarner Losh 
35ca987d46SWarner Losh /* #define BFORTH_DEBUG */
36ca987d46SWarner Losh 
37ca987d46SWarner Losh #ifdef BFORTH_DEBUG
387325df02SKyle Evans #define	DPRINTF(fmt, args...)	printf("%s: " fmt "\n" , __func__ , ## args)
39ca987d46SWarner Losh #else
4082c29d4fSToomas Soome #define	DPRINTF(fmt, args...)	((void)0)
41ca987d46SWarner Losh #endif
42ca987d46SWarner Losh 
43ca987d46SWarner Losh /*
44ca987d46SWarner Losh  * Eventually, all builtin commands throw codes must be defined
45ca987d46SWarner Losh  * elsewhere, possibly bootstrap.h. For now, just this code, used
46ca987d46SWarner Losh  * just in this file, it is getting defined.
47ca987d46SWarner Losh  */
48ca987d46SWarner Losh #define BF_PARSE 100
49ca987d46SWarner Losh 
50ca987d46SWarner Losh /*
51ca987d46SWarner Losh  * FreeBSD loader default dictionary cells
52ca987d46SWarner Losh  */
53ca987d46SWarner Losh #ifndef	BF_DICTSIZE
54ca987d46SWarner Losh #define	BF_DICTSIZE	10000
55ca987d46SWarner Losh #endif
56ca987d46SWarner Losh 
57ca987d46SWarner Losh /*
58ca987d46SWarner Losh  * BootForth   Interface to Ficl Forth interpreter.
59ca987d46SWarner Losh  */
60ba25195eSWarner Losh 
61ca987d46SWarner Losh FICL_SYSTEM *bf_sys;
62ca987d46SWarner Losh FICL_VM	*bf_vm;
63ca987d46SWarner Losh 
64ca987d46SWarner Losh /*
65ca987d46SWarner Losh  * Shim for taking commands from BF and passing them out to 'standard'
66ca987d46SWarner Losh  * argv/argc command functions.
67ca987d46SWarner Losh  */
68ca987d46SWarner Losh static void
bf_command(FICL_VM * vm)69ca987d46SWarner Losh bf_command(FICL_VM *vm)
70ca987d46SWarner Losh {
71ca987d46SWarner Losh 	char			*name, *line, *tail, *cp;
72ca987d46SWarner Losh 	size_t			len;
73ca987d46SWarner Losh 	struct bootblk_command	**cmdp;
74ca987d46SWarner Losh 	bootblk_cmd_t		*cmd;
75ca987d46SWarner Losh 	int			nstrings, i;
76ca987d46SWarner Losh 	int			argc, result;
77ca987d46SWarner Losh 	char			**argv;
78ca987d46SWarner Losh 
79ca987d46SWarner Losh 	/* Get the name of the current word */
80ca987d46SWarner Losh 	name = vm->runningWord->name;
81ca987d46SWarner Losh 
82ca987d46SWarner Losh 	/* Find our command structure */
83ca987d46SWarner Losh 	cmd = NULL;
84ca987d46SWarner Losh 	SET_FOREACH(cmdp, Xcommand_set) {
85ca987d46SWarner Losh 		if (((*cmdp)->c_name != NULL) && !strcmp(name, (*cmdp)->c_name))
86ca987d46SWarner Losh 			cmd = (*cmdp)->c_fn;
87ca987d46SWarner Losh 	}
88ca987d46SWarner Losh 	if (cmd == NULL)
89ca987d46SWarner Losh 		panic("callout for unknown command '%s'", name);
90ca987d46SWarner Losh 
91ca987d46SWarner Losh 	/* Check whether we have been compiled or are being interpreted */
92ca987d46SWarner Losh 	if (stackPopINT(vm->pStack)) {
93ca987d46SWarner Losh 		/*
94ca987d46SWarner Losh 		 * Get parameters from stack, in the format:
95ca987d46SWarner Losh 		 * an un ... a2 u2 a1 u1 n --
96ca987d46SWarner Losh 		 * Where n is the number of strings, a/u are pairs of
97ca987d46SWarner Losh 		 * address/size for strings, and they will be concatenated
98ca987d46SWarner Losh 		 * in LIFO order.
99ca987d46SWarner Losh 		 */
100ca987d46SWarner Losh 		nstrings = stackPopINT(vm->pStack);
101ca987d46SWarner Losh 		for (i = 0, len = 0; i < nstrings; i++)
102ca987d46SWarner Losh 			len += stackFetch(vm->pStack, i * 2).i + 1;
103ca987d46SWarner Losh 		line = malloc(strlen(name) + len + 1);
104ca987d46SWarner Losh 		strcpy(line, name);
105ca987d46SWarner Losh 
106ca987d46SWarner Losh 		if (nstrings)
107ca987d46SWarner Losh 			for (i = 0; i < nstrings; i++) {
108ca987d46SWarner Losh 				len = stackPopINT(vm->pStack);
109ca987d46SWarner Losh 				cp = stackPopPtr(vm->pStack);
110ca987d46SWarner Losh 				strcat(line, " ");
111ca987d46SWarner Losh 				strncat(line, cp, len);
112ca987d46SWarner Losh 			}
113ca987d46SWarner Losh 	} else {
114ca987d46SWarner Losh 		/* Get remainder of invocation */
115ca987d46SWarner Losh 		tail = vmGetInBuf(vm);
116ca987d46SWarner Losh 		for (cp = tail, len = 0; cp != vm->tib.end && *cp != 0 && *cp != '\n'; cp++, len++)
117ca987d46SWarner Losh 			;
118ca987d46SWarner Losh 
119ca987d46SWarner Losh 		line = malloc(strlen(name) + len + 2);
120ca987d46SWarner Losh 		strcpy(line, name);
121ca987d46SWarner Losh 		if (len > 0) {
122ca987d46SWarner Losh 			strcat(line, " ");
123ca987d46SWarner Losh 			strncat(line, tail, len);
124ca987d46SWarner Losh 			vmUpdateTib(vm, tail + len);
125ca987d46SWarner Losh 		}
126ca987d46SWarner Losh 	}
1277325df02SKyle Evans 	DPRINTF("cmd '%s'", line);
128ca987d46SWarner Losh 
129ca987d46SWarner Losh 	command_errmsg = command_errbuf;
130ca987d46SWarner Losh 	command_errbuf[0] = 0;
131ca987d46SWarner Losh 	if (!parse(&argc, &argv, line)) {
132ca987d46SWarner Losh 		result = (cmd)(argc, argv);
133ca987d46SWarner Losh 		free(argv);
134ca987d46SWarner Losh 	} else {
135ca987d46SWarner Losh 		result=BF_PARSE;
136ca987d46SWarner Losh 	}
137ca987d46SWarner Losh 
138ca987d46SWarner Losh 	switch (result) {
139ca987d46SWarner Losh 	case CMD_CRIT:
140ca987d46SWarner Losh 		printf("%s\n", command_errmsg);
141d92fb784SMaxim Sobolev 		command_errmsg = NULL;
142ca987d46SWarner Losh 		break;
143ca987d46SWarner Losh 	case CMD_FATAL:
14455d5c949SMaxim Sobolev 		panic("%s", command_errmsg);
145ca987d46SWarner Losh 	}
146ca987d46SWarner Losh 
147ca987d46SWarner Losh 	free(line);
148ca987d46SWarner Losh 	/*
149ca987d46SWarner Losh 	 * If there was error during nested ficlExec(), we may no longer have
150ca987d46SWarner Losh 	 * valid environment to return.  Throw all exceptions from here.
151ca987d46SWarner Losh 	 */
152ca987d46SWarner Losh 	if (result != CMD_OK)
153ca987d46SWarner Losh 		vmThrow(vm, result);
154ca987d46SWarner Losh 
155ca987d46SWarner Losh 	/* This is going to be thrown!!! */
156ca987d46SWarner Losh 	stackPushINT(vm->pStack,result);
157ca987d46SWarner Losh }
158ca987d46SWarner Losh 
159ca987d46SWarner Losh /*
160ca987d46SWarner Losh  * Replace a word definition (a builtin command) with another
161ca987d46SWarner Losh  * one that:
162ca987d46SWarner Losh  *
163ca987d46SWarner Losh  *        - Throw error results instead of returning them on the stack
164ca987d46SWarner Losh  *        - Pass a flag indicating whether the word was compiled or is
165ca987d46SWarner Losh  *          being interpreted.
166ca987d46SWarner Losh  *
167ca987d46SWarner Losh  * There is one major problem with builtins that cannot be overcome
168ca987d46SWarner Losh  * in anyway, except by outlawing it. We want builtins to behave
169ca987d46SWarner Losh  * differently depending on whether they have been compiled or they
170ca987d46SWarner Losh  * are being interpreted. Notice that this is *not* the interpreter's
171ca987d46SWarner Losh  * current state. For example:
172ca987d46SWarner Losh  *
173ca987d46SWarner Losh  * : example ls ; immediate
174ca987d46SWarner Losh  * : problem example ;		\ "ls" gets executed while compiling
175ca987d46SWarner Losh  * example			\ "ls" gets executed while interpreting
176ca987d46SWarner Losh  *
177ca987d46SWarner Losh  * Notice that, though the current state is different in the two
178ca987d46SWarner Losh  * invocations of "example", in both cases "ls" has been
179ca987d46SWarner Losh  * *compiled in*, which is what we really want.
180ca987d46SWarner Losh  *
181ca987d46SWarner Losh  * The problem arises when you tick the builtin. For example:
182ca987d46SWarner Losh  *
183ca987d46SWarner Losh  * : example-1 ['] ls postpone literal ; immediate
184ca987d46SWarner Losh  * : example-2 example-1 execute ; immediate
185ca987d46SWarner Losh  * : problem example-2 ;
186ca987d46SWarner Losh  * example-2
187ca987d46SWarner Losh  *
188ca987d46SWarner Losh  * We have no way, when we get EXECUTEd, of knowing what our behavior
189ca987d46SWarner Losh  * should be. Thus, our only alternative is to "outlaw" this. See RFI
190ca987d46SWarner Losh  * 0007, and ANS Forth Standard's appendix D, item 6.7 for a related
191ca987d46SWarner Losh  * problem, concerning compile semantics.
192ca987d46SWarner Losh  *
193ca987d46SWarner Losh  * The problem is compounded by the fact that "' builtin CATCH" is valid
194ca987d46SWarner Losh  * and desirable. The only solution is to create an intermediary word.
195ca987d46SWarner Losh  * For example:
196ca987d46SWarner Losh  *
197ca987d46SWarner Losh  * : my-ls ls ;
198ca987d46SWarner Losh  * : example ['] my-ls catch ;
199ca987d46SWarner Losh  *
200ca987d46SWarner Losh  * So, with the below implementation, here is a summary of the behavior
201ca987d46SWarner Losh  * of builtins:
202ca987d46SWarner Losh  *
203ca987d46SWarner Losh  * ls -l				\ "interpret" behavior, ie,
204ca987d46SWarner Losh  *					\ takes parameters from TIB
205ca987d46SWarner Losh  * : ex-1 s" -l" 1 ls ;			\ "compile" behavior, ie,
206ca987d46SWarner Losh  *					\ takes parameters from the stack
207ca987d46SWarner Losh  * : ex-2 ['] ls catch ; immediate	\ undefined behavior
208ca987d46SWarner Losh  * : ex-3 ['] ls catch ;		\ undefined behavior
209ca987d46SWarner Losh  * ex-2 ex-3				\ "interpret" behavior,
210ca987d46SWarner Losh  *					\ catch works
211ca987d46SWarner Losh  * : ex-4 ex-2 ;			\ "compile" behavior,
212ca987d46SWarner Losh  *					\ catch does not work
213ca987d46SWarner Losh  * : ex-5 ex-3 ; immediate		\ same as ex-2
214ca987d46SWarner Losh  * : ex-6 ex-3 ;			\ same as ex-3
215ca987d46SWarner Losh  * : ex-7 ['] ex-1 catch ;		\ "compile" behavior,
216ca987d46SWarner Losh  *					\ catch works
217ca987d46SWarner Losh  * : ex-8 postpone ls ;	immediate	\ same as ex-2
218ca987d46SWarner Losh  * : ex-9 postpone ls ;			\ same as ex-3
219ca987d46SWarner Losh  *
220ca987d46SWarner Losh  * As the definition below is particularly tricky, and it's side effects
221ca987d46SWarner Losh  * must be well understood by those playing with it, I'll be heavy on
222ca987d46SWarner Losh  * the comments.
223ca987d46SWarner Losh  *
224ca987d46SWarner Losh  * (if you edit this definition, pay attention to trailing spaces after
225ca987d46SWarner Losh  *  each word -- I warned you! :-) )
226ca987d46SWarner Losh  */
227ca987d46SWarner Losh #define BUILTIN_CONSTRUCTOR						\
228ca987d46SWarner Losh 	": builtin: "							\
229ca987d46SWarner Losh 	">in @ "		/* save the tib index pointer */	\
230ca987d46SWarner Losh 	"' "			/* get next word's xt */		\
231ca987d46SWarner Losh 	"swap >in ! "		/* point again to next word */		\
232ca987d46SWarner Losh 	"create "		/* create a new definition of the next word */ \
233ca987d46SWarner Losh 	", "			/* save previous definition's xt */	\
234ca987d46SWarner Losh 	"immediate "		/* make the new definition an immediate word */ \
235ca987d46SWarner Losh 									\
236ca987d46SWarner Losh 	"does> "		/* Now, the *new* definition will: */	\
237ca987d46SWarner Losh 	"state @ if "		/* if in compiling state: */		\
238ca987d46SWarner Losh 	"1 postpone literal "	/* pass 1 flag to indicate compile */	\
239ca987d46SWarner Losh 	"@ compile, "		/* compile in previous definition */	\
240ca987d46SWarner Losh 	"postpone throw "		/* throw stack-returned result */ \
241ca987d46SWarner Losh 	"else "		/* if in interpreting state: */			\
242ca987d46SWarner Losh 	"0 swap "			/* pass 0 flag to indicate interpret */ \
243ca987d46SWarner Losh 	"@ execute "		/* call previous definition */		\
244ca987d46SWarner Losh 	"throw "			/* throw stack-returned result */ \
245ca987d46SWarner Losh 	"then ; "
246ca987d46SWarner Losh 
247ca987d46SWarner Losh /*
248ca987d46SWarner Losh  * Initialise the Forth interpreter, create all our commands as words.
249ca987d46SWarner Losh  */
250ba25195eSWarner Losh void
bf_init(void)2516bc86037SWarner Losh bf_init(void)
252ca987d46SWarner Losh {
253ca987d46SWarner Losh 	struct bootblk_command	**cmdp;
254ca987d46SWarner Losh 	char create_buf[41];	/* 31 characters-long builtins */
255ba25195eSWarner Losh 	int fd;
256ca987d46SWarner Losh 
257ba25195eSWarner Losh 	bf_sys = ficlInitSystem(BF_DICTSIZE);
258ba25195eSWarner Losh 	bf_vm = ficlNewVM(bf_sys);
259ca987d46SWarner Losh 
260ca987d46SWarner Losh 	/* Put all private definitions in a "builtins" vocabulary */
261ca987d46SWarner Losh 	ficlExec(bf_vm, "vocabulary builtins also builtins definitions");
262ca987d46SWarner Losh 
263ca987d46SWarner Losh 	/* Builtin constructor word  */
264ca987d46SWarner Losh 	ficlExec(bf_vm, BUILTIN_CONSTRUCTOR);
265ca987d46SWarner Losh 
266ca987d46SWarner Losh 	/* make all commands appear as Forth words */
267ca987d46SWarner Losh 	SET_FOREACH(cmdp, Xcommand_set) {
268ca987d46SWarner Losh 		ficlBuild(bf_sys, (char *)(*cmdp)->c_name, bf_command, FW_DEFAULT);
269ca987d46SWarner Losh 		ficlExec(bf_vm, "forth definitions builtins");
270ca987d46SWarner Losh 		sprintf(create_buf, "builtin: %s", (*cmdp)->c_name);
271ca987d46SWarner Losh 		ficlExec(bf_vm, create_buf);
272ca987d46SWarner Losh 		ficlExec(bf_vm, "builtins definitions");
273ca987d46SWarner Losh 	}
274ca987d46SWarner Losh 	ficlExec(bf_vm, "only forth definitions");
275ca987d46SWarner Losh 
276ca987d46SWarner Losh 	/* Export some version numbers so that code can detect the loader/host version */
277ca987d46SWarner Losh 	ficlSetEnv(bf_sys, "FreeBSD_version", __FreeBSD_version);
278ca987d46SWarner Losh 	ficlSetEnv(bf_sys, "loader_version", bootprog_rev);
279ba25195eSWarner Losh 
280ba25195eSWarner Losh 	/* try to load and run init file if present */
2816bc86037SWarner Losh 	if ((fd = open("/boot/boot.4th", O_RDONLY)) != -1) {
282c42e554dSMarcin Wojtas #ifdef LOADER_VERIEXEC
283afc571b1SSimon J. Gerraty 		if (verify_file(fd, "/boot/boot.4th", 0, VE_GUESS, __func__) < 0) {
284c42e554dSMarcin Wojtas 			close(fd);
285c42e554dSMarcin Wojtas 			return;
286c42e554dSMarcin Wojtas 		}
287c42e554dSMarcin Wojtas #endif
288ba25195eSWarner Losh 		(void)ficlExecFD(bf_vm, fd);
289ba25195eSWarner Losh 		close(fd);
290ba25195eSWarner Losh 	}
291ba25195eSWarner Losh }
292ca987d46SWarner Losh 
293ca987d46SWarner Losh /*
294ca987d46SWarner Losh  * Feed a line of user input to the Forth interpreter
295ca987d46SWarner Losh  */
29679a6a17aSWarner Losh static int
bf_run(const char * line)29779a6a17aSWarner Losh bf_run(const char *line)
298ca987d46SWarner Losh {
299ca987d46SWarner Losh 	int		result;
300ca987d46SWarner Losh 
30179a6a17aSWarner Losh 	/*
30279a6a17aSWarner Losh 	 * ficl would require extensive changes to accept a const char *
30379a6a17aSWarner Losh 	 * interface. Instead, cast it away here and hope for the best.
30479a6a17aSWarner Losh 	 * We know at the present time the caller for us in the boot
30579a6a17aSWarner Losh 	 * forth loader can tolerate the string being modified because
30679a6a17aSWarner Losh 	 * the string is passed in here and then not touched again.
30779a6a17aSWarner Losh 	 */
30879a6a17aSWarner Losh 	result = ficlExec(bf_vm, __DECONST(char *, line));
309ca987d46SWarner Losh 
3107325df02SKyle Evans 	DPRINTF("ficlExec '%s' = %d", line, result);
311ca987d46SWarner Losh 	switch (result) {
312ca987d46SWarner Losh 	case VM_OUTOFTEXT:
313ca987d46SWarner Losh 	case VM_ABORTQ:
314ca987d46SWarner Losh 	case VM_QUIT:
315ca987d46SWarner Losh 	case VM_ERREXIT:
316ca987d46SWarner Losh 		break;
317ca987d46SWarner Losh 	case VM_USEREXIT:
318ca987d46SWarner Losh 		printf("No where to leave to!\n");
319ca987d46SWarner Losh 		break;
320ca987d46SWarner Losh 	case VM_ABORT:
321ca987d46SWarner Losh 		printf("Aborted!\n");
322ca987d46SWarner Losh 		break;
323ca987d46SWarner Losh 	case BF_PARSE:
324ca987d46SWarner Losh 		printf("Parse error!\n");
325ca987d46SWarner Losh 		break;
326ca987d46SWarner Losh 	default:
327ca987d46SWarner Losh 		if (command_errmsg != NULL) {
328ca987d46SWarner Losh 			printf("%s\n", command_errmsg);
329ca987d46SWarner Losh 			command_errmsg = NULL;
330ca987d46SWarner Losh 		}
331ca987d46SWarner Losh 	}
332ca987d46SWarner Losh 
333ca987d46SWarner Losh 	if (result == VM_USEREXIT)
334ca987d46SWarner Losh 		panic("interpreter exit");
335ba25195eSWarner Losh 	setenv("interpret", bf_vm->state ? "" : "OK", 1);
336ca987d46SWarner Losh 
337ca987d46SWarner Losh 	return (result);
338ca987d46SWarner Losh }
33979a6a17aSWarner Losh 
340*68344c9cSWarner Losh static bool preinit_run = false;
341*68344c9cSWarner Losh 
342*68344c9cSWarner Losh void
interp_preinit(void)343*68344c9cSWarner Losh interp_preinit(void)
344*68344c9cSWarner Losh {
345*68344c9cSWarner Losh 	if (preinit_run)
346*68344c9cSWarner Losh 		return;
347*68344c9cSWarner Losh 	setenv("script.lang", "forth", 1);
348*68344c9cSWarner Losh 	bf_init();
349*68344c9cSWarner Losh 	preinit_run = true;
350*68344c9cSWarner Losh }
351*68344c9cSWarner Losh 
35279a6a17aSWarner Losh void
interp_init(void)35379a6a17aSWarner Losh interp_init(void)
35479a6a17aSWarner Losh {
35579a6a17aSWarner Losh 	/* Read our default configuration. */
35679a6a17aSWarner Losh 	interp_include("/boot/loader.rc");
35779a6a17aSWarner Losh }
35879a6a17aSWarner Losh 
35979a6a17aSWarner Losh int
interp_run(const char * input)36079a6a17aSWarner Losh interp_run(const char *input)
36179a6a17aSWarner Losh {
36279a6a17aSWarner Losh 
36379a6a17aSWarner Losh 	bf_vm->sourceID.i = 0;
36479a6a17aSWarner Losh 	return bf_run(input);
36579a6a17aSWarner Losh }
36679a6a17aSWarner Losh 
36779a6a17aSWarner Losh /*
36879a6a17aSWarner Losh  * Header prepended to each line. The text immediately follows the header.
36979a6a17aSWarner Losh  * We try to make this short in order to save memory -- the loader has
37079a6a17aSWarner Losh  * limited memory available, and some of the forth files are very long.
37179a6a17aSWarner Losh  */
37279a6a17aSWarner Losh struct includeline
37379a6a17aSWarner Losh {
37479a6a17aSWarner Losh 	struct includeline	*next;
37579a6a17aSWarner Losh 	char			text[0];
37679a6a17aSWarner Losh };
37779a6a17aSWarner Losh 
37879a6a17aSWarner Losh int
interp_include(const char * filename)37979a6a17aSWarner Losh interp_include(const char *filename)
38079a6a17aSWarner Losh {
38179a6a17aSWarner Losh 	struct includeline	*script, *se, *sp;
38279a6a17aSWarner Losh 	char			input[256];			/* big enough? */
38379a6a17aSWarner Losh 	int			res;
38479a6a17aSWarner Losh 	char			*cp;
38579a6a17aSWarner Losh 	int			prevsrcid, fd, line;
38679a6a17aSWarner Losh 
38779a6a17aSWarner Losh 	if (((fd = open(filename, O_RDONLY)) == -1)) {
38879a6a17aSWarner Losh 		snprintf(command_errbuf, sizeof(command_errbuf),
38979a6a17aSWarner Losh 		    "can't open '%s': %s", filename, strerror(errno));
39079a6a17aSWarner Losh 		return(CMD_ERROR);
39179a6a17aSWarner Losh 	}
39279a6a17aSWarner Losh 
3938df8b2d3SSimon J. Gerraty #ifdef LOADER_VERIEXEC
394afc571b1SSimon J. Gerraty 	if (verify_file(fd, filename, 0, VE_GUESS, __func__) < 0) {
3958df8b2d3SSimon J. Gerraty 		close(fd);
3968df8b2d3SSimon J. Gerraty 		sprintf(command_errbuf,"can't verify '%s'", filename);
3978df8b2d3SSimon J. Gerraty 		return(CMD_ERROR);
3988df8b2d3SSimon J. Gerraty 	}
3998df8b2d3SSimon J. Gerraty #endif
40079a6a17aSWarner Losh 	/*
40179a6a17aSWarner Losh 	 * Read the script into memory.
40279a6a17aSWarner Losh 	 */
40379a6a17aSWarner Losh 	script = se = NULL;
40479a6a17aSWarner Losh 	line = 0;
40579a6a17aSWarner Losh 
40679a6a17aSWarner Losh 	while (fgetstr(input, sizeof(input), fd) >= 0) {
40779a6a17aSWarner Losh 		line++;
40879a6a17aSWarner Losh 		cp = input;
40979a6a17aSWarner Losh 		/* Allocate script line structure and copy line, flags */
41079a6a17aSWarner Losh 		if (*cp == '\0')
41179a6a17aSWarner Losh 			continue;	/* ignore empty line, save memory */
41279a6a17aSWarner Losh 		sp = malloc(sizeof(struct includeline) + strlen(cp) + 1);
41379a6a17aSWarner Losh 		/* On malloc failure (it happens!), free as much as possible and exit */
41479a6a17aSWarner Losh 		if (sp == NULL) {
41579a6a17aSWarner Losh 			while (script != NULL) {
41679a6a17aSWarner Losh 				se = script;
41779a6a17aSWarner Losh 				script = script->next;
41879a6a17aSWarner Losh 				free(se);
41979a6a17aSWarner Losh 			}
42079a6a17aSWarner Losh 			snprintf(command_errbuf, sizeof(command_errbuf),
42179a6a17aSWarner Losh 			    "file '%s' line %d: memory allocation failure - aborting",
42279a6a17aSWarner Losh 			    filename, line);
42379a6a17aSWarner Losh 			close(fd);
42479a6a17aSWarner Losh 			return (CMD_ERROR);
42579a6a17aSWarner Losh 		}
42679a6a17aSWarner Losh 		strcpy(sp->text, cp);
42779a6a17aSWarner Losh 		sp->next = NULL;
42879a6a17aSWarner Losh 
42979a6a17aSWarner Losh 		if (script == NULL) {
43079a6a17aSWarner Losh 			script = sp;
43179a6a17aSWarner Losh 		} else {
43279a6a17aSWarner Losh 			se->next = sp;
43379a6a17aSWarner Losh 		}
43479a6a17aSWarner Losh 		se = sp;
43579a6a17aSWarner Losh 	}
43679a6a17aSWarner Losh 	close(fd);
43779a6a17aSWarner Losh 
43879a6a17aSWarner Losh 	/*
43979a6a17aSWarner Losh 	 * Execute the script
44079a6a17aSWarner Losh 	 */
44179a6a17aSWarner Losh 	prevsrcid = bf_vm->sourceID.i;
44279a6a17aSWarner Losh 	bf_vm->sourceID.i = fd;
44379a6a17aSWarner Losh 	res = CMD_OK;
44479a6a17aSWarner Losh 	for (sp = script; sp != NULL; sp = sp->next) {
44579a6a17aSWarner Losh 		res = bf_run(sp->text);
44679a6a17aSWarner Losh 		if (res != VM_OUTOFTEXT) {
44779a6a17aSWarner Losh 			snprintf(command_errbuf, sizeof(command_errbuf),
44879a6a17aSWarner Losh 			    "Error while including %s, in the line:\n%s",
44979a6a17aSWarner Losh 			    filename, sp->text);
45079a6a17aSWarner Losh 			res = CMD_ERROR;
45179a6a17aSWarner Losh 			break;
45279a6a17aSWarner Losh 		} else
45379a6a17aSWarner Losh 			res = CMD_OK;
45479a6a17aSWarner Losh 	}
45579a6a17aSWarner Losh 	bf_vm->sourceID.i = prevsrcid;
45679a6a17aSWarner Losh 
45779a6a17aSWarner Losh 	while (script != NULL) {
45879a6a17aSWarner Losh 		se = script;
45979a6a17aSWarner Losh 		script = script->next;
46079a6a17aSWarner Losh 		free(se);
46179a6a17aSWarner Losh 	}
46279a6a17aSWarner Losh 	return(res);
46379a6a17aSWarner Losh }
464