Browse Source

[settings] Add "read" command

The "read" command allows a script to prompt a user to enter a
setting.  For example:

  echo -n Static IP address:
  read net0/ip

Total cost: 17 bytes.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 13 years ago
parent
commit
1cc991e132
1 changed files with 88 additions and 39 deletions
  1. 88
    39
      src/hci/commands/nvo_cmd.c

+ 88
- 39
src/hci/commands/nvo_cmd.c View File

25
 #include <ipxe/settings.h>
25
 #include <ipxe/settings.h>
26
 #include <ipxe/command.h>
26
 #include <ipxe/command.h>
27
 #include <ipxe/parseopt.h>
27
 #include <ipxe/parseopt.h>
28
+#include <readline/readline.h>
28
 
29
 
29
 FILE_LICENCE ( GPL2_OR_LATER );
30
 FILE_LICENCE ( GPL2_OR_LATER );
30
 
31
 
80
 	return 0;
81
 	return 0;
81
 }
82
 }
82
 
83
 
83
-/** "set" options */
84
-struct set_options {};
84
+/** "set", "clear", and "read" options */
85
+struct set_core_options {};
85
 
86
 
86
-/** "set" option list */
87
-static struct option_descriptor set_opts[] = {};
87
+/** "set", "clear", and "read" option list */
88
+static struct option_descriptor set_core_opts[] = {};
88
 
89
 
89
 /** "set" command descriptor */
90
 /** "set" command descriptor */
90
 static struct command_descriptor set_cmd =
91
 static struct command_descriptor set_cmd =
91
-	COMMAND_DESC ( struct set_options, set_opts, 1, MAX_ARGUMENTS,
92
+	COMMAND_DESC ( struct set_core_options, set_core_opts, 1, MAX_ARGUMENTS,
92
 		       "<setting> <value>" );
93
 		       "<setting> <value>" );
93
 
94
 
95
+/** "clear" and "read" command descriptor */
96
+static struct command_descriptor clear_read_cmd =
97
+	COMMAND_DESC ( struct set_core_options, set_core_opts, 1, 1,
98
+		       "<setting>" );
99
+
94
 /**
100
 /**
95
- * "set" command
101
+ * "set", "clear", and "read" command
96
  *
102
  *
97
  * @v argc		Argument count
103
  * @v argc		Argument count
98
  * @v argv		Argument list
104
  * @v argv		Argument list
105
+ * @v cmd		Command descriptor
106
+ * @v get_value		Method to obtain setting value
99
  * @ret rc		Return status code
107
  * @ret rc		Return status code
100
  */
108
  */
101
-static int set_exec ( int argc, char **argv ) {
102
-	struct set_options opts;
109
+static int set_core_exec ( int argc, char **argv,
110
+			   struct command_descriptor *cmd,
111
+			   int ( * get_value ) ( char **args, char **value ) ) {
112
+	struct set_core_options opts;
103
 	const char *name;
113
 	const char *name;
104
 	char *value;
114
 	char *value;
105
 	int rc;
115
 	int rc;
106
 
116
 
107
 	/* Parse options */
117
 	/* Parse options */
108
-	if ( ( rc = parse_options ( argc, argv, &set_cmd, &opts ) ) != 0 )
118
+	if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
109
 		goto err_parse_options;
119
 		goto err_parse_options;
110
 
120
 
111
 	/* Parse setting name */
121
 	/* Parse setting name */
112
 	name = argv[optind];
122
 	name = argv[optind];
113
 
123
 
114
 	/* Parse setting value */
124
 	/* Parse setting value */
115
-	value = concat_args ( &argv[ optind + 1 ] );
116
-	if ( ! value ) {
117
-		rc = -ENOMEM;
118
-		goto err_concat_args;
119
-	}
125
+	if ( ( rc = get_value ( &argv[ optind + 1 ], &value ) ) != 0 )
126
+		goto err_get_value;
120
 
127
 
121
 	/* Determine total length of command line */
128
 	/* Determine total length of command line */
122
 	if ( ( rc = storef_named_setting ( name, value ) ) != 0 ) {
129
 	if ( ( rc = storef_named_setting ( name, value ) ) != 0 ) {
123
-		printf ( "Could not set \"%s\"=\"%s\": %s\n",
124
-			 name, value, strerror ( rc ) );
130
+		printf ( "Could not %s \"%s\": %s\n",
131
+			 argv[0], name, strerror ( rc ) );
125
 		goto err_store;
132
 		goto err_store;
126
 	}
133
 	}
127
 
134
 
130
 
137
 
131
  err_store:
138
  err_store:
132
 	free ( value );
139
 	free ( value );
133
- err_concat_args:
140
+ err_get_value:
134
  err_parse_options:
141
  err_parse_options:
135
 	return rc;
142
 	return rc;
136
 }
143
 }
