소스 검색

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

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 13 년 전
부모
커밋
ae92700fd4
3개의 변경된 파일24개의 추가작업 그리고 23개의 파일을 삭제
  1. 11
    1
      src/core/image.c
  2. 12
    22
      src/image/script.c
  3. 1
    0
      src/include/ipxe/image.h

+ 11
- 1
src/core/image.c 파일 보기

@@ -39,6 +39,9 @@ FILE_LICENCE ( GPL2_OR_LATER );
39 39
 /** List of registered images */
40 40
 struct list_head images = LIST_HEAD_INIT ( images );
41 41
 
42
+/** Currently-executing image */
43
+struct image *current_image;
44
+
42 45
 /**
43 46
  * Free executable image
44 47
  *
@@ -200,6 +203,7 @@ int image_probe ( struct image *image ) {
200 203
  * @ret rc		Return status code
201 204
  */
202 205
 int image_exec ( struct image *image ) {
206
+	struct image *saved_current_image;
203 207
 	struct image *replacement;
204 208
 	struct uri *old_cwuri;
205 209
 	int rc;
@@ -212,11 +216,14 @@ int image_exec ( struct image *image ) {
212 216
 	old_cwuri = uri_get ( cwuri );
213 217
 	churi ( image->uri );
214 218
 
219
+	/* Preserve record of any currently-running image */
220
+	saved_current_image = current_image;
221
+
215 222
 	/* Take out a temporary reference to the image.  This allows
216 223
 	 * the image to unregister itself if necessary, without
217 224
 	 * automatically freeing itself.
218 225
 	 */
219
-	image_get ( image );
226
+	current_image = image_get ( image );
220 227
 
221 228
 	/* Try executing the image */
222 229
 	if ( ( rc = image->type->exec ( image ) ) != 0 ) {
@@ -233,6 +240,9 @@ int image_exec ( struct image *image ) {
233 240
 	/* Drop temporary reference to the original image */
234 241
 	image_put ( image );
235 242
 
243
+	/* Restore previous currently-running image */
244
+	current_image = saved_current_image;
245
+
236 246
 	/* Reset current working directory */
237 247
 	churi ( old_cwuri );
238 248
 	uri_put ( old_cwuri );

+ 12
- 22
src/image/script.c 파일 보기

@@ -38,13 +38,6 @@ FILE_LICENCE ( GPL2_OR_LATER );
38 38
 #include <usr/prompt.h>
39 39
 #include <ipxe/script.h>
40 40
 
41
-/** Currently running script
42
- *
43
- * This is a global in order to allow goto_exec() to update the
44
- * offset.
45
- */
46
-static struct image *script;
47
-
48 41
 /** Offset within current script
49 42
  *
50 43
  * This is a global in order to allow goto_exec() to update the
@@ -55,11 +48,13 @@ static size_t script_offset;
55 48
 /**
56 49
  * Process script lines
57 50
  *
51
+ * @v image		Script
58 52
  * @v process_line	Line processor
59 53
  * @v terminate		Termination check
60 54
  * @ret rc		Return status code
61 55
  */
62
-static int process_script ( int ( * process_line ) ( const char *line ),
56
+static int process_script ( struct image *image,
57
+			    int ( * process_line ) ( const char *line ),
63 58
 			    int ( * terminate ) ( int rc ) ) {
64 59
 	off_t eol;
65 60
 	size_t len;
@@ -70,17 +65,17 @@ static int process_script ( int ( * process_line ) ( const char *line ),
70 65
 	do {
71 66
 	
72 67
 		/* Find length of next line, excluding any terminating '\n' */
73
-		eol = memchr_user ( script->data, script_offset, '\n',
74
-				    ( script->len - script_offset ) );
68
+		eol = memchr_user ( image->data, script_offset, '\n',
69
+				    ( image->len - script_offset ) );
75 70
 		if ( eol < 0 )
76
-			eol = script->len;
71
+			eol = image->len;
77 72
 		len = ( eol - script_offset );
78 73
 
79 74
 		/* Copy line, terminate with NUL, and execute command */
80 75
 		{
81 76
 			char cmdbuf[ len + 1 ];
82 77
 
83
-			copy_from_user ( cmdbuf, script->data,
78
+			copy_from_user ( cmdbuf, image->data,
84 79
 					 script_offset, len );
85 80
 			cmdbuf[len] = '\0';
86 81
 			DBG ( "$ %s\n", cmdbuf );
@@ -94,7 +89,7 @@ static int process_script ( int ( * process_line ) ( const char *line ),
94 89
 				return rc;
95 90
 		}
96 91
 
97
-	} while ( script_offset < script->len );
92
+	} while ( script_offset < image->len );
98 93
 
99 94
 	return rc;
100 95
 }
@@ -138,7 +133,6 @@ static int script_exec_line ( const char *line ) {
138 133
  * @ret rc		Return status code
139 134
  */
140 135
 static int script_exec ( struct image *image ) {
141
-	struct image *saved_script;
142 136
 	size_t saved_offset;
143 137
 	int rc;
144 138
 
@@ -148,18 +142,14 @@ static int script_exec ( struct image *image ) {
148 142
 	unregister_image ( image );
149 143
 
150 144
 	/* Preserve state of any currently-running script */
151
-	saved_script = script;
152 145
 	saved_offset = script_offset;
153 146
 
154
-	/* Initialise state for this script */
155
-	script = image;
156
-
157 147
 	/* Process script */
158
-	rc = process_script ( script_exec_line, terminate_on_exit_or_failure );
148
+	rc = process_script ( image, script_exec_line,
149
+			      terminate_on_exit_or_failure );
159 150
 
160 151
 	/* Restore saved state, re-register image, and return */
161 152
 	script_offset = saved_offset;
162
-	script = saved_script;
163 153
 	register_image ( image );
164 154
 	return rc;
165 155
 }
@@ -262,7 +252,7 @@ static int goto_exec ( int argc, char **argv ) {
262 252
 		return rc;
263 253
 
264 254
 	/* Sanity check */
265
-	if ( ! script ) {
255
+	if ( ! current_image ) {
266 256
 		rc = -ENOTTY;
267 257
 		printf ( "Not in a script: %s\n", strerror ( rc ) );
268 258
 		return rc;
@@ -273,7 +263,7 @@ static int goto_exec ( int argc, char **argv ) {
273 263
 
274 264
 	/* Find label */
275 265
 	saved_offset = script_offset;
276
-	if ( ( rc = process_script ( goto_find_label,
266
+	if ( ( rc = process_script ( current_image, goto_find_label,
277 267
 				     terminate_on_label_found ) ) != 0 ) {
278 268
 		script_offset = saved_offset;
279 269
 		return rc;

+ 1
- 0
src/include/ipxe/image.h 파일 보기

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

Loading…
취소
저장