]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/boot/common/commands.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / boot / common / commands.c
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  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <stand.h>
31 #include <string.h>
32
33 #include "bootstrap.h"
34
35 char            *command_errmsg;
36 char            command_errbuf[256];    /* XXX should have procedural interface for setting, size limit? */
37
38 static 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
46 help
47 text
48 here
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  */
60 COMMAND_SET(help, "help", "detailed help", command_help);
61
62 static int
63 help_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
103 static int
104 help_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     return (pager_output("\n"));
123 }
124
125             
126 static int
127 command_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             if (help_emitsummary(t, s, d))
166                 break;
167
168         } else if (strcmp(topic, t)) {
169             /* topic mismatch */
170             if(matched)         /* nothing more on this topic, stop scanning */
171                 break;
172
173         } else {
174             /* topic matched */
175             matched = 1;
176             if (((subtopic == NULL) && (s == NULL)) ||
177                 ((subtopic != NULL) && (s != NULL) && !strcmp(subtopic, s))) {
178                 /* exact match, print text */
179                 while((fgetstr(buf, 80, hfd) >= 0) && (buf[0] != '#')) {
180                     if (pager_output(buf))
181                         break;
182                     if (pager_output("\n"))
183                         break;
184                 }
185             } else if ((subtopic == NULL) && (s != NULL)) {
186                 /* topic match, list subtopics */
187                 if (help_emitsummary(t, s, d))
188                     break;
189             }
190         }
191         free(t);
192         free(s);
193         free(d);
194     }
195     pager_close();
196     close(hfd);
197     if (!matched) {
198         sprintf(command_errbuf, "no help available for '%s'", topic);
199         free(topic);
200         if (subtopic)
201             free(subtopic);
202         return(CMD_ERROR);
203     }
204     free(topic);
205     if (subtopic)
206         free(subtopic);
207     return(CMD_OK);
208 }
209
210
211 COMMAND_SET(commandlist, "?", "list commands", command_commandlist);
212
213 static int
214 command_commandlist(int argc, char *argv[])
215 {
216     struct bootblk_command      **cmdp;
217     int         res;
218     char        name[20];
219
220     res = 0;
221     pager_open();
222     res = pager_output("Available commands:\n");
223     SET_FOREACH(cmdp, Xcommand_set) {
224         if (res)
225             break;
226         if (((*cmdp)->c_name != NULL) && ((*cmdp)->c_desc != NULL)) {
227             sprintf(name, "  %-15s  ", (*cmdp)->c_name);
228             pager_output(name);
229             pager_output((*cmdp)->c_desc);
230             res = pager_output("\n");
231         }
232     }
233     pager_close();
234     return(CMD_OK);
235 }
236
237 /*
238  * XXX set/show should become set/echo if we have variable
239  * substitution happening.
240  */
241
242 COMMAND_SET(show, "show", "show variable(s)", command_show);
243
244 static int
245 command_show(int argc, char *argv[])
246 {
247     struct env_var      *ev;
248     char                *cp;
249
250     if (argc < 2) {
251         /* 
252          * With no arguments, print everything.
253          */
254         pager_open();
255         for (ev = environ; ev != NULL; ev = ev->ev_next) {
256             pager_output(ev->ev_name);
257             cp = getenv(ev->ev_name);
258             if (cp != NULL) {
259                 pager_output("=");
260                 pager_output(cp);
261             }
262             if (pager_output("\n"))
263                 break;
264         }
265         pager_close();
266     } else {
267         if ((cp = getenv(argv[1])) != NULL) {
268             printf("%s\n", cp);
269         } else {
270             sprintf(command_errbuf, "variable '%s' not found", argv[1]);
271             return(CMD_ERROR);
272         }
273     }
274     return(CMD_OK);
275 }
276
277 COMMAND_SET(set, "set", "set a variable", command_set);
278
279 static int
280 command_set(int argc, char *argv[])
281 {
282     int         err;
283     
284     if (argc != 2) {
285         command_errmsg = "wrong number of arguments";
286         return(CMD_ERROR);
287     } else {
288         if ((err = putenv(argv[1])) != 0) {
289             command_errmsg = strerror(err);
290             return(CMD_ERROR);
291         }
292     }
293     return(CMD_OK);
294 }
295
296 COMMAND_SET(unset, "unset", "unset a variable", command_unset);
297
298 static int
299 command_unset(int argc, char *argv[]) 
300 {
301     int         err;
302     
303     if (argc != 2) {
304         command_errmsg = "wrong number of arguments";
305         return(CMD_ERROR);
306     } else {
307         if ((err = unsetenv(argv[1])) != 0) {
308             command_errmsg = strerror(err);
309             return(CMD_ERROR);
310         }
311     }
312     return(CMD_OK);
313 }
314
315 COMMAND_SET(echo, "echo", "echo arguments", command_echo);
316
317 static int
318 command_echo(int argc, char *argv[])
319 {
320     char        *s;
321     int         nl, ch;
322     
323     nl = 0;
324     optind = 1;
325     optreset = 1;
326     while ((ch = getopt(argc, argv, "n")) != -1) {
327         switch(ch) {
328         case 'n':
329             nl = 1;
330             break;
331         case '?':
332         default:
333             /* getopt has already reported an error */
334             return(CMD_OK);
335         }
336     }
337     argv += (optind);
338     argc -= (optind);
339
340     s = unargv(argc, argv);
341     if (s != NULL) {
342         printf("%s", s);
343         free(s);
344     }
345     if (!nl)
346         printf("\n");
347     return(CMD_OK);
348 }
349
350 /*
351  * A passable emulation of the sh(1) command of the same name.
352  */
353
354 COMMAND_SET(read, "read", "read input from the terminal", command_read);
355
356 static int
357 command_read(int argc, char *argv[])
358 {
359     char        *prompt;
360     int         timeout;
361     time_t      when;
362     char        *cp;
363     char        *name;
364     char        buf[256];               /* XXX size? */
365     int         c;
366     
367     timeout = -1;
368     prompt = NULL;
369     optind = 1;
370     optreset = 1;
371     while ((c = getopt(argc, argv, "p:t:")) != -1) {
372         switch(c) {
373             
374         case 'p':
375             prompt = optarg;
376             break;
377         case 't':
378             timeout = strtol(optarg, &cp, 0);
379             if (cp == optarg) {
380                 sprintf(command_errbuf, "bad timeout '%s'", optarg);
381                 return(CMD_ERROR);
382             }
383             break;
384         default:
385             return(CMD_OK);
386         }
387     }
388
389     argv += (optind);
390     argc -= (optind);
391     name = (argc > 0) ? argv[0]: NULL;
392         
393     if (prompt != NULL)
394         printf("%s", prompt);
395     if (timeout >= 0) {
396         when = time(NULL) + timeout;
397         while (!ischar())
398             if (time(NULL) >= when)
399                 return(CMD_OK);         /* is timeout an error? */
400     }
401
402     ngets(buf, sizeof(buf));
403
404     if (name != NULL)
405         setenv(name, buf, 1);
406     return(CMD_OK);
407 }
408
409 /*
410  * File pager
411  */
412 COMMAND_SET(more, "more", "show contents of a file", command_more);
413
414 static int
415 command_more(int argc, char *argv[])
416 {
417     int         i;
418     int         res;
419     char        line[80];
420
421     res=0;
422     pager_open();
423     for (i = 1; (i < argc) && (res == 0); i++) {
424         sprintf(line, "*** FILE %s BEGIN ***\n", argv[i]);
425         if (pager_output(line))
426                 break;
427         res = page_file(argv[i]);
428         if (!res) {
429             sprintf(line, "*** FILE %s END ***\n", argv[i]);
430             res = pager_output(line);
431         }
432     }
433     pager_close();
434
435     if (res == 0)
436         return CMD_OK;
437     else
438         return CMD_ERROR;
439 }
440
441 static int
442 page_file(char *filename)
443 {
444     int result;
445
446     result = pager_file(filename);
447
448     if (result == -1)
449         sprintf(command_errbuf, "error showing %s", filename);
450
451     return result;
452 }   
453
454 /*
455  * List all disk-like devices
456  */
457 COMMAND_SET(lsdev, "lsdev", "list all devices", command_lsdev);
458
459 static int
460 command_lsdev(int argc, char *argv[])
461 {
462     int         verbose, ch, i;
463     char        line[80];
464     
465     verbose = 0;
466     optind = 1;
467     optreset = 1;
468     while ((ch = getopt(argc, argv, "v")) != -1) {
469         switch(ch) {
470         case 'v':
471             verbose = 1;
472             break;
473         case '?':
474         default:
475             /* getopt has already reported an error */
476             return(CMD_OK);
477         }
478     }
479     argv += (optind);
480     argc -= (optind);
481
482     pager_open();
483     for (i = 0; devsw[i] != NULL; i++) {
484         if (devsw[i]->dv_print != NULL){
485             sprintf(line, "%s devices:\n", devsw[i]->dv_name);
486             if (pager_output(line))
487                     break;
488             devsw[i]->dv_print(verbose);
489         } else {
490             sprintf(line, "%s: (unknown)\n", devsw[i]->dv_name);
491             if (pager_output(line))
492                     break;
493         }
494     }
495     pager_close();
496     return(CMD_OK);
497 }
498