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