commands.c revision 1.5 1 1.5 martin /* $NetBSD: commands.c,v 1.5 2014/04/08 21:51:06 martin Exp $ */
2 1.1 cherry
3 1.1 cherry /*-
4 1.1 cherry * Copyright (c) 1998 Michael Smith <msmith (at) freebsd.org>
5 1.1 cherry * All rights reserved.
6 1.1 cherry *
7 1.1 cherry * Redistribution and use in source and binary forms, with or without
8 1.1 cherry * modification, are permitted provided that the following conditions
9 1.1 cherry * are met:
10 1.1 cherry * 1. Redistributions of source code must retain the above copyright
11 1.1 cherry * notice, this list of conditions and the following disclaimer.
12 1.1 cherry * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cherry * notice, this list of conditions and the following disclaimer in the
14 1.1 cherry * documentation and/or other materials provided with the distribution.
15 1.1 cherry *
16 1.1 cherry * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 cherry * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 cherry * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 cherry * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 cherry * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 cherry * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 cherry * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 cherry * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 cherry * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 cherry * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 cherry * SUCH DAMAGE.
27 1.1 cherry */
28 1.1 cherry
29 1.1 cherry #include <sys/cdefs.h>
30 1.2 cherry /* __FBSDID("$FreeBSD: src/sys/boot/common/commands.c,v 1.19 2003/08/25 23:30:41 obrien Exp $"); */
31 1.1 cherry
32 1.1 cherry #include <lib/libsa/stand.h>
33 1.3 kiyohara #include <lib/libsa/loadfile.h>
34 1.1 cherry #include <lib/libkern/libkern.h>
35 1.1 cherry
36 1.1 cherry #include "bootstrap.h"
37 1.1 cherry
38 1.4 christos static char command_errbuf[256];
39 1.4 christos
40 1.4 christos int
41 1.4 christos command_seterr(const char *fmt, ...)
42 1.4 christos {
43 1.4 christos int len;
44 1.4 christos va_list ap;
45 1.5 martin va_start(ap, fmt);
46 1.4 christos len = vsnprintf(command_errbuf, sizeof(command_errbuf), fmt, ap);
47 1.4 christos va_end(ap);
48 1.4 christos return len;
49 1.4 christos }
50 1.4 christos
51 1.4 christos const char *
52 1.4 christos command_geterr(void)
53 1.4 christos {
54 1.4 christos return command_errbuf;
55 1.4 christos }
56 1.1 cherry
57 1.1 cherry static int page_file(char *filename);
58 1.1 cherry
59 1.1 cherry /*
60 1.1 cherry * Help is read from a formatted text file.
61 1.1 cherry *
62 1.1 cherry * Entries in the file are formatted as
63 1.1 cherry
64 1.1 cherry # Ttopic [Ssubtopic] Ddescription
65 1.1 cherry help
66 1.1 cherry text
67 1.1 cherry here
68 1.1 cherry #
69 1.1 cherry
70 1.1 cherry *
71 1.1 cherry * Note that for code simplicity's sake, the above format must be followed
72 1.1 cherry * exactly.
73 1.1 cherry *
74 1.1 cherry * Subtopic entries must immediately follow the topic (this is used to
75 1.1 cherry * produce the listing of subtopics).
76 1.1 cherry *
77 1.1 cherry * If no argument(s) are supplied by the user, the help for 'help' is displayed.
78 1.1 cherry */
79 1.1 cherry
80 1.1 cherry static int
81 1.1 cherry help_getnext(int fd, char **topic, char **subtopic, char **desc)
82 1.1 cherry {
83 1.1 cherry char line[81], *cp, *ep;
84 1.1 cherry
85 1.1 cherry for (;;) {
86 1.1 cherry if (fgetstr(line, 80, fd) < 0)
87 1.1 cherry return(0);
88 1.1 cherry
89 1.1 cherry if ((strlen(line) < 3) || (line[0] != '#') || (line[1] != ' '))
90 1.1 cherry continue;
91 1.1 cherry
92 1.1 cherry *topic = *subtopic = *desc = NULL;
93 1.1 cherry cp = line + 2;
94 1.1 cherry while((cp != NULL) && (*cp != 0)) {
95 1.1 cherry ep = strchr(cp, ' ');
96 1.1 cherry if ((*cp == 'T') && (*topic == NULL)) {
97 1.1 cherry if (ep != NULL)
98 1.1 cherry *ep++ = 0;
99 1.1 cherry *topic = strdup(cp + 1);
100 1.1 cherry } else if ((*cp == 'S') && (*subtopic == NULL)) {
101 1.1 cherry if (ep != NULL)
102 1.1 cherry *ep++ = 0;
103 1.1 cherry *subtopic = strdup(cp + 1);
104 1.1 cherry } else if (*cp == 'D') {
105 1.1 cherry *desc = strdup(cp + 1);
106 1.1 cherry ep = NULL;
107 1.1 cherry }
108 1.1 cherry cp = ep;
109 1.1 cherry }
110 1.1 cherry if (*topic == NULL) {
111 1.1 cherry if (*subtopic != NULL)
112 1.1 cherry free(*subtopic);
113 1.1 cherry if (*desc != NULL)
114 1.1 cherry free(*desc);
115 1.1 cherry continue;
116 1.1 cherry }
117 1.1 cherry return(1);
118 1.1 cherry }
119 1.1 cherry }
120 1.1 cherry
121 1.1 cherry static int
122 1.1 cherry help_emitsummary(char *topic, char *subtopic, char *desc)
123 1.1 cherry {
124 1.1 cherry int i;
125 1.1 cherry
126 1.1 cherry pager_output(" ");
127 1.1 cherry pager_output(topic);
128 1.1 cherry i = strlen(topic);
129 1.1 cherry if (subtopic != NULL) {
130 1.1 cherry pager_output(" ");
131 1.1 cherry pager_output(subtopic);
132 1.1 cherry i += strlen(subtopic) + 1;
133 1.1 cherry }
134 1.1 cherry if (desc != NULL) {
135 1.1 cherry do {
136 1.1 cherry pager_output(" ");
137 1.1 cherry } while (i++ < 30);
138 1.1 cherry pager_output(desc);
139 1.1 cherry }
140 1.1 cherry return (pager_output("\n"));
141 1.1 cherry }
142 1.1 cherry
143 1.1 cherry
144 1.1 cherry int
145 1.1 cherry command_help(int argc, char *argv[])
146 1.1 cherry {
147 1.1 cherry char buf[81]; /* XXX buffer size? */
148 1.1 cherry int hfd, matched, doindex;
149 1.1 cherry char *topic, *subtopic, *t, *s, *d;
150 1.1 cherry
151 1.1 cherry /* page the help text from our load path */
152 1.4 christos snprintf(buf, sizeof(buf), "%s/boot/loader.help", getenv("loaddev"));
153 1.1 cherry if ((hfd = open(buf, O_RDONLY)) < 0) {
154 1.1 cherry printf("Verbose help not available, use '?' to list commands\n");
155 1.1 cherry return(CMD_OK);
156 1.1 cherry }
157 1.1 cherry
158 1.1 cherry /* pick up request from arguments */
159 1.1 cherry topic = subtopic = NULL;
160 1.1 cherry switch(argc) {
161 1.1 cherry case 3:
162 1.1 cherry subtopic = strdup(argv[2]);
163 1.1 cherry case 2:
164 1.1 cherry topic = strdup(argv[1]);
165 1.1 cherry break;
166 1.1 cherry case 1:
167 1.1 cherry topic = strdup("help");
168 1.1 cherry break;
169 1.1 cherry default:
170 1.4 christos command_seterr("usage is 'help <topic> [<subtopic>]");
171 1.1 cherry return(CMD_ERROR);
172 1.1 cherry }
173 1.1 cherry
174 1.1 cherry /* magic "index" keyword */
175 1.1 cherry doindex = !strcmp(topic, "index");
176 1.1 cherry matched = doindex;
177 1.1 cherry
178 1.1 cherry /* Scan the helpfile looking for help matching the request */
179 1.1 cherry pager_open();
180 1.1 cherry while(help_getnext(hfd, &t, &s, &d)) {
181 1.1 cherry
182 1.1 cherry if (doindex) { /* dink around formatting */
183 1.1 cherry if (help_emitsummary(t, s, d))
184 1.1 cherry break;
185 1.1 cherry
186 1.1 cherry } else if (strcmp(topic, t)) {
187 1.1 cherry /* topic mismatch */
188 1.1 cherry if(matched) /* nothing more on this topic, stop scanning */
189 1.1 cherry break;
190 1.1 cherry
191 1.1 cherry } else {
192 1.1 cherry /* topic matched */
193 1.1 cherry matched = 1;
194 1.1 cherry if (((subtopic == NULL) && (s == NULL)) ||
195 1.1 cherry ((subtopic != NULL) && (s != NULL) && !strcmp(subtopic, s))) {
196 1.1 cherry /* exact match, print text */
197 1.1 cherry while((fgetstr(buf, 80, hfd) >= 0) && (buf[0] != '#')) {
198 1.1 cherry if (pager_output(buf))
199 1.1 cherry break;
200 1.1 cherry if (pager_output("\n"))
201 1.1 cherry break;
202 1.1 cherry }
203 1.1 cherry } else if ((subtopic == NULL) && (s != NULL)) {
204 1.1 cherry /* topic match, list subtopics */
205 1.1 cherry if (help_emitsummary(t, s, d))
206 1.1 cherry break;
207 1.1 cherry }
208 1.1 cherry }
209 1.1 cherry free(t);
210 1.1 cherry free(s);
211 1.1 cherry free(d);
212 1.1 cherry }
213 1.1 cherry pager_close();
214 1.1 cherry close(hfd);
215 1.1 cherry if (!matched) {
216 1.4 christos command_seterr("no help available for '%s'", topic);
217 1.1 cherry free(topic);
218 1.1 cherry if (subtopic)
219 1.1 cherry free(subtopic);
220 1.1 cherry return(CMD_ERROR);
221 1.1 cherry }
222 1.1 cherry free(topic);
223 1.1 cherry if (subtopic)
224 1.1 cherry free(subtopic);
225 1.1 cherry return(CMD_OK);
226 1.1 cherry }
227 1.1 cherry
228 1.1 cherry
229 1.1 cherry int
230 1.1 cherry command_commandlist(int argc, char *argv[])
231 1.1 cherry {
232 1.1 cherry struct bootblk_command *cmdp;
233 1.1 cherry int res;
234 1.1 cherry char name[20];
235 1.1 cherry int i;
236 1.1 cherry
237 1.1 cherry res = 0;
238 1.1 cherry pager_open();
239 1.1 cherry res = pager_output("Available commands:\n");
240 1.1 cherry
241 1.1 cherry for (i = 0, cmdp = commands; (cmdp->c_name != NULL) && (cmdp->c_desc != NULL ); i++, cmdp = commands + i) {
242 1.1 cherry if (res)
243 1.1 cherry break;
244 1.1 cherry if ((cmdp->c_name != NULL) && (cmdp->c_desc != NULL)) {
245 1.4 christos snprintf(name, sizeof(name), " %s ", cmdp->c_name);
246 1.1 cherry pager_output(name);
247 1.1 cherry pager_output(cmdp->c_desc);
248 1.1 cherry res = pager_output("\n");
249 1.1 cherry }
250 1.1 cherry }
251 1.1 cherry pager_close();
252 1.1 cherry return(CMD_OK);
253 1.1 cherry }
254 1.1 cherry
255 1.1 cherry /*
256 1.1 cherry * XXX set/show should become set/echo if we have variable
257 1.1 cherry * substitution happening.
258 1.1 cherry */
259 1.1 cherry
260 1.1 cherry int
261 1.1 cherry command_show(int argc, char *argv[])
262 1.1 cherry {
263 1.1 cherry struct env_var *ev;
264 1.1 cherry char *cp;
265 1.1 cherry
266 1.1 cherry if (argc < 2) {
267 1.1 cherry /*
268 1.1 cherry * With no arguments, print everything.
269 1.1 cherry */
270 1.1 cherry pager_open();
271 1.1 cherry for (ev = environ; ev != NULL; ev = ev->ev_next) {
272 1.1 cherry pager_output(ev->ev_name);
273 1.1 cherry cp = getenv(ev->ev_name);
274 1.1 cherry if (cp != NULL) {
275 1.1 cherry pager_output("=");
276 1.1 cherry pager_output(cp);
277 1.1 cherry }
278 1.1 cherry if (pager_output("\n"))
279 1.1 cherry break;
280 1.1 cherry }
281 1.1 cherry pager_close();
282 1.1 cherry } else {
283 1.1 cherry if ((cp = getenv(argv[1])) != NULL) {
284 1.1 cherry printf("%s\n", cp);
285 1.1 cherry } else {
286 1.4 christos command_seterr("variable '%s' not found", argv[1]);
287 1.1 cherry return(CMD_ERROR);
288 1.1 cherry }
289 1.1 cherry }
290 1.1 cherry return(CMD_OK);
291 1.1 cherry }
292 1.1 cherry
293 1.1 cherry
294 1.1 cherry int
295 1.1 cherry command_set(int argc, char *argv[])
296 1.1 cherry {
297 1.1 cherry int err;
298 1.1 cherry
299 1.1 cherry if (argc != 2) {
300 1.4 christos command_seterr("wrong number of arguments");
301 1.1 cherry return(CMD_ERROR);
302 1.1 cherry } else {
303 1.1 cherry if ((err = putenv(argv[1])) != 0) {
304 1.4 christos command_seterr("%s", strerror(err));
305 1.1 cherry return(CMD_ERROR);
306 1.1 cherry }
307 1.1 cherry }
308 1.1 cherry return(CMD_OK);
309 1.1 cherry }
310 1.1 cherry
311 1.1 cherry
312 1.1 cherry int
313 1.1 cherry command_unset(int argc, char *argv[])
314 1.1 cherry {
315 1.1 cherry int err;
316 1.1 cherry
317 1.1 cherry if (argc != 2) {
318 1.4 christos command_seterr("wrong number of arguments");
319 1.1 cherry return(CMD_ERROR);
320 1.1 cherry } else {
321 1.1 cherry if ((err = unsetenv(argv[1])) != 0) {
322 1.4 christos command_seterr("%s", strerror(err));
323 1.1 cherry return(CMD_ERROR);
324 1.1 cherry }
325 1.1 cherry }
326 1.1 cherry return(CMD_OK);
327 1.1 cherry }
328 1.1 cherry
329 1.1 cherry
330 1.1 cherry int
331 1.1 cherry command_echo(int argc, char *argv[])
332 1.1 cherry {
333 1.1 cherry char *s;
334 1.1 cherry int nl, ch;
335 1.1 cherry
336 1.1 cherry nl = 0;
337 1.1 cherry optind = 1;
338 1.1 cherry optreset = 1;
339 1.1 cherry while ((ch = getopt(argc, argv, "n")) != -1) {
340 1.1 cherry switch(ch) {
341 1.1 cherry case 'n':
342 1.1 cherry nl = 1;
343 1.1 cherry break;
344 1.1 cherry case '?':
345 1.1 cherry default:
346 1.1 cherry /* getopt has already reported an error */
347 1.1 cherry return(CMD_OK);
348 1.1 cherry }
349 1.1 cherry }
350 1.1 cherry argv += (optind);
351 1.1 cherry argc -= (optind);
352 1.1 cherry
353 1.1 cherry s = unargv(argc, argv);
354 1.1 cherry if (s != NULL) {
355 1.1 cherry printf("%s", s);
356 1.1 cherry free(s);
357 1.1 cherry }
358 1.1 cherry if (!nl)
359 1.1 cherry printf("\n");
360 1.1 cherry return(CMD_OK);
361 1.1 cherry }
362 1.1 cherry
363 1.1 cherry /*
364 1.1 cherry * A passable emulation of the sh(1) command of the same name.
365 1.1 cherry */
366 1.1 cherry
367 1.1 cherry
368 1.1 cherry int
369 1.1 cherry command_read(int argc, char *argv[])
370 1.1 cherry {
371 1.1 cherry char *prompt;
372 1.1 cherry int timeout;
373 1.1 cherry time_t when;
374 1.1 cherry char *cp;
375 1.1 cherry char *name;
376 1.1 cherry char buf[256]; /* XXX size? */
377 1.1 cherry int c;
378 1.1 cherry
379 1.1 cherry timeout = -1;
380 1.1 cherry prompt = NULL;
381 1.1 cherry optind = 1;
382 1.1 cherry optreset = 1;
383 1.1 cherry while ((c = getopt(argc, argv, "p:t:")) != -1) {
384 1.1 cherry switch(c) {
385 1.1 cherry
386 1.1 cherry case 'p':
387 1.1 cherry prompt = optarg;
388 1.1 cherry break;
389 1.1 cherry case 't':
390 1.1 cherry timeout = strtol(optarg, &cp, 0);
391 1.1 cherry if (cp == optarg) {
392 1.4 christos command_seterr("bad timeout '%s'", optarg);
393 1.1 cherry return(CMD_ERROR);
394 1.1 cherry }
395 1.1 cherry break;
396 1.1 cherry default:
397 1.1 cherry return(CMD_OK);
398 1.1 cherry }
399 1.1 cherry }
400 1.1 cherry
401 1.1 cherry argv += (optind);
402 1.1 cherry argc -= (optind);
403 1.1 cherry name = (argc > 0) ? argv[0]: NULL;
404 1.1 cherry
405 1.1 cherry if (prompt != NULL)
406 1.1 cherry printf("%s", prompt);
407 1.1 cherry if (timeout >= 0) {
408 1.1 cherry when = time(NULL) + timeout;
409 1.1 cherry while (!ischar())
410 1.1 cherry if (time(NULL) >= when)
411 1.1 cherry return(CMD_OK); /* is timeout an error? */
412 1.1 cherry }
413 1.1 cherry
414 1.1 cherry ngets(buf, sizeof(buf));
415 1.1 cherry
416 1.1 cherry if (name != NULL)
417 1.1 cherry setenv(name, buf, 1);
418 1.1 cherry return(CMD_OK);
419 1.1 cherry }
420 1.1 cherry
421 1.1 cherry /*
422 1.1 cherry * File pager
423 1.1 cherry */
424 1.1 cherry
425 1.1 cherry int
426 1.1 cherry command_more(int argc, char *argv[])
427 1.1 cherry {
428 1.1 cherry int i;
429 1.1 cherry int res;
430 1.1 cherry char line[80];
431 1.1 cherry
432 1.1 cherry res=0;
433 1.1 cherry pager_open();
434 1.1 cherry for (i = 1; (i < argc) && (res == 0); i++) {
435 1.4 christos snprintf(line, sizeof(line), "*** FILE %s BEGIN ***\n", argv[i]);
436 1.1 cherry if (pager_output(line))
437 1.1 cherry break;
438 1.1 cherry res = page_file(argv[i]);
439 1.1 cherry if (!res) {
440 1.4 christos snprintf(line, sizeof(line), "*** FILE %s END ***\n", argv[i]);
441 1.1 cherry res = pager_output(line);
442 1.1 cherry }
443 1.1 cherry }
444 1.1 cherry pager_close();
445 1.1 cherry
446 1.1 cherry if (res == 0)
447 1.1 cherry return CMD_OK;
448 1.1 cherry else
449 1.1 cherry return CMD_ERROR;
450 1.1 cherry }
451 1.1 cherry
452 1.1 cherry static int
453 1.1 cherry page_file(char *filename)
454 1.1 cherry {
455 1.1 cherry int result;
456 1.1 cherry
457 1.1 cherry result = pager_file(filename);
458 1.1 cherry
459 1.1 cherry if (result == -1)
460 1.4 christos command_seterr("error showing %s", filename);
461 1.1 cherry
462 1.1 cherry return result;
463 1.1 cherry }
464 1.1 cherry
465 1.1 cherry /*
466 1.1 cherry * List all disk-like devices
467 1.1 cherry */
468 1.1 cherry
469 1.1 cherry int
470 1.1 cherry command_lsdev(int argc, char *argv[])
471 1.1 cherry {
472 1.1 cherry int verbose, ch, i;
473 1.1 cherry char line[80];
474 1.1 cherry
475 1.1 cherry verbose = 0;
476 1.1 cherry optind = 1;
477 1.1 cherry optreset = 1;
478 1.1 cherry while ((ch = getopt(argc, argv, "v")) != -1) {
479 1.1 cherry switch(ch) {
480 1.1 cherry case 'v':
481 1.1 cherry verbose = 1;
482 1.1 cherry break;
483 1.1 cherry case '?':
484 1.1 cherry default:
485 1.1 cherry /* getopt has already reported an error */
486 1.1 cherry return(CMD_OK);
487 1.1 cherry }
488 1.1 cherry }
489 1.1 cherry argv += (optind);
490 1.1 cherry argc -= (optind);
491 1.1 cherry
492 1.1 cherry pager_open();
493 1.1 cherry
494 1.4 christos snprintf(line, sizeof(line), "Device Enumeration:\n");
495 1.1 cherry pager_output(line);
496 1.1 cherry
497 1.1 cherry for (i = 0; i < ndevs; i++) {
498 1.4 christos snprintf(line, sizeof(line), "%s\n", devsw[i].dv_name);
499 1.1 cherry if (pager_output(line))
500 1.1 cherry break;
501 1.1 cherry }
502 1.1 cherry
503 1.1 cherry pager_close();
504 1.1 cherry return(CMD_OK);
505 1.1 cherry }
506 1.1 cherry
507