From 79bcbbe5c07cb60ee8b240ec0ffff6448f3c42c5 Mon Sep 17 00:00:00 2001
From: Shao Miller <shao.miller@yrdsb.edu.on.ca>
Date: Thu, 18 Mar 2010 22:50:54 -0400
Subject: [PATCH] [script] Allow control over error handling

Use 'clear script_errors' from within a script to prevent
any error during execution from exiting all scripts.

Use 'set script_errors 1' to restore the behaviour if it
was cleared.

Uncompressed code size cost: 97 bytes
---
 src/image/script.c |   23 ++++++++++++++++++++++-
 1 files changed, 22 insertions(+), 1 deletions(-)

diff --git a/src/image/script.c b/src/image/script.c
index 0835ecb..df12557 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/settings.h>
 
 struct image_type script_image_type __image_type ( PROBE_NORMAL );
 
@@ -44,12 +45,28 @@ static int script_exec ( struct image *image ) {
 	off_t eol;
 	size_t len;
 	int rc;
+	int error_control = 1;
+	static struct setting script_errors = {
+		.name = "script_errors",
+		.description = "",
+		.type = &setting_type_hex,
+		.tag = 0,
+	};
 
 	/* Temporarily de-register image, so that a "boot" command
 	 * doesn't throw us into an execution loop.
 	 */
 	unregister_image ( image );
 
+	/* Store the current error control */
+	rc = store_setting ( NULL, &script_errors, &error_control,
+			     sizeof ( error_control ) );
+	if ( rc < 0 ) {
+		DBG ( "Could not save script error control: %s\n",
+		      strerror ( rc ) );
+		goto done;
+	}
+
 	while ( offset < image->len ) {
 	
 		/* Find length of next line, excluding any terminating '\n' */
@@ -66,7 +83,11 @@ static int script_exec ( struct image *image ) {
 			copy_from_user ( cmdbuf, image->data, offset, len );
 			cmdbuf[len] = '\0';
 			DBG ( "$ %s\n", cmdbuf );
-			if ( ( rc = system ( cmdbuf ) ) != 0 ) {
+			rc = system ( cmdbuf );
+			/* Possibly update the error control flag */
+			fetch_setting ( NULL, &script_errors, &error_control,
+					sizeof ( error_control ) );
+			if ( rc != 0 && error_control != 0 ) {
 				DBG ( "Command \"%s\" failed: %s\n",
 				      cmdbuf, strerror ( rc ) );
 				goto done;
-- 
1.5.6.3

