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 3.3KB

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