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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <getopt.h>
  25. #include <readline/readline.h>
  26. #include <ipxe/command.h>
  27. #include <ipxe/parseopt.h>
  28. #include <ipxe/shell.h>
  29. /** @file
  30. *
  31. * Minimal command shell
  32. *
  33. */
  34. /** The shell prompt string */
  35. static const char shell_prompt[] = "iPXE> ";
  36. /**
  37. * "help" command
  38. *
  39. * @v argc Argument count
  40. * @v argv Argument list
  41. * @ret rc Return status code
  42. */
  43. static int help_exec ( int argc __unused, char **argv __unused ) {
  44. struct command *command;
  45. unsigned int hpos = 0;
  46. printf ( "\nAvailable commands:\n\n" );
  47. for_each_table_entry ( command, COMMANDS ) {
  48. hpos += printf ( " %s", command->name );
  49. if ( hpos > ( 16 * 4 ) ) {
  50. printf ( "\n" );
  51. hpos = 0;
  52. } else {
  53. while ( hpos % 16 ) {
  54. printf ( " " );
  55. hpos++;
  56. }
  57. }
  58. }
  59. printf ( "\n\nType \"<command> --help\" for further information\n\n" );
  60. return 0;
  61. }
  62. /** "help" command */
  63. struct command help_command __command = {
  64. .name = "help",
  65. .exec = help_exec,
  66. };
  67. /**
  68. * Start command shell
  69. *
  70. */
  71. int shell ( void ) {
  72. struct readline_history history;
  73. char *line;
  74. int rc = 0;
  75. /* Initialise shell history */
  76. memset ( &history, 0, sizeof ( history ) );
  77. /* Read and execute commands */
  78. do {
  79. readline_history ( shell_prompt, NULL, &history, &line );
  80. if ( line ) {
  81. rc = system ( line );
  82. free ( line );
  83. }
  84. } while ( ! shell_stopped ( SHELL_STOP_COMMAND_SEQUENCE ) );
  85. /* Discard shell history */
  86. history_free ( &history );
  87. return rc;
  88. }
  89. /** "shell" options */
  90. struct shell_options {};
  91. /** "shell" option list */
  92. static struct option_descriptor shell_opts[] = {};
  93. /** "shell" command descriptor */
  94. static struct command_descriptor shell_cmd =
  95. COMMAND_DESC ( struct shell_options, shell_opts, 0, 0, "" );
  96. /**
  97. * "shell" command
  98. *
  99. * @v argc Argument count
  100. * @v argv Argument list
  101. * @ret rc Return status code
  102. */
  103. static int shell_exec ( int argc, char **argv ) {
  104. struct shell_options opts;
  105. int rc;
  106. /* Parse options */
  107. if ( ( rc = parse_options ( argc, argv, &shell_cmd, &opts ) ) != 0 )
  108. return rc;
  109. /* Start shell */
  110. if ( ( rc = shell() ) != 0 )
  111. return rc;
  112. return 0;
  113. }
  114. /** "shell" command */
  115. struct command shell_command __command = {
  116. .name = "shell",
  117. .exec = shell_exec,
  118. };