You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shell.c 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <vsprintf.h>
  21. #include <readline/readline.h>
  22. #include <gpxe/command.h>
  23. #include <gpxe/shell.h>
  24. /** @file
  25. *
  26. * Minimal command shell
  27. *
  28. */
  29. static struct command commands[0] __table_start ( commands );
  30. static struct command commands_end[0] __table_end ( commands );
  31. /** The shell prompt string */
  32. static const char shell_prompt[] = "gPXE> ";
  33. /** Flag set in order to exit shell */
  34. static int exit_flag = 0;
  35. /** "exit" command body */
  36. static int exit_exec ( int argc, char **argv __unused ) {
  37. if ( argc == 1 ) {
  38. exit_flag = 1;
  39. } else {
  40. printf ( "Usage: exit\n"
  41. "Exits the command shell\n" );
  42. }
  43. return 0;
  44. }
  45. /** "exit" command definition */
  46. struct command exit_command __command = {
  47. .name = "exit",
  48. .exec = exit_exec,
  49. };
  50. /** "help" command body */
  51. static int help_exec ( int argc __unused, char **argv __unused ) {
  52. struct command *command;
  53. printf ( "\nAvailable commands:\n\n" );
  54. for ( command = commands ; command < commands_end ; command++ ) {
  55. printf ( " %s\n", command->name );
  56. }
  57. printf ( "\nType \"<command> --help\" for further information\n\n" );
  58. return 0;
  59. }
  60. /** "help" command definition */
  61. struct command help_command __command = {
  62. .name = "help",
  63. .exec = help_exec,
  64. };
  65. /**
  66. * Start command shell
  67. *
  68. */
  69. void shell ( void ) {
  70. char *line;
  71. exit_flag = 0;
  72. while ( ! exit_flag ) {
  73. line = readline ( shell_prompt );
  74. if ( line ) {
  75. system ( line );
  76. free ( line );
  77. }
  78. }
  79. }