199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 * are met:
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 *
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Simple commandline interpreter, toplevel and misc.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * XXX may be obsoleted by BootFORTH or some other, better, interpreter.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "bootstrap.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef BOOT_FORTH
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "ficl.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define RETURN(x) ficlStackPushInteger(ficlVmGetDataStack(bf_vm),!x); return(x)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern ficlVm *bf_vm;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define RETURN(x) return(x)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "linenoise/linenoise.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define MAXARGS 20 /* maximum number of arguments allowed */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char *prompt(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef BOOT_FORTH
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int perform(int argc, char *argv[]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Perform the command
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomeperform(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int result;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct bootblk_command **cmdp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bootblk_cmd_t *cmd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc < 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* set return defaults; a successful command will override these */
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = command_errbuf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcpy(command_errbuf, "no error message");
199767f8919635c4928607450d9e0abb932109ceToomas Soome cmd = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome result = CMD_ERROR;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* search the command set for the command */
199767f8919635c4928607450d9e0abb932109ceToomas Soome SET_FOREACH(cmdp, Xcommand_set) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (((*cmdp)->c_name != NULL) && !strcmp(argv[0], (*cmdp)->c_name))
199767f8919635c4928607450d9e0abb932109ceToomas Soome cmd = (*cmdp)->c_fn;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cmd != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome result = (cmd)(argc, argv);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "unknown command";
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome RETURN(result);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* ! BOOT_FORTH */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Interactive mode
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomeinteract(const char *rc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *input = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bf_init((rc) ? "" : NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rc == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Read our default configuration. */
b85b7e0b09678b7e318428f159109a868355dd38Toomas Soome include("/boot/loader.rc");
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (*rc != '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome include(rc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Before interacting, we might want to autoboot.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome autoboot_maybe();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Not autobooting, go manual
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\nType '?' for a list of commands, 'help' for more detailed help.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (getenv("prompt") == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("prompt", "${interpret}", 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (getenv("interpret") == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("interpret", "ok", 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while ((input = linenoise(prompt())) != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome bf_vm->sourceId.i = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bf_run(input);
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseHistoryAdd(input);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(input);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Read commands from a file, then execute them.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 *
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 Soome */
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(include, "include", "read commands from a file", command_include);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_include(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int res;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char **argvbuf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Since argv is static, we need to save it here.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome argvbuf = (char**) calloc((u_int)argc, sizeof(char*));
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < argc; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome argvbuf[i] = strdup(argv[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome res=CMD_OK;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 1; (i < argc) && (res == CMD_OK); i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome res = include(argvbuf[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < argc; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(argvbuf[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(argvbuf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(res);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct includeline
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct includeline *next;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int line;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char text[0];
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomeinclude(const char *filename)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct includeline *script, *se, *sp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int res = CMD_OK;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int prevsrcid, fd, line;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *cp, input[256]; /* big enough? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (((fd = open(filename, O_RDONLY)) == -1)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf), "can't open '%s': %s",
199767f8919635c4928607450d9e0abb932109ceToomas Soome filename, strerror(errno));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Read the script into memory.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome script = se = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome line = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (fgetstr(input, sizeof(input), fd) >= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome line++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = input;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Allocate script line structure and copy line, flags */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*cp == '\0')
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue; /* ignore empty line, save memory */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cp[0] == '\\' && cp[1] == ' ')
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue; /* ignore comment */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 if (sp == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (script != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome se = script;
199767f8919635c4928607450d9e0abb932109ceToomas Soome script = script->next;
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(se);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "file '%s' line %d: memory allocation failure - aborting",
199767f8919635c4928607450d9e0abb932109ceToomas Soome filename, line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome close(fd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcpy(sp->text, cp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sp->line = line;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sp->next = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (script == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome script = sp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome se->next = sp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome se = sp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome close(fd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Execute the script
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome prevsrcid = bf_vm->sourceId.i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bf_vm->sourceId.i = fd+1; /* 0 is user input device */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome res = CMD_OK;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (sp = script; sp != NULL; sp = sp->next) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome res = bf_run(sp->text);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (res != FICL_VM_STATUS_OUT_OF_TEXT) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "Error while including %s, in the line %d:\n%s",
199767f8919635c4928607450d9e0abb932109ceToomas Soome filename, sp->line, sp->text);
199767f8919635c4928607450d9e0abb932109ceToomas Soome res = CMD_ERROR;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else
199767f8919635c4928607450d9e0abb932109ceToomas Soome res = CMD_OK;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bf_vm->sourceId.i = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void) bf_run("");
199767f8919635c4928607450d9e0abb932109ceToomas Soome bf_vm->sourceId.i = prevsrcid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while(script != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome se = script;
199767f8919635c4928607450d9e0abb932109ceToomas Soome script = script->next;
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(se);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(res);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Emit the current prompt; use the same syntax as the parser
199767f8919635c4928607450d9e0abb932109ceToomas Soome * for embedding environment variables.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char *
199767f8919635c4928607450d9e0abb932109ceToomas Soomeprompt(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome static char promptbuf[20]; /* probably too large, but well... */
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *pr, *p, *cp, *ev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int n = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((cp = getenv("prompt")) == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = (char *)(uintptr_t)">";
199767f8919635c4928607450d9e0abb932109ceToomas Soome pr = p = strdup(cp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (*p != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((*p == '$') && (*(p+1) == '{')) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (cp = p + 2; (*cp != 0) && (*cp != '}'); cp++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome ;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *cp = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ev = getenv(p + 2);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ev != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome n = sprintf(promptbuf+n, "%s", ev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome p = cp + 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome promptbuf[n++] = *p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome p++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (promptbuf[n - 1] != ' ')
199767f8919635c4928607450d9e0abb932109ceToomas Soome promptbuf[n++] = ' ';
199767f8919635c4928607450d9e0abb932109ceToomas Soome promptbuf[n] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(pr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (promptbuf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}