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 /* XXX should have procedural interface for setting, size limit? */
37 char command_errbuf[COMMAND_ERRBUFSZ];
39 static int page_file(char *filename);
42 * Help is read from a formatted text file.
44 * Entries in the file are formatted as
46 # Ttopic [Ssubtopic] Ddescription
53 * Note that for code simplicity's sake, the above format must be followed
56 * Subtopic entries must immediately follow the topic (this is used to
57 * produce the listing of subtopics).
59 * If no argument(s) are supplied by the user, the help for 'help' is displayed.
61 COMMAND_SET(help, "help", "detailed help", command_help);
64 help_getnext(int fd, char **topic, char **subtopic, char **desc)
66 char line[81], *cp, *ep;
69 if (fgetstr(line, 80, fd) < 0)
72 if ((strlen(line) < 3) || (line[0] != '#') || (line[1] != ' '))
75 *topic = *subtopic = *desc = NULL;
77 while((cp != NULL) && (*cp != 0)) {
79 if ((*cp == 'T') && (*topic == NULL)) {
82 *topic = strdup(cp + 1);
83 } else if ((*cp == 'S') && (*subtopic == NULL)) {
86 *subtopic = strdup(cp + 1);
87 } else if (*cp == 'D') {
88 *desc = strdup(cp + 1);
94 if (*subtopic != NULL)
105 help_emitsummary(char *topic, char *subtopic, char *desc)
112 if (subtopic != NULL) {
114 pager_output(subtopic);
115 i += strlen(subtopic) + 1;
123 return (pager_output("\n"));
128 command_help(int argc, char *argv[])
130 char buf[81]; /* XXX buffer size? */
131 int hfd, matched, doindex;
132 char *topic, *subtopic, *t, *s, *d;
134 /* page the help text from our load path */
135 snprintf(buf, sizeof(buf), "%s/boot/loader.help", getenv("loaddev"));
136 if ((hfd = open(buf, O_RDONLY)) < 0) {
137 printf("Verbose help not available, use '?' to list commands\n");
141 /* pick up request from arguments */
142 topic = subtopic = NULL;
145 subtopic = strdup(argv[2]);
147 topic = strdup(argv[1]);
150 topic = strdup("help");
153 command_errmsg = "usage is 'help <topic> [<subtopic>]";
158 /* magic "index" keyword */
159 doindex = !strcmp(topic, "index");
162 /* Scan the helpfile looking for help matching the request */
164 while(help_getnext(hfd, &t, &s, &d)) {
166 if (doindex) { /* dink around formatting */
167 if (help_emitsummary(t, s, d))
170 } else if (strcmp(topic, t)) {
172 if(matched) /* nothing more on this topic, stop scanning */
178 if (((subtopic == NULL) && (s == NULL)) ||
179 ((subtopic != NULL) && (s != NULL) && !strcmp(subtopic, s))) {
180 /* exact match, print text */
181 while((fgetstr(buf, 80, hfd) >= 0) && (buf[0] != '#')) {
182 if (pager_output(buf))
184 if (pager_output("\n"))
187 } else if ((subtopic == NULL) && (s != NULL)) {
188 /* topic match, list subtopics */
189 if (help_emitsummary(t, s, d))
200 snprintf(command_errbuf, sizeof(command_errbuf),
201 "no help available for '%s'", topic);
214 COMMAND_SET(commandlist, "?", "list commands", command_commandlist);
217 * Please note: although we use the pager for the list of commands,
218 * this routine is called from the ? FORTH function which then
219 * unconditionally prints some commands. This will lead to anomalous
220 * behavior. There's no 'pager_output' binding to FORTH to allow
221 * things to work right, so I'm documenting the bug rather than
225 command_commandlist(int argc, char *argv[])
227 struct bootblk_command **cmdp;
233 res = pager_output("Available commands:\n");
234 SET_FOREACH(cmdp, Xcommand_set) {
237 if (((*cmdp)->c_name != NULL) && ((*cmdp)->c_desc != NULL)) {
238 sprintf(name, " %-15s ", (*cmdp)->c_name);
240 pager_output((*cmdp)->c_desc);
241 res = pager_output("\n");
249 * XXX set/show should become set/echo if we have variable
250 * substitution happening.
253 COMMAND_SET(show, "show", "show variable(s)", command_show);
256 command_show(int argc, char *argv[])
263 * With no arguments, print everything.
266 for (ev = environ; ev != NULL; ev = ev->ev_next) {
267 pager_output(ev->ev_name);
268 cp = getenv(ev->ev_name);
273 if (pager_output("\n"))
278 if ((cp = getenv(argv[1])) != NULL) {
281 snprintf(command_errbuf, sizeof(command_errbuf),
282 "variable '%s' not found", argv[1]);
289 COMMAND_SET(set, "set", "set a variable", command_set);
292 command_set(int argc, char *argv[])
297 command_errmsg = "wrong number of arguments";
300 if ((err = putenv(argv[1])) != 0) {
301 command_errmsg = strerror(err);
308 COMMAND_SET(unset, "unset", "unset a variable", command_unset);
311 command_unset(int argc, char *argv[])
316 command_errmsg = "wrong number of arguments";
319 if ((err = unsetenv(argv[1])) != 0) {
320 command_errmsg = strerror(err);
327 COMMAND_SET(echo, "echo", "echo arguments", command_echo);
330 command_echo(int argc, char *argv[])
338 while ((ch = getopt(argc, argv, "n")) != -1) {
345 /* getopt has already reported an error */
352 s = unargv(argc, argv);
363 * A passable emulation of the sh(1) command of the same name.
366 COMMAND_SET(read, "read", "read input from the terminal", command_read);
369 command_read(int argc, char *argv[])
376 char buf[256]; /* XXX size? */
383 while ((c = getopt(argc, argv, "p:t:")) != -1) {
390 timeout = strtol(optarg, &cp, 0);
392 snprintf(command_errbuf, sizeof(command_errbuf),
393 "bad timeout '%s'", optarg);
404 name = (argc > 0) ? argv[0]: NULL;
407 printf("%s", prompt);
409 when = time(NULL) + timeout;
411 if (time(NULL) >= when)
412 return(CMD_OK); /* is timeout an error? */
415 ngets(buf, sizeof(buf));
418 setenv(name, buf, 1);
425 COMMAND_SET(more, "more", "show contents of a file", command_more);
428 command_more(int argc, char *argv[])
436 for (i = 1; (i < argc) && (res == 0); i++) {
437 sprintf(line, "*** FILE %s BEGIN ***\n", argv[i]);
438 if (pager_output(line))
440 res = page_file(argv[i]);
442 sprintf(line, "*** FILE %s END ***\n", argv[i]);
443 res = pager_output(line);
455 page_file(char *filename)
459 result = pager_file(filename);
462 snprintf(command_errbuf, sizeof(command_errbuf),
463 "error showing %s", filename);
470 * List all disk-like devices
472 COMMAND_SET(lsdev, "lsdev", "list all devices", command_lsdev);
475 command_lsdev(int argc, char *argv[])
483 while ((ch = getopt(argc, argv, "v")) != -1) {
490 /* getopt has already reported an error */
498 for (i = 0; devsw[i] != NULL; i++) {
499 if (devsw[i]->dv_print != NULL){
500 if (devsw[i]->dv_print(verbose))
503 sprintf(line, "%s: (unknown)\n", devsw[i]->dv_name);
504 if (pager_output(line))