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[256];
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]);
150 topic = strdup(argv[1]);
153 topic = strdup("help");
156 command_errmsg = "usage is 'help <topic> [<subtopic>]";
160 /* magic "index" keyword */
161 doindex = !strcmp(topic, "index");
164 /* Scan the helpfile looking for help matching the request */
166 while (help_getnext(hfd, &t, &s, &d)) {
168 if (doindex) { /* dink around formatting */
169 help_emitsummary(t, s, d);
171 } else if (strcmp(topic, t)) {
173 if (matched) /* nothing more on this topic, stop scanning */
179 if (((subtopic == NULL) && (s == NULL)) ||
180 ((subtopic != NULL) && (s != NULL) && !strcmp(subtopic, s))) {
181 /* exact match, print text */
182 while ((fgetstr(buf, 80, hfd) >= 0) && (buf[0] != '#')) {
183 if (pager_output(buf))
185 if (pager_output("\n"))
188 } else if ((subtopic == NULL) && (s != NULL)) {
189 /* topic match, list subtopics */
190 help_emitsummary(t, s, d);
200 sprintf(command_errbuf, "no help available for '%s'", topic);
213 COMMAND_SET(commandlist, "?", "list commands", command_commandlist);
216 command_commandlist(int argc, char *argv[])
218 struct bootblk_command **cmdp;
222 printf("Available commands:\n");
223 SET_FOREACH(cmdp, Xcommand_set) {
224 if (((*cmdp)->c_name != NULL) && ((*cmdp)->c_desc != NULL)) {
225 snprintf(str, sizeof(str), " %-15s %s\n",
226 (*cmdp)->c_name, (*cmdp)->c_desc);
235 * XXX set/show should become set/echo if we have variable
236 * substitution happening.
239 COMMAND_SET(show, "show", "show kenv variable(s)", command_show);
242 command_show(int argc, char *argv[])
249 * With no arguments, print everything.
252 for (ev = environ; ev != NULL; ev = ev->ev_next) {
253 pager_output(ev->ev_name);
254 cp = getenv(ev->ev_name);
259 if (pager_output("\n"))
264 if ((cp = getenv(argv[1])) != NULL) {
267 sprintf(command_errbuf, "variable '%s' not found", argv[1]);
274 COMMAND_SET(set, "set", "set a kenv variable", command_set);
277 command_set(int argc, char *argv[])
282 command_errmsg = "wrong number of arguments";
285 if ((err = putenv(argv[1])) != 0) {
286 command_errmsg = strerror(err);
293 COMMAND_SET(unset, "unset", "unset a kenv variable", command_unset);
296 command_unset(int argc, char *argv[])
299 command_errmsg = "wrong number of arguments";
302 /* ignore any errors */
308 COMMAND_SET(echo, "echo", "print text ($VAR is kenv variable)", command_echo);
311 command_echo(int argc, char *argv[])
319 while ((ch = getopt(argc, argv, "n")) != -1) {
326 /* getopt has already reported an error */
333 s = unargv(argc, argv);
344 * A passable emulation of the sh(1) command of the same name.
347 COMMAND_SET(read, "read", "read to kenv variable", command_read);
350 command_read(int argc, char *argv[])
357 char buf[256]; /* XXX size? */
364 while ((c = getopt(argc, argv, "p:t:")) != -1) {
371 timeout = strtol(optarg, &cp, 0);
373 sprintf(command_errbuf, "bad timeout '%s'", optarg);
385 command_errmsg = "wrong number of arguments";
388 name = (argc > 0) ? argv[0]: NULL;
391 printf("%s", prompt);
393 when = time(NULL) + timeout;
395 if (time(NULL) >= when)
396 return(CMD_OK); /* is timeout an error? */
399 ngets(buf, sizeof(buf));
402 setenv(name, buf, 1);
409 COMMAND_SET(more, "more", "show contents of a file", command_more);
412 command_more(int argc, char *argv[])
420 for (i = 1; (i < argc) && (res == 0); i++) {
421 sprintf(line, "*** FILE %s BEGIN ***\n", argv[i]);
422 if (pager_output(line))
424 res = page_file(argv[i]);
426 sprintf(line, "*** FILE %s END ***\n", argv[i]);
427 res = pager_output(line);
439 page_file(char *filename)
445 if ((fd = rel_open(filename, &fullpath, O_RDONLY)) != -1) {
447 result = pager_file(fullpath);
453 sprintf(command_errbuf, "error showing %s", filename);
459 * List all disk-like devices
461 COMMAND_SET(lsdev, "lsdev", "list all devices", command_lsdev);
464 command_lsdev(int argc, char *argv[])
472 while ((ch = getopt(argc, argv, "v")) != -1) {
479 /* getopt has already reported an error */
487 for (i = 0; devsw[i] != NULL; i++) {
488 if (devsw[i]->dv_print != NULL){
489 sprintf(line, "%s devices:\n", devsw[i]->dv_name);
490 if (pager_output(line))
492 devsw[i]->dv_print(verbose);
494 sprintf(line, "%s: (unknown)\n", devsw[i]->dv_name);
495 if (pager_output(line))
506 COMMAND_SET_COND(ifexists, "ifexists", "conditional file/dir present",
514 static struct cond CondStack[32];
515 static int CondIndex;
518 command_ifexists(int argc, char *argv[])
520 if (CondIndex + 1 == sizeof(CondStack)/sizeof(CondStack[0])) {
521 sprintf(command_errbuf, "if stack too deep");
523 } else if (argc != 2) {
524 sprintf(command_errbuf, "ifexists requires one argument");
528 struct cond *cond = &CondStack[CondIndex++];
530 cond->inherit = CurrentCondition;
532 if (rel_stat(argv[1], &sb)) {
537 CurrentCondition = (cond->inherit && cond->current);
542 COMMAND_SET_COND(ifset, "ifset", "conditional kenv variable present", command_ifset);
545 command_ifset(int argc, char *argv[])
547 if (CondIndex + 1 == sizeof(CondStack)/sizeof(CondStack[0])) {
548 sprintf(command_errbuf, "if stack too deep");
550 } else if (argc != 2) {
551 sprintf(command_errbuf, "ifset requires one argument");
554 struct cond *cond = &CondStack[CondIndex++];
556 cond->inherit = CurrentCondition;
558 if (getenv(argv[1])) {
563 CurrentCondition = (cond->inherit && cond->current);
568 COMMAND_SET_COND(elseifexists, "elseifexists", "conditional file/dir present",
569 command_elseifexists);
572 command_elseifexists(int argc, char *argv[])
574 if (CondIndex == 0) {
575 sprintf(command_errbuf, "elseifexists without if");
577 } else if (argc != 2) {
578 sprintf(command_errbuf, "elseifexists requires one argument");
582 struct cond *cond = &CondStack[CondIndex - 1];
584 if (cond->inherit == 0) {
585 CurrentCondition = 0; /* already ran / can't run */
586 } else if (cond->current) {
587 cond->inherit = 0; /* can't run any more */
589 CurrentCondition = 0;
591 if (rel_stat(argv[1], &sb)) {
596 CurrentCondition = (cond->inherit && cond->current);
602 COMMAND_SET_COND(else, "else", "conditional if/else/endif", command_else);
605 command_else(int argc, char *argv[])
610 cond = &CondStack[CondIndex - 1];
611 cond->current = !cond->current;
612 CurrentCondition = (cond->inherit && cond->current);
615 sprintf(command_errbuf, "else without if");
620 COMMAND_SET_COND(endif, "endif", "conditional if/else/endif", command_endif);
623 command_endif(int argc, char *argv[])
630 cond = &CondStack[CondIndex - 1];
631 CurrentCondition = (cond->inherit && cond->current);
633 CurrentCondition = 1;
637 sprintf(command_errbuf, "endif without if");
642 COMMAND_SET(rmem, "rmem", "read memory", command_rmem);
645 command_rmem(int argc, char *argv[])
650 addr = strtol(argv[1], NULL, 0);
651 printf("%08x: %08x\n", addr, *(int *)(intptr_t)addr);
656 COMMAND_SET(wmem, "wmem", "write memory", command_wmem);
659 command_wmem(int argc, char *argv[])
665 addr = strtol(argv[1], NULL, 0);
666 data = strtol(argv[2], NULL, 0);
667 *(int *)(intptr_t)addr = data;
668 printf("%08x: %08x\n", addr, *(int *)(intptr_t)addr);