/** * Execute command line * * @v command Command line * @ret rc Return status code * * Execute the named command and arguments. */ int system ( const char *command ) { int count = split_command ( ( char * ) command, NULL ); char *all_tokens[ count + 1 ]; int ( * process_next ) ( int rc ); char *command_copy; char **tokens; int argc; int process; int rc = 0; /* Create modifiable copy of command */ command_copy = strdup ( command ); if ( ! command_copy ) return -ENOMEM; /* Split command into tokens */ split_command ( command_copy, all_tokens ); all_tokens[count] = NULL; /* Process individual commands */ process = 1; for ( tokens = all_tokens ; ; tokens += ( argc + 1 ) ) { /* Find command terminator */ argc = command_terminator ( tokens, &process_next ); /* Expand tokens and execute command */ if ( process ) { char *argv[ argc + 1 ]; /* Expand tokens */ if ( ( rc = expand_tokens ( argc, tokens, argv ) ) != 0) break; argv[argc] = NULL; /* Execute command */ rc = execv ( argv[0], argv ); /* Free tokens */ free_tokens ( argv ); } /* Stop processing, if applicable */ if ( shell_stopped ( SHELL_STOP_COMMAND ) ) break; /* Stop processing if we have reached the end of the * command. */ if ( ! process_next ) break; /* Determine whether or not to process next command */ process = process_next ( rc ); } /* Free modified copy of command */ free ( command_copy ); return rc; }
/** * Terminate script processing on shell exit or command failure * * @v rc Line processing status * @ret terminate Terminate script processing */ static int terminate_on_exit_or_failure ( int rc ) { return ( shell_stopped ( SHELL_STOP_COMMAND_SEQUENCE ) || ( rc != 0 ) ); }