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  * Form 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 };
94 
95 /** "param" option list */
96 static struct option_descriptor param_opts[] = {
97  OPTION_DESC ( "params", 'p', required_argument,
98  struct param_options, params, parse_string ),
99 };
100 
101 /** "param" command descriptor */
104  "<key> [<value>]" );
105 
106 /**
107  * The "param" command
108  *
109  * @v argc Argument count
110  * @v argv Argument list
111  * @ret rc Return status code
112  */
113 static int param_exec ( int argc, char **argv ) {
114  struct param_options opts;
115  char *key;
116  char *value;
117  struct parameters *params;
118  struct parameter *param;
119  int rc;
120 
121  /* Parse options */
122  if ( ( rc = parse_options ( argc, argv, &param_cmd, &opts ) ) != 0 )
123  goto err_parse_options;
124 
125  /* Parse key */
126  key = argv[optind];
127 
128  /* Parse value */
129  value = concat_args ( &argv[ optind + 1 ] );
130  if ( ! value ) {
131  rc = -ENOMEM;
132  goto err_parse_value;
133  }
134 
135  /* Identify parameter list */
136  if ( ( rc = parse_parameters ( opts.params, &params ) ) != 0 )
137  goto err_parse_parameters;
138 
139  /* Add parameter */
140  param = add_parameter ( params, key, value );
141  if ( ! param ) {
142  rc = -ENOMEM;
143  goto err_add_parameter;
144  }
145 
146  /* Success */
147  rc = 0;
148 
149  err_add_parameter:
150  err_parse_parameters:
151  free ( value );
152  err_parse_value:
153  err_parse_options:
154  return rc;
155 }
156 
157 /** Form parameter commands */
158 struct command param_commands[] __command = {
159  {
160  .name = "params",
161  .exec = params_exec,
162  },
163  {
164  .name = "param",
165  .exec = param_exec,
166  },
167 };
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
Form parameter commands.
Definition: param_cmd.c:158
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 form parameter list.
Definition: params.h:16
static int param_exec(int argc, char **argv)
The "param" command.
Definition: param_cmd.c:113
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:447
A command descriptor.
Definition: parseopt.h:76
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.
struct parameter * add_parameter(struct parameters *params, const char *key, const char *value)
Add form parameter.
Definition: params.c:128
Form parameters.
int parse_string(char *text, char **value)
Parse string value.
Definition: parseopt.c:70
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:96
"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 form parameter list.
Definition: params.c:86
"param" options
Definition: param_cmd.c:90
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
static union @423 opts
"cert<xxx>" option list
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition: parseopt.c:200
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
int parse_parameters(char *text, struct parameters **params)
Parse form parameter list name.
Definition: parseopt.c:311
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
const char * name
Name of the command.
Definition: command.h:11
static struct command_descriptor param_cmd
"param" command descriptor
Definition: param_cmd.c:102
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:66
Option requires an argument.
Definition: getopt.h:18
A command-line option descriptor.
Definition: parseopt.h:22
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:107
A form parameter.
Definition: params.h:28
static struct option_descriptor param_opts[]
"param" option list
Definition: param_cmd.c:96
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
union @375 key
Sense key.
Definition: scsi.h:18