xref: /freebsd/stand/common/interp.c (revision f0408ed054ef74bbe52461a52efc647ed4739158)
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * Copyright (c) 2011 Wojciech A. Koszek <wkoszek@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 /*
31  * Simple commandline interpreter, toplevel and misc.
32  *
33  * XXX may be obsoleted by BootFORTH or some other, better, interpreter.
34  */
35 
36 #include <stand.h>
37 #include <string.h>
38 #include "bootstrap.h"
39 #include "interp.h"
40 
41 
42 #define	MAXARGS	20			/* maximum number of arguments allowed */
43 
44 struct interp	*interp =
45 #if defined(BOOT_FORTH)
46 	&boot_interp_forth;
47 #else
48 	&boot_interp_simple;
49 #endif
50 
51 int
52 default_load_config(void *ctx)
53 {
54 	return INTERP_INCL(interp, "/boot/loader.rc");
55 }
56 
57 /*
58  * Interactive mode
59  */
60 void
61 interact(const char * rc)
62 {
63 	static char	input[256];			/* big enough? */
64 
65 	INTERP_INIT(interp);
66 
67 	/*
68 	 * Read our default configuration
69 	 */
70 	INTERP_LOAD_DEF_CONFIG(interp);
71 	printf("\n");
72 	/*
73 	 * Before interacting, we might want to autoboot.
74 	 */
75 	autoboot_maybe();
76 
77 	/*
78 	 * Not autobooting, go manual
79 	 */
80 	printf("\nType '?' for a list of commands, 'help' for more detailed help.\n");
81 	if (getenv("prompt") == NULL)
82 		setenv("prompt", "${interpret}", 1);
83 	if (getenv("interpret") == NULL)
84 		setenv("interpret", "OK", 1);
85 
86 
87 	for (;;) {
88 		input[0] = '\0';
89 		prompt();
90 		ngets(input, sizeof(input));
91 		INTERP_RUN(interp, input);
92 	}
93 }
94 
95 /*
96  * Read commands from a file, then execute them.
97  *
98  * We store the commands in memory and close the source file so that the media
99  * holding it can safely go away while we are executing.
100  *
101  * Commands may be prefixed with '@' (so they aren't displayed) or '-' (so
102  * that the script won't stop if they fail).
103  */
104 COMMAND_SET(include, "include", "read commands from a file", command_include);
105 
106 static int
107 command_include(int argc, char *argv[])
108 {
109 	int		i;
110 	int		res;
111 	char	**argvbuf;
112 
113 	/*
114 	 * Since argv is static, we need to save it here.
115 	 */
116 	argvbuf = (char**) calloc((u_int)argc, sizeof(char*));
117 	for (i = 0; i < argc; i++)
118 		argvbuf[i] = strdup(argv[i]);
119 
120 	res=CMD_OK;
121 	for (i = 1; (i < argc) && (res == CMD_OK); i++)
122 		res = INTERP_INCL(interp, argvbuf[i]);
123 
124 	for (i = 0; i < argc; i++)
125 		free(argvbuf[i]);
126 	free(argvbuf);
127 
128 	return(res);
129 }
130 
131 /*
132  * Perform the command
133  */
134 int
135 perform(int argc, char *argv[])
136 {
137 	int				result;
138 	struct bootblk_command	**cmdp;
139 	bootblk_cmd_t		*cmd;
140 
141 	if (argc < 1)
142 		return(CMD_OK);
143 
144 	/* set return defaults; a successful command will override these */
145 	command_errmsg = command_errbuf;
146 	strcpy(command_errbuf, "no error message");
147 	cmd = NULL;
148 	result = CMD_ERROR;
149 
150 	/* search the command set for the command */
151 	SET_FOREACH(cmdp, Xcommand_set) {
152 		if (((*cmdp)->c_name != NULL) && !strcmp(argv[0], (*cmdp)->c_name))
153 			cmd = (*cmdp)->c_fn;
154 	}
155 	if (cmd != NULL) {
156 		result = (cmd)(argc, argv);
157 	} else {
158 		command_errmsg = "unknown command";
159 	}
160 	return result;
161 }
162 
163 /*
164  * Emit the current prompt; use the same syntax as the parser
165  * for embedding environment variables.
166  */
167 void
168 prompt(void)
169 {
170 	char	*pr, *p, *cp, *ev;
171 
172 	if ((cp = getenv("prompt")) == NULL)
173 		cp = ">";
174 	pr = p = strdup(cp);
175 
176 	while (*p != 0) {
177 		if ((*p == '$') && (*(p+1) == '{')) {
178 			for (cp = p + 2; (*cp != 0) && (*cp != '}'); cp++)
179 				;
180 			*cp = 0;
181 			ev = getenv(p + 2);
182 
183 			if (ev != NULL)
184 				printf("%s", ev);
185 			p = cp + 1;
186 			continue;
187 		}
188 		putchar(*p++);
189 	}
190 	putchar(' ');
191 	free(pr);
192 }
193