Fully synchronize sys/boot from FreeBSD-5.x, but add / to the module path
[dragonfly.git] / sys / boot / common / commands.c
CommitLineData
984263bc
MD
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 * 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.
13 *
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
24 * SUCH DAMAGE.
25 *
5ee58eed
MD
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.3 2003/11/10 06:08:31 dillon Exp $
984263bc
MD
28 */
29
30#include <stand.h>
31#include <string.h>
984263bc
MD
32
33#include "bootstrap.h"
34
35char *command_errmsg;
36char command_errbuf[256]; /* XXX should have procedural interface for setting, size limit? */
37
38static int page_file(char *filename);
39
40/*
41 * Help is read from a formatted text file.
42 *
43 * Entries in the file are formatted as
44
45# Ttopic [Ssubtopic] Ddescription
46help
47text
48here
49#
50
51 *
52 * Note that for code simplicity's sake, the above format must be followed
53 * exactly.
54 *
55 * Subtopic entries must immediately follow the topic (this is used to
56 * produce the listing of subtopics).
57 *
58 * If no argument(s) are supplied by the user, the help for 'help' is displayed.
59 */
60COMMAND_SET(help, "help", "detailed help", command_help);
61
62static int
63help_getnext(int fd, char **topic, char **subtopic, char **desc)
64{
65 char line[81], *cp, *ep;
66
67 for (;;) {
68 if (fgetstr(line, 80, fd) < 0)
69 return(0);
70
71 if ((strlen(line) < 3) || (line[0] != '#') || (line[1] != ' '))
72 continue;
73
74 *topic = *subtopic = *desc = NULL;
75 cp = line + 2;
76 while((cp != NULL) && (*cp != 0)) {
77 ep = strchr(cp, ' ');
78 if ((*cp == 'T') && (*topic == NULL)) {
79 if (ep != NULL)
80 *ep++ = 0;
81 *topic = strdup(cp + 1);
82 } else if ((*cp == 'S') && (*subtopic == NULL)) {
83 if (ep != NULL)
84 *ep++ = 0;
85 *subtopic = strdup(cp + 1);
86 } else if (*cp == 'D') {
87 *desc = strdup(cp + 1);
88 ep = NULL;
89 }
90 cp = ep;
91 }
92 if (*topic == NULL) {
93 if (*subtopic != NULL)
94 free(*subtopic);
95 if (*desc != NULL)
96 free(*desc);
97 continue;
98 }
99 return(1);
100 }
101}
102
103static void
104help_emitsummary(char *topic, char *subtopic, char *desc)
105{
106 int i;
107
108 pager_output(" ");
109 pager_output(topic);
110 i = strlen(topic);
111 if (subtopic != NULL) {
112 pager_output(" ");
113 pager_output(subtopic);
114 i += strlen(subtopic) + 1;
115 }
116 if (desc != NULL) {
117 do {
118 pager_output(" ");
119 } while (i++ < 30);
120 pager_output(desc);
121 }
122 pager_output("\n");
123}
124
125
126static int
127command_help(int argc, char *argv[])
128{
129 char buf[81]; /* XXX buffer size? */
130 int hfd, matched, doindex;
131 char *topic, *subtopic, *t, *s, *d;
132
133 /* page the help text from our load path */
134 sprintf(buf, "%s/boot/loader.help", getenv("loaddev"));
135 if ((hfd = open(buf, O_RDONLY)) < 0) {
136 printf("Verbose help not available, use '?' to list commands\n");
137 return(CMD_OK);
138 }
139
140 /* pick up request from arguments */
141 topic = subtopic = NULL;
142 switch(argc) {
143 case 3:
144 subtopic = strdup(argv[2]);
145 case 2:
146 topic = strdup(argv[1]);
147 break;
148 case 1:
149 topic = strdup("help");
150 break;
151 default:
152 command_errmsg = "usage is 'help <topic> [<subtopic>]";
153 return(CMD_ERROR);
154 }
155
156 /* magic "index" keyword */
157 doindex = !strcmp(topic, "index");
158 matched = doindex;
159
160 /* Scan the helpfile looking for help matching the request */
161 pager_open();
162 while(help_getnext(hfd, &t, &s, &d)) {
163
164 if (doindex) { /* dink around formatting */
165 help_emitsummary(t, s, d);
166
167 } else if (strcmp(topic, t)) {
168 /* topic mismatch */
169 if(matched) /* nothing more on this topic, stop scanning */
170 break;
171
172 } else {
173 /* topic matched */
174 matched = 1;
175 if (((subtopic == NULL) && (s == NULL)) ||
176 ((subtopic != NULL) && (s != NULL) && !strcmp(subtopic, s))) {
177 /* exact match, print text */
178 while((fgetstr(buf, 80, hfd) >= 0) && (buf[0] != '#')) {
179 if (pager_output(buf))
180 break;
181 if (pager_output("\n"))
182 break;
183 }
184 } else if ((subtopic == NULL) && (s != NULL)) {
185 /* topic match, list subtopics */
186 help_emitsummary(t, s, d);
187 }
188 }
189 free(t);
190 free(s);
191 free(d);
192 }
193 pager_close();
194 close(hfd);
195 if (!matched) {
196 sprintf(command_errbuf, "no help available for '%s'", topic);
197 free(topic);
198 if (subtopic)
199 free(subtopic);
200 return(CMD_ERROR);
201 }
202 free(topic);
203 if (subtopic)
204 free(subtopic);
205 return(CMD_OK);
206}
207
208
209COMMAND_SET(commandlist, "?", "list commands", command_commandlist);
210
211static int
212command_commandlist(int argc, char *argv[])
213{
214 struct bootblk_command **cmdp;
215
216 printf("Available commands:\n");
217 SET_FOREACH(cmdp, Xcommand_set) {
218 if (((*cmdp)->c_name != NULL) && ((*cmdp)->c_desc != NULL))
219 printf(" %-15s %s\n", (*cmdp)->c_name, (*cmdp)->c_desc);
220 }
221 return(CMD_OK);
222}
223
224/*
225 * XXX set/show should become set/echo if we have variable
226 * substitution happening.
227 */
228
229COMMAND_SET(show, "show", "show variable(s)", command_show);
230
231static int
232command_show(int argc, char *argv[])
233{
234 struct env_var *ev;
235 char *cp;
236
237 if (argc < 2) {
238 /*
239 * With no arguments, print everything.
240 */
241 pager_open();
242 for (ev = environ; ev != NULL; ev = ev->ev_next) {
243 pager_output(ev->ev_name);
244 cp = getenv(ev->ev_name);
245 if (cp != NULL) {
246 pager_output("=");
247 pager_output(cp);
248 }
249 if (pager_output("\n"))
250 break;
251 }
252 pager_close();
253 } else {
254 if ((cp = getenv(argv[1])) != NULL) {
255 printf("%s\n", cp);
256 } else {
257 sprintf(command_errbuf, "variable '%s' not found", argv[1]);
258 return(CMD_ERROR);
259 }
260 }
261 return(CMD_OK);
262}
263
264COMMAND_SET(set, "set", "set a variable", command_set);
265
266static int
267command_set(int argc, char *argv[])
268{
269 int err;
270
271 if (argc != 2) {
272 command_errmsg = "wrong number of arguments";
273 return(CMD_ERROR);
274 } else {
275 if ((err = putenv(argv[1])) != 0) {
276 command_errmsg = strerror(err);
277 return(CMD_ERROR);
278 }
279 }
280 return(CMD_OK);
281}
282
283COMMAND_SET(unset, "unset", "unset a variable", command_unset);
284
285static int
286command_unset(int argc, char *argv[])
287{
288 int err;
289
290 if (argc != 2) {
291 command_errmsg = "wrong number of arguments";
292 return(CMD_ERROR);
293 } else {
294 if ((err = unsetenv(argv[1])) != 0) {
295 command_errmsg = strerror(err);
296 return(CMD_ERROR);
297 }
298 }
299 return(CMD_OK);
300}
301
302COMMAND_SET(echo, "echo", NULL, command_echo);
303
304static int
305command_echo(int argc, char *argv[])
306{
307 char *s;
308 int nl, ch;
309
310 nl = 0;
311 optind = 1;
312 optreset = 1;
313 while ((ch = getopt(argc, argv, "n")) != -1) {
314 switch(ch) {
315 case 'n':
316 nl = 1;
317 break;
318 case '?':
319 default:
320 /* getopt has already reported an error */
321 return(CMD_OK);
322 }
323 }
324 argv += (optind);
325 argc -= (optind);
326
327 s = unargv(argc, argv);
328 if (s != NULL) {
329 printf("%s", s);
330 free(s);
331 }
332 if (!nl)
333 printf("\n");
334 return(CMD_OK);
335}
336
337/*
338 * A passable emulation of the sh(1) command of the same name.
339 */
340
341COMMAND_SET(read, "read", NULL, command_read);
342
343static int
344command_read(int argc, char *argv[])
345{
346 char *prompt;
347 int timeout;
348 time_t when;
349 char *cp;
350 char *name;
351 char buf[256]; /* XXX size? */
352 int c;
353
354 timeout = -1;
355 prompt = NULL;
356 optind = 1;
357 optreset = 1;
358 while ((c = getopt(argc, argv, "p:t:")) != -1) {
359 switch(c) {
360
361 case 'p':
362 prompt = optarg;
363 break;
364 case 't':
365 timeout = strtol(optarg, &cp, 0);
366 if (cp == optarg) {
367 sprintf(command_errbuf, "bad timeout '%s'", optarg);
368 return(CMD_ERROR);
369 }
370 break;
371 default:
372 return(CMD_OK);
373 }
374 }
375
376 argv += (optind);
377 argc -= (optind);
378 name = (argc > 0) ? argv[0]: NULL;
379
380 if (prompt != NULL)
381 printf("%s", prompt);
382 if (timeout >= 0) {
383 when = time(NULL) + timeout;
384 while (!ischar())
385 if (time(NULL) >= when)
386 return(CMD_OK); /* is timeout an error? */
387 }
388
389 ngets(buf, sizeof(buf));
390
391 if (name != NULL)
392 setenv(name, buf, 1);
393 return(CMD_OK);
394}
395
396/*
397 * File pager
398 */
399COMMAND_SET(more, "more", "show contents of a file", command_more);
400
401static int
402command_more(int argc, char *argv[])
403{
404 int i;
405 int res;
406 char line[80];
407
408 res=0;
409 pager_open();
410 for (i = 1; (i < argc) && (res == 0); i++) {
411 sprintf(line, "*** FILE %s BEGIN ***\n", argv[i]);
412 if (pager_output(line))
413 break;
414 res = page_file(argv[i]);
415 if (!res) {
416 sprintf(line, "*** FILE %s END ***\n", argv[i]);
417 res = pager_output(line);
418 }
419 }
420 pager_close();
421
422 if (res == 0)
423 return CMD_OK;
424 else
425 return CMD_ERROR;
426}
427
428static int
429page_file(char *filename)
430{
431 int result;
432
433 result = pager_file(filename);
434
435 if (result == -1)
436 sprintf(command_errbuf, "error showing %s", filename);
437
438 return result;
439}
440
441/*
442 * List all disk-like devices
443 */
444COMMAND_SET(lsdev, "lsdev", "list all devices", command_lsdev);
445
446static int
447command_lsdev(int argc, char *argv[])
448{
449 int verbose, ch, i;
450 char line[80];
451
452 verbose = 0;
453 optind = 1;
454 optreset = 1;
455 while ((ch = getopt(argc, argv, "v")) != -1) {
456 switch(ch) {
457 case 'v':
458 verbose = 1;
459 break;
460 case '?':
461 default:
462 /* getopt has already reported an error */
463 return(CMD_OK);
464 }
465 }
466 argv += (optind);
467 argc -= (optind);
468
469 pager_open();
470 for (i = 0; devsw[i] != NULL; i++) {
471 if (devsw[i]->dv_print != NULL){
5ee58eed 472 sprintf(line, "%s devices:\n", devsw[i]->dv_name);
984263bc
MD
473 if (pager_output(line))
474 break;
475 devsw[i]->dv_print(verbose);
476 } else {
477 sprintf(line, "%s: (unknown)\n", devsw[i]->dv_name);
478 if (pager_output(line))
479 break;
480 }
481 }
482 pager_close();
483 return(CMD_OK);
484}
485