123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
-
-
- #include <stdint.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.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>
-
-
-
- static struct command commands[0]
- __table_start ( struct command, commands );
- static struct command commands_end[0]
- __table_end ( struct command, commands );
-
-
- int optind;
- int nextchar;
-
-
- int execv ( const char *command, char * const argv[] ) {
- struct command *cmd;
- int argc;
-
-
- for ( argc = 0 ; argv[argc] ; argc++ ) {}
-
-
- if ( ! command ) {
- DBG ( "No command\n" );
- return -EINVAL;
- }
- if ( ! argc ) {
- DBG ( "%s: empty argument list\n", command );
- return -EINVAL;
- }
-
-
-
- reset_getopt();
-
-
- for ( cmd = commands ; cmd < commands_end ; cmd++ ) {
- if ( strcmp ( command, cmd->name ) == 0 )
- return cmd->exec ( argc, ( char ** ) argv );
- }
-
- printf ( "%s: command not found\n", command );
- return -ENOEXEC;
- }
-
-
- static char * expand_command ( const char *command ) {
- char *expcmd;
- char *start;
- char *end;
- char *head;
- char *name;
- char *tail;
- int setting_len;
- int new_len;
- char *tmp;
-
-
- expcmd = strdup ( command );
- if ( ! expcmd )
- return NULL;
-
-
- while ( 1 ) {
-
- head = expcmd;
-
-
- start = strstr ( expcmd, "${" );
- if ( ! start )
- break;
- *start = '\0';
- name = ( start + 2 );
-
-
- end = strstr ( name, "}" );
- if ( ! end )
- break;
- *end = '\0';
- tail = ( end + 1 );
-
-
- setting_len = fetchf_named_setting ( name, NULL, 0 );
- if ( setting_len < 0 )
- setting_len = 0;
-
-
- {
- char setting_buf[ setting_len + 1 ];
-
- setting_buf[0] = '\0';
- fetchf_named_setting ( name, setting_buf,
- sizeof ( setting_buf ) );
-
-
- tmp = expcmd;
- new_len = asprintf ( &expcmd, "%s%s%s",
- head, setting_buf, tail );
- free ( tmp );
- if ( new_len < 0 )
- return NULL;
- }
- }
-
- return expcmd;
- }
-
-
- static int split_args ( char *args, char * argv[] ) {
- int argc = 0;
-
- while ( 1 ) {
-
- while ( *args == ' ' ) {
- if ( argv )
- *args = '\0';
- args++;
- }
-
- if ( ! *args )
- break;
-
- if ( argv )
- argv[argc] = args;
- argc++;
-
- while ( *args && ( *args != ' ' ) ) {
- args++;
- }
- }
- return argc;
- }
-
-
- int system ( const char *command ) {
- char *args;
- int argc;
- int rc = 0;
-
-
- args = expand_command ( command );
- if ( ! args )
- return -ENOMEM;
-
-
- argc = split_args ( args, NULL );
-
-
- if ( argc ) {
- char * argv[argc + 1];
-
- split_args ( args, argv );
- argv[argc] = NULL;
-
- if ( argv[0][0] != '#' )
- rc = execv ( argv[0], argv );
- }
-
- free ( args );
- return rc;
- }
-
-
- static int echo_exec ( int argc, char **argv ) {
- int i;
-
- for ( i = 1 ; i < argc ; i++ ) {
- printf ( "%s%s", ( ( i == 1 ) ? "" : " " ), argv[i] );
- }
- printf ( "\n" );
- return 0;
- }
-
-
- struct command echo_command __command = {
- .name = "echo",
- .exec = echo_exec,
- };
|