iPXE
param_cmd.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 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 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * Form parameter commands
00029  *
00030  */
00031 
00032 #include <stdlib.h>
00033 #include <errno.h>
00034 #include <getopt.h>
00035 #include <ipxe/params.h>
00036 #include <ipxe/parseopt.h>
00037 #include <ipxe/command.h>
00038 
00039 /** "params" options */
00040 struct params_options {
00041         /** Name */
00042         char *name;
00043         /** Delete */
00044         int delete;
00045 };
00046 
00047 /** "params" option list */
00048 static struct option_descriptor params_opts[] = {
00049         OPTION_DESC ( "name", 'n', required_argument,
00050                       struct params_options, name, parse_string ),
00051         OPTION_DESC ( "delete", 'd', no_argument,
00052                       struct params_options, delete, parse_flag ),
00053 };
00054 
00055 /** "params" command descriptor */
00056 static struct command_descriptor params_cmd =
00057         COMMAND_DESC ( struct params_options, params_opts, 0, 0, NULL );
00058 
00059 /**
00060  * The "params" command
00061  *
00062  * @v argc              Argument count
00063  * @v argv              Argument list
00064  * @ret rc              Return status code
00065  */
00066 static int params_exec ( int argc, char **argv ) {
00067         struct params_options opts;
00068         struct parameters *params;
00069         int rc;
00070 
00071         /* Parse options */
00072         if ( ( rc = parse_options ( argc, argv, &params_cmd, &opts ) ) != 0)
00073                 return rc;
00074 
00075         /* Create parameter list */
00076         params = create_parameters ( opts.name );
00077         if ( ! params )
00078                 return -ENOMEM;
00079 
00080         /* Destroy parameter list, if applicable */
00081         if ( opts.delete ) {
00082                 claim_parameters ( params );
00083                 params_put ( params );
00084         }
00085 
00086         return 0;
00087 }
00088 
00089 /** "param" options */
00090 struct param_options {
00091         /** Parameter list name */
00092         char *params;
00093 };
00094 
00095 /** "param" option list */
00096 static struct option_descriptor param_opts[] = {
00097         OPTION_DESC ( "params", 'p', required_argument,
00098                       struct param_options, params, parse_string ),
00099 };
00100 
00101 /** "param" command descriptor */
00102 static struct command_descriptor param_cmd =
00103         COMMAND_DESC ( struct param_options, param_opts, 1, MAX_ARGUMENTS,
00104                        "<key> [<value>]" );
00105 
00106 /**
00107  * The "param" command
00108  *
00109  * @v argc              Argument count
00110  * @v argv              Argument list
00111  * @ret rc              Return status code
00112  */
00113 static int param_exec ( int argc, char **argv ) {
00114         struct param_options opts;
00115         char *key;
00116         char *value;
00117         struct parameters *params;
00118         struct parameter *param;
00119         int rc;
00120 
00121         /* Parse options */
00122         if ( ( rc = parse_options ( argc, argv, &param_cmd, &opts ) ) != 0 )
00123                 goto err_parse_options;
00124 
00125         /* Parse key */
00126         key = argv[optind];
00127 
00128         /* Parse value */
00129         value = concat_args ( &argv[ optind + 1 ] );
00130         if ( ! value ) {
00131                 rc = -ENOMEM;
00132                 goto err_parse_value;
00133         }
00134 
00135         /* Identify parameter list */
00136         if ( ( rc = parse_parameters ( opts.params, &params ) ) != 0 )
00137                 goto err_parse_parameters;
00138 
00139         /* Add parameter */
00140         param = add_parameter ( params, key, value );
00141         if ( ! param ) {
00142                 rc = -ENOMEM;
00143                 goto err_add_parameter;
00144         }
00145 
00146         /* Success */
00147         rc = 0;
00148 
00149  err_add_parameter:
00150  err_parse_parameters:
00151         free ( value );
00152  err_parse_value:
00153  err_parse_options:
00154         return rc;
00155 }
00156 
00157 /** Form parameter commands */
00158 struct command param_commands[] __command = {
00159         {
00160                 .name = "params",
00161                 .exec = params_exec,
00162         },
00163         {
00164                 .name = "param",
00165                 .exec = param_exec,
00166         },
00167 };