xref: /freebsd/stand/common/interp_simple.c (revision a91a246563dffa876a52f53a98de4af9fa364c52)
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 /*
28  * Simple commandline interpreter, toplevel and misc.
29  */
30 
31 #include <stand.h>
32 #include <string.h>
33 #include "bootstrap.h"
34 
35 INTERP_DEFINE("simp");
36 
37 void
38 interp_init(void)
39 {
40 
41 	setenv("script.lang", "simple", 1);
42 	/* Read our default configuration. */
43 	interp_include("/boot/loader.rc");
44 }
45 
46 int
47 interp_run(const char *input)
48 {
49 	int			argc;
50 	char			**argv;
51 
52 	if (parse(&argc, &argv, input)) {
53 		printf("parse error\n");
54 		return CMD_ERROR;
55 	}
56 
57 	if (interp_builtin_cmd(argc, argv)) {
58 		printf("%s: %s\n", argv[0], command_errmsg);
59 		free(argv);
60 		return CMD_ERROR;
61 	}
62 	free(argv);
63 	return CMD_OK;
64 }
65 
66 /*
67  * Header prepended to each line. The text immediately follows the header.
68  * We try to make this short in order to save memory -- the loader has
69  * limited memory available, and some of the forth files are very long.
70  */
71 struct includeline
72 {
73 	struct includeline	*next;
74 	int			flags;
75 	int			line;
76 #define SL_QUIET	(1<<0)
77 #define SL_IGNOREERR	(1<<1)
78 	char			text[0];
79 };
80 
81 int
82 interp_include(const char *filename)
83 {
84 	struct includeline	*script, *se, *sp;
85 	char			input[256];			/* big enough? */
86 	int			argc,res;
87 	char			**argv, *cp;
88 	int			fd, flags, line;
89 
90 	if (((fd = open(filename, O_RDONLY)) == -1)) {
91 		snprintf(command_errbuf, sizeof(command_errbuf),
92 		    "can't open '%s': %s", filename, strerror(errno));
93 		return(CMD_ERROR);
94 	}
95 
96 #ifdef LOADER_VERIEXEC
97 	if (verify_file(fd, filename, 0, VE_GUESS, __func__) < 0) {
98 		close(fd);
99 		sprintf(command_errbuf,"can't verify '%s'", filename);
100 		return(CMD_ERROR);
101 	}
102 #endif
103 
104 	/*
105 	 * Read the script into memory.
106 	 */
107 	script = se = NULL;
108 	line = 0;
109 
110 	while (fgetstr(input, sizeof(input), fd) >= 0) {
111 		line++;
112 		flags = 0;
113 		/* Discard comments */
114 		if (strncmp(input+strspn(input, " "), "\\", 1) == 0)
115 			continue;
116 		cp = input;
117 		/* Echo? */
118 		if (input[0] == '@') {
119 			cp++;
120 			flags |= SL_QUIET;
121 		}
122 		/* Error OK? */
123 		if (input[0] == '-') {
124 			cp++;
125 			flags |= SL_IGNOREERR;
126 		}
127 
128 		/* Allocate script line structure and copy line, flags */
129 		if (*cp == '\0')
130 			continue;	/* ignore empty line, save memory */
131 		sp = malloc(sizeof(struct includeline) + strlen(cp) + 1);
132 		/* On malloc failure (it happens!), free as much as possible and exit */
133 		if (sp == NULL) {
134 			while (script != NULL) {
135 				se = script;
136 				script = script->next;
137 				free(se);
138 			}
139 			snprintf(command_errbuf, sizeof(command_errbuf),
140 			    "file '%s' line %d: memory allocation failure - aborting",
141 			    filename, line);
142 			close(fd);
143 			return (CMD_ERROR);
144 		}
145 		strcpy(sp->text, cp);
146 		sp->flags = flags;
147 		sp->line = line;
148 		sp->next = NULL;
149 
150 		if (script == NULL) {
151 			script = sp;
152 		} else {
153 			se->next = sp;
154 		}
155 		se = sp;
156 	}
157 	close(fd);
158 
159 	/*
160 	 * Execute the script
161 	 */
162 	argv = NULL;
163 	res = CMD_OK;
164 	for (sp = script; sp != NULL; sp = sp->next) {
165 
166 		/* print if not being quiet */
167 		if (!(sp->flags & SL_QUIET)) {
168 			interp_emit_prompt();
169 			printf("%s\n", sp->text);
170 		}
171 
172 		/* Parse the command */
173 		if (!parse(&argc, &argv, sp->text)) {
174 			if ((argc > 0) && (interp_builtin_cmd(argc, argv) != 0)) {
175 				/* normal command */
176 				printf("%s: %s\n", argv[0], command_errmsg);
177 				if (!(sp->flags & SL_IGNOREERR)) {
178 					res=CMD_ERROR;
179 					break;
180 				}
181 			}
182 			free(argv);
183 			argv = NULL;
184 		} else {
185 			printf("%s line %d: parse error\n", filename, sp->line);
186 			res=CMD_ERROR;
187 			break;
188 		}
189 	}
190 	if (argv != NULL)
191 		free(argv);
192 
193 	while (script != NULL) {
194 		se = script;
195 		script = script->next;
196 		free(se);
197 	}
198 	return(res);
199 }
200 
201 /*
202  * There's no graphics commands for the simple interpreter.
203  */
204 void
205 gfx_interp_ref(void)
206 {
207 }
208