Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / hci / commands / param_cmd.c
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, NULL );
54
55 /**
56  * The "params" command
57  *
58  * @v argc              Argument count
59  * @v argv              Argument list
60  * @ret rc              Return status code
61  */
62 static int params_exec ( int argc, char **argv ) {
63         struct params_options opts;
64         struct parameters *params;
65         int rc;
66
67         /* Parse options */
68         if ( ( rc = parse_options ( argc, argv, &params_cmd, &opts ) ) != 0)
69                 return rc;
70
71         /* Create parameter list */
72         params = create_parameters ( opts.name );
73         if ( ! params )
74                 return -ENOMEM;
75
76         /* Destroy parameter list, if applicable */
77         if ( opts.delete ) {
78                 claim_parameters ( params );
79                 params_put ( params );
80         }
81
82         return 0;
83 }
84
85 /** "param" options */
86 struct param_options {
87         /** Parameter list name */
88         char *params;
89 };
90
91 /** "param" option list */
92 static struct option_descriptor param_opts[] = {
93         OPTION_DESC ( "params", 'p', required_argument,
94                       struct param_options, params, parse_string ),
95 };
96
97 /** "param" command descriptor */
98 static struct command_descriptor param_cmd =
99         COMMAND_DESC ( struct param_options, param_opts, 1, MAX_ARGUMENTS,
100                        "<key> [<value>]" );
101
102 /**
103  * The "param" command
104  *
105  * @v argc              Argument count
106  * @v argv              Argument list
107  * @ret rc              Return status code
108  */
109 static int param_exec ( int argc, char **argv ) {
110         struct param_options opts;
111         char *key;
112         char *value;
113         struct parameters *params;
114         struct parameter *param;
115         int rc;
116
117         /* Parse options */
118         if ( ( rc = parse_options ( argc, argv, &param_cmd, &opts ) ) != 0 )
119                 goto err_parse_options;
120
121         /* Parse key */
122         key = argv[optind];
123
124         /* Parse value */
125         value = concat_args ( &argv[ optind + 1 ] );
126         if ( ! value ) {
127                 rc = -ENOMEM;
128                 goto err_parse_value;
129         }
130
131         /* Identify parameter list */
132         if ( ( rc = parse_parameters ( opts.params, &params ) ) != 0 )
133                 goto err_parse_parameters;
134
135         /* Add parameter */
136         param = add_parameter ( params, key, value );
137         if ( ! param ) {
138                 rc = -ENOMEM;
139                 goto err_add_parameter;
140         }
141
142         /* Success */
143         rc = 0;
144
145  err_add_parameter:
146  err_parse_parameters:
147         free ( value );
148  err_parse_value:
149  err_parse_options:
150         return rc;
151 }
152
153 /** Form parameter commands */
154 struct command param_commands[] __command = {
155         {
156                 .name = "params",
157                 .exec = params_exec,
158         },
159         {
160                 .name = "param",
161                 .exec = param_exec,
162         },
163 };