|
@@ -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;
|