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 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_init(); 64 65 printf("\n"); 66 67 /* 68 * Before interacting, we might want to autoboot. 69 */ 70 autoboot_maybe(); 71 72 /* 73 * Not autobooting, go manual 74 */ 75 printf("\nType '?' for a list of commands, 'help' for more detailed help.\n"); 76 if (getenv("prompt") == NULL) 77 setenv("prompt", "${interpret}", 1); 78 if (getenv("interpret") == NULL) 79 setenv("interpret", "OK", 1); 80 81 for (;;) { 82 input[0] = '\0'; 83 interp_emit_prompt(); 84 ngets(input, sizeof(input)); 85 #ifdef LOADER_VERIEXEC 86 /* some settings should be restritcted */ 87 ve_status_set(-1, VE_UNVERIFIED_OK); 88 #endif 89 interp_run(input); 90 } 91 } 92 93 /* 94 * Read commands from a file, then execute them. 95 * 96 * We store the commands in memory and close the source file so that the media 97 * holding it can safely go away while we are executing. 98 * 99 * Commands may be prefixed with '@' (so they aren't displayed) or '-' (so 100 * that the script won't stop if they fail). 101 */ 102 COMMAND_SET(include, "include", "read commands from a file", command_include); 103 104 static int 105 command_include(int argc, char *argv[]) 106 { 107 int i; 108 int res; 109 char **argvbuf; 110 111 /* 112 * Since argv is static, we need to save it here. 113 */ 114 argvbuf = (char**) calloc((u_int)argc, sizeof(char*)); 115 for (i = 0; i < argc; i++) 116 argvbuf[i] = strdup(argv[i]); 117 118 res=CMD_OK; 119 for (i = 1; (i < argc) && (res == CMD_OK); i++) 120 res = interp_include(argvbuf[i]); 121 122 for (i = 0; i < argc; i++) 123 free(argvbuf[i]); 124 free(argvbuf); 125 126 return(res); 127 } 128 129 /* 130 * Emit the current prompt; use the same syntax as the parser 131 * for embedding environment variables. Does not accept input. 132 */ 133 void 134 interp_emit_prompt(void) 135 { 136 char *pr, *p, *cp, *ev; 137 138 if ((cp = getenv("prompt")) == NULL) 139 cp = ">"; 140 pr = p = strdup(cp); 141 142 while (*p != 0) { 143 if ((*p == '$') && (*(p+1) == '{')) { 144 for (cp = p + 2; (*cp != 0) && (*cp != '}'); cp++) 145 ; 146 *cp = 0; 147 ev = getenv(p + 2); 148 149 if (ev != NULL) 150 printf("%s", ev); 151 p = cp + 1; 152 continue; 153 } 154 putchar(*p++); 155 } 156 putchar(' '); 157 free(pr); 158 } 159 160 static struct bootblk_command * 161 interp_lookup_cmd(const char *cmd) 162 { 163 struct bootblk_command **cmdp; 164 165 /* search the command set for the command */ 166 SET_FOREACH(cmdp, Xcommand_set) { 167 if (((*cmdp)->c_name != NULL) && !strcmp(cmd, (*cmdp)->c_name)) 168 return (*cmdp); 169 } 170 return (NULL); 171 } 172 173 /* 174 * Perform a builtin command 175 */ 176 int 177 interp_builtin_cmd(int argc, char *argv[]) 178 { 179 int result; 180 struct bootblk_command *cmd; 181 182 if (argc < 1) 183 return (CMD_OK); 184 185 /* set return defaults; a successful command will override these */ 186 command_errmsg = command_errbuf; 187 strcpy(command_errbuf, "no error message"); 188 result = CMD_ERROR; 189 190 cmd = interp_lookup_cmd(argv[0]); 191 if (cmd != NULL && cmd->c_fn) { 192 result = cmd->c_fn(argc, argv); 193 } else { 194 command_errmsg = "unknown command"; 195 } 196 return (result); 197 } 198 199 /* 200 * Return true if the builtin command exists 201 */ 202 bool 203 interp_has_builtin_cmd(const char *cmd) 204 { 205 return (interp_lookup_cmd(cmd) != NULL); 206 } 207