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., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <stdint.h>
00022 #include <stdlib.h>
00023 #include <stdio.h>
00024 #include <readline/readline.h>
00025 #include <gpxe/command.h>
00026 #include <gpxe/shell.h>
00027 
00028 /** @file
00029  *
00030  * Minimal command shell
00031  *
00032  */
00033 
00034 /** The shell prompt string */
00035 static const char shell_prompt[] = "gPXE> ";
00036 
00037 /** Flag set in order to exit shell */
00038 static int exit_flag = 0;
00039 
00040 /** "exit" command body */
00041 static int exit_exec ( int argc, char **argv __unused ) {
00042 
00043         if ( argc == 1 ) {
00044                 exit_flag = 1;
00045         } else {
00046                 printf ( "Usage: exit\n"
00047                          "Exits the command shell\n" );
00048         }
00049 
00050         return 0;
00051 }
00052 
00053 /** "exit" command definition */
00054 struct command exit_command __command = {
00055         .name = "exit",
00056         .exec = exit_exec,
00057 };
00058 
00059 /** "help" command body */
00060 static int help_exec ( int argc __unused, char **argv __unused ) {
00061         struct command *command;
00062         unsigned int hpos = 0;
00063 
00064         printf ( "\nAvailable commands:\n\n" );
00065         for_each_table_entry ( command, COMMANDS ) {
00066                 hpos += printf ( "  %s", command->name );
00067                 if ( hpos > ( 16 * 4 ) ) {
00068                         printf ( "\n" );
00069                         hpos = 0;
00070                 } else {
00071                         while ( hpos % 16 ) {
00072                                 printf ( " " );
00073                                 hpos++;
00074                         }
00075                 }
00076         }
00077         printf ( "\n\nType \"<command> --help\" for further information\n\n" );
00078         return 0;
00079 }
00080 
00081 /** "help" command definition */
00082 struct command help_command __command = {
00083         .name = "help",
00084         .exec = help_exec,
00085 };
00086 
00087 /**
00088  * Start command shell
00089  *
00090  */
00091 void shell ( void ) {
00092         char *line;
00093 
00094         exit_flag = 0;
00095         while ( ! exit_flag ) {
00096                 line = readline ( shell_prompt );
00097                 if ( line ) {
00098                         system ( line );
00099                         free ( line );
00100                 }
00101         }
00102 }

Generated on Tue Apr 6 20:01:06 2010 for gPXE by  doxygen 1.5.7.1