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 * XXX may be obsoleted by BootFORTH or some other, better, interpreter.
31 */
32
33 #include <stand.h>
34 #include <string.h>
35 #include "bootstrap.h"
36
37 #ifdef LOADER_VERIEXEC
38 #include <verify_file.h>
39 #endif
40
41 #define MAXARGS 20 /* maximum number of arguments allowed */
42
43 const char * volatile interp_identifier;
44
45 /*
46 * Interactive mode
47 */
48 void
interact(void)49 interact(void)
50 {
51 static char input[256]; /* big enough? */
52
53 TSENTER();
54
55 /*
56 * Because interp_identifier is volatile, it cannot be optimized out by
57 * the compiler as it's considered an externally observable event. This
58 * prevents the compiler from optimizing out our carefully placed
59 * $Interpreter:4th string that userboot may use to determine that
60 * we need to switch interpreters.
61 */
62 interp_identifier = bootprog_interp;
63 interp_preinit();
64 interp_init();
65
66 printf("\n");
67
68 /*
69 * Before interacting, we might want to autoboot.
70 */
71 autoboot_maybe();
72
73 /*
74 * Not autobooting, go manual
75 */
76 printf("\nType '?' for a list of commands, 'help' for more detailed help.\n");
77 if (getenv("prompt") == NULL)
78 setenv("prompt", "${interpret}", 1);
79 if (getenv("interpret") == NULL)
80 setenv("interpret", "OK", 1);
81
82 for (;;) {
83 input[0] = '\0';
84 interp_emit_prompt();
85 ngets(input, sizeof(input));
86 #ifdef LOADER_VERIEXEC
87 /* some settings should be restritcted */
88 ve_status_set(-1, VE_UNVERIFIED_OK);
89 #endif
90 interp_run(input);
91 }
92 }
93
94 /*
95 * Read commands from a file, then execute them.
96 *
97 * We store the commands in memory and close the source file so that the media
98 * holding it can safely go away while we are executing.
99 *
100 * Commands may be prefixed with '@' (so they aren't displayed) or '-' (so
101 * that the script won't stop if they fail).
102 */
103 COMMAND_SET(include, "include", "read commands from a file", command_include);
104
105 static int
command_include(int argc,char * argv[])106 command_include(int argc, char *argv[])
107 {
108 int i;
109 int res;
110 char **argvbuf;
111
112 /*
113 * Since argv is static, we need to save it here.
114 */
115 argvbuf = (char**) calloc((u_int)argc, sizeof(char*));
116 for (i = 0; i < argc; i++)
117 argvbuf[i] = strdup(argv[i]);
118
119 res=CMD_OK;
120 for (i = 1; (i < argc) && (res == CMD_OK); i++)
121 res = interp_include(argvbuf[i]);
122
123 for (i = 0; i < argc; i++)
124 free(argvbuf[i]);
125 free(argvbuf);
126
127 return(res);
128 }
129
130 /*
131 * Emit the current prompt; use the same syntax as the parser
132 * for embedding environment variables. Does not accept input.
133 */
134 void
interp_emit_prompt(void)135 interp_emit_prompt(void)
136 {
137 char *pr, *p, *cp, *ev;
138
139 if ((cp = getenv("prompt")) == NULL)
140 cp = ">";
141 pr = p = strdup(cp);
142
143 while (*p != 0) {
144 if ((*p == '$') && (*(p+1) == '{')) {
145 for (cp = p + 2; (*cp != 0) && (*cp != '}'); cp++)
146 ;
147 *cp = 0;
148 ev = getenv(p + 2);
149
150 if (ev != NULL)
151 printf("%s", ev);
152 p = cp + 1;
153 continue;
154 }
155 putchar(*p++);
156 }
157 putchar(' ');
158 free(pr);
159 }
160
161 static struct bootblk_command *
interp_lookup_cmd(const char * cmd)162 interp_lookup_cmd(const char *cmd)
163 {
164 struct bootblk_command **cmdp;
165
166 /* search the command set for the command */
167 SET_FOREACH(cmdp, Xcommand_set) {
168 if (((*cmdp)->c_name != NULL) && !strcmp(cmd, (*cmdp)->c_name))
169 return (*cmdp);
170 }
171 return (NULL);
172 }
173
174 /*
175 * Perform a builtin command
176 */
177 int
interp_builtin_cmd(int argc,char * argv[])178 interp_builtin_cmd(int argc, char *argv[])
179 {
180 int result;
181 struct bootblk_command *cmd;
182
183 if (argc < 1)
184 return (CMD_OK);
185
186 /* set return defaults; a successful command will override these */
187 command_errmsg = command_errbuf;
188 strcpy(command_errbuf, "no error message");
189 result = CMD_ERROR;
190
191 cmd = interp_lookup_cmd(argv[0]);
192 if (cmd != NULL && cmd->c_fn) {
193 TSENTER2(argv[0]);
194 result = cmd->c_fn(argc, argv);
195 TSEXIT();
196 } else {
197 command_errmsg = "unknown command";
198 }
199 return (result);
200 }
201
202 /*
203 * Return true if the builtin command exists
204 */
205 bool
interp_has_builtin_cmd(const char * cmd)206 interp_has_builtin_cmd(const char *cmd)
207 {
208 return (interp_lookup_cmd(cmd) != NULL);
209 }
210