1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 FILE_LICENCE ( GPL2_OR_LATER );
20 
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <readline/readline.h>
25 #include <gpxe/command.h>
26 #include <gpxe/shell.h>
27 
28 /** @file
29  *
30  * Minimal command shell
31  *
32  */
33 
34 /** The shell prompt string */
35 static const char shell_prompt[] = "gPXE> ";
36 
37 /** Flag set in order to exit shell */
38 static int exit_flag = 0;
39 
40 /** "exit" command body */
exit_exec(int argc,char ** argv __unused)41 static int exit_exec ( int argc, char **argv __unused ) {
42 
43 	if ( argc == 1 ) {
44 		exit_flag = 1;
45 	} else {
46 		printf ( "Usage: exit\n"
47 			 "Exits the command shell\n" );
48 	}
49 
50 	return 0;
51 }
52 
53 /** "exit" command definition */
54 struct command exit_command __command = {
55 	.name = "exit",
56 	.exec = exit_exec,
57 };
58 
59 /** "help" command body */
help_exec(int argc __unused,char ** argv __unused)60 static int help_exec ( int argc __unused, char **argv __unused ) {
61 	struct command *command;
62 	unsigned int hpos = 0;
63 
64 	printf ( "\nAvailable commands:\n\n" );
65 	for_each_table_entry ( command, COMMANDS ) {
66 		hpos += printf ( "  %s", command->name );
67 		if ( hpos > ( 16 * 4 ) ) {
68 			printf ( "\n" );
69 			hpos = 0;
70 		} else {
71 			while ( hpos % 16 ) {
72 				printf ( " " );
73 				hpos++;
74 			}
75 		}
76 	}
77 	printf ( "\n\nType \"<command> --help\" for further information\n\n" );
78 	return 0;
79 }
80 
81 /** "help" command definition */
82 struct command help_command __command = {
83 	.name = "help",
84 	.exec = help_exec,
85 };
86 
87 /**
88  * Start command shell
89  *
90  */
shell(void)91 void shell ( void ) {
92 	char *line;
93 
94 	exit_flag = 0;
95 	while ( ! exit_flag ) {
96 		line = readline ( shell_prompt );
97 		if ( line ) {
98 			system ( line );
99 			free ( line );
100 		}
101 	}
102 }
103