199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Simple commandline interpreter, toplevel and misc.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * XXX may be obsoleted by BootFORTH or some other, better, interpreter.
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define RETURN(x) ficlStackPushInteger(ficlVmGetDataStack(bf_vm),!x); return(x)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define MAXARGS 20 /* maximum number of arguments allowed */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char *prompt(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Perform the command
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* set return defaults; a successful command will override these */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* search the command set for the command */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (((*cmdp)->c_name != NULL) && !strcmp(argv[0], (*cmdp)->c_name))
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* ! BOOT_FORTH */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Interactive mode
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Read our default configuration. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Before interacting, we might want to autoboot.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Not autobooting, go manual
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\nType '?' for a list of commands, 'help' for more detailed help.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome while ((input = linenoise(prompt())) != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Read commands from a file, then execute them.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We store the commands in memory and close the source file so that the media
199767f8919635c4928607450d9e0abb932109ceToomas Soome * holding it can safely go away while we are executing.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Commands may be prefixed with '@' (so they aren't displayed) or '-' (so
199767f8919635c4928607450d9e0abb932109ceToomas Soome * that the script won't stop if they fail).
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(include, "include", "read commands from a file", command_include);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Since argv is static, we need to save it here.
199767f8919635c4928607450d9e0abb932109ceToomas Soome argvbuf = (char**) calloc((u_int)argc, sizeof(char*));
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < argc; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 1; (i < argc) && (res == CMD_OK); i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < argc; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Header prepended to each line. The text immediately follows the header.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We try to make this short in order to save memory -- the loader has
199767f8919635c4928607450d9e0abb932109ceToomas Soome * limited memory available, and some of the forth files are very long.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The PXE TFTP service allows opening exactly one connection at the time,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * so we need to read included file into memory, then process line by line
199767f8919635c4928607450d9e0abb932109ceToomas Soome * as it may contain embedded include commands.
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf), "can't open '%s': %s",
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Read the script into memory.
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (fgetstr(input, sizeof(input), fd) >= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Allocate script line structure and copy line, flags */
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue; /* ignore empty line, save memory */
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue; /* ignore comment */
199767f8919635c4928607450d9e0abb932109ceToomas Soome sp = malloc(sizeof(struct includeline) + strlen(cp) + 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* On malloc failure (it happens!), free as much as possible and exit */
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "file '%s' line %d: memory allocation failure - aborting",
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Execute the script
199767f8919635c4928607450d9e0abb932109ceToomas Soome bf_vm->sourceId.i = fd+1; /* 0 is user input device */
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "Error while including %s, in the line %d:\n%s",
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Emit the current prompt; use the same syntax as the parser
199767f8919635c4928607450d9e0abb932109ceToomas Soome * for embedding environment variables.
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char *
199767f8919635c4928607450d9e0abb932109ceToomas Soome static char promptbuf[20]; /* probably too large, but well... */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*p != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (cp = p + 2; (*cp != 0) && (*cp != '}'); cp++)