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
26 * $FreeBSD: src/sys/boot/common/commands.c,v 1.19 2003/08/25 23:30:41 obrien Exp $
32 #include "bootstrap.h"
35 char command_errbuf[COMMAND_ERRBUFSZ];
36 int CurrentCondition = 1;
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;
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 /* page the help text from our base path */
136 snprintf(buf, sizeof(buf), "%sloader.help", getenv("base"));
137 if ((hfd = open(buf, O_RDONLY)) < 0) {
138 if ((hfd = rel_open("loader.help", NULL, O_RDONLY)) < 0) {
139 printf("Verbose help not available, use '?' to list commands\n");
144 /* pick up request from arguments */
145 topic = subtopic = NULL;
148 subtopic = strdup(argv[2]);
151 topic = strdup(argv[1]);
154 topic = strdup("help");
157 command_errmsg = "usage is 'help <topic> [<subtopic>]";
161 /* magic "index" keyword */
162 doindex = !strcmp(topic, "index");
165 /* Scan the helpfile looking for help matching the request */
167 while (help_getnext(hfd, &t, &s, &d)) {
169 if (doindex) { /* dink around formatting */
170 help_emitsummary(t, s, d);
172 } else if (strcmp(topic, t)) {
174 if (matched) /* nothing more on this topic, stop scanning */
180 if (((subtopic == NULL) && (s == NULL)) ||
181 ((subtopic != NULL) && (s != NULL) && !strcmp(subtopic, s))) {
182 /* exact match, print text */
183 while ((fgetstr(buf, 80, hfd) >= 0) && (buf[0] != '#')) {
184 if (pager_output(buf))
186 if (pager_output("\n"))
189 } else if ((subtopic == NULL) && (s != NULL)) {
190 /* topic match, list subtopics */
191 help_emitsummary(t, s, d);
201 snprintf(command_errbuf, sizeof(command_errbuf),
202 "no help available for '%s'", topic);
215 COMMAND_SET(commandlist, "?", "list commands", command_commandlist);
218 command_commandlist(int argc, char *argv[])
220 struct bootblk_command **cmdp;
224 printf("Available commands:\n");
225 SET_FOREACH(cmdp, Xcommand_set) {
226 if (((*cmdp)->c_name != NULL) && ((*cmdp)->c_desc != NULL)) {
227 snprintf(str, sizeof(str), " %-15s %s\n",
228 (*cmdp)->c_name, (*cmdp)->c_desc);
237 * XXX set/show should become set/echo if we have variable
238 * substitution happening.
241 COMMAND_SET(show, "show", "show kenv variable(s)", command_show);
244 command_show(int argc, char *argv[])
251 * With no arguments, print everything.
254 for (ev = environ; ev != NULL; ev = ev->ev_next) {
255 pager_output(ev->ev_name);
256 cp = getenv(ev->ev_name);
261 if (pager_output("\n"))
266 if ((cp = getenv(argv[1])) != NULL) {
269 snprintf(command_errbuf, sizeof(command_errbuf),
270 "variable '%s' not found", argv[1]);
277 COMMAND_SET(set, "set", "set a kenv variable", command_set);
280 command_set(int argc, char *argv[])
285 command_errmsg = "wrong number of arguments";
288 if ((err = putenv(argv[1])) != 0) {
289 command_errmsg = strerror(err);
296 COMMAND_SET(unset, "unset", "unset a kenv variable", command_unset);
299 command_unset(int argc, char *argv[])
302 command_errmsg = "wrong number of arguments";
305 /* ignore any errors */
311 COMMAND_SET(echo, "echo", "print text ($VAR is kenv variable)", command_echo);
314 command_echo(int argc, char *argv[])
322 while ((ch = getopt(argc, argv, "n")) != -1) {
329 /* getopt has already reported an error */
336 s = unargv(argc, argv);
347 * A passable emulation of the sh(1) command of the same name.
350 COMMAND_SET(read, "read", "read to kenv variable", command_read);
353 command_read(int argc, char *argv[])
360 char buf[256]; /* XXX size? */
367 while ((c = getopt(argc, argv, "p:t:")) != -1) {
374 timeout = strtol(optarg, &cp, 0);
376 snprintf(command_errbuf, sizeof(command_errbuf),
377 "bad timeout '%s'", optarg);
389 command_errmsg = "wrong number of arguments";
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)
449 if ((fd = rel_open(filename, &fullpath, O_RDONLY)) != -1) {
451 result = pager_file(fullpath);
457 snprintf(command_errbuf, sizeof(command_errbuf),
458 "error showing %s", filename);
465 * List all disk-like devices
467 COMMAND_SET(lsdev, "lsdev", "list all devices", command_lsdev);
470 command_lsdev(int argc, char *argv[])
478 while ((ch = getopt(argc, argv, "v")) != -1) {
485 /* getopt has already reported an error */
493 for (i = 0; devsw[i] != NULL; i++) {
494 if (devsw[i]->dv_print != NULL){
495 sprintf(line, "%s devices:\n", devsw[i]->dv_name);
496 if (pager_output(line))
498 devsw[i]->dv_print(verbose);
500 sprintf(line, "%s: (unknown)\n", devsw[i]->dv_name);
501 if (pager_output(line))
512 COMMAND_SET_COND(ifexists, "ifexists", "conditional file/dir present",
520 static struct cond CondStack[32];
521 static int CondIndex;
524 command_ifexists(int argc, char *argv[])
526 if (CondIndex + 1 == sizeof(CondStack)/sizeof(CondStack[0])) {
527 sprintf(command_errbuf, "if stack too deep");
529 } else if (argc != 2) {
530 sprintf(command_errbuf, "ifexists requires one argument");
534 struct cond *cond = &CondStack[CondIndex++];
536 cond->inherit = CurrentCondition;
538 if (rel_stat(argv[1], &sb)) {
543 CurrentCondition = (cond->inherit && cond->current);
548 COMMAND_SET_COND(ifset, "ifset", "conditional kenv variable present", command_ifset);
551 command_ifset(int argc, char *argv[])
553 if (CondIndex + 1 == sizeof(CondStack)/sizeof(CondStack[0])) {
554 sprintf(command_errbuf, "if stack too deep");
556 } else if (argc != 2) {
557 sprintf(command_errbuf, "ifset requires one argument");
560 struct cond *cond = &CondStack[CondIndex++];
562 cond->inherit = CurrentCondition;
564 if (getenv(argv[1])) {
569 CurrentCondition = (cond->inherit && cond->current);
574 COMMAND_SET_COND(elseifexists, "elseifexists", "conditional file/dir present",
575 command_elseifexists);
578 command_elseifexists(int argc, char *argv[])
580 if (CondIndex == 0) {
581 sprintf(command_errbuf, "elseifexists without if");
583 } else if (argc != 2) {
584 sprintf(command_errbuf, "elseifexists requires one argument");
588 struct cond *cond = &CondStack[CondIndex - 1];
590 if (cond->inherit == 0) {
591 CurrentCondition = 0; /* already ran / can't run */
592 } else if (cond->current) {
593 cond->inherit = 0; /* can't run any more */
595 CurrentCondition = 0;
597 if (rel_stat(argv[1], &sb)) {
602 CurrentCondition = (cond->inherit && cond->current);
608 COMMAND_SET_COND(else, "else", "conditional if/else/endif", command_else);
611 command_else(int argc, char *argv[])
616 cond = &CondStack[CondIndex - 1];
617 cond->current = !cond->current;
618 CurrentCondition = (cond->inherit && cond->current);
621 sprintf(command_errbuf, "else without if");
626 COMMAND_SET_COND(endif, "endif", "conditional if/else/endif", command_endif);
629 command_endif(int argc, char *argv[])
636 cond = &CondStack[CondIndex - 1];
637 CurrentCondition = (cond->inherit && cond->current);
639 CurrentCondition = 1;
643 sprintf(command_errbuf, "endif without if");
648 COMMAND_SET(rmem, "rmem", "read memory", command_rmem);
651 command_rmem(int argc, char *argv[])
656 addr = strtol(argv[1], NULL, 0);
657 printf("%08x: %08x\n", addr, *(int *)(intptr_t)addr);
662 COMMAND_SET(wmem, "wmem", "write memory", command_wmem);
665 command_wmem(int argc, char *argv[])
671 addr = strtol(argv[1], NULL, 0);
672 data = strtol(argv[2], NULL, 0);
673 *(int *)(intptr_t)addr = data;
674 printf("%08x: %08x\n", addr, *(int *)(intptr_t)addr);