Browse Source

[script] Allow initial whitespace on lines containing labels

Initial whitespace is already accepted on lines containing commands,
since it gets ignored by the system() call.  Minimise surprise and
allow for neater indentation of scripts by also allowing whitespace on
lines containing labels.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 11 years ago
parent
commit
7fc18ea8ab
1 changed files with 29 additions and 4 deletions
  1. 29
    4
      src/image/script.c

+ 29
- 4
src/image/script.c View File

136
 		 ( rc != 0 ) );
136
 		 ( rc != 0 ) );
137
 }
137
 }
138
 
138
 
139
+/**
140
+ * Find label within script line
141
+ *
142
+ * @v line		Line of script
143
+ * @ret label		Start of label name, or NULL if not found
144
+ */
145
+static const char * find_label ( const char *line ) {
146
+
147
+	/* Skip any leading whitespace */
148
+	while ( isspace ( *line ) )
149
+		line++;
150
+
151
+	/* If first non-whitespace character is a ':', then we have a label */
152
+	if ( *line == ':' ) {
153
+		return ( line + 1 );
154
+	} else {
155
+		return NULL;
156
+	}
157
+}
158
+
139
 /**
159
 /**
140
  * Execute script line
160
  * Execute script line
141
  *
161
  *
146
 	int rc;
166
 	int rc;
147
 
167
 
148
 	/* Skip label lines */
168
 	/* Skip label lines */
149
-	if ( line[0] == ':' )
169
+	if ( find_label ( line ) != NULL )
150
 		return 0;
170
 		return 0;
151
 
171
 
152
 	/* Execute command */
172
 	/* Execute command */
252
  */
272
  */
253
 static int goto_find_label ( const char *line ) {
273
 static int goto_find_label ( const char *line ) {
254
 	size_t len = strlen ( goto_label );
274
 	size_t len = strlen ( goto_label );
275
+	const char *label;
255
 
276
 
256
-	if ( line[0] != ':' )
277
+	/* Find label */
278
+	label = find_label ( line );
279
+	if ( ! label )
257
 		return -ENOENT;
280
 		return -ENOENT;
258
 
281
 
259
-	if ( strncmp ( goto_label, &line[1], len ) != 0 )
282
+	/* Check if label matches */
283
+	if ( strncmp ( goto_label, label, len ) != 0 )
260
 		return -ENOENT;
284
 		return -ENOENT;
261
 
285
 
262
-	if ( line[ 1 + len ] && ! isspace ( line[ 1 + len ] ) )
286
+	/* Check label is terminated by a NUL or whitespace */
287
+	if ( label[len] && ! isspace ( label[len] ) )
263
 		return -ENOENT;
288
 		return -ENOENT;
264
 
289
 
265
 	return 0;
290
 	return 0;

Loading…
Cancel
Save