Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

shell.c 3.1KB

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