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., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <getopt.h>
31 #include <readline/readline.h>
32 #include <ipxe/command.h>
33 #include <ipxe/parseopt.h>
34 #include <ipxe/shell.h>
35 #include <config/branding.h>
36
37 /** @file
38 *
39 * Minimal command shell
40 *
41 */
42
43 /** The shell prompt string */
44 static const char shell_prompt[] = PRODUCT_SHORT_NAME "> ";
45
46 /**
47 * "help" command
48 *
49 * @v argc Argument count
50 * @v argv Argument list
51 * @ret rc Return status code
52 */
help_exec(int argc __unused,char ** argv __unused)53 static int help_exec ( int argc __unused, char **argv __unused ) {
54 struct command *command;
55 unsigned int hpos = 0;
56
57 printf ( "\nAvailable commands:\n\n" );
58 for_each_table_entry ( command, COMMANDS ) {
59 hpos += printf ( " %s", command->name );
60 if ( hpos > ( 16 * 4 ) ) {
61 printf ( "\n" );
62 hpos = 0;
63 } else {
64 while ( hpos % 16 ) {
65 printf ( " " );
66 hpos++;
67 }
68 }
69 }
70 printf ( "\n\nType \"<command> --help\" for further information\n\n" );
71 return 0;
72 }
73
74 /** "help" command */
75 struct command help_command __command = {
76 .name = "help",
77 .exec = help_exec,
78 };
79
80 /**
81 * Start command shell
82 *
83 */
shell(void)84 int shell ( void ) {
85 struct readline_history history;
86 char *line;
87 int rc = 0;
88
89 /* Initialise shell history */
90 memset ( &history, 0, sizeof ( history ) );
91
92 /* Read and execute commands */
93 do {
94 readline_history ( shell_prompt, NULL, &history, &line );
95 if ( line ) {
96 rc = system ( line );
97 free ( line );
98 }
99 } while ( ! shell_stopped ( SHELL_STOP_COMMAND_SEQUENCE ) );
100
101 /* Discard shell history */
102 history_free ( &history );
103
104 return rc;
105 }
106
107 /** "shell" options */
108 struct shell_options {};
109
110 /** "shell" option list */
111 static struct option_descriptor shell_opts[] = {};
112
113 /** "shell" command descriptor */
114 static struct command_descriptor shell_cmd =
115 COMMAND_DESC ( struct shell_options, shell_opts, 0, 0, NULL );
116
117 /**
118 * "shell" command
119 *
120 * @v argc Argument count
121 * @v argv Argument list
122 * @ret rc Return status code
123 */
shell_exec(int argc,char ** argv)124 static int shell_exec ( int argc, char **argv ) {
125 struct shell_options opts;
126 int rc;
127
128 /* Parse options */
129 if ( ( rc = parse_options ( argc, argv, &shell_cmd, &opts ) ) != 0 )
130 return rc;
131
132 /* Start shell */
133 if ( ( rc = shell() ) != 0 )
134 return rc;
135
136 return 0;
137 }
138
139 /** "shell" command */
140 struct command shell_command __command = {
141 .name = "shell",
142 .exec = shell_exec,
143 };
144