選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

exec.c 5.4KB

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