ソースを参照

[cmdline] Add setting expansion using ${...} syntax

Allow settings to be expanded in arbitrary commands, such as

  kernel http://10.0.0.1/boot.php?uuid=${uuid}

Also add the "echo" command, as being the easiest way to test this
features.
tags/v0.9.4
Michael Brown 16年前
コミット
34a06b6ba7
1個のファイルの変更96行の追加2行の削除
  1. 96
    2
      src/core/exec.c

+ 96
- 2
src/core/exec.c ファイルの表示

@@ -26,6 +26,7 @@
26 26
 #include <assert.h>
27 27
 #include <gpxe/tables.h>
28 28
 #include <gpxe/command.h>
29
+#include <gpxe/settings.h>
29 30
 
30 31
 /** @file
31 32
  *
@@ -86,6 +87,76 @@ int execv ( const char *command, char * const argv[] ) {
86 87
 	return -ENOEXEC;
87 88
 }
88 89
 
90
+/**
91
+ * Expand variables within command line
92
+ *
93
+ * @v command		Command line
94
+ * @ret expcmd		Expanded command line
95
+ *
96
+ * The expanded command line is allocated with malloc() and the caller
97
+ * must eventually free() it.
98
+ */
99
+static char * expand_command ( const char *command ) {
100
+	char *expcmd;
101
+	char *start;
102
+	char *end;
103
+	char *head;
104
+	char *name;
105
+	char *tail;
106
+	int setting_len;
107
+	int new_len;
108
+	char *tmp;
109
+
110
+	/* Obtain temporary modifiable copy of command line */
111
+	expcmd = strdup ( command );
112
+	if ( ! expcmd )
113
+		return NULL;
114
+
115
+	/* Expand while expansions remain */
116
+	while ( 1 ) {
117
+
118
+		head = expcmd;
119
+
120
+		/* Locate opener */
121
+		start = strstr ( expcmd, "${" );
122
+		if ( ! start )
123
+			break;
124
+		*start = '\0';
125
+		name = ( start + 2 );
126
+
127
+		/* Locate closer */
128
+		end = strstr ( name, "}" );
129
+		if ( ! end )
130
+			break;
131
+		*end = '\0';
132
+		tail = ( end + 1 );
133
+
134
+		/* Determine setting length */
135
+		setting_len = fetchf_named_setting ( name, NULL, 0 );
136
+		if ( setting_len < 0 )
137
+			setting_len = 0; /* Treat error as empty setting */
138
+
139
+		/* Read setting into temporary buffer */
140
+		{
141
+			char setting_buf[ setting_len + 1 ];
142
+
143
+			setting_buf[0] = '\0';
144
+			fetchf_named_setting ( name, setting_buf,
145
+					       sizeof ( setting_buf ) );
146
+
147
+			/* Construct expanded string and discard old string */
148
+			tmp = expcmd;
149
+			new_len = asprintf ( &expcmd, "%s%s%s",
150
+					     head, setting_buf, tail );
151
+			free ( tmp );
152
+			if ( new_len < 0 )
153
+				return NULL;
154
+		}
155
+	}
156
+
157
+	return expcmd;
158
+}
159
+
89 160
 /**
90 161
  * Split command line into argv array
91 162
  *
@@ -135,8 +206,8 @@ int system ( const char *command ) {
135 206
 	int argc;
136 207
 	int rc = 0;
137 208
 
138
-	/* Obtain temporary modifiable copy of command line */
139
-	args = strdup ( command );
209
+	/* Perform variable expansion */
210
+	args = expand_command ( command );
140 211
 	if ( ! args )
141 212
 		return -ENOMEM;
142 213
 
@@ -157,3 +228,26 @@ int system ( const char *command ) {
157 228
 	free ( args );
158 229
 	return rc;
159 230
 }
231
+
232
+/**
233
+ * The "echo" command
234
+ *
235
+ * @v argc		Argument count
236
+ * @v argv		Argument list
237
+ * @ret rc		Exit code
238
+ */
239
+static int echo_exec ( int argc, char **argv ) {
240
+	int i;
241
+
242
+	for ( i = 1 ; i < argc ; i++ ) {
243
+		printf ( "%s%s", ( ( i == 1 ) ? "" : " " ), argv[i] );
244
+	}
245
+	printf ( "\n" );
246
+	return 0;
247
+}
248
+
249
+/** "echo" command */
250
+struct command echo_command __command = {
251
+	.name = "echo",
252
+	.exec = echo_exec,
253
+};

読み込み中…
キャンセル
保存