Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

shell.c 3.0KB

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