Deleted Added
full compact
commands.c (40553) commands.c (40775)
1/*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 9 unchanged lines hidden (view full) ---

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
24 * SUCH DAMAGE.
25 *
1/*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 9 unchanged lines hidden (view full) ---

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
24 * SUCH DAMAGE.
25 *
26 * $Id: commands.c,v 1.5 1998/10/09 07:09:22 msmith Exp $
26 * $Id: commands.c,v 1.6 1998/10/21 20:07:04 msmith Exp $
27 */
28
29#include <stand.h>
30#include <string.h>
31#include <sys/reboot.h>
32
33#include "bootstrap.h"
34
35char *command_errmsg;
36char command_errbuf[256]; /* XXX should have procedural interface for setting, size limit? */
27 */
28
29#include <stand.h>
30#include <string.h>
31#include <sys/reboot.h>
32
33#include "bootstrap.h"
34
35char *command_errmsg;
36char command_errbuf[256]; /* XXX should have procedural interface for setting, size limit? */
37
37
38
39/*
40 * Help is read from a formatted text file.
41 *
42 * Entries in the file are formatted as
43
44# Ttopic [Ssubtopic] Ddescription
45help
46text
47here
48#
49
50 *
51 * Note that for code simplicity's sake, the above format must be followed
52 * exactly.
53 *
54 * Subtopic entries must immediately follow the topic (this is used to
55 * produce the listing of subtopics).
56 *
57 * If no argument(s) are supplied by the user, the help for 'help' is displayed.
58 */
38COMMAND_SET(help, "help", "detailed help", command_help);
39
40static int
59COMMAND_SET(help, "help", "detailed help", command_help);
60
61static int
41command_help(int argc, char *argv[])
62help_getnext(int fd, char **topic, char **subtopic, char **desc)
42{
63{
43 char helppath[80]; /* XXX buffer size? */
64 char line[81], *cp, *ep;
65
66 for (;;) {
67 if (fgetstr(line, 80, fd) < 0)
68 return(0);
69
70 if ((strlen(line) < 3) || (line[0] != '#') || (line[1] != ' '))
71 continue;
44
72
73 *topic = *subtopic = *desc = NULL;
74 cp = line + 2;
75 while((cp != NULL) && (*cp != 0)) {
76 ep = strchr(cp, ' ');
77 if ((*cp == 'T') && (*topic == NULL)) {
78 if (ep != NULL)
79 *ep++ = 0;
80 *topic = strdup(cp + 1);
81 } else if ((*cp == 'S') && (*subtopic == NULL)) {
82 if (ep != NULL)
83 *ep++ = 0;
84 *subtopic = strdup(cp + 1);
85 } else if (*cp == 'D') {
86 *desc = strdup(cp + 1);
87 ep = NULL;
88 }
89 cp = ep;
90 }
91 if (*topic == NULL) {
92 if (*subtopic != NULL)
93 free(*subtopic);
94 if (*desc != NULL)
95 free(*desc);
96 continue;
97 }
98 return(1);
99 }
100}
101
102static void
103help_emitsummary(char *topic, char *subtopic, char *desc)
104{
105 int i;
106
107 pager_output(" ");
108 pager_output(topic);
109 i = strlen(topic);
110 if (subtopic != NULL) {
111 pager_output(" ");
112 pager_output(subtopic);
113 i += strlen(subtopic) + 1;
114 }
115 if (desc != NULL) {
116 do {
117 pager_output(" ");
118 } while (i++ < 30);
119 pager_output(desc);
120 }
121 pager_output("\n");
122}
123
124
125static int
126command_help(int argc, char *argv[])
127{
128 char buf[81]; /* XXX buffer size? */
129 int hfd, matched, doindex;
130 char *topic, *subtopic, *t, *s, *d;
131
45 /* page the help text from our load path */
132 /* page the help text from our load path */
46 sprintf(helppath, "%s/boot/boot.help", getenv("loaddev"));
47 printf("%s\n", helppath);
48 if (pager_file(helppath) == -1)
133 sprintf(buf, "%s/boot/loader.help", getenv("loaddev"));
134 if ((hfd = open(buf, O_RDONLY)) < 0) {
49 printf("Verbose help not available, use '?' to list commands\n");
135 printf("Verbose help not available, use '?' to list commands\n");
136 return(CMD_OK);
137 }
138
139 /* pick up request from arguments */
140 topic = subtopic = NULL;
141 switch(argc) {
142 case 3:
143 subtopic = strdup(argv[2]);
144 case 2:
145 topic = strdup(argv[1]);
146 break;
147 case 1:
148 topic = strdup("help");
149 break;
150 default:
151 command_errmsg = "usage is 'help <topic> [<subtopic>]";
152 return(CMD_ERROR);
153 }
154
155 /* magic "index" keyword */
156 doindex = !strcmp(topic, "index");
157 matched = doindex;
158
159 /* Scan the helpfile looking for help matching the request */
160 pager_open();
161 while(help_getnext(hfd, &t, &s, &d)) {
162
163 if (doindex) { /* dink around formatting */
164 help_emitsummary(t, s, d);
165
166 } else if (strcmp(topic, t)) {
167 /* topic mismatch */
168 if(matched) /* nothing more on this topic, stop scanning */
169 break;
170
171 } else {
172 /* topic matched */
173 matched = 1;
174 if (((subtopic == NULL) && (s == NULL)) ||
175 ((subtopic != NULL) && (s != NULL) && !strcmp(subtopic, s))) {
176 /* exact match, print text */
177 while((fgetstr(buf, 80, hfd) >= 0) && (buf[0] != '#')) {
178 pager_output(buf);
179 pager_output("\n");
180 }
181 } else if ((subtopic == NULL) && (s != NULL)) {
182 /* topic match, list subtopics */
183 help_emitsummary(t, s, d);
184 }
185 }
186 free(t);
187 free(s);
188 free(d);
189 }
190 pager_close();
191 close(hfd);
192 if (!matched) {
193 sprintf(command_errbuf, "no help available for '%s'", topic);
194 return(CMD_ERROR);
195 }
50 return(CMD_OK);
51}
52
196 return(CMD_OK);
197}
198
199
53COMMAND_SET(commandlist, "?", "list commands", command_commandlist);
54
55static int
56command_commandlist(int argc, char *argv[])
57{
58 struct bootblk_command **cmdp;
200COMMAND_SET(commandlist, "?", "list commands", command_commandlist);
201
202static int
203command_commandlist(int argc, char *argv[])
204{
205 struct bootblk_command **cmdp;
59 int i;
60
61 printf("Available commands:\n");
62 SET_FOREACH(cmdp, Xcommand_set) {
63 if (((*cmdp)->c_name != NULL) && ((*cmdp)->c_desc != NULL))
64 printf(" %-15s %s\n", (*cmdp)->c_name, (*cmdp)->c_desc);
65 }
66 return(CMD_OK);
67}

--- 161 unchanged lines hidden (view full) ---

229 }
230
231 ngets(buf, sizeof(buf));
232
233 if (name != NULL)
234 setenv(name, buf, 1);
235 return(CMD_OK);
236}
206
207 printf("Available commands:\n");
208 SET_FOREACH(cmdp, Xcommand_set) {
209 if (((*cmdp)->c_name != NULL) && ((*cmdp)->c_desc != NULL))
210 printf(" %-15s %s\n", (*cmdp)->c_name, (*cmdp)->c_desc);
211 }
212 return(CMD_OK);
213}

