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>
32 #include "bootstrap.h"
34 const char *command_errmsg
;
35 /* XXX should have procedural interface for setting, size limit? */
36 char command_errbuf
[COMMAND_ERRBUFSZ
];
38 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.
62 COMMAND_SET(help
, "help", "detailed help", command_help
);
65 help_getnext(int fd
, char **topic
, char **subtopic
, char **desc
)
67 char line
[81], *cp
, *ep
;
69 /* Make sure we provide sane values. */
70 *topic
= *subtopic
= *desc
= NULL
;
72 if (fgetstr(line
, 80, fd
) < 0)
75 if (strlen(line
) < 3 || line
[0] != '#' || line
[1] != ' ')
78 *topic
= *subtopic
= *desc
= NULL
;
80 while (cp
!= NULL
&& *cp
!= 0) {
82 if (*cp
== 'T' && *topic
== NULL
) {
85 *topic
= strdup(cp
+ 1);
86 } else if (*cp
== 'S' && *subtopic
== NULL
) {
89 *subtopic
= strdup(cp
+ 1);
90 } else if (*cp
== 'D') {
91 *desc
= strdup(cp
+ 1);
99 *subtopic
= *desc
= NULL
;
107 help_emitsummary(char *topic
, char *subtopic
, char *desc
)
114 if (subtopic
!= NULL
) {
116 pager_output(subtopic
);
117 i
+= strlen(subtopic
) + 1;
125 return (pager_output("\n"));
130 command_help(int argc
, char *argv
[])
132 char buf
[81]; /* XXX buffer size? */
133 int hfd
, matched
, doindex
;
134 char *topic
, *subtopic
, *t
, *s
, *d
;
136 /* page the help text from our load path */
137 snprintf(buf
, sizeof (buf
), "%s/boot/loader.help", getenv("loaddev"));
138 if ((hfd
= open(buf
, O_RDONLY
)) < 0) {
139 printf("Verbose help not available, "
140 "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>]";
162 /* magic "index" keyword */
163 doindex
= strcmp(topic
, "index") == 0? 1 : 0;
166 /* Scan the helpfile looking for help matching the request */
168 while (help_getnext(hfd
, &t
, &s
, &d
)) {
170 if (doindex
) { /* dink around formatting */
171 if (help_emitsummary(t
, s
, d
))
174 } else if (strcmp(topic
, t
)) {
177 /* nothing more on this topic, stop scanning */
183 if ((subtopic
== NULL
&& s
== NULL
) ||
184 (subtopic
!= NULL
&& s
!= NULL
&&
185 strcmp(subtopic
, s
) == 0)) {
186 /* exact match, print text */
187 while (fgetstr(buf
, 80, hfd
) >= 0 &&
189 if (pager_output(buf
))
191 if (pager_output("\n"))
194 } else if (subtopic
== NULL
&& s
!= NULL
) {
195 /* topic match, list subtopics */
196 if (help_emitsummary(t
, s
, d
))
211 snprintf(command_errbuf
, sizeof (command_errbuf
),
212 "no help available for '%s'", topic
);
222 COMMAND_SET(commandlist
, "?", "list commands", command_commandlist
);
225 command_commandlist(int argc __unused
, char *argv
[] __unused
)
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 snprintf(name
, sizeof (name
)," %-15s ",
241 pager_output((*cmdp
)->c_desc
);
242 res
= pager_output("\n");
250 * XXX set/show should become set/echo if we have variable
251 * substitution happening.
254 COMMAND_SET(show
, "show", "show variable(s)", command_show
);
257 command_show(int argc
, char *argv
[])
264 * With no arguments, print everything.
267 for (ev
= environ
; ev
!= NULL
; ev
= ev
->ev_next
) {
268 pager_output(ev
->ev_name
);
269 cp
= getenv(ev
->ev_name
);
274 if (pager_output("\n"))
279 if ((cp
= getenv(argv
[1])) != NULL
) {
282 snprintf(command_errbuf
, sizeof (command_errbuf
),
283 "variable '%s' not found", argv
[1]);
290 COMMAND_SET(set
, "set", "set a variable", command_set
);
293 command_set(int argc
, char *argv
[])
298 command_errmsg
= "wrong number of arguments";
301 if ((err
= putenv(argv
[1])) != 0) {
302 command_errmsg
= strerror(err
);
309 COMMAND_SET(setprop
, "setprop", "set a variable", command_setprop
);
312 command_setprop(int argc
, char *argv
[])
317 command_errmsg
= "wrong number of arguments";
320 if ((err
= setenv(argv
[1], argv
[2], 1)) != 0) {
321 command_errmsg
= strerror(err
);
328 COMMAND_SET(unset
, "unset", "unset a variable", command_unset
);
331 command_unset(int argc
, char *argv
[])
336 command_errmsg
= "wrong number of arguments";
339 if ((err
= unsetenv(argv
[1])) != 0) {
340 command_errmsg
= strerror(err
);
347 COMMAND_SET(echo
, "echo", "echo arguments", command_echo
);
350 command_echo(int argc
, char *argv
[])
358 while ((ch
= getopt(argc
, argv
, "n")) != -1) {
365 /* getopt has already reported an error */
372 s
= unargv(argc
, argv
);
383 * A passable emulation of the sh(1) command of the same name.
386 COMMAND_SET(read
, "read", "read input from the terminal", command_read
);
389 command_read(int argc
, char *argv
[])
396 char buf
[256]; /* XXX size? */
403 while ((c
= getopt(argc
, argv
, "p:t:")) != -1) {
409 timeout
= strtol(optarg
, &cp
, 0);
411 snprintf(command_errbuf
,
412 sizeof (command_errbuf
),
413 "bad timeout '%s'", optarg
);
424 name
= (argc
> 0) ? argv
[0]: NULL
;
427 printf("%s", prompt
);
429 when
= time(NULL
) + timeout
;
431 if (time(NULL
) >= when
)
432 return (CMD_OK
); /* is timeout an error? */
435 ngets(buf
, sizeof (buf
));
438 setenv(name
, buf
, 1);
445 COMMAND_SET(more
, "more", "show contents of a file", command_more
);
448 command_more(int argc
, char *argv
[])
456 for (i
= 1; (i
< argc
) && (res
== 0); i
++) {
457 snprintf(line
, sizeof (line
), "*** FILE %s BEGIN ***\n",
459 if (pager_output(line
))
461 res
= page_file(argv
[i
]);
463 snprintf(line
, sizeof (line
), "*** FILE %s END ***\n",
465 res
= pager_output(line
);
477 page_file(char *filename
)
481 result
= pager_file(filename
);
484 snprintf(command_errbuf
, sizeof (command_errbuf
),
485 "error showing %s", filename
);
492 * List all disk-like devices
494 COMMAND_SET(lsdev
, "lsdev", "list all devices", command_lsdev
);
497 command_lsdev(int argc
, char *argv
[])
505 while ((ch
= getopt(argc
, argv
, "v")) != -1) {
512 /* getopt has already reported an error */
520 for (i
= 0; devsw
[i
] != NULL
; i
++) {
521 if (devsw
[i
]->dv_print
!= NULL
) {
522 if (devsw
[i
]->dv_print(verbose
))
525 snprintf(line
, sizeof (line
), "%s: (unknown)\n",
527 if (pager_output(line
))