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

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