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.

exec.c 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #include <stdint.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <getopt.h>
  23. #include <errno.h>
  24. #include <assert.h>
  25. #include <vsprintf.h>
  26. #include <gpxe/tables.h>
  27. #include <gpxe/command.h>
  28. /** @file
  29. *
  30. * Command execution
  31. *
  32. */
  33. static struct command commands[0] __table_start ( commands );
  34. static struct command commands_end[0] __table_end ( commands );
  35. /* Avoid dragging in getopt.o unless a command really uses it */
  36. int optind;
  37. int nextchar;
  38. /**
  39. * Execute command
  40. *
  41. * @v command Command name
  42. * @v argv Argument list
  43. * @ret rc Command exit status
  44. *
  45. * Execute the named command. Unlike a traditional POSIX execv(),
  46. * this function returns the exit status of the command.
  47. */
  48. int execv ( const char *command, char * const argv[] ) {
  49. struct command *cmd;
  50. int argc;
  51. /* Count number of arguments */
  52. for ( argc = 0 ; argv[argc] ; argc++ ) {}
  53. /* Sanity checks */
  54. if ( ! command ) {
  55. DBG ( "No command\n" );
  56. return -EINVAL;
  57. }
  58. if ( ! argc ) {
  59. DBG ( "%s: empty argument list\n", command );
  60. return -EINVAL;
  61. }
  62. /* Reset getopt() library ready for use by the command. This
  63. * is an artefact of the POSIX getopt() API within the context
  64. * of Etherboot; see the documentation for reset_getopt() for
  65. * details.
  66. */
  67. reset_getopt();
  68. /* Hand off to command implementation */
  69. for ( cmd = commands ; cmd < commands_end ; cmd++ ) {
  70. if ( strcmp ( command, cmd->name ) == 0 )
  71. return cmd->exec ( argc, ( char ** ) argv );
  72. }
  73. printf ( "%s: command not found\n", command );
  74. return -ENOEXEC;
  75. }
  76. /**
  77. * Split command line into argv array
  78. *
  79. * @v args Command line
  80. * @v argv Argument array to populate, or NULL
  81. * @ret argc Argument count
  82. *
  83. * Splits the command line into whitespace-delimited arguments. If @c
  84. * argv is non-NULL, any whitespace in the command line will be
  85. * replaced with NULs.
  86. */
  87. static int split_args ( char *args, char * argv[] ) {
  88. int argc = 0;
  89. while ( 1 ) {
  90. /* Skip over any whitespace / convert to NUL */
  91. while ( *args == ' ' ) {
  92. if ( argv )
  93. *args = '\0';
  94. args++;
  95. }
  96. /* Check for end of line */
  97. if ( ! *args )
  98. break;
  99. /* We have found the start of the next argument */
  100. if ( argv )
  101. argv[argc] = args;
  102. argc++;
  103. /* Skip to start of next whitespace, if any */
  104. while ( *args && ( *args != ' ' ) ) {
  105. args++;
  106. }
  107. }
  108. return argc;
  109. }
  110. /**
  111. * Execute command line
  112. *
  113. * @v command Command line
  114. * @ret rc Command exit status
  115. *
  116. * Execute the named command and arguments.
  117. */
  118. int system ( const char *command ) {
  119. char *args;
  120. int argc;
  121. int rc;
  122. /* Obtain temporary modifiable copy of command line */
  123. args = strdup ( command );
  124. if ( ! args )
  125. return -ENOMEM;
  126. /* Count arguments */
  127. argc = split_args ( args, NULL );
  128. /* Create argv array and execute command */
  129. {
  130. char * argv[argc + 1];
  131. split_args ( args, argv );
  132. argv[argc] = NULL;
  133. rc = execv ( argv[0], argv );
  134. }
  135. free ( args );
  136. return rc;
  137. }