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

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