2 * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include <ipxe/settings.h>
28 #include <ipxe/command.h>
29 #include <ipxe/parseopt.h>
30 #include <readline/readline.h>
32 FILE_LICENCE ( GPL2_OR_LATER );
36 * Non-volatile option commands
41 struct show_options {};
43 /** "show" option list */
44 static struct option_descriptor show_opts[] = {};
46 /** "show" command descriptor */
47 static struct command_descriptor show_cmd =
48 COMMAND_DESC ( struct show_options, show_opts, 1, 1, "<setting>" );
53 * @v argc Argument count
54 * @v argv Argument list
55 * @ret rc Return status code
57 static int show_exec ( int argc, char **argv ) {
58 struct show_options opts;
59 struct named_setting setting;
60 struct settings *origin;
61 struct setting fetched;
68 if ( ( rc = parse_options ( argc, argv, &show_cmd, &opts ) ) != 0 )
69 goto err_parse_options;
71 /* Parse setting name */
72 if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 )
73 goto err_parse_setting;
75 /* Fetch formatted setting value */
76 if ( ( len = fetchf_setting_copy ( setting.settings, &setting.setting,
77 &origin, &fetched, &value ) ) < 0 ) {
79 printf ( "Could not find \"%s\": %s\n",
80 setting.setting.name, strerror ( rc ) );
84 /* Print setting value */
85 setting_name ( origin, &fetched, name_buf, sizeof ( name_buf ) );
86 printf ( "%s = %s\n", name_buf, value );
98 /** "set", "clear", and "read" options */
99 struct set_core_options {};
101 /** "set", "clear", and "read" option list */
102 static struct option_descriptor set_core_opts[] = {};
104 /** "set" command descriptor */
105 static struct command_descriptor set_cmd =
106 COMMAND_DESC ( struct set_core_options, set_core_opts, 1, MAX_ARGUMENTS,
107 "<setting> <value>" );
109 /** "clear" and "read" command descriptor */
110 static struct command_descriptor clear_read_cmd =
111 COMMAND_DESC ( struct set_core_options, set_core_opts, 1, 1,
115 * "set", "clear", and "read" command
117 * @v argc Argument count
118 * @v argv Argument list
119 * @v cmd Command descriptor
120 * @v get_value Method to obtain setting value
121 * @ret rc Return status code
123 static int set_core_exec ( int argc, char **argv,
124 struct command_descriptor *cmd,
125 int ( * get_value ) ( struct named_setting *setting,
126 char **args, char **value ) ) {
127 struct set_core_options opts;
128 struct named_setting setting;
133 if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
134 goto err_parse_options;
136 /* Parse setting name */
137 if ( ( rc = parse_autovivified_setting ( argv[optind],
139 goto err_parse_setting;
141 /* Parse setting value */
142 if ( ( rc = get_value ( &setting, &argv[ optind + 1 ], &value ) ) != 0 )
145 /* Apply default type if necessary */
146 if ( ! setting.setting.type )
147 setting.setting.type = &setting_type_string;
150 if ( ( rc = storef_setting ( setting.settings, &setting.setting,
152 printf ( "Could not store \"%s\": %s\n",
153 setting.setting.name, strerror ( rc ) );
166 * Get setting value for "set" command
168 * @v setting Named setting
169 * @v args Remaining arguments
170 * @ret value Setting value
171 * @ret rc Return status code
173 static int set_value ( struct named_setting *setting __unused,
174 char **args, char **value ) {
176 *value = concat_args ( args );
186 * @v argc Argument count
187 * @v argv Argument list
188 * @ret rc Return status code
190 static int set_exec ( int argc, char **argv ) {
191 return set_core_exec ( argc, argv, &set_cmd, set_value );
195 * Get setting value for "clear" command
197 * @v setting Named setting
198 * @v args Remaining arguments
199 * @ret value Setting value
200 * @ret rc Return status code
202 static int clear_value ( struct named_setting *setting __unused,
203 char **args __unused, char **value ) {
212 * @v argc Argument count
213 * @v argv Argument list
214 * @ret rc Return status code
216 static int clear_exec ( int argc, char **argv ) {
217 return set_core_exec ( argc, argv, &clear_read_cmd, clear_value );
221 * Get setting value for "read" command
223 * @v setting Named setting
224 * @v args Remaining arguments
225 * @ret value Setting value
226 * @ret rc Return status code
228 static int read_value ( struct named_setting *setting, char **args __unused,
233 /* Read existing value, treating errors as equivalent to an
234 * empty initial setting.
236 fetchf_setting_copy ( setting->settings, &setting->setting,
237 NULL, &setting->setting, &existing );
240 if ( ( rc = readline_history ( NULL, existing, NULL, value ) ) != 0 )
251 * @v argc Argument count
252 * @v argv Argument list
253 * @ret rc Return status code
255 static int read_exec ( int argc, char **argv ) {
256 return set_core_exec ( argc, argv, &clear_read_cmd, read_value );
260 struct inc_options {};
262 /** "inc" option list */
263 static struct option_descriptor inc_opts[] = {};
265 /** "inc" command descriptor */
266 static struct command_descriptor inc_cmd =
267 COMMAND_DESC ( struct inc_options, inc_opts, 1, 2,
268 "<setting> [<increment>]" );
273 * @v argc Argument count
274 * @v argv Argument list
275 * @ret rc Return status code
277 static int inc_exec ( int argc, char **argv ) {
278 struct inc_options opts;
279 struct named_setting setting;
280 unsigned int increment = 1;
285 if ( ( rc = parse_options ( argc, argv, &inc_cmd, &opts ) ) != 0 )
286 goto err_parse_options;
288 /* Parse setting name */
289 if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 )
290 goto err_parse_setting;
292 /* Parse increment (if present) */
293 if ( ( ( optind + 1 ) < argc ) &&
294 ( ( rc = parse_integer ( argv[ optind + 1 ], &increment ) ) != 0))
295 goto err_parse_increment;
297 /* Read existing value, treating errors as equivalent to a
298 * zero-valued :int32 initial setting.
300 if ( ( rc = fetchn_setting ( setting.settings, &setting.setting,
301 NULL, &setting.setting, &value ) ) != 0 ) {
303 if ( ! setting.setting.type )
304 setting.setting.type = &setting_type_int32;
307 /* Increment value */
310 /* Store updated setting value */
311 if ( ( rc = storen_setting ( setting.settings, &setting.setting,
313 printf ( "Could not store \"%s\": %s\n",
314 setting.setting.name, strerror ( rc ) );
325 /** Non-volatile option commands */
326 struct command nvo_commands[] __command = {