Bläddra i källkod

[settings] Generalise expand_command() to expand_settings()

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 14 år sedan
förälder
incheckning
990cbb8f2c
3 ändrade filer med 83 tillägg och 75 borttagningar
  1. 1
    75
      src/core/exec.c
  2. 81
    0
      src/core/settings.c
  3. 1
    0
      src/include/ipxe/settings.h

+ 1
- 75
src/core/exec.c Visa fil

@@ -86,80 +86,6 @@ int execv ( const char *command, char * const argv[] ) {
86 86
 	return -ENOEXEC;
87 87
 }
88 88
 
89
-/**
90
- * Expand variables within command line
91
- *
92
- * @v command		Command line
93
- * @ret expcmd		Expanded command line
94
- *
95
- * The expanded command line is allocated with malloc() and the caller
96
- * must eventually free() it.
97
- */
98
-static char * expand_command ( const char *command ) {
99
-	char *expcmd;
100
-	char *start;
101
-	char *end;
102
-	char *head;
103
-	char *name;
104
-	char *tail;
105
-	int setting_len;
106
-	int new_len;
107
-	char *tmp;
108
-
109
-	/* Obtain temporary modifiable copy of command line */
110
-	expcmd = strdup ( command );
111
-	if ( ! expcmd )
112
-		return NULL;
113
-
114
-	/* Expand while expansions remain */
115
-	while ( 1 ) {
116
-
117
-		head = expcmd;
118
-
119
-		/* Locate setting to be expanded */
120
-		start = NULL;
121
-		end = NULL;
122
-		for ( tmp = expcmd ; *tmp ; tmp++ ) {
123
-			if ( ( tmp[0] == '$' ) && ( tmp[1] == '{' ) )
124
-				start = tmp;
125
-			if ( start && ( tmp[0] == '}' ) ) {
126
-				end = tmp;
127
-				break;
128
-			}
129
-		}
130
-		if ( ! end )
131
-			break;
132
-		*start = '\0';
133
-		name = ( start + 2 );
134
-		*end = '\0';
135
-		tail = ( end + 1 );
136
-
137
-		/* Determine setting length */
138
-		setting_len = fetchf_named_setting ( name, NULL, 0 );
139
-		if ( setting_len < 0 )
140
-			setting_len = 0; /* Treat error as empty setting */
141
-
142
-		/* Read setting into temporary buffer */
143
-		{
144
-			char setting_buf[ setting_len + 1 ];
145
-
146
-			setting_buf[0] = '\0';
147
-			fetchf_named_setting ( name, setting_buf,
148
-					       sizeof ( setting_buf ) );
149
-
150
-			/* Construct expanded string and discard old string */
151
-			tmp = expcmd;
152
-			new_len = asprintf ( &expcmd, "%s%s%s",
153
-					     head, setting_buf, tail );
154
-			free ( tmp );
155
-			if ( new_len < 0 )
156
-				return NULL;
157
-		}
158
-	}
159
-
160
-	return expcmd;
161
-}
162
-
163 89
 /**
164 90
  * Split command line into tokens
165 91
  *
@@ -294,7 +220,7 @@ int system ( const char *command ) {
294 220
 	int rc = 0;
295 221
 
296 222
 	/* Perform variable expansion */
297
-	expcmd = expand_command ( command );
223
+	expcmd = expand_settings ( command );
298 224
 	if ( ! expcmd )
299 225
 		return -ENOMEM;
300 226
 

+ 81
- 0
src/core/settings.c Visa fil

@@ -1467,6 +1467,87 @@ struct setting_type setting_type_uuid __setting_type = {
1467 1467
 	.fetchf = fetchf_uuid,
1468 1468
 };
1469 1469
 
1470
+/******************************************************************************
1471
+ *
1472
+ * Setting expansion
1473
+ *
1474
+ ******************************************************************************
1475
+ */
1476
+
1477
+/**
1478
+ * Expand variables within string
1479
+ *
1480
+ * @v string		String
1481
+ * @ret expstr		Expanded string
1482
+ *
1483
+ * The expanded string is allocated with malloc() and the caller must
1484
+ * eventually free() it.
1485
+ */
1486
+char * expand_settings ( const char *string ) {
1487
+	char *expstr;
1488
+	char *start;
1489
+	char *end;
1490
+	char *head;
1491
+	char *name;
1492
+	char *tail;
1493
+	int setting_len;
1494
+	int new_len;
1495
+	char *tmp;
1496
+
1497
+	/* Obtain temporary modifiable copy of string */
1498
+	expstr = strdup ( string );
1499
+	if ( ! expstr )
1500
+		return NULL;
1501
+
1502
+	/* Expand while expansions remain */
1503
+	while ( 1 ) {
1504
+
1505
+		head = expstr;
1506
+
1507
+		/* Locate setting to be expanded */
1508
+		start = NULL;
1509
+		end = NULL;
1510
+		for ( tmp = expstr ; *tmp ; tmp++ ) {
1511
+			if ( ( tmp[0] == '$' ) && ( tmp[1] == '{' ) )
1512
+				start = tmp;
1513
+			if ( start && ( tmp[0] == '}' ) ) {
1514
+				end = tmp;
1515
+				break;
1516
+			}
1517
+		}
1518
+		if ( ! end )
1519
+			break;
1520
+		*start = '\0';
1521
+		name = ( start + 2 );
1522
+		*end = '\0';
1523
+		tail = ( end + 1 );
1524
+
1525
+		/* Determine setting length */
1526
+		setting_len = fetchf_named_setting ( name, NULL, 0 );
1527
+		if ( setting_len < 0 )
1528
+			setting_len = 0; /* Treat error as empty setting */
1529
+
1530
+		/* Read setting into temporary buffer */
1531
+		{
1532
+			char setting_buf[ setting_len + 1 ];
1533
+
1534
+			setting_buf[0] = '\0';
1535
+			fetchf_named_setting ( name, setting_buf,
1536
+					       sizeof ( setting_buf ) );
1537
+
1538
+			/* Construct expanded string and discard old string */
1539
+			tmp = expstr;
1540
+			new_len = asprintf ( &expstr, "%s%s%s",
1541
+					     head, setting_buf, tail );
1542
+			free ( tmp );
1543
+			if ( new_len < 0 )
1544
+				return NULL;
1545
+		}
1546
+	}
1547
+
1548
+	return expstr;
1549
+}
1550
+
1470 1551
 /******************************************************************************
1471 1552
  *
1472 1553
  * Settings

+ 1
- 0
src/include/ipxe/settings.h Visa fil

@@ -221,6 +221,7 @@ extern int storef_setting ( struct settings *settings,
221 221
 			    const char *value );
222 222
 extern int storef_named_setting ( const char *name, const char *value );
223 223
 extern int fetchf_named_setting ( const char *name, char *buf, size_t len );
224
+extern char * expand_settings ( const char *string );
224 225
 
225 226
 extern struct setting_type setting_type_string __setting_type;
226 227
 extern struct setting_type setting_type_ipv4 __setting_type;

Laddar…
Avbryt
Spara