iPXE
nvo_cmd.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 #include <stdint.h>
00025 #include <stdlib.h>
00026 #include <stdio.h>
00027 #include <string.h>
00028 #include <errno.h>
00029 #include <getopt.h>
00030 #include <byteswap.h>
00031 #include <ipxe/settings.h>
00032 #include <ipxe/command.h>
00033 #include <ipxe/parseopt.h>
00034 #include <readline/readline.h>
00035 
00036 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00037 
00038 /** @file
00039  *
00040  * Non-volatile option commands
00041  *
00042  */
00043 
00044 /** "show" options */
00045 struct show_options {};
00046 
00047 /** "show" option list */
00048 static struct option_descriptor show_opts[] = {};
00049 
00050 /** "show" command descriptor */
00051 static struct command_descriptor show_cmd =
00052         COMMAND_DESC ( struct show_options, show_opts, 1, 1, "<setting>" );
00053 
00054 /**
00055  * "show" command
00056  *
00057  * @v argc              Argument count
00058  * @v argv              Argument list
00059  * @ret rc              Return status code
00060  */
00061 static int show_exec ( int argc, char **argv ) {
00062         struct show_options opts;
00063         struct named_setting setting;
00064         struct settings *origin;
00065         struct setting fetched;
00066         char name_buf[32];
00067         char *value;
00068         int len;
00069         int rc;
00070 
00071         /* Parse options */
00072         if ( ( rc = parse_options ( argc, argv, &show_cmd, &opts ) ) != 0 )
00073                 goto err_parse_options;
00074 
00075         /* Parse setting name */
00076         if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 )
00077                 goto err_parse_setting;
00078 
00079         /* Fetch formatted setting value */
00080         if ( ( len = fetchf_setting_copy ( setting.settings, &setting.setting,
00081                                            &origin, &fetched, &value ) ) < 0 ) {
00082                 rc = len;
00083                 printf ( "Could not find \"%s\": %s\n",
00084                          setting.setting.name, strerror ( rc ) );
00085                 goto err_fetchf;
00086         }
00087 
00088         /* Print setting value */
00089         setting_name ( origin, &fetched, name_buf, sizeof ( name_buf ) );
00090         printf ( "%s = %s\n", name_buf, value );
00091 
00092         /* Success */
00093         rc = 0;
00094 
00095         free ( value );
00096  err_fetchf:
00097  err_parse_setting:
00098  err_parse_options:
00099         return rc;
00100 }
00101 
00102 /** "set", "clear", and "read" options */
00103 struct set_core_options {};
00104 
00105 /** "set", "clear", and "read" option list */
00106 static struct option_descriptor set_core_opts[] = {};
00107 
00108 /** "set" command descriptor */
00109 static struct command_descriptor set_cmd =
00110         COMMAND_DESC ( struct set_core_options, set_core_opts, 1, MAX_ARGUMENTS,
00111                        "<setting> <value>" );
00112 
00113 /** "clear" and "read" command descriptor */
00114 static struct command_descriptor clear_read_cmd =
00115         COMMAND_DESC ( struct set_core_options, set_core_opts, 1, 1,
00116                        "<setting>" );
00117 
00118 /**
00119  * "set", "clear", and "read" command
00120  *
00121  * @v argc              Argument count
00122  * @v argv              Argument list
00123  * @v cmd               Command descriptor
00124  * @v get_value         Method to obtain setting value
00125  * @ret rc              Return status code
00126  */
00127 static int set_core_exec ( int argc, char **argv,
00128                            struct command_descriptor *cmd,
00129                            int ( * get_value ) ( struct named_setting *setting,
00130                                                  char **args, char **value ) ) {
00131         struct set_core_options opts;
00132         struct named_setting setting;
00133         char *value;
00134         int rc;
00135 
00136         /* Parse options */
00137         if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
00138                 goto err_parse_options;
00139 
00140         /* Parse setting name */
00141         if ( ( rc = parse_autovivified_setting ( argv[optind],
00142                                                  &setting ) ) != 0 )
00143                 goto err_parse_setting;
00144 
00145         /* Parse setting value */
00146         if ( ( rc = get_value ( &setting, &argv[ optind + 1 ], &value ) ) != 0 )
00147                 goto err_get_value;
00148 
00149         /* Apply default type if necessary */
00150         if ( ! setting.setting.type )
00151                 setting.setting.type = &setting_type_string;
00152 
00153         /* Store setting */
00154         if ( ( rc = storef_setting ( setting.settings, &setting.setting,
00155                                      value ) ) != 0 ) {
00156                 printf ( "Could not store \"%s\": %s\n",
00157                          setting.setting.name, strerror ( rc ) );
00158                 goto err_store;
00159         }
00160 
00161  err_store:
00162         free ( value );
00163  err_get_value:
00164  err_parse_setting:
00165  err_parse_options:
00166         return rc;
00167 }
00168 
00169 /**
00170  * Get setting value for "set" command
00171  *
00172  * @v setting           Named setting
00173  * @v args              Remaining arguments
00174  * @ret value           Setting value
00175  * @ret rc              Return status code
00176  */
00177 static int set_value ( struct named_setting *setting __unused,
00178                        char **args, char **value ) {
00179 
00180         *value = concat_args ( args );
00181         if ( ! *value )
00182                 return -ENOMEM;
00183 
00184         return 0;
00185 }
00186 
00187 /**
00188  * "set" command
00189  *
00190  * @v argc              Argument count
00191  * @v argv              Argument list
00192  * @ret rc              Return status code
00193  */
00194 static int set_exec ( int argc, char **argv ) {
00195         return set_core_exec ( argc, argv, &set_cmd, set_value );
00196 }
00197 
00198 /**
00199  * Get setting value for "clear" command
00200  *
00201  * @v setting           Named setting
00202  * @v args              Remaining arguments
00203  * @ret value           Setting value
00204  * @ret rc              Return status code
00205  */
00206 static int clear_value ( struct named_setting *setting __unused,
00207                          char **args __unused, char **value ) {
00208 
00209         *value = NULL;
00210         return 0;
00211 }
00212 
00213 /**
00214  * "clear" command
00215  *
00216  * @v argc              Argument count
00217  * @v argv              Argument list
00218  * @ret rc              Return status code
00219  */
00220 static int clear_exec ( int argc, char **argv ) {
00221         return set_core_exec ( argc, argv, &clear_read_cmd, clear_value );
00222 }
00223 
00224 /**
00225  * Get setting value for "read" command
00226  *
00227  * @v setting           Named setting
00228  * @v args              Remaining arguments
00229  * @ret value           Setting value
00230  * @ret rc              Return status code
00231  */
00232 static int read_value ( struct named_setting *setting, char **args __unused,
00233                         char **value ) {
00234         char *existing;
00235         int rc;
00236 
00237         /* Read existing value, treating errors as equivalent to an
00238          * empty initial setting.
00239          */
00240         fetchf_setting_copy ( setting->settings, &setting->setting,
00241                               NULL, &setting->setting, &existing );
00242 
00243         /* Read new value */
00244         if ( ( rc = readline_history ( NULL, existing, NULL, value ) ) != 0 )
00245                 goto err_readline;
00246 
00247  err_readline:
00248         free ( existing );
00249         return rc;
00250 }
00251 
00252 /**
00253  * "read" command
00254  *
00255  * @v argc              Argument count
00256  * @v argv              Argument list
00257  * @ret rc              Return status code
00258  */
00259 static int read_exec ( int argc, char **argv ) {
00260         return set_core_exec ( argc, argv, &clear_read_cmd, read_value );
00261 }
00262 
00263 /** "inc" options */
00264 struct inc_options {};
00265 
00266 /** "inc" option list */
00267 static struct option_descriptor inc_opts[] = {};
00268 
00269 /** "inc" command descriptor */
00270 static struct command_descriptor inc_cmd =
00271         COMMAND_DESC ( struct inc_options, inc_opts, 1, 2,
00272                        "<setting> [<increment>]" );
00273 
00274 /**
00275  * "inc" command
00276  *
00277  * @v argc              Argument count
00278  * @v argv              Argument list
00279  * @ret rc              Return status code
00280  */
00281 static int inc_exec ( int argc, char **argv ) {
00282         struct inc_options opts;
00283         struct named_setting setting;
00284         unsigned int increment = 1;
00285         unsigned long value;
00286         int rc;
00287 
00288         /* Parse options */
00289         if ( ( rc = parse_options ( argc, argv, &inc_cmd, &opts ) ) != 0 )
00290                 goto err_parse_options;
00291 
00292         /* Parse setting name */
00293         if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 )
00294                 goto err_parse_setting;
00295 
00296         /* Parse increment (if present) */
00297         if ( ( ( optind + 1 ) < argc ) &&
00298              ( ( rc = parse_integer ( argv[ optind + 1 ], &increment ) ) != 0))
00299                 goto err_parse_increment;
00300 
00301         /* Read existing value, treating errors as equivalent to a
00302          * zero-valued :int32 initial setting.
00303          */
00304         if ( ( rc = fetchn_setting ( setting.settings, &setting.setting,
00305                                      NULL, &setting.setting, &value ) ) != 0 ) {
00306                 value = 0;
00307                 if ( ! setting.setting.type )
00308                         setting.setting.type = &setting_type_int32;
00309         }
00310 
00311         /* Increment value */
00312         value += increment;
00313 
00314         /* Store updated setting value */
00315         if ( ( rc = storen_setting ( setting.settings, &setting.setting,
00316                                      value ) ) != 0 ) {
00317                 printf ( "Could not store \"%s\": %s\n",
00318                          setting.setting.name, strerror ( rc ) );
00319                 goto err_store;
00320         }
00321 
00322  err_store:
00323  err_parse_increment:
00324  err_parse_setting:
00325  err_parse_options:
00326         return rc;
00327 }
00328 
00329 /** Non-volatile option commands */
00330 struct command nvo_commands[] __command = {
00331         {
00332                 .name = "show",
00333                 .exec = show_exec,
00334         },
00335         {
00336                 .name = "set",
00337                 .exec = set_exec,
00338         },      
00339         {
00340                 .name = "clear",
00341                 .exec = clear_exec,
00342         },
00343         {
00344                 .name = "read",
00345                 .exec = read_exec,
00346         },
00347         {
00348                 .name = "inc",
00349                 .exec = inc_exec,
00350         },
00351 };