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.

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