iPXE
param_cmd.c
Go to the documentation of this file.
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  * 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.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 FILE_SECBOOT ( PERMITTED );
26 
27 /** @file
28  *
29  * Request parameter commands
30  *
31  */
32 
33 #include <stdlib.h>
34 #include <errno.h>
35 #include <getopt.h>
36 #include <ipxe/params.h>
37 #include <ipxe/parseopt.h>
38 #include <ipxe/command.h>
39 
40 /** "params" options */
42  /** Name */
43  char *name;
44  /** Delete */
45  int delete;
46 };
47 
48 /** "params" option list */
49 static struct option_descriptor params_opts[] = {
50  OPTION_DESC ( "name", 'n', required_argument,
51  struct params_options, name, parse_string ),
52  OPTION_DESC ( "delete", 'd', no_argument,
53  struct params_options, delete, parse_flag ),
54 };
55 
56 /** "params" command descriptor */
58  COMMAND_DESC ( struct params_options, params_opts, 0, 0, NULL );
59 
60 /**
61  * The "params" command
62  *
63  * @v argc Argument count
64  * @v argv Argument list
65  * @ret rc Return status code
66  */
67 static int params_exec ( int argc, char **argv ) {
68  struct params_options opts;
69  struct parameters *params;
70  int rc;
71 
72  /* Parse options */
73  if ( ( rc = parse_options ( argc, argv, &params_cmd, &opts ) ) != 0)
74  return rc;
75 
76  /* Create parameter list */
77  params = create_parameters ( opts.name );
78  if ( ! params )
79  return -ENOMEM;
80 
81  /* Destroy parameter list, if applicable */
82  if ( opts.delete ) {
83  claim_parameters ( params );
84  params_put ( params );
85  }
86 
87  return 0;
88 }
89 
90 /** "param" options */
91 struct param_options {
92  /** Parameter list name */
93  char *params;
94  /** Parameter is a header */
95  int header;
96 };
97 
98 /** "param" option list */
99 static struct option_descriptor param_opts[] = {
100  OPTION_DESC ( "params", 'p', required_argument,
101  struct param_options, params, parse_string ),
102  OPTION_DESC ( "header", 'H', no_argument,
103  struct param_options, header, parse_flag ),
104 };
105 
106 /** "param" command descriptor */
109  "<key> [<value>]" );
110 
111 /**
112  * The "param" command
113  *
114  * @v argc Argument count
115  * @v argv Argument list
116  * @ret rc Return status code
117  */
118 static int param_exec ( int argc, char **argv ) {
119  struct param_options opts;
120  char *key;
121  char *value;
122  unsigned int flags;
123  struct parameters *params;
124  struct parameter *param;
125  int rc;
126 
127  /* Parse options */
128  if ( ( rc = parse_options ( argc, argv, &param_cmd, &opts ) ) != 0 )
129  goto err_parse_options;
130 
131  /* Parse key */
132  key = argv[optind];
133 
134  /* Parse value */
135  value = concat_args ( &argv[ optind + 1 ] );
136  if ( ! value ) {
137  rc = -ENOMEM;
138  goto err_parse_value;
139  }
140 
141  /* Construct flags */
142  flags = ( opts.header ? PARAMETER_HEADER : PARAMETER_FORM );
143 
144  /* Identify parameter list */
145  if ( ( rc = parse_parameters ( opts.params, &params ) ) != 0 )
146  goto err_parse_parameters;
147 
148  /* Add parameter */
149  param = add_parameter ( params, key, value, flags );
150  if ( ! param ) {
151  rc = -ENOMEM;
152  goto err_add_parameter;
153  }
154 
155  /* Success */
156  rc = 0;
157 
158  err_add_parameter:
159  err_parse_parameters:
160  free ( value );
161  err_parse_value:
162  err_parse_options:
163  return rc;
164 }
165 
166 /** Request parameter commands */
167 COMMAND ( params, params_exec );
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
FILE_SECBOOT(PERMITTED)
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1986
int optind
Current option index.
Definition: getopt.c:52
char * name
Name.
Definition: param_cmd.c:43
Error codes.
A request parameter list.
Definition: params.h:17
static int param_exec(int argc, char **argv)
The "param" command.
Definition: param_cmd.c:118
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:485
A command descriptor.
Definition: parseopt.h:78
static int params_exec(int argc, char **argv)
The "params" command.
Definition: param_cmd.c:67
#define ENOMEM
Not enough space.
Definition: errno.h:535
Parse command-line options.
Request parameters.
COMMAND(params, params_exec)
Request parameter commands.
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:74
static struct option_descriptor params_opts[]
"params" option list
Definition: param_cmd.c:49
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:98
"params" options
Definition: param_cmd.c:41
char * concat_args(char **args)
Concatenate arguments.
Definition: exec.c:359
struct parameters * create_parameters(const char *name)
Create request parameter list.
Definition: params.c:87
"param" options
Definition: param_cmd.c:91
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:227
uint8_t flags
Flags.
Definition: ena.h:18
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:55
int parse_parameters(char *text, struct parameters **params)
Parse request parameter list name.
Definition: parseopt.c:349
struct hv_monitor_parameter param[4][32]
Parameters.
Definition: hyperv.h:24
Command line option parsing.
Option does not take an argument.
Definition: getopt.h:17
#define PARAMETER_FORM
Request parameter is a form parameter.
Definition: params.h:41
static struct command_descriptor param_cmd
"param" command descriptor
Definition: param_cmd.c:107
int header
Parameter is a header.
Definition: param_cmd.c:95
char * params
Parameter list name.
Definition: param_cmd.c:93
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:68
Option requires an argument.
Definition: getopt.h:19
A command-line option descriptor.
Definition: parseopt.h:24
struct ena_llq_option header
Header locations.
Definition: ena.h:16
static struct command_descriptor params_cmd
"params" command descriptor
Definition: param_cmd.c:57
static union @447 opts
"cert<xxx>" option list
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:109
A request parameter.
Definition: params.h:29
struct parameter * add_parameter(struct parameters *params, const char *key, const char *value, unsigned int flags)
Add request parameter.
Definition: params.c:130
static struct option_descriptor param_opts[]
"param" option list
Definition: param_cmd.c:99
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
#define PARAMETER_HEADER
Request parameter is a header parameter.
Definition: params.h:44
union @391 key
Sense key.
Definition: scsi.h:18