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