137
 
144
 
138
-/** "clear" options */
139
-struct clear_options {};
145
+/**
146
+ * Get setting value for "set" command
147
+ *
148
+ * @v args		Remaining arguments
149
+ * @ret value		Setting value
150
+ * @ret rc		Return status code
151
+ */
152
+static int set_value ( char **args, char **value ) {
153
+
154
+	*value = concat_args ( args );
155
+	if ( ! *value )
156
+		return -ENOMEM;
157
+
158
+	return 0;
159
+}
160
+
161
+/**
162
+ * "set" command
163
+ *
164
+ * @v argc		Argument count
165
+ * @v argv		Argument list
166
+ * @ret rc		Return status code
167
+ */
168
+static int set_exec ( int argc, char **argv ) {
169
+	return set_core_exec ( argc, argv, &set_cmd, set_value );
170
+}
140
 
171
 
141
-/** "clear" option list */
142
-static struct option_descriptor clear_opts[] = {};
172
+/**
173
+ * Get setting value for "clear" command
174
+ *
175
+ * @v args		Remaining arguments
176
+ * @ret value		Setting value
177
+ * @ret rc		Return status code
178
+ */
179
+static int clear_value ( char **args __unused, char **value ) {
143
 
180
 
144
-/** "clear" command descriptor */
145
-static struct command_descriptor clear_cmd =
146
-	COMMAND_DESC ( struct clear_options, clear_opts, 1, 1, "<setting>" );
181
+	*value = NULL;
182
+	return 0;
183
+}
147
 
184
 
148
 /**
185
 /**
149
  * "clear" command
186
  * "clear" command
153
  * @ret rc		Return status code
190
  * @ret rc		Return status code
154
  */
191
  */
155
 static int clear_exec ( int argc, char **argv ) {
192
 static int clear_exec ( int argc, char **argv ) {
156
-	struct clear_options opts;
157
-	const char *name;
158
-	int rc;
193
+	return set_core_exec ( argc, argv, &clear_read_cmd, clear_value );
194
+}
159
 
195
 
160
-	/* Parse options */
161
-	if ( ( rc = parse_options ( argc, argv, &clear_cmd, &opts ) ) != 0 )
162
-		return rc;
196
+/**
197
+ * Get setting value for "read" command
198
+ *
199
+ * @ret value		Setting value
200
+ * @ret rc		Return status code
201
+ */
202
+static int read_value ( char **args __unused, char **value ) {
163
 
203
 
164
-	/* Parse setting name */
165
-	name = argv[optind];
204
+	*value = readline ( NULL );
205
+	if ( ! *value )
206
+		return -ENOMEM;
166
 
207
 
167
-	/* Clear setting */
168
-	if ( ( rc = delete_named_setting ( name ) ) != 0 ) {
169
-		printf ( "Could not clear \"%s\": %s\n",
170
-			 name, strerror ( rc ) );
171
-		return rc;
172
-	}
173
-	
174
 	return 0;
208
 	return 0;
175
 }
209
 }
176
 
210
 
211
+/**
212
+ * "read" command
213
+ *
214
+ * @v argc		Argument count
215
+ * @v argv		Argument list
216
+ * @ret rc		Return status code
217
+ */
218
+static int read_exec ( int argc, char **argv ) {
219
+	return set_core_exec ( argc, argv, &clear_read_cmd, read_value );
220
+}
221
+
177
 /** Non-volatile option commands */
222
 /** Non-volatile option commands */
178
 struct command nvo_commands[] __command = {
223
 struct command nvo_commands[] __command = {
179
 	{
224
 	{
188
 		.name = "clear",
233
 		.name = "clear",
189
 		.exec = clear_exec,
234
 		.exec = clear_exec,
190
 	},
235
 	},
236
+	{
237
+		.name = "read",
238
+		.exec = read_exec,
239
+	},
191
 };
240
 };

Loading…
Cancel
Save