Pārlūkot izejas kodu

[parseopt] Add parse_setting()

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 11 gadus atpakaļ
vecāks
revīzija
1625a8c05f
2 mainītis faili ar 70 papildinājumiem un 1 dzēšanām
  1. 55
    0
      src/core/parseopt.c
  2. 15
    1
      src/include/ipxe/parseopt.h

+ 55
- 0
src/core/parseopt.c Parādīt failu

@@ -195,6 +195,61 @@ int parse_settings ( char *text, struct settings **value ) {
195 195
 	return 0;
196 196
 }
197 197
 
198
+/**
199
+ * Parse setting name
200
+ *
201
+ * @v text		Text
202
+ * @v setting		Named setting to fill in
203
+ * @v get_child		Function to find or create child settings block
204
+ * @ret rc		Return status code
205
+ *
206
+ * Note that this function modifies the original @c text.
207
+ */
208
+int parse_setting ( char *text, struct named_setting *setting,
209
+		    get_child_settings_t get_child ) {
210
+	int rc;
211
+
212
+	/* Sanity check */
213
+	assert ( text != NULL );
214
+
215
+	/* Parse setting name */
216
+	if ( ( rc = parse_setting_name ( text, get_child, &setting->settings,
217
+					 &setting->setting ) ) != 0 ) {
218
+		printf ( "\"%s\": invalid setting\n", text );
219
+		return rc;
220
+	}
221
+
222
+	return 0;
223
+}
224
+
225
+/**
226
+ * Parse existing setting name
227
+ *
228
+ * @v text		Text
229
+ * @v setting		Named setting to fill in
230
+ * @ret rc		Return status code
231
+ *
232
+ * Note that this function modifies the original @c text.
233
+ */
234
+int parse_existing_setting ( char *text, struct named_setting *setting ) {
235
+
236
+	return parse_setting ( text, setting, find_child_settings );
237
+}
238
+
239
+/**
240
+ * Parse and autovivify setting name
241
+ *
242
+ * @v text		Text
243
+ * @v setting		Named setting to fill in
244
+ * @ret rc		Return status code
245
+ *
246
+ * Note that this function modifies the original @c text.
247
+ */
248
+int parse_autovivified_setting ( char *text, struct named_setting *setting ) {
249
+
250
+	return parse_setting ( text, setting, autovivify_child_settings );
251
+}
252
+
198 253
 /**
199 254
  * Print command usage message
200 255
  *

+ 15
- 1
src/include/ipxe/parseopt.h Parādīt failu

@@ -11,10 +11,10 @@ FILE_LICENCE ( GPL2_OR_LATER );
11 11
 
12 12
 #include <stdint.h>
13 13
 #include <stddef.h>
14
+#include <ipxe/settings.h>
14 15
 
15 16
 struct net_device;
16 17
 struct menu;
17
-struct settings;
18 18
 
19 19
 /** A command-line option descriptor */
20 20
 struct option_descriptor {
@@ -115,6 +115,14 @@ struct command_descriptor {
115 115
 		.usage = _usage,					      \
116 116
 	 }
117 117
 
118
+/** A parsed named setting */
119
+struct named_setting {
120
+	/** Settings block */
121
+	struct settings *settings;
122
+	/** Setting */
123
+	struct setting setting;
124
+};
125
+
118 126
 extern int parse_string ( char *text, char **value );
119 127
 extern int parse_integer ( char *text, unsigned int *value );
120 128
 extern int parse_netdev ( char *text, struct net_device **netdev );
@@ -122,6 +130,12 @@ extern int parse_menu ( char *text, struct menu **menu );
122 130
 extern int parse_flag ( char *text __unused, int *flag );
123 131
 extern int parse_key ( char *text, unsigned int *key );
124 132
 extern int parse_settings ( char *text, struct settings **settings );
133
+extern int parse_setting ( char *text, struct named_setting *setting,
134
+			   get_child_settings_t get_child );
135
+extern int parse_existing_setting ( char *text, struct named_setting *setting );
136
+extern int parse_autovivified_setting ( char *text,
137
+					struct named_setting *setting );
138
+extern void print_usage ( struct command_descriptor *cmd, char **argv );
125 139
 extern int reparse_options ( int argc, char **argv,
126 140
 			     struct command_descriptor *cmd, void *opts );
127 141
 extern int parse_options ( int argc, char **argv,

Notiek ielāde…
Atcelt
Saglabāt