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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. static struct command commands[0]
  30. __table_start ( struct command, commands );
  31. static struct command commands_end[0]
  32. __table_end ( struct command, commands );
  33. /** The shell prompt string */
  34. static const char shell_prompt[] = "gPXE> ";
  35. /** Flag set in order to exit shell */
  36. static int exit_flag = 0;
  37. /** "exit" command body */
  38. static int exit_exec ( int argc, char **argv __unused ) {
  39. if ( argc == 1 ) {
  40. exit_flag = 1;
  41. } else {
  42. printf ( "Usage: exit\n"
  43. "Exits the command shell\n" );
  44. }
  45. return 0;
  46. }
  47. /** "exit" command definition */
  48. struct command exit_command __command = {
  49. .name = "exit",
  50. .exec = exit_exec,
  51. };
  52. /** "help" command body */
  53. static int help_exec ( int argc __unused, char **argv __unused ) {
  54. struct command *command;
  55. unsigned int hpos = 0;
  56. printf ( "\nAvailable commands:\n\n" );
  57. for ( command = commands ; command < commands_end ; command++ ) {
  58. hpos += printf ( " %s", command->name );
  59. if ( hpos > ( 16 * 4 ) ) {
  60. printf ( "\n" );
  61. hpos = 0;
  62. } else {
  63. while ( hpos % 16 ) {
  64. printf ( " " );
  65. hpos++;
  66. }
  67. }
  68. }
  69. printf ( "\n\nType \"<command> --help\" for further information\n\n" );
  70. return 0;
  71. }
  72. /** "help" command definition */
  73. struct command help_command __command = {
  74. .name = "help",
  75. .exec = help_exec,
  76. };
  77. /**
  78. * Start command shell
  79. *
  80. */
  81. void shell ( void ) {
  82. char *line;
  83. exit_flag = 0;
  84. while ( ! exit_flag ) {
  85. line = readline ( shell_prompt );
  86. if ( line ) {
  87. system ( line );
  88. free ( line );
  89. }
  90. }
  91. }