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#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "bootstrap.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeconst char *command_errmsg;
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* XXX should have procedural interface for setting, size limit? */
199767f8919635c4928607450d9e0abb932109ceToomas Soomechar command_errbuf[COMMAND_ERRBUFSZ];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int page_file(char *filename);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Help is read from a formatted text file.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Entries in the file are formatted as
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome# Ttopic [Ssubtopic] Ddescription
199767f8919635c4928607450d9e0abb932109ceToomas Soomehelp
199767f8919635c4928607450d9e0abb932109ceToomas Soometext
199767f8919635c4928607450d9e0abb932109ceToomas Soomehere
199767f8919635c4928607450d9e0abb932109ceToomas Soome#
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note that for code simplicity's sake, the above format must be followed
199767f8919635c4928607450d9e0abb932109ceToomas Soome * exactly.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Subtopic entries must immediately follow the topic (this is used to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * produce the listing of subtopics).
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If no argument(s) are supplied by the user, the help for 'help' is displayed.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(help, "help", "detailed help", command_help);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomehelp_getnext(int fd, char **topic, char **subtopic, char **desc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char line[81], *cp, *ep;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (;;) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fgetstr(line, 80, fd) < 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((strlen(line) < 3) || (line[0] != '#') || (line[1] != ' '))
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome *topic = *subtopic = *desc = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = line + 2;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while((cp != NULL) && (*cp != 0)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome ep = strchr(cp, ' ');
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((*cp == 'T') && (*topic == NULL)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ep != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *ep++ = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *topic = strdup(cp + 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if ((*cp == 'S') && (*subtopic == NULL)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ep != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *ep++ = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *subtopic = strdup(cp + 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (*cp == 'D') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome *desc = strdup(cp + 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ep = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = ep;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*topic == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*subtopic != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(*subtopic);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (*desc != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(*desc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomehelp_emitsummary(char *topic, char *subtopic, char *desc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(" ");
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(topic);
199767f8919635c4928607450d9e0abb932109ceToomas Soome i = strlen(topic);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (subtopic != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(" ");
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(subtopic);
199767f8919635c4928607450d9e0abb932109ceToomas Soome i += strlen(subtopic) + 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (desc != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome do {
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(" ");
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while (i++ < 30);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(desc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (pager_output("\n"));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_help(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char buf[81]; /* XXX buffer size? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int hfd, matched, doindex;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *topic, *subtopic, *t, *s, *d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* page the help text from our load path */
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(buf, "%s/boot/loader.help", getenv("loaddev"));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((hfd = open(buf, O_RDONLY)) < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Verbose help not available, use '?' to list commands\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* pick up request from arguments */
199767f8919635c4928607450d9e0abb932109ceToomas Soome topic = subtopic = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch(argc) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 3:
199767f8919635c4928607450d9e0abb932109ceToomas Soome subtopic = strdup(argv[2]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 2:
199767f8919635c4928607450d9e0abb932109ceToomas Soome topic = strdup(argv[1]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 1:
199767f8919635c4928607450d9e0abb932109ceToomas Soome topic = strdup("help");
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "usage is 'help <topic> [<subtopic>]";
199767f8919635c4928607450d9e0abb932109ceToomas Soome close(hfd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* magic "index" keyword */
199767f8919635c4928607450d9e0abb932109ceToomas Soome doindex = !strcmp(topic, "index");
199767f8919635c4928607450d9e0abb932109ceToomas Soome matched = doindex;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Scan the helpfile looking for help matching the request */
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_open();
199767f8919635c4928607450d9e0abb932109ceToomas Soome while(help_getnext(hfd, &t, &s, &d)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (doindex) { /* dink around formatting */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (help_emitsummary(t, s, d))
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (strcmp(topic, t)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* topic mismatch */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if(matched) /* nothing more on this topic, stop scanning */
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* topic matched */
199767f8919635c4928607450d9e0abb932109ceToomas Soome matched = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (((subtopic == NULL) && (s == NULL)) ||
199767f8919635c4928607450d9e0abb932109ceToomas Soome ((subtopic != NULL) && (s != NULL) && !strcmp(subtopic, s))) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* exact match, print text */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while((fgetstr(buf, 80, hfd) >= 0) && (buf[0] != '#')) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pager_output(buf))
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pager_output("\n"))
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if ((subtopic == NULL) && (s != NULL)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* topic match, list subtopics */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (help_emitsummary(t, s, d))
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(s);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(d);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_close();
199767f8919635c4928607450d9e0abb932109ceToomas Soome close(hfd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!matched) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "no help available for '%s'", topic);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(topic);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (subtopic)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(subtopic);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(topic);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (subtopic)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(subtopic);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(commandlist, "?", "list commands", command_commandlist);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_commandlist(int argc __attribute((unused)),
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *argv[] __attribute((unused)))
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct bootblk_command **cmdp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int res;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char name[20];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome res = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_open();
199767f8919635c4928607450d9e0abb932109ceToomas Soome res = pager_output("Available commands:\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome SET_FOREACH(cmdp, Xcommand_set) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (res)
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (((*cmdp)->c_name != NULL) && ((*cmdp)->c_desc != NULL)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(name, " %-15s ", (*cmdp)->c_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output((*cmdp)->c_desc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome res = pager_output("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_close();
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * XXX set/show should become set/echo if we have variable
199767f8919635c4928607450d9e0abb932109ceToomas Soome * substitution happening.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(show, "show", "show variable(s)", command_show);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_show(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct env_var *ev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *cp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc < 2) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * With no arguments, print everything.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_open();
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (ev = environ; ev != NULL; ev = ev->ev_next) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(ev->ev_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = getenv(ev->ev_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cp != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output("=");
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_output(cp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pager_output("\n"))
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_close();
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((cp = getenv(argv[1])) != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s\n", cp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "variable '%s' not found", argv[1]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(set, "set", "set a variable", command_set);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_set(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc != 2) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "wrong number of arguments";
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((err = putenv(argv[1])) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = strerror(err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(setprop, "setprop", "set a variable", command_setprop);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_setprop(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc != 3) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "wrong number of arguments";
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((err = setenv(argv[1], argv[2], 1)) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = strerror(err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(unset, "unset", "unset a variable", command_unset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_unset(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc != 2) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "wrong number of arguments";
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((err = unsetenv(argv[1])) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = strerror(err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(echo, "echo", "echo arguments", command_echo);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_echo(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *s;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int nl, ch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome nl = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome optind = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome optreset = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while ((ch = getopt(argc, argv, "n")) != -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch(ch) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'n':
199767f8919635c4928607450d9e0abb932109ceToomas Soome nl = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '?':
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* getopt has already reported an error */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome argv += (optind);
199767f8919635c4928607450d9e0abb932109ceToomas Soome argc -= (optind);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome s = unargv(argc, argv);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (s != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s", s);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(s);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!nl)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * A passable emulation of the sh(1) command of the same name.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(read, "read", "read input from the terminal", command_read);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_read(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *prompt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int timeout;
199767f8919635c4928607450d9e0abb932109ceToomas Soome time_t when;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *cp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *name;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char buf[256]; /* XXX size? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome timeout = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome prompt = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome optind = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome optreset = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while ((c = getopt(argc, argv, "p:t:")) != -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch(c) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'p':
199767f8919635c4928607450d9e0abb932109ceToomas Soome prompt = optarg;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 't':
199767f8919635c4928607450d9e0abb932109ceToomas Soome timeout = strtol(optarg, &cp, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cp == optarg) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "bad timeout '%s'", optarg);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome argv += (optind);
199767f8919635c4928607450d9e0abb932109ceToomas Soome argc -= (optind);
199767f8919635c4928607450d9e0abb932109ceToomas Soome name = (argc > 0) ? argv[0]: NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (prompt != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s", prompt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (timeout >= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome when = time(NULL) + timeout;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (!ischar())
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (time(NULL) >= when)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK); /* is timeout an error? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ngets(buf, sizeof(buf));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (name != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv(name, buf, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * File pager
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(more, "more", "show contents of a file", command_more);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_more(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int res;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char line[80];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome res=0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_open();
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 1; (i < argc) && (res == 0); i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, "*** FILE %s BEGIN ***\n", argv[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pager_output(line))
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome res = page_file(argv[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!res) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, "*** FILE %s END ***\n", argv[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome res = pager_output(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_close();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (res == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return CMD_OK;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome return CMD_ERROR;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomepage_file(char *filename)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int result;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome result = pager_file(filename);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (result == -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(command_errbuf, sizeof (command_errbuf),
199767f8919635c4928607450d9e0abb932109ceToomas Soome "error showing %s", filename);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return result;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * List all disk-like devices
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(lsdev, "lsdev", "list all devices", command_lsdev);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_lsdev(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int verbose, ch, i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char line[80];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome verbose = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome optind = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome optreset = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while ((ch = getopt(argc, argv, "v")) != -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch(ch) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'v':
199767f8919635c4928607450d9e0abb932109ceToomas Soome verbose = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '?':
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* getopt has already reported an error */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome argv += (optind);
199767f8919635c4928607450d9e0abb932109ceToomas Soome argc -= (optind);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_open();
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; devsw[i] != NULL; i++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (devsw[i]->dv_print != NULL){
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, "%s devices:\n", devsw[i]->dv_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pager_output(line))
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (devsw[i]->dv_print(verbose))
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(line, "%s: (unknown)\n", devsw[i]->dv_name);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pager_output(line))
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome pager_close();
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}