david/ipxe
david
/
ipxe
Archived
1
0
Fork 0

[image] Generalise "currently-running script" to "currently-running image"

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown 2011-03-09 13:38:56 +00:00
parent d17a84a056
commit ae92700fd4
3 changed files with 24 additions and 23 deletions

View File

@ -39,6 +39,9 @@ FILE_LICENCE ( GPL2_OR_LATER );
/** List of registered images */
struct list_head images = LIST_HEAD_INIT ( images );
/** Currently-executing image */
struct image *current_image;
/**
* Free executable image
*
@ -200,6 +203,7 @@ int image_probe ( struct image *image ) {
* @ret rc Return status code
*/
int image_exec ( struct image *image ) {
struct image *saved_current_image;
struct image *replacement;
struct uri *old_cwuri;
int rc;
@ -212,11 +216,14 @@ int image_exec ( struct image *image ) {
old_cwuri = uri_get ( cwuri );
churi ( image->uri );
/* Preserve record of any currently-running image */
saved_current_image = current_image;
/* Take out a temporary reference to the image. This allows
* the image to unregister itself if necessary, without
* automatically freeing itself.
*/
image_get ( image );
current_image = image_get ( image );
/* Try executing the image */
if ( ( rc = image->type->exec ( image ) ) != 0 ) {
@ -233,6 +240,9 @@ int image_exec ( struct image *image ) {
/* Drop temporary reference to the original image */
image_put ( image );
/* Restore previous currently-running image */
current_image = saved_current_image;
/* Reset current working directory */
churi ( old_cwuri );
uri_put ( old_cwuri );

View File

@ -38,13 +38,6 @@ FILE_LICENCE ( GPL2_OR_LATER );
#include <usr/prompt.h>
#include <ipxe/script.h>
/** 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
@ -55,11 +48,13 @@ static size_t script_offset;
/**
* Process script lines
*
* @v image Script
* @v process_line Line processor
* @v terminate Termination check
* @ret rc Return status code
*/
static int process_script ( int ( * process_line ) ( const char *line ),
static int process_script ( struct image *image,
int ( * process_line ) ( const char *line ),
int ( * terminate ) ( int rc ) ) {
off_t eol;
size_t len;
@ -70,17 +65,17 @@ static int process_script ( int ( * process_line ) ( const char *line ),
do {
/* Find length of next line, excluding any terminating '\n' */
eol = memchr_user ( script->data, script_offset, '\n',
( script->len - script_offset ) );
eol = memchr_user ( image->data, script_offset, '\n',
( image->len - script_offset ) );
if ( eol < 0 )
eol = script->len;
eol = image->len;
len = ( eol - script_offset );
/* Copy line, terminate with NUL, and execute command */
{
char cmdbuf[ len + 1 ];
copy_from_user ( cmdbuf, script->data,
copy_from_user ( cmdbuf, image->data,
script_offset, len );
cmdbuf[len] = '\0';
DBG ( "$ %s\n", cmdbuf );
@ -94,7 +89,7 @@ static int process_script ( int ( * process_line ) ( const char *line ),
return rc;
}
} while ( script_offset < script->len );
} while ( script_offset < image->len );
return rc;
}
@ -138,7 +133,6 @@ static int script_exec_line ( const char *line ) {
* @ret rc Return status code
*/
static int script_exec ( struct image *image ) {
struct image *saved_script;
size_t saved_offset;
int rc;
@ -148,18 +142,14 @@ static int script_exec ( struct image *image ) {
unregister_image ( image );
/* Preserve state of any currently-running script */
saved_script = script;
saved_offset = script_offset;
/* Initialise state for this script */
script = image;
/* Process script */
rc = process_script ( script_exec_line, terminate_on_exit_or_failure );
rc = process_script ( image, script_exec_line,
terminate_on_exit_or_failure );
/* Restore saved state, re-register image, and return */
script_offset = saved_offset;
script = saved_script;
register_image ( image );
return rc;
}
@ -262,7 +252,7 @@ static int goto_exec ( int argc, char **argv ) {
return rc;
/* Sanity check */
if ( ! script ) {
if ( ! current_image ) {
rc = -ENOTTY;
printf ( "Not in a script: %s\n", strerror ( rc ) );
return rc;
@ -273,7 +263,7 @@ static int goto_exec ( int argc, char **argv ) {
/* Find label */
saved_offset = script_offset;
if ( ( rc = process_script ( goto_find_label,
if ( ( rc = process_script ( current_image, goto_find_label,
terminate_on_label_found ) ) != 0 ) {
script_offset = saved_offset;
return rc;

View File

@ -110,6 +110,7 @@ struct image_type {
#define __image_type( probe_order ) __table_entry ( IMAGE_TYPES, probe_order )
extern struct list_head images;
extern struct image *current_image;
/** Iterate over all registered images */
#define for_each_image( image ) \