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>
31 #include "bootstrap.h"
33 const char *command_errmsg;
34 /* XXX should have procedural interface for setting, size limit? */
35 char command_errbuf[COMMAND_ERRBUFSZ];
37 static int page_file(char *filename);
40 * Help is read from a formatted text file.
42 * Entries in the file are formatted as
44 # Ttopic [Ssubtopic] Ddescription
51 * Note that for code simplicity's sake, the above format must be followed
54 * Subtopic entries must immediately follow the topic (this is used to
55 * produce the listing of subtopics).
57 * If no argument(s) are supplied by the user, the help for 'help' is displayed.
59 COMMAND_SET(help, "help", "detailed help", command_help);
62 help_getnext(int fd, char **topic, char **subtopic, char **desc)
64 char line[81], *cp, *ep;
66 /* Make sure we provide sane values. */
67 *topic = *subtopic = *desc = NULL;
69 if (fgetstr(line, 80, fd) < 0)
72 if (strlen(line) < 3 || line[0] != '#' || line[1] != ' ')
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);
95 *subtopic = *desc = NULL;
103 help_emitsummary(char *topic, char *subtopic, char *desc)
110 if (subtopic != NULL) {
112 pager_output(subtopic);
113 i += strlen(subtopic) + 1;
121 return (pager_output("\n"));
125 command_help(int argc, char *argv[])
127 char buf[81]; /* XXX buffer size? */
128 int hfd, matched, doindex;
129 char *topic, *subtopic, *t, *s, *d;
131 /* page the help text from our load path */
132 snprintf(buf, sizeof(buf), "%s/boot/%s", getenv("loaddev"),
134 if ((hfd = open(buf, O_RDONLY)) < 0) {
135 printf("Verbose help not available, "
136 "use '?' to list commands\n");
140 /* pick up request from arguments */
141 topic = subtopic = NULL;
144 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") == 0? 1 : 0;
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)) {
173 /* nothing more on this topic, stop scanning */
179 if ((subtopic == NULL && s == NULL) ||
180 (subtopic != NULL && s != NULL &&
181 strcmp(subtopic, s) == 0)) {
182 /* exact match, print text */
183 while (fgetstr(buf, 80, hfd) >= 0 &&
185 if (pager_output(buf))
187 if (pager_output("\n"))
190 } else if (subtopic == NULL && s != NULL) {
191 /* topic match, list subtopics */
192 if (help_emitsummary(t, s, d))
207 snprintf(command_errbuf, sizeof(command_errbuf),
208 "no help available for '%s'", topic);
218 COMMAND_SET(commandlist, "?", "list commands", command_commandlist);
221 * Please note: although we use the pager for the list of commands,
222 * this routine is called from the ? FORTH function which then
223 * unconditionally prints some commands. This will lead to anomalous
224 * behavior. There's no 'pager_output' binding to FORTH to allow
225 * things to work right, so I'm documenting the bug rather than
229 command_commandlist(int argc __unused, char *argv[] __unused)
231 struct bootblk_command **cmdp;
237 res = pager_output("Available commands:\n");
238 SET_FOREACH(cmdp, Xcommand_set) {
241 if ((*cmdp)->c_name != NULL && (*cmdp)->c_desc != NULL) {
242 snprintf(name, sizeof(name), " %-15s ",
245 pager_output((*cmdp)->c_desc);
246 res = pager_output("\n");
254 * XXX set/show should become set/echo if we have variable
255 * substitution happening.
258 COMMAND_SET(show, "show", "show variable(s)", command_show);
261 command_show(int argc, char *argv[])
268 * With no arguments, print everything.
271 for (ev = environ; ev != NULL; ev = ev->ev_next) {
272 pager_output(ev->ev_name);
273 cp = getenv(ev->ev_name);
278 if (pager_output("\n"))
283 if ((cp = getenv(argv[1])) != NULL) {
286 snprintf(command_errbuf, sizeof(command_errbuf),
287 "variable '%s' not found", argv[1]);
294 COMMAND_SET(set, "set", "set a variable", command_set);
297 command_set(int argc, char *argv[])
302 command_errmsg = "wrong number of arguments";
305 #ifdef LOADER_VERIEXEC
307 * Impose restrictions if input is not verified
309 const char *restricted[] = {
321 ves = ve_status_get(-1);
322 if (ves == VE_UNVERIFIED_OK) {
323 #ifdef LOADER_VERIEXEC_TESTING
324 printf("Checking: %s\n", argv[1]);
326 for (cp = restricted; *cp; cp++) {
327 if (strncmp(argv[1], *cp, strlen(*cp)) == 0) {
328 printf("Ignoring restricted variable: %s\n",
335 if ((err = putenv(argv[1])) != 0) {
336 command_errmsg = strerror(err);
343 COMMAND_SET(unset, "unset", "unset a variable", command_unset);
346 command_unset(int argc, char *argv[])
351 command_errmsg = "wrong number of arguments";
354 if ((err = unsetenv(argv[1])) != 0) {
355 command_errmsg = strerror(err);
362 COMMAND_SET(echo, "echo", "echo arguments", command_echo);
365 command_echo(int argc, char *argv[])
373 while ((ch = getopt(argc, argv, "n")) != -1) {
380 /* getopt has already reported an error */
387 s = unargv(argc, argv);
398 * A passable emulation of the sh(1) command of the same name.
401 COMMAND_SET(read, "read", "read input from the terminal", command_read);
404 command_read(int argc, char *argv[])
411 char buf[256]; /* XXX size? */
418 while ((c = getopt(argc, argv, "p:t:")) != -1) {
424 timeout = strtol(optarg, &cp, 0);
426 snprintf(command_errbuf,
427 sizeof(command_errbuf),
428 "bad timeout '%s'", optarg);
439 name = (argc > 0) ? argv[0]: NULL;
442 printf("%s", prompt);
444 when = time(NULL) + timeout;
446 if (time(NULL) >= when)
447 return (CMD_OK); /* is timeout an error? */
450 ngets(buf, sizeof(buf));
453 setenv(name, buf, 1);
460 COMMAND_SET(more, "more", "show contents of a file", command_more);
463 command_more(int argc, char *argv[])
471 for (i = 1; (i < argc) && (res == 0); i++) {
472 snprintf(line, sizeof(line), "*** FILE %s BEGIN ***\n",
474 if (pager_output(line))
476 res = page_file(argv[i]);
478 snprintf(line, sizeof(line), "*** FILE %s END ***\n",
480 res = pager_output(line);
489 page_file(char *filename)
493 result = pager_file(filename);
496 snprintf(command_errbuf, sizeof(command_errbuf),
497 "error showing %s", filename);
504 * List all disk-like devices
506 COMMAND_SET(lsdev, "lsdev", "list all devices", command_lsdev);
509 command_lsdev(int argc, char *argv[])
517 while ((ch = getopt(argc, argv, "v")) != -1) {
524 /* getopt has already reported an error */
532 for (i = 0; devsw[i] != NULL; i++) {
533 if (devsw[i]->dv_print != NULL) {
534 if (devsw[i]->dv_print(verbose))
537 snprintf(line, sizeof(line), "%s: (unknown)\n",
539 if (pager_output(line))
548 command_readtest(int argc, char *argv[])
553 ssize_t rv, count = 0;
556 snprintf(command_errbuf, sizeof(command_errbuf),
557 "Usage: readtest <filename>");
562 if ((fd = open(argv[1], O_RDONLY)) < 0) {
563 snprintf(command_errbuf, sizeof(command_errbuf),
564 "can't open '%s'", argv[1]);
567 while ((rv = read(fd, buf, sizeof(buf))) > 0)
571 printf("Received %zd bytes during %jd seconds\n", count, (intmax_t)end - start);
576 COMMAND_SET(readtest, "readtest", "Time a file read", command_readtest);
579 command_quit(int argc, char *argv[])
585 COMMAND_SET(quit, "quit", "exit the loader", command_quit);