diff --git a/src/image/script.c b/src/image/script.c index b65fa061..ba098c2c 100644 --- a/src/image/script.c +++ b/src/image/script.c @@ -27,12 +27,119 @@ FILE_LICENCE ( GPL2_OR_LATER ); #include #include +#include #include #include +#include +#include +#include #include struct image_type script_image_type __image_type ( PROBE_NORMAL ); +/** Currently running script + * + * This is a global in order to allow goto_exec() to update the + * offset. + */ +static struct image *script; + +/** Offset within current script + * + * This is a global in order to allow goto_exec() to update the + * offset. + */ +static size_t script_offset; + +/** + * Process script lines + * + * @v process_line Line processor + * @v terminate Termination check + * @ret rc Return status code + */ +static int process_script ( int ( * process_line ) ( const char *line ), + int ( * terminate ) ( int rc ) ) { + off_t eol; + size_t len; + int rc; + + script_offset = 0; + + do { + + /* Find length of next line, excluding any terminating '\n' */ + eol = memchr_user ( script->data, script_offset, '\n', + ( script->len - script_offset ) ); + if ( eol < 0 ) + eol = script->len; + len = ( eol - script_offset ); + + /* Copy line, terminate with NUL, and execute command */ + { + char cmdbuf[ len + 1 ]; + + copy_from_user ( cmdbuf, script->data, + script_offset, len ); + cmdbuf[len] = '\0'; + DBG ( "$ %s\n", cmdbuf ); + + /* Move to next line */ + script_offset += ( len + 1 ); + + /* Process line */ + rc = process_line ( cmdbuf ); + if ( terminate ( rc ) ) + return rc; + } + + } while ( script_offset < script->len ); + + return rc; +} + +/** + * Terminate script processing if line processing failed + * + * @v rc Line processing status + * @ret terminate Terminate script processing + */ +static int terminate_on_failure ( int rc ) { + return ( rc != 0 ); +} + +/** + * Terminate script processing if line processing succeeded + * + * @v rc Line processing status + * @ret terminate Terminate script processing + */ +static int terminate_on_success ( int rc ) { + return ( rc == 0 ); +} + +/** + * Execute script line + * + * @v line Line of script + * @ret rc Return status code + */ +static int script_exec_line ( const char *line ) { + int rc; + + /* Skip label lines */ + if ( line[0] == ':' ) + return 0; + + /* Execute command */ + if ( ( rc = system ( line ) ) != 0 ) { + printf ( "Aborting on \"%s\"\n", line ); + return rc; + } + + return 0; +} + /** * Execute script * @@ -40,9 +147,8 @@ struct image_type script_image_type __image_type ( PROBE_NORMAL ); * @ret rc Return status code */ static int script_exec ( struct image *image ) { - size_t offset = 0; - off_t eol; - size_t len; + struct image *saved_script; + size_t saved_offset; int rc; /* Temporarily de-register image, so that a "boot" command @@ -50,36 +156,19 @@ static int script_exec ( struct image *image ) { */ unregister_image ( image ); - while ( offset < image->len ) { - - /* Find length of next line, excluding any terminating '\n' */ - eol = memchr_user ( image->data, offset, '\n', - ( image->len - offset ) ); - if ( eol < 0 ) - eol = image->len; - len = ( eol - offset ); + /* Preserve state of any currently-running script */ + saved_script = script; + saved_offset = script_offset; - /* Copy line, terminate with NUL, and execute command */ - { - char cmdbuf[ len + 1 ]; + /* Initialise state for this script */ + script = image; - copy_from_user ( cmdbuf, image->data, offset, len ); - cmdbuf[len] = '\0'; - DBG ( "$ %s\n", cmdbuf ); - if ( ( rc = system ( cmdbuf ) ) != 0 ) { - DBG ( "Command \"%s\" failed: %s\n", - cmdbuf, strerror ( rc ) ); - goto done; - } - } - - /* Move to next line */ - offset += ( len + 1 ); - } + /* Process script */ + rc = process_script ( script_exec_line, terminate_on_failure ); - rc = 0; - done: - /* Re-register image and return */ + /* Restore saved state, re-register image, and return */ + script_offset = saved_offset; + script = saved_script; register_image ( image ); return rc; } @@ -129,3 +218,78 @@ struct image_type script_image_type __image_type ( PROBE_NORMAL ) = { .load = script_load, .exec = script_exec, }; + +/** "goto" options */ +struct goto_options {}; + +/** "goto" option list */ +static struct option_descriptor goto_opts[] = {}; + +/** "goto" command descriptor */ +static struct command_descriptor goto_cmd = + COMMAND_DESC ( struct goto_options, goto_opts, 1, 1, + "