Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / hci / shell.c
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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <getopt.h>
27 #include <readline/readline.h>
28 #include <ipxe/command.h>
29 #include <ipxe/parseopt.h>
30 #include <ipxe/shell.h>
31
32 /** @file
33  *
34  * Minimal command shell
35  *
36  */
37
38 /** The shell prompt string */
39 static const char shell_prompt[] = "iPXE> ";
40
41 /**
42  * "help" command
43  *
44  * @v argc              Argument count
45  * @v argv              Argument list
46  * @ret rc              Return status code
47  */
48 static int help_exec ( int argc __unused, char **argv __unused ) {
49         struct command *command;
50         unsigned int hpos = 0;
51
52         printf ( "\nAvailable commands:\n\n" );
53         for_each_table_entry ( command, COMMANDS ) {
54                 hpos += printf ( "  %s", command->name );
55                 if ( hpos > ( 16 * 4 ) ) {
56                         printf ( "\n" );
57                         hpos = 0;
58                 } else {
59                         while ( hpos % 16 ) {
60                                 printf ( " " );
61                                 hpos++;
62                         }
63                 }
64         }
65         printf ( "\n\nType \"<command> --help\" for further information\n\n" );
66         return 0;
67 }
68
69 /** "help" command */
70 struct command help_command __command = {
71         .name = "help",
72         .exec = help_exec,
73 };
74
75 /**
76  * Start command shell
77  *
78  */
79 int shell ( void ) {
80         struct readline_history history;
81         char *line;
82         int rc = 0;
83
84         /* Initialise shell history */
85         memset ( &history, 0, sizeof ( history ) );
86
87         /* Read and execute commands */
88         do {
89                 readline_history ( shell_prompt, NULL, &history, &line );
90                 if ( line ) {
91                         rc = system ( line );
92                         free ( line );
93                 }
94         } while ( ! shell_stopped ( SHELL_STOP_COMMAND_SEQUENCE ) );
95
96         /* Discard shell history */
97         history_free ( &history );
98
99         return rc;
100 }
101
102 /** "shell" options */
103 struct shell_options {};
104
105 /** "shell" option list */
106 static struct option_descriptor shell_opts[] = {};
107
108 /** "shell" command descriptor */
109 static struct command_descriptor shell_cmd =
110         COMMAND_DESC ( struct shell_options, shell_opts, 0, 0, NULL );
111
112 /**
113  * "shell" command
114  *
115  * @v argc              Argument count
116  * @v argv              Argument list
117  * @ret rc              Return status code
118  */
119 static int shell_exec ( int argc, char **argv ) {
120         struct shell_options opts;
121         int rc;
122
123         /* Parse options */
124         if ( ( rc = parse_options ( argc, argv, &shell_cmd, &opts ) ) != 0 )
125                 return rc;
126
127         /* Start shell */
128         if ( ( rc = shell() ) != 0 )
129                 return rc;
130
131         return 0;
132 }
133
134 /** "shell" command */
135 struct command shell_command __command = {
136         .name = "shell",
137         .exec = shell_exec,
138 };