--- 161 unchanged lines hidden (view full) ---

375 }
376
377 ngets(buf, sizeof(buf));
378
379 if (name != NULL)
380 setenv(name, buf, 1);
381 return(CMD_OK);
382}
383
384/*
385 * List all disk-like devices
386 */
387COMMAND_SET(lsdev, "lsdev", NULL, command_lsdev);
388
389static int
390command_lsdev(int argc, char *argv[])
391{
392 int verbose, ch, i;
393 char line[80];
394
395 verbose = 0;
396 optind = 1;
397 while ((ch = getopt(argc, argv, "v")) != -1) {
398 switch(ch) {
399 case 'v':
400 verbose = 1;
401 break;
402 case '?':
403 default:
404 /* getopt has already reported an error */
405 return(CMD_OK);
406 }
407 }
408 argv += (optind);
409 argc -= (optind);
410
411 pager_open();
412 for (i = 0; devsw[i] != NULL; i++) {
413 if (devsw[i]->dv_print != NULL){
414 sprintf(line, "%s @ %p\n", devsw[i]->dv_name, devsw[i]->dv_print);
415 pager_output(line);
416 devsw[i]->dv_print(verbose);
417 } else {
418 sprintf(line, "%s: (unknown)\n", devsw[i]->dv_name);
419 pager_output(line);
420 }
421 }
422 pager_close();
423 return(CMD_OK);
424}