xref: /freebsd/stand/common/interp.c (revision 3a4a3639d2b695cf559fd9e637b005a0a9ecd1c2)
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  */
26ba25195eSWarner Losh 
27ca987d46SWarner Losh #include <sys/cdefs.h>
28ca987d46SWarner Losh __FBSDID("$FreeBSD$");
29ca987d46SWarner Losh 
30ca987d46SWarner Losh /*
31ca987d46SWarner Losh  * Simple commandline interpreter, toplevel and misc.
32ca987d46SWarner Losh  *
33ca987d46SWarner Losh  * XXX may be obsoleted by BootFORTH or some other, better, interpreter.
34ca987d46SWarner Losh  */
35ca987d46SWarner Losh 
36ca987d46SWarner Losh #include <stand.h>
37ca987d46SWarner Losh #include <string.h>
38ca987d46SWarner Losh #include "bootstrap.h"
39ca987d46SWarner Losh 
40ca987d46SWarner Losh #define	MAXARGS	20			/* maximum number of arguments allowed */
41ca987d46SWarner Losh 
42ba25195eSWarner Losh /*
43ba25195eSWarner Losh  * Interactive mode
44ba25195eSWarner Losh  */
45ba25195eSWarner Losh void
466bc86037SWarner Losh interact(void)
47ba25195eSWarner Losh {
48ba25195eSWarner Losh 	static char	input[256];			/* big enough? */
49ba25195eSWarner Losh 
5079a6a17aSWarner Losh 	interp_init();
51ba25195eSWarner Losh 
52ba25195eSWarner Losh 	printf("\n");
53ba25195eSWarner Losh 
54ba25195eSWarner Losh 	/*
55ba25195eSWarner Losh 	 * Before interacting, we might want to autoboot.
56ba25195eSWarner Losh 	 */
57ba25195eSWarner Losh 	autoboot_maybe();
58ba25195eSWarner Losh 
59ba25195eSWarner Losh 	/*
60ba25195eSWarner Losh 	 * Not autobooting, go manual
61ba25195eSWarner Losh 	 */
62ba25195eSWarner Losh 	printf("\nType '?' for a list of commands, 'help' for more detailed help.\n");
63ba25195eSWarner Losh 	if (getenv("prompt") == NULL)
64ba25195eSWarner Losh 		setenv("prompt", "${interpret}", 1);
65ba25195eSWarner Losh 	if (getenv("interpret") == NULL)
66ba25195eSWarner Losh 		setenv("interpret", "OK", 1);
67ba25195eSWarner Losh 
68ba25195eSWarner Losh 	for (;;) {
69ba25195eSWarner Losh 		input[0] = '\0';
7079a6a17aSWarner Losh 		interp_emit_prompt();
71ba25195eSWarner Losh 		ngets(input, sizeof(input));
7279a6a17aSWarner Losh 		interp_run(input);
73ba25195eSWarner Losh 	}
74ba25195eSWarner Losh }
75ba25195eSWarner Losh 
76ba25195eSWarner Losh /*
77ba25195eSWarner Losh  * Read commands from a file, then execute them.
78ba25195eSWarner Losh  *
79ba25195eSWarner Losh  * We store the commands in memory and close the source file so that the media
80ba25195eSWarner Losh  * holding it can safely go away while we are executing.
81ba25195eSWarner Losh  *
82ba25195eSWarner Losh  * Commands may be prefixed with '@' (so they aren't displayed) or '-' (so
83ba25195eSWarner Losh  * that the script won't stop if they fail).
84ba25195eSWarner Losh  */
85ba25195eSWarner Losh COMMAND_SET(include, "include", "read commands from a file", command_include);
86ba25195eSWarner Losh 
87ba25195eSWarner Losh static int
88ba25195eSWarner Losh command_include(int argc, char *argv[])
89ba25195eSWarner Losh {
90ba25195eSWarner Losh 	int		i;
91ba25195eSWarner Losh 	int		res;
92ba25195eSWarner Losh 	char		**argvbuf;
93ba25195eSWarner Losh 
94ba25195eSWarner Losh 	/*
95ba25195eSWarner Losh 	 * Since argv is static, we need to save it here.
96ba25195eSWarner Losh 	 */
97ba25195eSWarner Losh 	argvbuf = (char**) calloc((u_int)argc, sizeof(char*));
98ba25195eSWarner Losh 	for (i = 0; i < argc; i++)
99ba25195eSWarner Losh 		argvbuf[i] = strdup(argv[i]);
100ba25195eSWarner Losh 
101ba25195eSWarner Losh 	res=CMD_OK;
102ba25195eSWarner Losh 	for (i = 1; (i < argc) && (res == CMD_OK); i++)
10379a6a17aSWarner Losh 		res = interp_include(argvbuf[i]);
104ba25195eSWarner Losh 
105ba25195eSWarner Losh 	for (i = 0; i < argc; i++)
106ba25195eSWarner Losh 		free(argvbuf[i]);
107ba25195eSWarner Losh 	free(argvbuf);
108ba25195eSWarner Losh 
109ba25195eSWarner Losh 	return(res);
110ba25195eSWarner Losh }
111ba25195eSWarner Losh 
112ba25195eSWarner Losh /*
113ca987d46SWarner Losh  * Emit the current prompt; use the same syntax as the parser
11479a6a17aSWarner Losh  * for embedding environment variables. Does not accept input.
115ca987d46SWarner Losh  */
11679a6a17aSWarner Losh void
11779a6a17aSWarner Losh interp_emit_prompt(void)
118ca987d46SWarner Losh {
119ca987d46SWarner Losh 	char		*pr, *p, *cp, *ev;
120ca987d46SWarner Losh 
121ca987d46SWarner Losh 	if ((cp = getenv("prompt")) == NULL)
122ca987d46SWarner Losh 		cp = ">";
123ca987d46SWarner Losh 	pr = p = strdup(cp);
124ca987d46SWarner Losh 
125ca987d46SWarner Losh 	while (*p != 0) {
126ca987d46SWarner Losh 		if ((*p == '$') && (*(p+1) == '{')) {
127ca987d46SWarner Losh 			for (cp = p + 2; (*cp != 0) && (*cp != '}'); cp++)
128ca987d46SWarner Losh 				;
129ca987d46SWarner Losh 			*cp = 0;
130ca987d46SWarner Losh 			ev = getenv(p + 2);
131ca987d46SWarner Losh 
132ca987d46SWarner Losh 			if (ev != NULL)
133ca987d46SWarner Losh 				printf("%s", ev);
134ca987d46SWarner Losh 			p = cp + 1;
135ca987d46SWarner Losh 			continue;
136ca987d46SWarner Losh 		}
137ca987d46SWarner Losh 		putchar(*p++);
138ca987d46SWarner Losh 	}
139ca987d46SWarner Losh 	putchar(' ');
140ca987d46SWarner Losh 	free(pr);
141ca987d46SWarner Losh }
142*3a4a3639SWarner Losh 
143*3a4a3639SWarner Losh /*
144*3a4a3639SWarner Losh  * Perform a builtin command
145*3a4a3639SWarner Losh  */
146*3a4a3639SWarner Losh int
147*3a4a3639SWarner Losh interp_builtin_cmd(int argc, char *argv[])
148*3a4a3639SWarner Losh {
149*3a4a3639SWarner Losh 	int			result;
150*3a4a3639SWarner Losh 	struct bootblk_command	**cmdp;
151*3a4a3639SWarner Losh 	bootblk_cmd_t		*cmd;
152*3a4a3639SWarner Losh 
153*3a4a3639SWarner Losh 	if (argc < 1)
154*3a4a3639SWarner Losh 		return(CMD_OK);
155*3a4a3639SWarner Losh 
156*3a4a3639SWarner Losh 	/* set return defaults; a successful command will override these */
157*3a4a3639SWarner Losh 	command_errmsg = command_errbuf;
158*3a4a3639SWarner Losh 	strcpy(command_errbuf, "no error message");
159*3a4a3639SWarner Losh 	cmd = NULL;
160*3a4a3639SWarner Losh 	result = CMD_ERROR;
161*3a4a3639SWarner Losh 
162*3a4a3639SWarner Losh 	/* search the command set for the command */
163*3a4a3639SWarner Losh 	SET_FOREACH(cmdp, Xcommand_set) {
164*3a4a3639SWarner Losh 		if (((*cmdp)->c_name != NULL) && !strcmp(argv[0], (*cmdp)->c_name))
165*3a4a3639SWarner Losh 			cmd = (*cmdp)->c_fn;
166*3a4a3639SWarner Losh 	}
167*3a4a3639SWarner Losh 	if (cmd != NULL) {
168*3a4a3639SWarner Losh 		result = (cmd)(argc, argv);
169*3a4a3639SWarner Losh 	} else {
170*3a4a3639SWarner Losh 		command_errmsg = "unknown command";
171*3a4a3639SWarner Losh 	}
172*3a4a3639SWarner Losh 	return(result);
173*3a4a3639SWarner Losh }
174