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