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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
31 #include <ipxe/settings.h>
32 #include <ipxe/command.h>
33 #include <ipxe/parseopt.h>
34 #include <readline/readline.h>
36 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
40 * Non-volatile option commands
45 struct show_options {};
47 /** "show" option list */
48 static struct option_descriptor show_opts[] = {};
50 /** "show" command descriptor */
51 static struct command_descriptor show_cmd =
52 COMMAND_DESC ( struct show_options, show_opts, 1, 1, "<setting>" );
57 * @v argc Argument count
58 * @v argv Argument list
59 * @ret rc Return status code
61 static int show_exec ( int argc, char **argv ) {
62 struct show_options opts;
63 struct named_setting setting;
64 struct settings *origin;
65 struct setting fetched;
72 if ( ( rc = parse_options ( argc, argv, &show_cmd, &opts ) ) != 0 )
73 goto err_parse_options;
75 /* Parse setting name */
76 if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 )
77 goto err_parse_setting;
79 /* Fetch formatted setting value */
80 if ( ( len = fetchf_setting_copy ( setting.settings, &setting.setting,
81 &origin, &fetched, &value ) ) < 0 ) {
83 printf ( "Could not find \"%s\": %s\n",
84 setting.setting.name, strerror ( rc ) );
88 /* Print setting value */
89 setting_name ( origin, &fetched, name_buf, sizeof ( name_buf ) );
90 printf ( "%s = %s\n", name_buf, value );
102 /** "set", "clear", and "read" options */
103 struct set_core_options {};
105 /** "set", "clear", and "read" option list */
106 static struct option_descriptor set_core_opts[] = {};
108 /** "set" command descriptor */
109 static struct command_descriptor set_cmd =
110 COMMAND_DESC ( struct set_core_options, set_core_opts, 1, MAX_ARGUMENTS,
111 "<setting> <value>" );
113 /** "clear" and "read" command descriptor */
114 static struct command_descriptor clear_read_cmd =
115 COMMAND_DESC ( struct set_core_options, set_core_opts, 1, 1,
119 * "set", "clear", and "read" command
121 * @v argc Argument count
122 * @v argv Argument list
123 * @v cmd Command descriptor
124 * @v get_value Method to obtain setting value
125 * @ret rc Return status code
127 static int set_core_exec ( int argc, char **argv,
128 struct command_descriptor *cmd,
129 int ( * get_value ) ( struct named_setting *setting,
130 char **args, char **value ) ) {
131 struct set_core_options opts;
132 struct named_setting setting;
137 if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
138 goto err_parse_options;
140 /* Parse setting name */
141 if ( ( rc = parse_autovivified_setting ( argv[optind],
143 goto err_parse_setting;
145 /* Parse setting value */
146 if ( ( rc = get_value ( &setting, &argv[ optind + 1 ], &value ) ) != 0 )
149 /* Apply default type if necessary */
150 if ( ! setting.setting.type )
151 setting.setting.type = &setting_type_string;
154 if ( ( rc = storef_setting ( setting.settings, &setting.setting,
156 printf ( "Could not store \"%s\": %s\n",
157 setting.setting.name, strerror ( rc ) );
170 * Get setting value for "set" command
172 * @v setting Named setting
173 * @v args Remaining arguments
174 * @ret value Setting value
175 * @ret rc Return status code
177 static int set_value ( struct named_setting *setting __unused,
178 char **args, char **value ) {
180 *value = concat_args ( args );
190 * @v argc Argument count
191 * @v argv Argument list
192 * @ret rc Return status code
194 static int set_exec ( int argc, char **argv ) {
195 return set_core_exec ( argc, argv, &set_cmd, set_value );
199 * Get setting value for "clear" command
201 * @v setting Named setting
202 * @v args Remaining arguments
203 * @ret value Setting value
204 * @ret rc Return status code
206 static int clear_value ( struct named_setting *setting __unused,
207 char **args __unused, char **value ) {
216 * @v argc Argument count
217 * @v argv Argument list
218 * @ret rc Return status code
220 static int clear_exec ( int argc, char **argv ) {
221 return set_core_exec ( argc, argv, &clear_read_cmd, clear_value );
225 * Get setting value for "read" command
227 * @v setting Named setting
228 * @v args Remaining arguments
229 * @ret value Setting value
230 * @ret rc Return status code
232 static int read_value ( struct named_setting *setting, char **args __unused,
237 /* Read existing value, treating errors as equivalent to an
238 * empty initial setting.
240 fetchf_setting_copy ( setting->settings, &setting->setting,
241 NULL, &setting->setting, &existing );
244 if ( ( rc = readline_history ( NULL, existing, NULL, value ) ) != 0 )
255 * @v argc Argument count
256 * @v argv Argument list
257 * @ret rc Return status code
259 static int read_exec ( int argc, char **argv ) {
260 return set_core_exec ( argc, argv, &clear_read_cmd, read_value );
264 struct inc_options {};
266 /** "inc" option list */
267 static struct option_descriptor inc_opts[] = {};
269 /** "inc" command descriptor */
270 static struct command_descriptor inc_cmd =
271 COMMAND_DESC ( struct inc_options, inc_opts, 1, 2,
272 "<setting> [<increment>]" );
277 * @v argc Argument count
278 * @v argv Argument list
279 * @ret rc Return status code
281 static int inc_exec ( int argc, char **argv ) {
282 struct inc_options opts;
283 struct named_setting setting;
284 unsigned int increment = 1;
289 if ( ( rc = parse_options ( argc, argv, &inc_cmd, &opts ) ) != 0 )
290 goto err_parse_options;
292 /* Parse setting name */
293 if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 )
294 goto err_parse_setting;
296 /* Parse increment (if present) */
297 if ( ( ( optind + 1 ) < argc ) &&
298 ( ( rc = parse_integer ( argv[ optind + 1 ], &increment ) ) != 0))
299 goto err_parse_increment;
301 /* Read existing value, treating errors as equivalent to a
302 * zero-valued :int32 initial setting.
304 if ( ( rc = fetchn_setting ( setting.settings, &setting.setting,
305 NULL, &setting.setting, &value ) ) != 0 ) {
307 if ( ! setting.setting.type )
308 setting.setting.type = &setting_type_int32;
311 /* Increment value */
314 /* Store updated setting value */
315 if ( ( rc = storen_setting ( setting.settings, &setting.setting,
317 printf ( "Could not store \"%s\": %s\n",
318 setting.setting.name, strerror ( rc ) );
329 /** Non-volatile option commands */
330 struct command nvo_commands[] __command = {