#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <assert.h>
#include <gpxe/tables.h>
#include <gpxe/command.h>
#include <gpxe/settings.h>
Go to the source code of this file.
Functions | |
| FILE_LICENCE (GPL2_OR_LATER) | |
| int | execv (const char *command, char *const argv[]) |
| Execute command. | |
| static char * | expand_command (const char *command) |
| Expand variables within command line. | |
| static int | split_args (char *args, char *argv[]) |
| Split command line into argv array. | |
| int | system (const char *command) |
| Execute command line. | |
| static int | echo_exec (int argc, char **argv) |
| The "echo" command. | |
Variables | |
| int | optind |
| int | nextchar |
| struct command echo_command | __command |
| "echo" command | |
Definition in file exec.c.
| FILE_LICENCE | ( | GPL2_OR_LATER | ) |
| int execv | ( | const char * | command, | |
| char *const | argv[] | |||
| ) |
Execute command.
| command | Command name | |
| argv | Argument list |
| rc | Command exit status |
Definition at line 54 of file exec.c.
References COMMANDS, DBG, EINVAL, ENOEXEC, command::exec, for_each_table_entry, command::name, printf(), reset_getopt(), and strcmp().
Referenced by system(), and time_exec().
00054 { 00055 struct command *cmd; 00056 int argc; 00057 00058 /* Count number of arguments */ 00059 for ( argc = 0 ; argv[argc] ; argc++ ) {} 00060 00061 /* Sanity checks */ 00062 if ( ! command ) { 00063 DBG ( "No command\n" ); 00064 return -EINVAL; 00065 } 00066 if ( ! argc ) { 00067 DBG ( "%s: empty argument list\n", command ); 00068 return -EINVAL; 00069 } 00070 00071 /* Reset getopt() library ready for use by the command. This 00072 * is an artefact of the POSIX getopt() API within the context 00073 * of Etherboot; see the documentation for reset_getopt() for 00074 * details. 00075 */ 00076 reset_getopt(); 00077 00078 /* Hand off to command implementation */ 00079 for_each_table_entry ( cmd, COMMANDS ) { 00080 if ( strcmp ( command, cmd->name ) == 0 ) 00081 return cmd->exec ( argc, ( char ** ) argv ); 00082 } 00083 00084 printf ( "%s: command not found\n", command ); 00085 return -ENOEXEC; 00086 }
| static char* expand_command | ( | const char * | command | ) | [static] |
Expand variables within command line.
| command | Command line |
| expcmd | Expanded command line |
Definition at line 97 of file exec.c.
References asprintf(), fetchf_named_setting(), free(), name, NULL, strdup(), and strstr().
Referenced by system().
00097 { 00098 char *expcmd; 00099 char *start; 00100 char *end; 00101 char *head; 00102 char *name; 00103 char *tail; 00104 int setting_len; 00105 int new_len; 00106 char *tmp; 00107 00108 /* Obtain temporary modifiable copy of command line */ 00109 expcmd = strdup ( command ); 00110 if ( ! expcmd ) 00111 return NULL; 00112 00113 /* Expand while expansions remain */ 00114 while ( 1 ) { 00115 00116 head = expcmd; 00117 00118 /* Locate opener */ 00119 start = strstr ( expcmd, "${" ); 00120 if ( ! start ) 00121 break; 00122 *start = '\0'; 00123 name = ( start + 2 ); 00124 00125 /* Locate closer */ 00126 end = strstr ( name, "}" ); 00127 if ( ! end ) 00128 break; 00129 *end = '\0'; 00130 tail = ( end + 1 ); 00131 00132 /* Determine setting length */ 00133 setting_len = fetchf_named_setting ( name, NULL, 0 ); 00134 if ( setting_len < 0 ) 00135 setting_len = 0; /* Treat error as empty setting */ 00136 00137 /* Read setting into temporary buffer */ 00138 { 00139 char setting_buf[ setting_len + 1 ]; 00140 00141 setting_buf[0] = '\0'; 00142 fetchf_named_setting ( name, setting_buf, 00143 sizeof ( setting_buf ) ); 00144 00145 /* Construct expanded string and discard old string */ 00146 tmp = expcmd; 00147 new_len = asprintf ( &expcmd, "%s%s%s", 00148 head, setting_buf, tail ); 00149 free ( tmp ); 00150 if ( new_len < 0 ) 00151 return NULL; 00152 } 00153 } 00154 00155 return expcmd; 00156 }
| static int split_args | ( | char * | args, | |
| char * | argv[] | |||
| ) | [static] |
Split command line into argv array.
| args | Command line | |
| argv | Argument array to populate, or NULL |
| argc | Argument count |
argv is non-NULL, any whitespace in the command line will be replaced with NULs.
Definition at line 169 of file exec.c.
References isspace().
Referenced by system().
00169 { 00170 int argc = 0; 00171 00172 while ( 1 ) { 00173 /* Skip over any whitespace / convert to NUL */ 00174 while ( isspace ( *args ) ) { 00175 if ( argv ) 00176 *args = '\0'; 00177 args++; 00178 } 00179 /* Check for end of line */ 00180 if ( ! *args ) 00181 break; 00182 /* We have found the start of the next argument */ 00183 if ( argv ) 00184 argv[argc] = args; 00185 argc++; 00186 /* Skip to start of next whitespace, if any */ 00187 while ( *args && ! isspace ( *args ) ) { 00188 args++; 00189 } 00190 } 00191 return argc; 00192 }
| int system | ( | const char * | command | ) |
Execute command line.
| command | Command line |
| rc | Command exit status |
Definition at line 202 of file exec.c.
References ENOMEM, execv(), expand_command(), free(), NULL, and split_args().
Referenced by int22(), pxenv_file_exec(), script_exec(), and shell().
00202 { 00203 char *args; 00204 int argc; 00205 int rc = 0; 00206 00207 /* Perform variable expansion */ 00208 args = expand_command ( command ); 00209 if ( ! args ) 00210 return -ENOMEM; 00211 00212 /* Count arguments */ 00213 argc = split_args ( args, NULL ); 00214 00215 /* Create argv array and execute command */ 00216 if ( argc ) { 00217 char * argv[argc + 1]; 00218 00219 split_args ( args, argv ); 00220 argv[argc] = NULL; 00221 00222 if ( argv[0][0] != '#' ) 00223 rc = execv ( argv[0], argv ); 00224 } 00225 00226 free ( args ); 00227 return rc; 00228 }
| static int echo_exec | ( | int | argc, | |
| char ** | argv | |||
| ) | [static] |
The "echo" command.
| argc | Argument count | |
| argv | Argument list |
| rc | Exit code |
Definition at line 237 of file exec.c.
References printf().
00237 { 00238 int i; 00239 00240 for ( i = 1 ; i < argc ; i++ ) { 00241 printf ( "%s%s", ( ( i == 1 ) ? "" : " " ), argv[i] ); 00242 } 00243 printf ( "\n" ); 00244 return 0; 00245 }
| int optind |
Definition at line 41 of file exec.c.
Referenced by dhcp_exec(), gdbstub_exec(), get_argv_argument(), getopt_long(), ifcommon_exec(), imgargs_exec(), imgexec_exec(), imgfetch_core_exec(), imgfree_exec(), imgload_exec(), imgstat_exec(), match_long_option(), match_short_option(), pxebs_exec(), reset_getopt(), route_exec(), and sanboot_exec().
| int nextchar |
Definition at line 42 of file exec.c.
Referenced by getopt_long(), match_short_option(), and reset_getopt().
1.5.7.1