2006-12-20 00:55:05 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-07-20 20:55:45 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
2006-12-20 00:55:05 +01:00
|
|
|
*/
|
|
|
|
|
2009-05-01 16:41:06 +02:00
|
|
|
FILE_LICENCE ( GPL2_OR_LATER );
|
|
|
|
|
2006-12-20 00:55:05 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
2007-01-19 02:13:12 +01:00
|
|
|
#include <stdio.h>
|
2011-03-30 20:44:15 +02:00
|
|
|
#include <string.h>
|
2010-11-22 21:20:33 +01:00
|
|
|
#include <getopt.h>
|
2006-12-20 00:55:05 +01:00
|
|
|
#include <readline/readline.h>
|
2010-04-19 21:16:01 +02:00
|
|
|
#include <ipxe/command.h>
|
2010-11-22 21:20:33 +01:00
|
|
|
#include <ipxe/parseopt.h>
|
2010-04-19 21:16:01 +02:00
|
|
|
#include <ipxe/shell.h>
|
2006-12-20 00:55:05 +01:00
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* Minimal command shell
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** The shell prompt string */
|
2010-04-19 21:16:01 +02:00
|
|
|
static const char shell_prompt[] = "iPXE> ";
|
2006-12-20 00:55:05 +01:00
|
|
|
|
2010-11-22 21:20:33 +01:00
|
|
|
/**
|
|
|
|
* "help" command
|
|
|
|
*
|
|
|
|
* @v argc Argument count
|
|
|
|
* @v argv Argument list
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2006-12-20 00:55:05 +01:00
|
|
|
static int help_exec ( int argc __unused, char **argv __unused ) {
|
|
|
|
struct command *command;
|
2007-08-03 02:03:21 +02:00
|
|
|
unsigned int hpos = 0;
|
2006-12-20 00:55:05 +01:00
|
|
|
|
|
|
|
printf ( "\nAvailable commands:\n\n" );
|
2009-03-12 20:41:40 +01:00
|
|
|
for_each_table_entry ( command, COMMANDS ) {
|
2007-08-03 02:03:21 +02:00
|
|
|
hpos += printf ( " %s", command->name );
|
|
|
|
if ( hpos > ( 16 * 4 ) ) {
|
|
|
|
printf ( "\n" );
|
|
|
|
hpos = 0;
|
|
|
|
} else {
|
|
|
|
while ( hpos % 16 ) {
|
|
|
|
printf ( " " );
|
|
|
|
hpos++;
|
|
|
|
}
|
|
|
|
}
|
2006-12-20 00:55:05 +01:00
|
|
|
}
|
2007-08-03 02:03:21 +02:00
|
|
|
printf ( "\n\nType \"<command> --help\" for further information\n\n" );
|
2006-12-20 00:55:05 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-22 21:20:33 +01:00
|
|
|
/** "help" command */
|
2006-12-20 00:55:05 +01:00
|
|
|
struct command help_command __command = {
|
|
|
|
.name = "help",
|
|
|
|
.exec = help_exec,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start command shell
|
|
|
|
*
|
|
|
|
*/
|
2010-11-28 22:09:33 +01:00
|
|
|
int shell ( void ) {
|
2011-03-30 20:44:15 +02:00
|
|
|
struct readline_history history;
|
2006-12-20 00:55:05 +01:00
|
|
|
char *line;
|
2010-11-28 22:09:33 +01:00
|
|
|
int rc = 0;
|
2006-12-20 00:55:05 +01:00
|
|
|
|
2011-03-30 20:44:15 +02:00
|
|
|
/* Initialise shell history */
|
|
|
|
memset ( &history, 0, sizeof ( history ) );
|
|
|
|
|
|
|
|
/* Read and execute commands */
|
2010-11-22 21:20:33 +01:00
|
|
|
do {
|
2012-10-25 08:21:49 +02:00
|
|
|
readline_history ( shell_prompt, NULL, &history, &line );
|
2006-12-20 00:55:05 +01:00
|
|
|
if ( line ) {
|
2010-11-28 22:09:33 +01:00
|
|
|
rc = system ( line );
|
2006-12-20 00:55:05 +01:00
|
|
|
free ( line );
|
|
|
|
}
|
2010-11-29 15:19:59 +01:00
|
|
|
} while ( ! shell_stopped ( SHELL_STOP_COMMAND_SEQUENCE ) );
|
2010-11-28 22:09:33 +01:00
|
|
|
|
2011-03-30 20:44:15 +02:00
|
|
|
/* Discard shell history */
|
|
|
|
history_free ( &history );
|
|
|
|
|
2010-11-28 22:09:33 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** "shell" options */
|
|
|
|
struct shell_options {};
|
|
|
|
|
|
|
|
/** "shell" option list */
|
|
|
|
static struct option_descriptor shell_opts[] = {};
|
|
|
|
|
|
|
|
/** "shell" command descriptor */
|
|
|
|
static struct command_descriptor shell_cmd =
|
2011-03-04 13:14:51 +01:00
|
|
|
COMMAND_DESC ( struct shell_options, shell_opts, 0, 0, "" );
|
2010-11-28 22:09:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* "shell" command
|
|
|
|
*
|
|
|
|
* @v argc Argument count
|
|
|
|
* @v argv Argument list
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int shell_exec ( int argc, char **argv ) {
|
|
|
|
struct shell_options opts;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Parse options */
|
|
|
|
if ( ( rc = parse_options ( argc, argv, &shell_cmd, &opts ) ) != 0 )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
/* Start shell */
|
|
|
|
if ( ( rc = shell() ) != 0 )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
return 0;
|
2006-12-20 00:55:05 +01:00
|
|
|
}
|
2010-11-28 22:09:33 +01:00
|
|
|
|
|
|
|
/** "shell" command */
|
|
|
|
struct command shell_command __command = {
|
|
|
|
.name = "shell",
|
|
|
|
.exec = shell_exec,
|
|
|
|
};
|