|  | @@ -0,0 +1,162 @@
 | 
		
	
		
			
			|  | 1 | +/*
 | 
		
	
		
			
			|  | 2 | + * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
 | 
		
	
		
			
			|  | 3 | + *
 | 
		
	
		
			
			|  | 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.
 | 
		
	
		
			
			|  | 8 | + *
 | 
		
	
		
			
			|  | 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.
 | 
		
	
		
			
			|  | 13 | + *
 | 
		
	
		
			
			|  | 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
 | 
		
	
		
			
			|  | 17 | + * 02110-1301, USA.
 | 
		
	
		
			
			|  | 18 | + */
 | 
		
	
		
			
			|  | 19 | +
 | 
		
	
		
			
			|  | 20 | +FILE_LICENCE ( GPL2_OR_LATER );
 | 
		
	
		
			
			|  | 21 | +
 | 
		
	
		
			
			|  | 22 | +/** @file
 | 
		
	
		
			
			|  | 23 | + *
 | 
		
	
		
			
			|  | 24 | + * Form parameter commands
 | 
		
	
		
			
			|  | 25 | + *
 | 
		
	
		
			
			|  | 26 | + */
 | 
		
	
		
			
			|  | 27 | +
 | 
		
	
		
			
			|  | 28 | +#include <stdlib.h>
 | 
		
	
		
			
			|  | 29 | +#include <errno.h>
 | 
		
	
		
			
			|  | 30 | +#include <getopt.h>
 | 
		
	
		
			
			|  | 31 | +#include <ipxe/params.h>
 | 
		
	
		
			
			|  | 32 | +#include <ipxe/parseopt.h>
 | 
		
	
		
			
			|  | 33 | +#include <ipxe/command.h>
 | 
		
	
		
			
			|  | 34 | +
 | 
		
	
		
			
			|  | 35 | +/** "params" options */
 | 
		
	
		
			
			|  | 36 | +struct params_options {
 | 
		
	
		
			
			|  | 37 | +	/** Name */
 | 
		
	
		
			
			|  | 38 | +	char *name;
 | 
		
	
		
			
			|  | 39 | +	/** Delete */
 | 
		
	
		
			
			|  | 40 | +	int delete;
 | 
		
	
		
			
			|  | 41 | +};
 | 
		
	
		
			
			|  | 42 | +
 | 
		
	
		
			
			|  | 43 | +/** "params" option list */
 | 
		
	
		
			
			|  | 44 | +static struct option_descriptor params_opts[] = {
 | 
		
	
		
			
			|  | 45 | +	OPTION_DESC ( "name", 'n', required_argument,
 | 
		
	
		
			
			|  | 46 | +		      struct params_options, name, parse_string ),
 | 
		
	
		
			
			|  | 47 | +	OPTION_DESC ( "delete", 'd', no_argument,
 | 
		
	
		
			
			|  | 48 | +		      struct params_options, delete, parse_flag ),
 | 
		
	
		
			
			|  | 49 | +};
 | 
		
	
		
			
			|  | 50 | +
 | 
		
	
		
			
			|  | 51 | +/** "params" command descriptor */
 | 
		
	
		
			
			|  | 52 | +static struct command_descriptor params_cmd =
 | 
		
	
		
			
			|  | 53 | +	COMMAND_DESC ( struct params_options, params_opts, 0, 0,
 | 
		
	
		
			
			|  | 54 | +		       "[--name <name>] [--delete]" );
 | 
		
	
		
			
			|  | 55 | +
 | 
		
	
		
			
			|  | 56 | +/**
 | 
		
	
		
			
			|  | 57 | + * The "params" command
 | 
		
	
		
			
			|  | 58 | + *
 | 
		
	
		
			
			|  | 59 | + * @v argc		Argument count
 | 
		
	
		
			
			|  | 60 | + * @v argv		Argument list
 | 
		
	
		
			
			|  | 61 | + * @ret rc		Return status code
 | 
		
	
		
			
			|  | 62 | + */
 | 
		
	
		
			
			|  | 63 | +static int params_exec ( int argc, char **argv ) {
 | 
		
	
		
			
			|  | 64 | +	struct params_options opts;
 | 
		
	
		
			
			|  | 65 | +	struct parameters *params;
 | 
		
	
		
			
			|  | 66 | +	int rc;
 | 
		
	
		
			
			|  | 67 | +
 | 
		
	
		
			
			|  | 68 | +	/* Parse options */
 | 
		
	
		
			
			|  | 69 | +	if ( ( rc = parse_options ( argc, argv, ¶ms_cmd, &opts ) ) != 0)
 | 
		
	
		
			
			|  | 70 | +		return rc;
 | 
		
	
		
			
			|  | 71 | +
 | 
		
	
		
			
			|  | 72 | +	/* Create parameter list */
 | 
		
	
		
			
			|  | 73 | +	params = create_parameters ( opts.name );
 | 
		
	
		
			
			|  | 74 | +	if ( ! params )
 | 
		
	
		
			
			|  | 75 | +		return -ENOMEM;
 | 
		
	
		
			
			|  | 76 | +
 | 
		
	
		
			
			|  | 77 | +	/* Destroy parameter list, if applicable */
 | 
		
	
		
			
			|  | 78 | +	if ( opts.delete )
 | 
		
	
		
			
			|  | 79 | +		destroy_parameters ( params );
 | 
		
	
		
			
			|  | 80 | +
 | 
		
	
		
			
			|  | 81 | +	return 0;
 | 
		
	
		
			
			|  | 82 | +}
 | 
		
	
		
			
			|  | 83 | +
 | 
		
	
		
			
			|  | 84 | +/** "param" options */
 | 
		
	
		
			
			|  | 85 | +struct param_options {
 | 
		
	
		
			
			|  | 86 | +	/** Parameter list name */
 | 
		
	
		
			
			|  | 87 | +	char *params;
 | 
		
	
		
			
			|  | 88 | +};
 | 
		
	
		
			
			|  | 89 | +
 | 
		
	
		
			
			|  | 90 | +/** "param" option list */
 | 
		
	
		
			
			|  | 91 | +static struct option_descriptor param_opts[] = {
 | 
		
	
		
			
			|  | 92 | +	OPTION_DESC ( "params", 'p', required_argument,
 | 
		
	
		
			
			|  | 93 | +		      struct param_options, params, parse_string ),
 | 
		
	
		
			
			|  | 94 | +};
 | 
		
	
		
			
			|  | 95 | +
 | 
		
	
		
			
			|  | 96 | +/** "param" command descriptor */
 | 
		
	
		
			
			|  | 97 | +static struct command_descriptor param_cmd =
 | 
		
	
		
			
			|  | 98 | +	COMMAND_DESC ( struct param_options, param_opts, 1, MAX_ARGUMENTS,
 | 
		
	
		
			
			|  | 99 | +		       "[--params <params>] <key> [<value>]" );
 | 
		
	
		
			
			|  | 100 | +
 | 
		
	
		
			
			|  | 101 | +/**
 | 
		
	
		
			
			|  | 102 | + * The "param" command
 | 
		
	
		
			
			|  | 103 | + *
 | 
		
	
		
			
			|  | 104 | + * @v argc		Argument count
 | 
		
	
		
			
			|  | 105 | + * @v argv		Argument list
 | 
		
	
		
			
			|  | 106 | + * @ret rc		Return status code
 | 
		
	
		
			
			|  | 107 | + */
 | 
		
	
		
			
			|  | 108 | +static int param_exec ( int argc, char **argv ) {
 | 
		
	
		
			
			|  | 109 | +	struct param_options opts;
 | 
		
	
		
			
			|  | 110 | +	char *key;
 | 
		
	
		
			
			|  | 111 | +	char *value;
 | 
		
	
		
			
			|  | 112 | +	struct parameters *params;
 | 
		
	
		
			
			|  | 113 | +	struct parameter *param;
 | 
		
	
		
			
			|  | 114 | +	int rc;
 | 
		
	
		
			
			|  | 115 | +
 | 
		
	
		
			
			|  | 116 | +	/* Parse options */
 | 
		
	
		
			
			|  | 117 | +	if ( ( rc = parse_options ( argc, argv, ¶m_cmd, &opts ) ) != 0 )
 | 
		
	
		
			
			|  | 118 | +		goto err_parse_options;
 | 
		
	
		
			
			|  | 119 | +
 | 
		
	
		
			
			|  | 120 | +	/* Parse key */
 | 
		
	
		
			
			|  | 121 | +	key = argv[optind];
 | 
		
	
		
			
			|  | 122 | +
 | 
		
	
		
			
			|  | 123 | +	/* Parse value */
 | 
		
	
		
			
			|  | 124 | +	value = concat_args ( &argv[ optind + 1 ] );
 | 
		
	
		
			
			|  | 125 | +	if ( ! value ) {
 | 
		
	
		
			
			|  | 126 | +		rc = -ENOMEM;
 | 
		
	
		
			
			|  | 127 | +		goto err_parse_value;
 | 
		
	
		
			
			|  | 128 | +	}
 | 
		
	
		
			
			|  | 129 | +
 | 
		
	
		
			
			|  | 130 | +	/* Identify parameter list */
 | 
		
	
		
			
			|  | 131 | +	if ( ( rc = parse_parameters ( opts.params, ¶ms ) ) != 0 )
 | 
		
	
		
			
			|  | 132 | +		goto err_parse_parameters;
 | 
		
	
		
			
			|  | 133 | +
 | 
		
	
		
			
			|  | 134 | +	/* Add parameter */
 | 
		
	
		
			
			|  | 135 | +	param = add_parameter ( params, key, value );
 | 
		
	
		
			
			|  | 136 | +	if ( ! param ) {
 | 
		
	
		
			
			|  | 137 | +		rc = -ENOMEM;
 | 
		
	
		
			
			|  | 138 | +		goto err_add_parameter;
 | 
		
	
		
			
			|  | 139 | +	}
 | 
		
	
		
			
			|  | 140 | +
 | 
		
	
		
			
			|  | 141 | +	/* Success */
 | 
		
	
		
			
			|  | 142 | +	rc = 0;
 | 
		
	
		
			
			|  | 143 | +
 | 
		
	
		
			
			|  | 144 | + err_add_parameter:
 | 
		
	
		
			
			|  | 145 | + err_parse_parameters:
 | 
		
	
		
			
			|  | 146 | +	free ( value );
 | 
		
	
		
			
			|  | 147 | + err_parse_value:
 | 
		
	
		
			
			|  | 148 | + err_parse_options:
 | 
		
	
		
			
			|  | 149 | +	return rc;
 | 
		
	
		
			
			|  | 150 | +}
 | 
		
	
		
			
			|  | 151 | +
 | 
		
	
		
			
			|  | 152 | +/** Form parameter commands */
 | 
		
	
		
			
			|  | 153 | +struct command param_commands[] __command = {
 | 
		
	
		
			
			|  | 154 | +	{
 | 
		
	
		
			
			|  | 155 | +		.name = "params",
 | 
		
	
		
			
			|  | 156 | +		.exec = params_exec,
 | 
		
	
		
			
			|  | 157 | +	},
 | 
		
	
		
			
			|  | 158 | +	{
 | 
		
	
		
			
			|  | 159 | +		.name = "param",
 | 
		
	
		
			
			|  | 160 | +		.exec = param_exec,
 | 
		
	
		
			
			|  | 161 | +	},
 | 
		
	
		
			
			|  | 162 | +};
 |