exec.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020
00021 #include <stdint.h>
00022 #include <string.h>
00023 #include <stdlib.h>
00024 #include <stdio.h>
00025 #include <ctype.h>
00026 #include <unistd.h>
00027 #include <getopt.h>
00028 #include <errno.h>
00029 #include <assert.h>
00030 #include <gpxe/tables.h>
00031 #include <gpxe/command.h>
00032 #include <gpxe/settings.h>
00033
00034
00035
00036
00037
00038
00039
00040
00041 int optind;
00042 int nextchar;
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 int execv ( const char *command, char * const argv[] ) {
00055 struct command *cmd;
00056 int argc;
00057
00058
00059 for ( argc = 0 ; argv[argc] ; argc++ ) {}
00060
00061
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
00072
00073
00074
00075
00076 reset_getopt();
00077
00078
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 }
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 static char * expand_command ( const char *command ) {
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
00109 expcmd = strdup ( command );
00110 if ( ! expcmd )
00111 return NULL;
00112
00113
00114 while ( 1 ) {
00115
00116 head = expcmd;
00117
00118
00119 start = strstr ( expcmd, "${" );
00120 if ( ! start )
00121 break;
00122 *start = '\0';
00123 name = ( start + 2 );
00124
00125
00126 end = strstr ( name, "}" );
00127 if ( ! end )
00128 break;
00129 *end = '\0';
00130 tail = ( end + 1 );
00131
00132
00133 setting_len = fetchf_named_setting ( name, NULL, 0 );
00134 if ( setting_len < 0 )
00135 setting_len = 0;
00136
00137
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
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 }
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 static int split_args ( char *args, char * argv[] ) {
00170 int argc = 0;
00171
00172 while ( 1 ) {
00173
00174 while ( isspace ( *args ) ) {
00175 if ( argv )
00176 *args = '\0';
00177 args++;
00178 }
00179
00180 if ( ! *args )
00181 break;
00182
00183 if ( argv )
00184 argv[argc] = args;
00185 argc++;
00186
00187 while ( *args && ! isspace ( *args ) ) {
00188 args++;
00189 }
00190 }
00191 return argc;
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 int system ( const char *command ) {
00203 char *args;
00204 int argc;
00205 int rc = 0;
00206
00207
00208 args = expand_command ( command );
00209 if ( ! args )
00210 return -ENOMEM;
00211
00212
00213 argc = split_args ( args, NULL );
00214
00215
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 }
00229
00230
00231
00232
00233
00234
00235
00236
00237 static int echo_exec ( int argc, char **argv ) {
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 }
00246
00247
00248 struct command echo_command __command = {
00249 .name = "echo",
00250 .exec = echo_exec,
00251 };