[tcpip] Allow supported address families to be detected at runtime
[ipxe.git] / 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 * 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 */
40 struct 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 */
56 static struct command_descriptor params_cmd =
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 */
102 static struct command_descriptor param_cmd =
103 COMMAND_DESC ( struct param_options, param_opts, 1, MAX_ARGUMENTS,
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 };