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