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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <stdio.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. /** The shell prompt string */
  30. static const char shell_prompt[] = "gPXE> ";
  31. /** Flag set in order to exit shell */
  32. static int exit_flag = 0;
  33. /** "exit" command body */
  34. static int exit_exec ( int argc, char **argv __unused ) {
  35. if ( argc == 1 ) {
  36. exit_flag = 1;
  37. } else {
  38. printf ( "Usage: exit\n"
  39. "Exits the command shell\n" );
  40. }
  41. return 0;
  42. }
  43. /** "exit" command definition */
  44. struct command exit_command __command = {
  45. .name = "exit",
  46. .exec = exit_exec,
  47. };
  48. /** "help" command body */
  49. static int help_exec ( int argc __unused, char **argv __unused ) {
  50. struct command *command;
  51. unsigned int hpos = 0;
  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. /** "help" command definition */
  69. struct command help_command __command = {
  70. .name = "help",
  71. .exec = help_exec,
  72. };
  73. /**
  74. * Start command shell
  75. *
  76. */
  77. void shell ( void ) {
  78. char *line;
  79. exit_flag = 0;
  80. while ( ! exit_flag ) {
  81. line = readline ( shell_prompt );
  82. if ( line ) {
  83. system ( line );
  84. free ( line );
  85. }
  86. }
  87. }