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 $
27 * $DragonFly: src/sys/boot/common/commands.c,v 1.4 2008/09/02 17:21:12 dillon Exp $
33 #include "bootstrap.h"
36 char command_errbuf[256];
37 int CurrentCondition = 1;
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;
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 /* sprintf(buf, "%s/boot/loader.help", getenv("loaddev")); */
136 /* page the help text from our base path */
137 snprintf(buf, sizeof(buf), "%sloader.help", getenv("base"));
138 if ((hfd = open(buf, O_RDONLY)) < 0) {
139 if ((hfd = rel_open("loader.help", NULL, O_RDONLY)) < 0) {
140 printf("Verbose help not available, use '?' to list commands\n");
145 /* pick up request from arguments */
146 topic = subtopic = NULL;
149 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 sprintf(command_errbuf, "no help available for '%s'", topic);
214 COMMAND_SET(commandlist, "?", "list commands", command_commandlist);
217 command_commandlist(int argc, char *argv[])
219 struct bootblk_command **cmdp;
223 printf("Available commands:\n");
224 SET_FOREACH(cmdp, Xcommand_set) {
225 if (((*cmdp)->c_name != NULL) && ((*cmdp)->c_desc != NULL)) {
226 snprintf(str, sizeof(str), " %-15s %s\n",
227 (*cmdp)->c_name, (*cmdp)->c_desc);
236 * XXX set/show should become set/echo if we have variable
237 * substitution happening.
240 COMMAND_SET(show, "show", "show variable(s)", command_show);
243 command_show(int argc, char *argv[])
250 * With no arguments, print everything.
253 for (ev = environ; ev != NULL; ev = ev->ev_next) {
254 pager_output(ev->ev_name);
255 cp = getenv(ev->ev_name);
260 if (pager_output("\n"))
265 if ((cp = getenv(argv[1])) != NULL) {
268 sprintf(command_errbuf, "variable '%s' not found", argv[1]);
275 COMMAND_SET(set, "set", "set a variable", command_set);
278 command_set(int argc, char *argv[])
283 command_errmsg = "wrong number of arguments";
286 if ((err = putenv(argv[1])) != 0) {
287 command_errmsg = strerror(err);
294 COMMAND_SET(unset, "unset", "unset a variable", command_unset);
297 command_unset(int argc, char *argv[])
300 command_errmsg = "wrong number of arguments";
303 /* ignore any errors */
309 COMMAND_SET(echo, "echo", NULL, command_echo);
312 command_echo(int argc, char *argv[])
320 while ((ch = getopt(argc, argv, "n")) != -1) {
327 /* getopt has already reported an error */
334 s = unargv(argc, argv);
345 * A passable emulation of the sh(1) command of the same name.
348 COMMAND_SET(read, "read", NULL, command_read);
351 command_read(int argc, char *argv[])
358 char buf[256]; /* XXX size? */
365 while ((c = getopt(argc, argv, "p:t:")) != -1) {
372 timeout = strtol(optarg, &cp, 0);
374 sprintf(command_errbuf, "bad timeout '%s'", optarg);
385 name = (argc > 0) ? argv[0]: NULL;
388 printf("%s", prompt);
390 when = time(NULL) + timeout;
392 if (time(NULL) >= when)
393 return(CMD_OK); /* is timeout an error? */
396 ngets(buf, sizeof(buf));
399 setenv(name, buf, 1);
406 COMMAND_SET(more, "more", "show contents of a file", command_more);
409 command_more(int argc, char *argv[])
417 for (i = 1; (i < argc) && (res == 0); i++) {
418 sprintf(line, "*** FILE %s BEGIN ***\n", argv[i]);
419 if (pager_output(line))
421 res = page_file(argv[i]);
423 sprintf(line, "*** FILE %s END ***\n", argv[i]);
424 res = pager_output(line);
436 page_file(char *filename)
442 if ((fd = rel_open(filename, &fullpath, O_RDONLY)) != -1) {
444 result = pager_file(fullpath);
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))
503 COMMAND_SET_COND(ifexists, "ifexists", "conditional f/d present",
511 static struct cond CondStack[32];
512 static int CondIndex;
515 command_ifexists(int argc, char *argv[])
517 if (CondIndex + 1 == sizeof(CondStack)/sizeof(CondStack[0])) {
518 sprintf(command_errbuf, "if stack too deep");
520 } else if (argc != 2) {
521 sprintf(command_errbuf, "ifexists requires one argument");
525 struct cond *cond = &CondStack[CondIndex++];
527 cond->inherit = CurrentCondition;
529 if (rel_stat(argv[1], &sb)) {
534 CurrentCondition = (cond->inherit && cond->current);
539 COMMAND_SET_COND(ifset, "ifset", "conditional kenv present", command_ifset);
542 command_ifset(int argc, char *argv[])
544 if (CondIndex + 1 == sizeof(CondStack)/sizeof(CondStack[0])) {
545 sprintf(command_errbuf, "if stack too deep");
547 } else if (argc != 2) {
548 sprintf(command_errbuf, "ifset requires one argument");
551 struct cond *cond = &CondStack[CondIndex++];
553 cond->inherit = CurrentCondition;
555 if (getenv(argv[1])) {
560 CurrentCondition = (cond->inherit && cond->current);
565 COMMAND_SET_COND(elseifexists, "elseifexists", "conditional f/d present",
566 command_elseifexists);
569 command_elseifexists(int argc, char *argv[])
571 if (CondIndex == 0) {
572 sprintf(command_errbuf, "elseifexists without if");
574 } else if (argc != 2) {
575 sprintf(command_errbuf, "elseifexists requires one argument");
579 struct cond *cond = &CondStack[CondIndex - 1];
581 if (cond->inherit == 0) {
582 CurrentCondition = 0; /* already ran / can't run */
583 } else if (cond->current) {
584 cond->inherit = 0; /* can't run any more */
586 CurrentCondition = 0;
588 if (rel_stat(argv[1], &sb)) {
593 CurrentCondition = (cond->inherit && cond->current);
599 COMMAND_SET_COND(else, "else", "cond if/else/endif", command_else);
602 command_else(int argc, char *argv[])
607 cond = &CondStack[CondIndex - 1];
608 cond->current = !cond->current;
609 CurrentCondition = (cond->inherit && cond->current);
612 sprintf(command_errbuf, "else without if");
617 COMMAND_SET_COND(endif, "endif", "cond if/else/endif", command_endif);
620 command_endif(int argc, char *argv[])
627 cond = &CondStack[CondIndex - 1];
628 CurrentCondition = (cond->inherit && cond->current);
630 CurrentCondition = 1;
634 sprintf(command_errbuf, "endif without if");