[gPXE] [PATCH] [script] Allow for exiting scripts

Shao Miller Shao.Miller at yrdsb.edu.on.ca
Fri Mar 5 00:05:34 EST 2010


In staging: 
http://git.etherboot.org/?p=gpxe-staging.git;a=commitdiff;h=963f18ec1127b55b42434002587ac565ce33cb3a

In tracking system: 
http://support.etherboot.org/index.php?do=details&task_id=73

Also attached.

- Shao Miller
-----

 From 963f18ec1127b55b42434002587ac565ce33cb3a Mon Sep 17 00:00:00 2001
From: Shao Miller <shao.miller at yrdsb.edu.on.ca>
Date: Thu, 4 Mar 2010 23:14:56 -0500
Subject: [PATCH] [script] Allow for exiting scripts

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

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: 0001--script-Allow-for-exiting-scripts.patch
Url: http://etherboot.org/pipermail/gpxe/attachments/20100305/ff923c9a/attachment.cc 


More information about the gPXE mailing list