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