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