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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. #include <gpxe/settings.h>
  29. /** @file
  30. *
  31. * Command execution
  32. *
  33. */
  34. /* Avoid dragging in getopt.o unless a command really uses it */
  35. int optind;
  36. int nextchar;
  37. /**
  38. * Execute command
  39. *
  40. * @v command Command name
  41. * @v argv Argument list
  42. * @ret rc Command exit status
  43. *
  44. * Execute the named command. Unlike a traditional POSIX execv(),
  45. * this function returns the exit status of the command.
  46. */
  47. int execv ( const char *command, char * const argv[] ) {
  48. struct command *cmd;
  49. int argc;
  50. /* Count number of arguments */
  51. for ( argc = 0 ; argv[argc] ; argc++ ) {}
  52. /* Sanity checks */
  53. if ( ! command ) {
  54. DBG ( "No command\n" );
  55. return -EINVAL;
  56. }
  57. if ( ! argc ) {
  58. DBG ( "%s: empty argument list\n", command );
  59. return -EINVAL;
  60. }
  61. /* Reset getopt() library ready for use by the command. This
  62. * is an artefact of the POSIX getopt() API within the context
  63. * of Etherboot; see the documentation for reset_getopt() for
  64. * details.
  65. */
  66. reset_getopt();
  67. /* Hand off to command implementation */
  68. for_each_table_entry ( cmd, COMMANDS ) {
  69. if ( strcmp ( command, cmd->name ) == 0 )
  70. return cmd->exec ( argc, ( char ** ) argv );
  71. }
  72. printf ( "%s: command not found\n", command );
  73. return -ENOEXEC;
  74. }
  75. /**
  76. * Expand variables within command line
  77. *
  78. * @v command Command line
  79. * @ret expcmd Expanded command line
  80. *
  81. * The expanded command line is allocated with malloc() and the caller
  82. * must eventually free() it.
  83. */
  84. static char * expand_command ( const char *command ) {
  85. char *expcmd;
  86. char *start;
  87. char *end;
  88. char *head;
  89. char *name;
  90. char *tail;
  91. int setting_len;
  92. int new_len;
  93. char *tmp;
  94. /* Obtain temporary modifiable copy of command line */
  95. expcmd = strdup ( command );
  96. if ( ! expcmd )
  97. return NULL;
  98. /* Expand while expansions remain */
  99. while ( 1 ) {
  100. head = expcmd;
  101. /* Locate opener */
  102. start = strstr ( expcmd, "${" );
  103. if ( ! start )
  104. break;
  105. *start = '\0';
  106. name = ( start + 2 );
  107. /* Locate closer */
  108. end = strstr ( name, "}" );
  109. if ( ! end )
  110. break;
  111. *end = '\0';
  112. tail = ( end + 1 );
  113. /* Determine setting length */
  114. setting_len = fetchf_named_setting ( name, NULL, 0 );
  115. if ( setting_len < 0 )
  116. setting_len = 0; /* Treat error as empty setting */
  117. /* Read setting into temporary buffer */
  118. {
  119. char setting_buf[ setting_len + 1 ];
  120. setting_buf[0] = '\0';
  121. fetchf_named_setting ( name, setting_buf,
  122. sizeof ( setting_buf ) );
  123. /* Construct expanded string and discard old string */
  124. tmp = expcmd;
  125. new_len = asprintf ( &expcmd, "%s%s%s",
  126. head, setting_buf, tail );
  127. free ( tmp );
  128. if ( new_len < 0 )
  129. return NULL;
  130. }
  131. }
  132. return expcmd;
  133. }
  134. /**
  135. * Split command line into argv array
  136. *
  137. * @v args Command line
  138. * @v argv Argument array to populate, or NULL
  139. * @ret argc Argument count
  140. *
  141. * Splits the command line into whitespace-delimited arguments. If @c
  142. * argv is non-NULL, any whitespace in the command line will be
  143. * replaced with NULs.
  144. */
  145. static int split_args ( char *args, char * argv[] ) {
  146. int argc = 0;
  147. while ( 1 ) {
  148. /* Skip over any whitespace / convert to NUL */
  149. while ( *args == ' ' ) {
  150. if ( argv )
  151. *args = '\0';
  152. args++;
  153. }
  154. /* Check for end of line */
  155. if ( ! *args )
  156. break;
  157. /* We have found the start of the next argument */
  158. if ( argv )
  159. argv[argc] = args;
  160. argc++;
  161. /* Skip to start of next whitespace, if any */
  162. while ( *args && ( *args != ' ' ) ) {
  163. args++;
  164. }
  165. }
  166. return argc;
  167. }
  168. /**
  169. * Execute command line
  170. *
  171. * @v command Command line
  172. * @ret rc Command exit status
  173. *
  174. * Execute the named command and arguments.
  175. */
  176. int system ( const char *command ) {
  177. char *args;
  178. int argc;
  179. int rc = 0;
  180. /* Perform variable expansion */
  181. args = expand_command ( command );
  182. if ( ! args )
  183. return -ENOMEM;
  184. /* Count arguments */
  185. argc = split_args ( args, NULL );
  186. /* Create argv array and execute command */
  187. if ( argc ) {
  188. char * argv[argc + 1];
  189. split_args ( args, argv );
  190. argv[argc] = NULL;
  191. if ( argv[0][0] != '#' )
  192. rc = execv ( argv[0], argv );
  193. }
  194. free ( args );
  195. return rc;
  196. }
  197. /**
  198. * The "echo" command
  199. *
  200. * @v argc Argument count
  201. * @v argv Argument list
  202. * @ret rc Exit code
  203. */
  204. static int echo_exec ( int argc, char **argv ) {
  205. int i;
  206. for ( i = 1 ; i < argc ; i++ ) {
  207. printf ( "%s%s", ( ( i == 1 ) ? "" : " " ), argv[i] );
  208. }
  209. printf ( "\n" );
  210. return 0;
  211. }
  212. /** "echo" command */
  213. struct command echo_command __command = {
  214. .name = "echo",
  215. .exec = echo_exec,
  216. };