iPXE
shell.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <stdlib.h>
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <getopt.h>
00031 #include <readline/readline.h>
00032 #include <ipxe/command.h>
00033 #include <ipxe/parseopt.h>
00034 #include <ipxe/shell.h>
00035 #include <config/branding.h>
00036 
00037 /** @file
00038  *
00039  * Minimal command shell
00040  *
00041  */
00042 
00043 /** The shell prompt string */
00044 static const char shell_prompt[] = PRODUCT_SHORT_NAME "> ";
00045 
00046 /**
00047  * "help" command
00048  *
00049  * @v argc              Argument count
00050  * @v argv              Argument list
00051  * @ret rc              Return status code
00052  */
00053 static int help_exec ( int argc __unused, char **argv __unused ) {
00054         struct command *command;
00055         unsigned int hpos = 0;
00056 
00057         printf ( "\nAvailable commands:\n\n" );
00058         for_each_table_entry ( command, COMMANDS ) {
00059                 hpos += printf ( "  %s", command->name );
00060                 if ( hpos > ( 16 * 4 ) ) {
00061                         printf ( "\n" );
00062                         hpos = 0;
00063                 } else {
00064                         while ( hpos % 16 ) {
00065                                 printf ( " " );
00066                                 hpos++;
00067                         }
00068                 }
00069         }
00070         printf ( "\n\nType \"<command> --help\" for further information\n\n" );
00071         return 0;
00072 }
00073 
00074 /** "help" command */
00075 struct command help_command __command = {
00076         .name = "help",
00077         .exec = help_exec,
00078 };
00079 
00080 /**
00081  * Start command shell
00082  *
00083  */
00084 int shell ( void ) {
00085         struct readline_history history;
00086         char *line;
00087         int rc = 0;
00088 
00089         /* Initialise shell history */
00090         memset ( &history, 0, sizeof ( history ) );
00091 
00092         /* Read and execute commands */
00093         do {
00094                 readline_history ( shell_prompt, NULL, &history, &line );
00095                 if ( line ) {
00096                         rc = system ( line );
00097                         free ( line );
00098                 }
00099         } while ( ! shell_stopped ( SHELL_STOP_COMMAND_SEQUENCE ) );
00100 
00101         /* Discard shell history */
00102         history_free ( &history );
00103 
00104         return rc;
00105 }
00106 
00107 /** "shell" options */
00108 struct shell_options {};
00109 
00110 /** "shell" option list */
00111 static struct option_descriptor shell_opts[] = {};
00112 
00113 /** "shell" command descriptor */
00114 static struct command_descriptor shell_cmd =
00115         COMMAND_DESC ( struct shell_options, shell_opts, 0, 0, NULL );
00116 
00117 /**
00118  * "shell" command
00119  *
00120  * @v argc              Argument count
00121  * @v argv              Argument list
00122  * @ret rc              Return status code
00123  */
00124 static int shell_exec ( int argc, char **argv ) {
00125         struct shell_options opts;
00126         int rc;
00127 
00128         /* Parse options */
00129         if ( ( rc = parse_options ( argc, argv, &shell_cmd, &opts ) ) != 0 )
00130                 return rc;
00131 
00132         /* Start shell */
00133         if ( ( rc = shell() ) != 0 )
00134                 return rc;
00135 
00136         return 0;
00137 }
00138 
00139 /** "shell" command */
00140 struct command shell_command __command = {
00141         .name = "shell",
00142         .exec = shell_exec,
00143 };