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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_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 */
49static struct option_descriptor params_opts[] = {
50 OPTION_DESC ( "name", 'n', required_argument,
52 OPTION_DESC ( "delete", 'd', no_argument,
53 struct params_options, delete, parse_flag ),
54};
55
56/** "params" command descriptor */
59
60/**
61 * The "params" command
62 *
63 * @v argc Argument count
64 * @v argv Argument list
65 * @ret rc Return status code
66 */
67static 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 */
92 /** Parameter list name */
93 char *params;
94 /** Parameter is a header */
95 int header;
96};
97
98/** "param" option list */
99static 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,
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 */
118static 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 */
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
union @162305117151260234136356364136041353210355154177 key
Sense key.
Definition scsi.h:3
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
pseudo_bit_t value[0x00020]
Definition arbel.h:2
const char * name
Definition ath9k_hw.c:1986
static union @024010030001061367220137227263210031030210157031 opts
"cert<xxx>" option list
#define COMMAND(name, exec)
Definition command.h:27
struct ena_llq_option header
Header locations.
Definition ena.h:5
uint8_t flags
Flags.
Definition ena.h:7
Error codes.
char * concat_args(char **args)
Concatenate arguments.
Definition exec.c:359
int optind
Current option index.
Definition getopt.c:52
Parse command-line options.
@ required_argument
Option requires an argument.
Definition getopt.h:19
@ no_argument
Option does not take an argument.
Definition getopt.h:17
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define ENOMEM
Not enough space.
Definition errno.h:535
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
struct hv_monitor_parameter param[4][32]
Parameters.
Definition hyperv.h:13
Request parameters.
#define PARAMETER_HEADER
Request parameter is a header parameter.
Definition params.h:44
#define PARAMETER_FORM
Request parameter is a form parameter.
Definition params.h:41
static struct command_descriptor params_cmd
"params" command descriptor
Definition param_cmd.c:57
static int params_exec(int argc, char **argv)
The "params" command.
Definition param_cmd.c:67
static struct option_descriptor param_opts[]
"param" option list
Definition param_cmd.c:99
static struct option_descriptor params_opts[]
"params" option list
Definition param_cmd.c:49
static struct command_descriptor param_cmd
"param" command descriptor
Definition param_cmd.c:107
static int param_exec(int argc, char **argv)
The "param" command.
Definition param_cmd.c:118
struct parameters * create_parameters(const char *name)
Create request parameter list.
Definition params.c:87
struct parameter * add_parameter(struct parameters *params, const char *key, const char *value, unsigned int flags)
Add request parameter.
Definition params.c:130
int parse_flag(char *text __unused, int *flag)
Parse flag.
Definition parseopt.c:227
int parse_string(char *text, char **value)
Parse string value.
Definition parseopt.c:74
int parse_parameters(char *text, struct parameters **params)
Parse request parameter list name.
Definition parseopt.c:349
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition parseopt.c:485
Command line option parsing.
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition parseopt.h:98
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition parseopt.h:109
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition parseopt.h:68
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
A command descriptor.
Definition parseopt.h:78
A command-line option descriptor.
Definition parseopt.h:24
"param" options
Definition param_cmd.c:91
char * params
Parameter list name.
Definition param_cmd.c:93
int header
Parameter is a header.
Definition param_cmd.c:95
A request parameter.
Definition params.h:29
A request parameter list.
Definition params.h:17
"params" options
Definition param_cmd.c:41
char * name
Name.
Definition param_cmd.c:43