2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
33 #include "bootstrap.h"
36 char command_errbuf[256]; /* XXX should have procedural interface for setting, size limit? */
38 static int page_file(char *filename);
41 * Help is read from a formatted text file.
43 * Entries in the file are formatted as
45 # Ttopic [Ssubtopic] Ddescription
52 * Note that for code simplicity's sake, the above format must be followed
55 * Subtopic entries must immediately follow the topic (this is used to
56 * produce the listing of subtopics).
58 * If no argument(s) are supplied by the user, the help for 'help' is displayed.
60 COMMAND_SET(help, "help", "detailed help", command_help);
63 help_getnext(int fd, char **topic, char **subtopic, char **desc)
65 char line[81], *cp, *ep;
68 if (fgetstr(line, 80, fd) < 0)
71 if ((strlen(line) < 3) || (line[0] != '#') || (line[1] != ' '))
74 *topic = *subtopic = *desc = NULL;
76 while((cp != NULL) && (*cp != 0)) {
78 if ((*cp == 'T') && (*topic == NULL)) {
81 *topic = strdup(cp + 1);
82 } else if ((*cp == 'S') && (*subtopic == NULL)) {
85 *subtopic = strdup(cp + 1);
86 } else if (*cp == 'D') {
87 *desc = strdup(cp + 1);
93 if (*subtopic != NULL)
104 help_emitsummary(char *topic, char *subtopic, char *desc)
111 if (subtopic != NULL) {
113 pager_output(subtopic);
114 i += strlen(subtopic) + 1;
122 return (pager_output("\n"));
127 command_help(int argc, char *argv[])
129 char buf[81]; /* XXX buffer size? */
130 int hfd, matched, doindex;
131 char *topic, *subtopic, *t, *s, *d;
133 /* page the help text from our load path */
134 sprintf(buf, "%s/boot/loader.help", getenv("loaddev"));
135 if ((hfd = open(buf, O_RDONLY)) < 0) {
136 printf("Verbose help not available, use '?' to list commands\n");
140 /* pick up request from arguments */
141 topic = subtopic = NULL;
144 subtopic = strdup(argv[2]);
146 topic = strdup(argv[1]);
149 topic = strdup("help");
152 command_errmsg = "usage is 'help <topic> [<subtopic>]";
157 /* magic "index" keyword */
158 doindex = !strcmp(topic, "index");
161 /* Scan the helpfile looking for help matching the request */
163 while(help_getnext(hfd, &t, &s, &d)) {
165 if (doindex) { /* dink around formatting */
166 if (help_emitsummary(t, s, d))
169 } else if (strcmp(topic, t)) {
171 if(matched) /* nothing more on this topic, stop scanning */
177 if (((subtopic == NULL) && (s == NULL)) ||
178 ((subtopic != NULL) && (s != NULL) && !strcmp(subtopic, s))) {
179 /* exact match, print text */
180 while((fgetstr(buf, 80, hfd) >= 0) && (buf[0] != '#')) {
181 if (pager_output(buf))
183 if (pager_output("\n"))
186 } else if ((subtopic == NULL) && (s != NULL)) {
187 /* topic match, list subtopics */
188 if (help_emitsummary(t, s, d))
199 sprintf(command_errbuf, "no help available for '%s'", topic);
212 COMMAND_SET(commandlist, "?", "list commands", command_commandlist);
215 command_commandlist(int argc, char *argv[])
217 struct bootblk_command **cmdp;
223 res = pager_output("Available commands:\n");
224 SET_FOREACH(cmdp, Xcommand_set) {
227 if (((*cmdp)->c_name != NULL) && ((*cmdp)->c_desc != NULL)) {
228 sprintf(name, " %-15s ", (*cmdp)->c_name);
230 pager_output((*cmdp)->c_desc);
231 res = pager_output("\n");
239 * XXX set/show should become set/echo if we have variable
240 * substitution happening.
243 COMMAND_SET(show, "show", "show variable(s)", command_show);
246 command_show(int argc, char *argv[])
253 * With no arguments, print everything.
256 for (ev = environ; ev != NULL; ev = ev->ev_next) {
257 pager_output(ev->ev_name);
258 cp = getenv(ev->ev_name);
263 if (pager_output("\n"))
268 if ((cp = getenv(argv[1])) != NULL) {
271 sprintf(command_errbuf, "variable '%s' not found", argv[1]);
278 COMMAND_SET(set, "set", "set a variable", command_set);
281 command_set(int argc, char *argv[])
286 command_errmsg = "wrong number of arguments";
289 if ((err = putenv(argv[1])) != 0) {
290 command_errmsg = strerror(err);
297 COMMAND_SET(unset, "unset", "unset a variable", command_unset);
300 command_unset(int argc, char *argv[])
305 command_errmsg = "wrong number of arguments";
308 if ((err = unsetenv(argv[1])) != 0) {
309 command_errmsg = strerror(err);
316 COMMAND_SET(echo, "echo", "echo arguments", command_echo);
319 command_echo(int argc, char *argv[])
327 while ((ch = getopt(argc, argv, "n")) != -1) {
334 /* getopt has already reported an error */
341 s = unargv(argc, argv);
352 * A passable emulation of the sh(1) command of the same name.
355 COMMAND_SET(read, "read", "read input from the terminal", command_read);
358 command_read(int argc, char *argv[])
365 char buf[256]; /* XXX size? */
372 while ((c = getopt(argc, argv, "p:t:")) != -1) {
379 timeout = strtol(optarg, &cp, 0);
381 sprintf(command_errbuf, "bad timeout '%s'", optarg);
392 name = (argc > 0) ? argv[0]: NULL;
395 printf("%s", prompt);
397 when = time(NULL) + timeout;
399 if (time(NULL) >= when)
400 return(CMD_OK); /* is timeout an error? */
403 ngets(buf, sizeof(buf));
406 setenv(name, buf, 1);
413 COMMAND_SET(more, "more", "show contents of a file", command_more);
416 command_more(int argc, char *argv[])
424 for (i = 1; (i < argc) && (res == 0); i++) {
425 sprintf(line, "*** FILE %s BEGIN ***\n", argv[i]);
426 if (pager_output(line))
428 res = page_file(argv[i]);
430 sprintf(line, "*** FILE %s END ***\n", argv[i]);
431 res = pager_output(line);
443 page_file(char *filename)
447 result = pager_file(filename);
450 sprintf(command_errbuf, "error showing %s", filename);
456 * List all disk-like devices
458 COMMAND_SET(lsdev, "lsdev", "list all devices", command_lsdev);
461 command_lsdev(int argc, char *argv[])
469 while ((ch = getopt(argc, argv, "v")) != -1) {
476 /* getopt has already reported an error */
484 for (i = 0; devsw[i] != NULL; i++) {
485 if (devsw[i]->dv_print != NULL){
486 sprintf(line, "%s devices:\n", devsw[i]->dv_name);
487 if (pager_output(line))
489 devsw[i]->dv_print(verbose);
491 sprintf(line, "%s: (unknown)\n", devsw[i]->dv_name);
492 if (pager_output(line))