xref: /freebsd/stand/common/commands.c (revision f5147e312f43a9050468de539aeafa072caa1a60)
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 /* XXX should have procedural interface for setting, size limit? */
37 char		command_errbuf[COMMAND_ERRBUFSZ];
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 	    free(*subtopic);
95 	    free(*desc);
96 	    continue;
97 	}
98 	return(1);
99     }
100 }
101 
102 static int
103 help_emitsummary(char *topic, char *subtopic, char *desc)
104 {
105     int		i;
106 
107     pager_output("    ");
108     pager_output(topic);
109     i = strlen(topic);
110     if (subtopic != NULL) {
111 	pager_output(" ");
112 	pager_output(subtopic);
113 	i += strlen(subtopic) + 1;
114     }
115     if (desc != NULL) {
116 	do {
117 	    pager_output(" ");
118 	} while (i++ < 30);
119 	pager_output(desc);
120     }
121     return (pager_output("\n"));
122 }
123 
124 
125 static int
126 command_help(int argc, char *argv[])
127 {
128     char	buf[81];	/* XXX buffer size? */
129     int		hfd, matched, doindex;
130     char	*topic, *subtopic, *t, *s, *d;
131 
132     /* page the help text from our load path */
133     snprintf(buf, sizeof(buf), "%s/boot/loader.help", getenv("loaddev"));
134     if ((hfd = open(buf, O_RDONLY)) < 0) {
135 	printf("Verbose help not available, use '?' to list commands\n");
136 	return(CMD_OK);
137     }
138 
139     /* pick up request from arguments */
140     topic = subtopic = NULL;
141     switch(argc) {
142     case 3:
143 	subtopic = strdup(argv[2]);
144     case 2:
145 	topic = strdup(argv[1]);
146 	break;
147     case 1:
148 	topic = strdup("help");
149 	break;
150     default:
151 	command_errmsg = "usage is 'help <topic> [<subtopic>]";
152 	close(hfd);
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 	t = s = d = NULL;
195     }
196     free(t);
197     free(s);
198     free(d);
199     pager_close();
200     close(hfd);
201     if (!matched) {
202 	snprintf(command_errbuf, sizeof(command_errbuf),
203 	    "no help available for '%s'", topic);
204 	free(topic);
205 	free(subtopic);
206 	return(CMD_ERROR);
207     }
208     free(topic);
209     free(subtopic);
210     return(CMD_OK);
211 }
212 
213 COMMAND_SET(commandlist, "?", "list commands", command_commandlist);
214 
215 /*
216  * Please note: although we use the pager for the list of commands,
217  * this routine is called from the ? FORTH function which then
218  * unconditionally prints some commands. This will lead to anomalous
219  * behavior. There's no 'pager_output' binding to FORTH to allow
220  * things to work right, so I'm documenting the bug rather than
221  * fixing it.
222  */
223 static int
224 command_commandlist(int argc, char *argv[])
225 {
226     struct bootblk_command	**cmdp;
227     int		res;
228     char	name[20];
229 
230     res = 0;
231     pager_open();
232     res = pager_output("Available commands:\n");
233     SET_FOREACH(cmdp, Xcommand_set) {
234 	if (res)
235 	    break;
236 	if (((*cmdp)->c_name != NULL) && ((*cmdp)->c_desc != NULL)) {
237 	    sprintf(name, "  %-15s  ", (*cmdp)->c_name);
238 	    pager_output(name);
239 	    pager_output((*cmdp)->c_desc);
240 	    res = pager_output("\n");
241 	}
242     }
243     pager_close();
244     return(CMD_OK);
245 }
246 
247 /*
248  * XXX set/show should become set/echo if we have variable
249  * substitution happening.
250  */
251 
252 COMMAND_SET(show, "show", "show variable(s)", command_show);
253 
254 static int
255 command_show(int argc, char *argv[])
256 {
257     struct env_var	*ev;
258     char		*cp;
259 
260     if (argc < 2) {
261 	/*
262 	 * With no arguments, print everything.
263 	 */
264 	pager_open();
265 	for (ev = environ; ev != NULL; ev = ev->ev_next) {
266 	    pager_output(ev->ev_name);
267 	    cp = getenv(ev->ev_name);
268 	    if (cp != NULL) {
269 		pager_output("=");
270 		pager_output(cp);
271 	    }
272 	    if (pager_output("\n"))
273 		break;
274 	}
275 	pager_close();
276     } else {
277 	if ((cp = getenv(argv[1])) != NULL) {
278 	    printf("%s\n", cp);
279 	} else {
280 	    snprintf(command_errbuf, sizeof(command_errbuf),
281 		"variable '%s' not found", argv[1]);
282 	    return(CMD_ERROR);
283 	}
284     }
285     return(CMD_OK);
286 }
287 
288 COMMAND_SET(set, "set", "set a variable", command_set);
289 
290 static int
291 command_set(int argc, char *argv[])
292 {
293     int		err;
294 
295     if (argc != 2) {
296 	command_errmsg = "wrong number of arguments";
297 	return(CMD_ERROR);
298     } else {
299 	if ((err = putenv(argv[1])) != 0) {
300 	    command_errmsg = strerror(err);
301 	    return(CMD_ERROR);
302 	}
303     }
304     return(CMD_OK);
305 }
306 
307 COMMAND_SET(unset, "unset", "unset a variable", command_unset);
308 
309 static int
310 command_unset(int argc, char *argv[])
311 {
312     int		err;
313 
314     if (argc != 2) {
315 	command_errmsg = "wrong number of arguments";
316 	return(CMD_ERROR);
317     } else {
318 	if ((err = unsetenv(argv[1])) != 0) {
319 	    command_errmsg = strerror(err);
320 	    return(CMD_ERROR);
321 	}
322     }
323     return(CMD_OK);
324 }
325 
326 COMMAND_SET(echo, "echo", "echo arguments", command_echo);
327 
328 static int
329 command_echo(int argc, char *argv[])
330 {
331     char	*s;
332     int		nl, ch;
333 
334     nl = 0;
335     optind = 1;
336     optreset = 1;
337     while ((ch = getopt(argc, argv, "n")) != -1) {
338 	switch(ch) {
339 	case 'n':
340 	    nl = 1;
341 	    break;
342 	case '?':
343 	default:
344 	    /* getopt has already reported an error */
345 	    return(CMD_OK);
346 	}
347     }
348     argv += (optind);
349     argc -= (optind);
350 
351     s = unargv(argc, argv);
352     if (s != NULL) {
353 	printf("%s", s);
354 	free(s);
355     }
356     if (!nl)
357 	printf("\n");
358     return(CMD_OK);
359 }
360 
361 /*
362  * A passable emulation of the sh(1) command of the same name.
363  */
364 
365 COMMAND_SET(read, "read", "read input from the terminal", command_read);
366 
367 static int
368 command_read(int argc, char *argv[])
369 {
370     char	*prompt;
371     int		timeout;
372     time_t	when;
373     char	*cp;
374     char	*name;
375     char	buf[256];		/* XXX size? */
376     int		c;
377 
378     timeout = -1;
379     prompt = NULL;
380     optind = 1;
381     optreset = 1;
382     while ((c = getopt(argc, argv, "p:t:")) != -1) {
383 	switch(c) {
384 
385 	case 'p':
386 	    prompt = optarg;
387 	    break;
388 	case 't':
389 	    timeout = strtol(optarg, &cp, 0);
390 	    if (cp == optarg) {
391 		snprintf(command_errbuf, sizeof(command_errbuf),
392 		    "bad timeout '%s'", optarg);
393 		return(CMD_ERROR);
394 	    }
395 	    break;
396 	default:
397 	    return(CMD_OK);
398 	}
399     }
400 
401     argv += (optind);
402     argc -= (optind);
403     name = (argc > 0) ? argv[0]: NULL;
404 
405     if (prompt != NULL)
406 	printf("%s", prompt);
407     if (timeout >= 0) {
408 	when = time(NULL) + timeout;
409 	while (!ischar())
410 	    if (time(NULL) >= when)
411 		return(CMD_OK);		/* is timeout an error? */
412     }
413 
414     ngets(buf, sizeof(buf));
415 
416     if (name != NULL)
417 	setenv(name, buf, 1);
418     return(CMD_OK);
419 }
420 
421 /*
422  * File pager
423  */
424 COMMAND_SET(more, "more", "show contents of a file", command_more);
425 
426 static int
427 command_more(int argc, char *argv[])
428 {
429     int         i;
430     int         res;
431     char	line[80];
432 
433     res=0;
434     pager_open();
435     for (i = 1; (i < argc) && (res == 0); i++) {
436 	sprintf(line, "*** FILE %s BEGIN ***\n", argv[i]);
437 	if (pager_output(line))
438 		break;
439         res = page_file(argv[i]);
440 	if (!res) {
441 	    sprintf(line, "*** FILE %s END ***\n", argv[i]);
442 	    res = pager_output(line);
443 	}
444     }
445     pager_close();
446 
447     if (res == 0)
448 	return CMD_OK;
449     else
450 	return CMD_ERROR;
451 }
452 
453 static int
454 page_file(char *filename)
455 {
456     int result;
457 
458     result = pager_file(filename);
459 
460     if (result == -1) {
461 	snprintf(command_errbuf, sizeof(command_errbuf),
462 	    "error showing %s", filename);
463     }
464 
465     return result;
466 }
467 
468 /*
469  * List all disk-like devices
470  */
471 COMMAND_SET(lsdev, "lsdev", "list all devices", command_lsdev);
472 
473 static int
474 command_lsdev(int argc, char *argv[])
475 {
476     int		verbose, ch, i;
477     char	line[80];
478 
479     verbose = 0;
480     optind = 1;
481     optreset = 1;
482     while ((ch = getopt(argc, argv, "v")) != -1) {
483 	switch(ch) {
484 	case 'v':
485 	    verbose = 1;
486 	    break;
487 	case '?':
488 	default:
489 	    /* getopt has already reported an error */
490 	    return(CMD_OK);
491 	}
492     }
493     argv += (optind);
494     argc -= (optind);
495 
496     pager_open();
497     for (i = 0; devsw[i] != NULL; i++) {
498 	if (devsw[i]->dv_print != NULL){
499 	    if (devsw[i]->dv_print(verbose))
500 		break;
501 	} else {
502 	    sprintf(line, "%s: (unknown)\n", devsw[i]->dv_name);
503 	    if (pager_output(line))
504 		    break;
505 	}
506     }
507     pager_close();
508     return(CMD_OK);
509 }
510 
511