[PATCH] [script] Allow for exiting scripts

Shao Miller shao.miller at yrdsb.edu.on.ca
Thu Mar 4 23:14:56 EST 2010


This commit allows the 'exit' command to set a flag which the
script parser will use to choose whether or not to terminate
execution of a script.  The flag is reset so that any calling
script is not also terminated.

Previously, the 'exit' command in a script did nothing.  This
would suggest that adding it as a new feature should not
negatively impact any gPXE scripts currently in existence.

For example,

 #!gpxe
 # caller.gpxe
 echo Caller executing
 imgload callee.gpxe
 boot callee.gpxe
 echo Callee must have terminated
 exit
 echo Impossibility

 #!gpxe
 # callee.gpxe
 echo Callee executing
 echo Pre-exit
 exit
 echo Post-exit

 $ make EMBEDDED_IMAGE=caller.gpxe,callee.gpxe bin/gpxe.pdsk
 $ qemu -fda bin/gpxe.pdsk -boot a

Code size cost: 19 bytes uncompressed (!)
---
 src/hci/shell.c          |    8 ++++----
 src/image/script.c       |    7 +++++++
 src/include/gpxe/shell.h |    1 +
 3 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/hci/shell.c b/src/hci/shell.c
index 5bedbdc..825dac3 100644
--- a/src/hci/shell.c
+++ b/src/hci/shell.c
@@ -35,13 +35,13 @@ FILE_LICENCE ( GPL2_OR_LATER );
 static const char shell_prompt[] = "gPXE> ";
 
 /** Flag set in order to exit shell */
-static int exit_flag = 0;
+int shell_exit_flag = 0;
 
 /** "exit" command body */
 static int exit_exec ( int argc, char **argv __unused ) {
 
 	if ( argc == 1 ) {
-		exit_flag = 1;
+		shell_exit_flag = 1;
 	} else {
 		printf ( "Usage: exit\n"
 			 "Exits the command shell\n" );
@@ -91,8 +91,8 @@ struct command help_command __command = {
 void shell ( void ) {
 	char *line;
 
-	exit_flag = 0;
-	while ( ! exit_flag ) {
+	shell_exit_flag = 0;
+	while ( ! shell_exit_flag ) {
 		line = readline ( shell_prompt );
 		if ( line ) {
 			system ( line );
diff --git a/src/image/script.c b/src/image/script.c
index 0835ecb..4073eb0 100644
--- a/src/image/script.c
+++ b/src/image/script.c
@@ -30,6 +30,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 #include <ctype.h>
 #include <errno.h>
 #include <gpxe/image.h>
+#include <gpxe/shell.h>
 
 struct image_type script_image_type __image_type ( PROBE_NORMAL );
 
@@ -73,12 +74,18 @@ static int script_exec ( struct image *image ) {
 			}
 		}
 		
+		if ( shell_exit_flag )
+			/* "exit" command should exit this script */
+			goto done;
 		/* Move to next line */
 		offset += ( len + 1 );
 	}
 
 	rc = 0;
  done:
+	/* Reset exit flag */
+	shell_exit_flag = 0;
+
 	/* Re-register image and return */
 	register_image ( image );
 	return rc;
diff --git a/src/include/gpxe/shell.h b/src/include/gpxe/shell.h
index a65a344..148ea83 100644
--- a/src/include/gpxe/shell.h
+++ b/src/include/gpxe/shell.h
@@ -9,6 +9,7 @@
 
 FILE_LICENCE ( GPL2_OR_LATER );
 
+extern int shell_exit_flag;
 extern void shell ( void );
 
 #endif /* _GPXE_SHELL_H */
-- 
1.5.6.3


--------------050105070305030405090700--


More information about the gPXE mailing list