root / qemu-option.c @ 60e1b2a6
History | View | Annotate | Download (26.2 kB)
1 | d3f24367 | Kevin Wolf | /*
|
---|---|---|---|
2 | d3f24367 | Kevin Wolf | * Commandline option parsing functions
|
3 | d3f24367 | Kevin Wolf | *
|
4 | d3f24367 | Kevin Wolf | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | d3f24367 | Kevin Wolf | * Copyright (c) 2009 Kevin Wolf <kwolf@redhat.com>
|
6 | d3f24367 | Kevin Wolf | *
|
7 | d3f24367 | Kevin Wolf | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
8 | d3f24367 | Kevin Wolf | * of this software and associated documentation files (the "Software"), to deal
|
9 | d3f24367 | Kevin Wolf | * in the Software without restriction, including without limitation the rights
|
10 | d3f24367 | Kevin Wolf | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 | d3f24367 | Kevin Wolf | * copies of the Software, and to permit persons to whom the Software is
|
12 | d3f24367 | Kevin Wolf | * furnished to do so, subject to the following conditions:
|
13 | d3f24367 | Kevin Wolf | *
|
14 | d3f24367 | Kevin Wolf | * The above copyright notice and this permission notice shall be included in
|
15 | d3f24367 | Kevin Wolf | * all copies or substantial portions of the Software.
|
16 | d3f24367 | Kevin Wolf | *
|
17 | d3f24367 | Kevin Wolf | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18 | d3f24367 | Kevin Wolf | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19 | d3f24367 | Kevin Wolf | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
20 | d3f24367 | Kevin Wolf | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21 | d3f24367 | Kevin Wolf | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22 | d3f24367 | Kevin Wolf | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
23 | d3f24367 | Kevin Wolf | * THE SOFTWARE.
|
24 | d3f24367 | Kevin Wolf | */
|
25 | d3f24367 | Kevin Wolf | |
26 | d3f24367 | Kevin Wolf | #include <stdio.h> |
27 | d3f24367 | Kevin Wolf | #include <string.h> |
28 | d3f24367 | Kevin Wolf | |
29 | d3f24367 | Kevin Wolf | #include "qemu-common.h" |
30 | 827b0813 | Markus Armbruster | #include "qemu-error.h" |
31 | 01e7f188 | Markus Armbruster | #include "qemu-objects.h" |
32 | d3f24367 | Kevin Wolf | #include "qemu-option.h" |
33 | 975b63a4 | Markus Armbruster | #include "qerror.h" |
34 | d3f24367 | Kevin Wolf | |
35 | d3f24367 | Kevin Wolf | /*
|
36 | d3f24367 | Kevin Wolf | * Extracts the name of an option from the parameter string (p points at the
|
37 | d3f24367 | Kevin Wolf | * first byte of the option name)
|
38 | d3f24367 | Kevin Wolf | *
|
39 | d3f24367 | Kevin Wolf | * The option name is delimited by delim (usually , or =) or the string end
|
40 | d3f24367 | Kevin Wolf | * and is copied into buf. If the option name is longer than buf_size, it is
|
41 | d3f24367 | Kevin Wolf | * truncated. buf is always zero terminated.
|
42 | d3f24367 | Kevin Wolf | *
|
43 | d3f24367 | Kevin Wolf | * The return value is the position of the delimiter/zero byte after the option
|
44 | d3f24367 | Kevin Wolf | * name in p.
|
45 | d3f24367 | Kevin Wolf | */
|
46 | d3f24367 | Kevin Wolf | const char *get_opt_name(char *buf, int buf_size, const char *p, char delim) |
47 | d3f24367 | Kevin Wolf | { |
48 | d3f24367 | Kevin Wolf | char *q;
|
49 | d3f24367 | Kevin Wolf | |
50 | d3f24367 | Kevin Wolf | q = buf; |
51 | d3f24367 | Kevin Wolf | while (*p != '\0' && *p != delim) { |
52 | d3f24367 | Kevin Wolf | if (q && (q - buf) < buf_size - 1) |
53 | d3f24367 | Kevin Wolf | *q++ = *p; |
54 | d3f24367 | Kevin Wolf | p++; |
55 | d3f24367 | Kevin Wolf | } |
56 | d3f24367 | Kevin Wolf | if (q)
|
57 | d3f24367 | Kevin Wolf | *q = '\0';
|
58 | d3f24367 | Kevin Wolf | |
59 | d3f24367 | Kevin Wolf | return p;
|
60 | d3f24367 | Kevin Wolf | } |
61 | d3f24367 | Kevin Wolf | |
62 | d3f24367 | Kevin Wolf | /*
|
63 | d3f24367 | Kevin Wolf | * Extracts the value of an option from the parameter string p (p points at the
|
64 | d3f24367 | Kevin Wolf | * first byte of the option value)
|
65 | d3f24367 | Kevin Wolf | *
|
66 | d3f24367 | Kevin Wolf | * This function is comparable to get_opt_name with the difference that the
|
67 | d3f24367 | Kevin Wolf | * delimiter is fixed to be comma which starts a new option. To specify an
|
68 | d3f24367 | Kevin Wolf | * option value that contains commas, double each comma.
|
69 | d3f24367 | Kevin Wolf | */
|
70 | d3f24367 | Kevin Wolf | const char *get_opt_value(char *buf, int buf_size, const char *p) |
71 | d3f24367 | Kevin Wolf | { |
72 | d3f24367 | Kevin Wolf | char *q;
|
73 | d3f24367 | Kevin Wolf | |
74 | d3f24367 | Kevin Wolf | q = buf; |
75 | d3f24367 | Kevin Wolf | while (*p != '\0') { |
76 | d3f24367 | Kevin Wolf | if (*p == ',') { |
77 | d3f24367 | Kevin Wolf | if (*(p + 1) != ',') |
78 | d3f24367 | Kevin Wolf | break;
|
79 | d3f24367 | Kevin Wolf | p++; |
80 | d3f24367 | Kevin Wolf | } |
81 | d3f24367 | Kevin Wolf | if (q && (q - buf) < buf_size - 1) |
82 | d3f24367 | Kevin Wolf | *q++ = *p; |
83 | d3f24367 | Kevin Wolf | p++; |
84 | d3f24367 | Kevin Wolf | } |
85 | d3f24367 | Kevin Wolf | if (q)
|
86 | d3f24367 | Kevin Wolf | *q = '\0';
|
87 | d3f24367 | Kevin Wolf | |
88 | d3f24367 | Kevin Wolf | return p;
|
89 | d3f24367 | Kevin Wolf | } |
90 | d3f24367 | Kevin Wolf | |
91 | 62c5802e | Gerd Hoffmann | int get_next_param_value(char *buf, int buf_size, |
92 | 62c5802e | Gerd Hoffmann | const char *tag, const char **pstr) |
93 | 62c5802e | Gerd Hoffmann | { |
94 | 62c5802e | Gerd Hoffmann | const char *p; |
95 | 62c5802e | Gerd Hoffmann | char option[128]; |
96 | 62c5802e | Gerd Hoffmann | |
97 | 62c5802e | Gerd Hoffmann | p = *pstr; |
98 | 62c5802e | Gerd Hoffmann | for(;;) {
|
99 | 62c5802e | Gerd Hoffmann | p = get_opt_name(option, sizeof(option), p, '='); |
100 | 62c5802e | Gerd Hoffmann | if (*p != '=') |
101 | 62c5802e | Gerd Hoffmann | break;
|
102 | 62c5802e | Gerd Hoffmann | p++; |
103 | 62c5802e | Gerd Hoffmann | if (!strcmp(tag, option)) {
|
104 | 62c5802e | Gerd Hoffmann | *pstr = get_opt_value(buf, buf_size, p); |
105 | 62c5802e | Gerd Hoffmann | if (**pstr == ',') { |
106 | 62c5802e | Gerd Hoffmann | (*pstr)++; |
107 | 62c5802e | Gerd Hoffmann | } |
108 | 62c5802e | Gerd Hoffmann | return strlen(buf);
|
109 | 62c5802e | Gerd Hoffmann | } else {
|
110 | 62c5802e | Gerd Hoffmann | p = get_opt_value(NULL, 0, p); |
111 | 62c5802e | Gerd Hoffmann | } |
112 | 62c5802e | Gerd Hoffmann | if (*p != ',') |
113 | 62c5802e | Gerd Hoffmann | break;
|
114 | 62c5802e | Gerd Hoffmann | p++; |
115 | 62c5802e | Gerd Hoffmann | } |
116 | 62c5802e | Gerd Hoffmann | return 0; |
117 | 62c5802e | Gerd Hoffmann | } |
118 | 62c5802e | Gerd Hoffmann | |
119 | 62c5802e | Gerd Hoffmann | int get_param_value(char *buf, int buf_size, |
120 | 62c5802e | Gerd Hoffmann | const char *tag, const char *str) |
121 | 62c5802e | Gerd Hoffmann | { |
122 | 62c5802e | Gerd Hoffmann | return get_next_param_value(buf, buf_size, tag, &str);
|
123 | 62c5802e | Gerd Hoffmann | } |
124 | 62c5802e | Gerd Hoffmann | |
125 | 62c5802e | Gerd Hoffmann | int check_params(char *buf, int buf_size, |
126 | 62c5802e | Gerd Hoffmann | const char * const *params, const char *str) |
127 | 62c5802e | Gerd Hoffmann | { |
128 | 62c5802e | Gerd Hoffmann | const char *p; |
129 | 62c5802e | Gerd Hoffmann | int i;
|
130 | 62c5802e | Gerd Hoffmann | |
131 | 62c5802e | Gerd Hoffmann | p = str; |
132 | 62c5802e | Gerd Hoffmann | while (*p != '\0') { |
133 | 62c5802e | Gerd Hoffmann | p = get_opt_name(buf, buf_size, p, '=');
|
134 | 62c5802e | Gerd Hoffmann | if (*p != '=') { |
135 | 62c5802e | Gerd Hoffmann | return -1; |
136 | 62c5802e | Gerd Hoffmann | } |
137 | 62c5802e | Gerd Hoffmann | p++; |
138 | 62c5802e | Gerd Hoffmann | for (i = 0; params[i] != NULL; i++) { |
139 | 62c5802e | Gerd Hoffmann | if (!strcmp(params[i], buf)) {
|
140 | 62c5802e | Gerd Hoffmann | break;
|
141 | 62c5802e | Gerd Hoffmann | } |
142 | 62c5802e | Gerd Hoffmann | } |
143 | 62c5802e | Gerd Hoffmann | if (params[i] == NULL) { |
144 | 62c5802e | Gerd Hoffmann | return -1; |
145 | 62c5802e | Gerd Hoffmann | } |
146 | 62c5802e | Gerd Hoffmann | p = get_opt_value(NULL, 0, p); |
147 | 62c5802e | Gerd Hoffmann | if (*p != ',') { |
148 | 62c5802e | Gerd Hoffmann | break;
|
149 | 62c5802e | Gerd Hoffmann | } |
150 | 62c5802e | Gerd Hoffmann | p++; |
151 | 62c5802e | Gerd Hoffmann | } |
152 | 62c5802e | Gerd Hoffmann | return 0; |
153 | 62c5802e | Gerd Hoffmann | } |
154 | 62c5802e | Gerd Hoffmann | |
155 | d3f24367 | Kevin Wolf | /*
|
156 | d3f24367 | Kevin Wolf | * Searches an option list for an option with the given name
|
157 | d3f24367 | Kevin Wolf | */
|
158 | d3f24367 | Kevin Wolf | QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list, |
159 | d3f24367 | Kevin Wolf | const char *name) |
160 | d3f24367 | Kevin Wolf | { |
161 | d3f24367 | Kevin Wolf | while (list && list->name) {
|
162 | d3f24367 | Kevin Wolf | if (!strcmp(list->name, name)) {
|
163 | d3f24367 | Kevin Wolf | return list;
|
164 | d3f24367 | Kevin Wolf | } |
165 | d3f24367 | Kevin Wolf | list++; |
166 | d3f24367 | Kevin Wolf | } |
167 | d3f24367 | Kevin Wolf | |
168 | d3f24367 | Kevin Wolf | return NULL; |
169 | d3f24367 | Kevin Wolf | } |
170 | d3f24367 | Kevin Wolf | |
171 | f02b77c9 | M. Mohan Kumar | static int parse_option_bool(const char *name, const char *value, bool *ret) |
172 | 67b1355b | Gerd Hoffmann | { |
173 | 67b1355b | Gerd Hoffmann | if (value != NULL) { |
174 | 67b1355b | Gerd Hoffmann | if (!strcmp(value, "on")) { |
175 | 67b1355b | Gerd Hoffmann | *ret = 1;
|
176 | 67b1355b | Gerd Hoffmann | } else if (!strcmp(value, "off")) { |
177 | 67b1355b | Gerd Hoffmann | *ret = 0;
|
178 | 67b1355b | Gerd Hoffmann | } else {
|
179 | c64f27d4 | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "'on' or 'off'");
|
180 | 67b1355b | Gerd Hoffmann | return -1; |
181 | 67b1355b | Gerd Hoffmann | } |
182 | 67b1355b | Gerd Hoffmann | } else {
|
183 | 67b1355b | Gerd Hoffmann | *ret = 1;
|
184 | 67b1355b | Gerd Hoffmann | } |
185 | 67b1355b | Gerd Hoffmann | return 0; |
186 | 67b1355b | Gerd Hoffmann | } |
187 | 67b1355b | Gerd Hoffmann | |
188 | e27c88fe | Gerd Hoffmann | static int parse_option_number(const char *name, const char *value, uint64_t *ret) |
189 | e27c88fe | Gerd Hoffmann | { |
190 | e27c88fe | Gerd Hoffmann | char *postfix;
|
191 | e27c88fe | Gerd Hoffmann | uint64_t number; |
192 | e27c88fe | Gerd Hoffmann | |
193 | e27c88fe | Gerd Hoffmann | if (value != NULL) { |
194 | e27c88fe | Gerd Hoffmann | number = strtoull(value, &postfix, 0);
|
195 | e27c88fe | Gerd Hoffmann | if (*postfix != '\0') { |
196 | c64f27d4 | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a number");
|
197 | e27c88fe | Gerd Hoffmann | return -1; |
198 | e27c88fe | Gerd Hoffmann | } |
199 | 21c9f4cd | Gerd Hoffmann | *ret = number; |
200 | e27c88fe | Gerd Hoffmann | } else {
|
201 | c64f27d4 | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a number");
|
202 | e27c88fe | Gerd Hoffmann | return -1; |
203 | e27c88fe | Gerd Hoffmann | } |
204 | e27c88fe | Gerd Hoffmann | return 0; |
205 | e27c88fe | Gerd Hoffmann | } |
206 | e27c88fe | Gerd Hoffmann | |
207 | 7695019b | Gerd Hoffmann | static int parse_option_size(const char *name, const char *value, uint64_t *ret) |
208 | 7695019b | Gerd Hoffmann | { |
209 | 7695019b | Gerd Hoffmann | char *postfix;
|
210 | 7695019b | Gerd Hoffmann | double sizef;
|
211 | 7695019b | Gerd Hoffmann | |
212 | 7695019b | Gerd Hoffmann | if (value != NULL) { |
213 | 7695019b | Gerd Hoffmann | sizef = strtod(value, &postfix); |
214 | 7695019b | Gerd Hoffmann | switch (*postfix) {
|
215 | 7695019b | Gerd Hoffmann | case 'T': |
216 | 7695019b | Gerd Hoffmann | sizef *= 1024;
|
217 | 0b0404bf | Stefan Weil | /* fall through */
|
218 | 7695019b | Gerd Hoffmann | case 'G': |
219 | 7695019b | Gerd Hoffmann | sizef *= 1024;
|
220 | 0b0404bf | Stefan Weil | /* fall through */
|
221 | 7695019b | Gerd Hoffmann | case 'M': |
222 | 7695019b | Gerd Hoffmann | sizef *= 1024;
|
223 | 0b0404bf | Stefan Weil | /* fall through */
|
224 | 7695019b | Gerd Hoffmann | case 'K': |
225 | 7695019b | Gerd Hoffmann | case 'k': |
226 | 7695019b | Gerd Hoffmann | sizef *= 1024;
|
227 | 0b0404bf | Stefan Weil | /* fall through */
|
228 | 7695019b | Gerd Hoffmann | case 'b': |
229 | 7695019b | Gerd Hoffmann | case '\0': |
230 | 7695019b | Gerd Hoffmann | *ret = (uint64_t) sizef; |
231 | 7695019b | Gerd Hoffmann | break;
|
232 | 7695019b | Gerd Hoffmann | default:
|
233 | c64f27d4 | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size");
|
234 | c64f27d4 | Markus Armbruster | error_printf_unless_qmp("You may use k, M, G or T suffixes for "
|
235 | 7695019b | Gerd Hoffmann | "kilobytes, megabytes, gigabytes and terabytes.\n");
|
236 | 7695019b | Gerd Hoffmann | return -1; |
237 | 7695019b | Gerd Hoffmann | } |
238 | 7695019b | Gerd Hoffmann | } else {
|
239 | c64f27d4 | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size");
|
240 | 7695019b | Gerd Hoffmann | return -1; |
241 | 7695019b | Gerd Hoffmann | } |
242 | 7695019b | Gerd Hoffmann | return 0; |
243 | 7695019b | Gerd Hoffmann | } |
244 | 7695019b | Gerd Hoffmann | |
245 | d3f24367 | Kevin Wolf | /*
|
246 | d3f24367 | Kevin Wolf | * Sets the value of a parameter in a given option list. The parsing of the
|
247 | d3f24367 | Kevin Wolf | * value depends on the type of option:
|
248 | d3f24367 | Kevin Wolf | *
|
249 | d3f24367 | Kevin Wolf | * OPT_FLAG (uses value.n):
|
250 | d3f24367 | Kevin Wolf | * If no value is given, the flag is set to 1.
|
251 | d3f24367 | Kevin Wolf | * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
|
252 | d3f24367 | Kevin Wolf | *
|
253 | d3f24367 | Kevin Wolf | * OPT_STRING (uses value.s):
|
254 | d3f24367 | Kevin Wolf | * value is strdup()ed and assigned as option value
|
255 | d3f24367 | Kevin Wolf | *
|
256 | d3f24367 | Kevin Wolf | * OPT_SIZE (uses value.n):
|
257 | d3f24367 | Kevin Wolf | * The value is converted to an integer. Suffixes for kilobytes etc. are
|
258 | d3f24367 | Kevin Wolf | * allowed (powers of 1024).
|
259 | d3f24367 | Kevin Wolf | *
|
260 | d3f24367 | Kevin Wolf | * Returns 0 on succes, -1 in error cases
|
261 | d3f24367 | Kevin Wolf | */
|
262 | d3f24367 | Kevin Wolf | int set_option_parameter(QEMUOptionParameter *list, const char *name, |
263 | d3f24367 | Kevin Wolf | const char *value) |
264 | d3f24367 | Kevin Wolf | { |
265 | f02b77c9 | M. Mohan Kumar | bool flag;
|
266 | 67b1355b | Gerd Hoffmann | |
267 | d3f24367 | Kevin Wolf | // Find a matching parameter
|
268 | d3f24367 | Kevin Wolf | list = get_option_parameter(list, name); |
269 | d3f24367 | Kevin Wolf | if (list == NULL) { |
270 | d3f24367 | Kevin Wolf | fprintf(stderr, "Unknown option '%s'\n", name);
|
271 | d3f24367 | Kevin Wolf | return -1; |
272 | d3f24367 | Kevin Wolf | } |
273 | d3f24367 | Kevin Wolf | |
274 | d3f24367 | Kevin Wolf | // Process parameter
|
275 | d3f24367 | Kevin Wolf | switch (list->type) {
|
276 | d3f24367 | Kevin Wolf | case OPT_FLAG:
|
277 | 3df04ac3 | Mark McLoughlin | if (parse_option_bool(name, value, &flag) == -1) |
278 | 67b1355b | Gerd Hoffmann | return -1; |
279 | 67b1355b | Gerd Hoffmann | list->value.n = flag; |
280 | d3f24367 | Kevin Wolf | break;
|
281 | d3f24367 | Kevin Wolf | |
282 | d3f24367 | Kevin Wolf | case OPT_STRING:
|
283 | d3f24367 | Kevin Wolf | if (value != NULL) { |
284 | 7267c094 | Anthony Liguori | list->value.s = g_strdup(value); |
285 | d3f24367 | Kevin Wolf | } else {
|
286 | d3f24367 | Kevin Wolf | fprintf(stderr, "Option '%s' needs a parameter\n", name);
|
287 | d3f24367 | Kevin Wolf | return -1; |
288 | d3f24367 | Kevin Wolf | } |
289 | d3f24367 | Kevin Wolf | break;
|
290 | d3f24367 | Kevin Wolf | |
291 | d3f24367 | Kevin Wolf | case OPT_SIZE:
|
292 | 3df04ac3 | Mark McLoughlin | if (parse_option_size(name, value, &list->value.n) == -1) |
293 | d3f24367 | Kevin Wolf | return -1; |
294 | d3f24367 | Kevin Wolf | break;
|
295 | 7695019b | Gerd Hoffmann | |
296 | d3f24367 | Kevin Wolf | default:
|
297 | d3f24367 | Kevin Wolf | fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
|
298 | d3f24367 | Kevin Wolf | return -1; |
299 | d3f24367 | Kevin Wolf | } |
300 | d3f24367 | Kevin Wolf | |
301 | d3f24367 | Kevin Wolf | return 0; |
302 | d3f24367 | Kevin Wolf | } |
303 | d3f24367 | Kevin Wolf | |
304 | d3f24367 | Kevin Wolf | /*
|
305 | d3f24367 | Kevin Wolf | * Sets the given parameter to an integer instead of a string.
|
306 | d3f24367 | Kevin Wolf | * This function cannot be used to set string options.
|
307 | d3f24367 | Kevin Wolf | *
|
308 | d3f24367 | Kevin Wolf | * Returns 0 on success, -1 in error cases
|
309 | d3f24367 | Kevin Wolf | */
|
310 | d3f24367 | Kevin Wolf | int set_option_parameter_int(QEMUOptionParameter *list, const char *name, |
311 | d3f24367 | Kevin Wolf | uint64_t value) |
312 | d3f24367 | Kevin Wolf | { |
313 | d3f24367 | Kevin Wolf | // Find a matching parameter
|
314 | d3f24367 | Kevin Wolf | list = get_option_parameter(list, name); |
315 | d3f24367 | Kevin Wolf | if (list == NULL) { |
316 | d3f24367 | Kevin Wolf | fprintf(stderr, "Unknown option '%s'\n", name);
|
317 | d3f24367 | Kevin Wolf | return -1; |
318 | d3f24367 | Kevin Wolf | } |
319 | d3f24367 | Kevin Wolf | |
320 | d3f24367 | Kevin Wolf | // Process parameter
|
321 | d3f24367 | Kevin Wolf | switch (list->type) {
|
322 | d3f24367 | Kevin Wolf | case OPT_FLAG:
|
323 | d3f24367 | Kevin Wolf | case OPT_NUMBER:
|
324 | d3f24367 | Kevin Wolf | case OPT_SIZE:
|
325 | d3f24367 | Kevin Wolf | list->value.n = value; |
326 | d3f24367 | Kevin Wolf | break;
|
327 | d3f24367 | Kevin Wolf | |
328 | d3f24367 | Kevin Wolf | default:
|
329 | d3f24367 | Kevin Wolf | return -1; |
330 | d3f24367 | Kevin Wolf | } |
331 | d3f24367 | Kevin Wolf | |
332 | d3f24367 | Kevin Wolf | return 0; |
333 | d3f24367 | Kevin Wolf | } |
334 | d3f24367 | Kevin Wolf | |
335 | d3f24367 | Kevin Wolf | /*
|
336 | d3f24367 | Kevin Wolf | * Frees a option list. If it contains strings, the strings are freed as well.
|
337 | d3f24367 | Kevin Wolf | */
|
338 | d3f24367 | Kevin Wolf | void free_option_parameters(QEMUOptionParameter *list)
|
339 | d3f24367 | Kevin Wolf | { |
340 | d3f24367 | Kevin Wolf | QEMUOptionParameter *cur = list; |
341 | d3f24367 | Kevin Wolf | |
342 | d3f24367 | Kevin Wolf | while (cur && cur->name) {
|
343 | d3f24367 | Kevin Wolf | if (cur->type == OPT_STRING) {
|
344 | 7267c094 | Anthony Liguori | g_free(cur->value.s); |
345 | d3f24367 | Kevin Wolf | } |
346 | d3f24367 | Kevin Wolf | cur++; |
347 | d3f24367 | Kevin Wolf | } |
348 | d3f24367 | Kevin Wolf | |
349 | 7267c094 | Anthony Liguori | g_free(list); |
350 | d3f24367 | Kevin Wolf | } |
351 | d3f24367 | Kevin Wolf | |
352 | d3f24367 | Kevin Wolf | /*
|
353 | b50cbabc | MORITA Kazutaka | * Count valid options in list
|
354 | b50cbabc | MORITA Kazutaka | */
|
355 | b50cbabc | MORITA Kazutaka | static size_t count_option_parameters(QEMUOptionParameter *list)
|
356 | b50cbabc | MORITA Kazutaka | { |
357 | b50cbabc | MORITA Kazutaka | size_t num_options = 0;
|
358 | b50cbabc | MORITA Kazutaka | |
359 | b50cbabc | MORITA Kazutaka | while (list && list->name) {
|
360 | b50cbabc | MORITA Kazutaka | num_options++; |
361 | b50cbabc | MORITA Kazutaka | list++; |
362 | b50cbabc | MORITA Kazutaka | } |
363 | b50cbabc | MORITA Kazutaka | |
364 | b50cbabc | MORITA Kazutaka | return num_options;
|
365 | b50cbabc | MORITA Kazutaka | } |
366 | b50cbabc | MORITA Kazutaka | |
367 | b50cbabc | MORITA Kazutaka | /*
|
368 | b50cbabc | MORITA Kazutaka | * Append an option list (list) to an option list (dest).
|
369 | b50cbabc | MORITA Kazutaka | *
|
370 | b50cbabc | MORITA Kazutaka | * If dest is NULL, a new copy of list is created.
|
371 | b50cbabc | MORITA Kazutaka | *
|
372 | b50cbabc | MORITA Kazutaka | * Returns a pointer to the first element of dest (or the newly allocated copy)
|
373 | b50cbabc | MORITA Kazutaka | */
|
374 | b50cbabc | MORITA Kazutaka | QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest, |
375 | b50cbabc | MORITA Kazutaka | QEMUOptionParameter *list) |
376 | b50cbabc | MORITA Kazutaka | { |
377 | b50cbabc | MORITA Kazutaka | size_t num_options, num_dest_options; |
378 | b50cbabc | MORITA Kazutaka | |
379 | b50cbabc | MORITA Kazutaka | num_options = count_option_parameters(dest); |
380 | b50cbabc | MORITA Kazutaka | num_dest_options = num_options; |
381 | b50cbabc | MORITA Kazutaka | |
382 | b50cbabc | MORITA Kazutaka | num_options += count_option_parameters(list); |
383 | b50cbabc | MORITA Kazutaka | |
384 | 7267c094 | Anthony Liguori | dest = g_realloc(dest, (num_options + 1) * sizeof(QEMUOptionParameter)); |
385 | bd69fe84 | Kevin Wolf | dest[num_dest_options].name = NULL;
|
386 | b50cbabc | MORITA Kazutaka | |
387 | b50cbabc | MORITA Kazutaka | while (list && list->name) {
|
388 | b50cbabc | MORITA Kazutaka | if (get_option_parameter(dest, list->name) == NULL) { |
389 | b50cbabc | MORITA Kazutaka | dest[num_dest_options++] = *list; |
390 | b50cbabc | MORITA Kazutaka | dest[num_dest_options].name = NULL;
|
391 | b50cbabc | MORITA Kazutaka | } |
392 | b50cbabc | MORITA Kazutaka | list++; |
393 | b50cbabc | MORITA Kazutaka | } |
394 | b50cbabc | MORITA Kazutaka | |
395 | b50cbabc | MORITA Kazutaka | return dest;
|
396 | b50cbabc | MORITA Kazutaka | } |
397 | b50cbabc | MORITA Kazutaka | |
398 | b50cbabc | MORITA Kazutaka | /*
|
399 | d3f24367 | Kevin Wolf | * Parses a parameter string (param) into an option list (dest).
|
400 | d3f24367 | Kevin Wolf | *
|
401 | 0e72e753 | Stefan Hajnoczi | * list is the template option list. If dest is NULL, a new copy of list is
|
402 | 0e72e753 | Stefan Hajnoczi | * created. If list is NULL, this function fails.
|
403 | d3f24367 | Kevin Wolf | *
|
404 | d3f24367 | Kevin Wolf | * A parameter string consists of one or more parameters, separated by commas.
|
405 | d3f24367 | Kevin Wolf | * Each parameter consists of its name and possibly of a value. In the latter
|
406 | d3f24367 | Kevin Wolf | * case, the value is delimited by an = character. To specify a value which
|
407 | d3f24367 | Kevin Wolf | * contains commas, double each comma so it won't be recognized as the end of
|
408 | d3f24367 | Kevin Wolf | * the parameter.
|
409 | d3f24367 | Kevin Wolf | *
|
410 | d3f24367 | Kevin Wolf | * For more details of the parsing see above.
|
411 | d3f24367 | Kevin Wolf | *
|
412 | d3f24367 | Kevin Wolf | * Returns a pointer to the first element of dest (or the newly allocated copy)
|
413 | d3f24367 | Kevin Wolf | * or NULL in error cases
|
414 | d3f24367 | Kevin Wolf | */
|
415 | d3f24367 | Kevin Wolf | QEMUOptionParameter *parse_option_parameters(const char *param, |
416 | d3f24367 | Kevin Wolf | QEMUOptionParameter *list, QEMUOptionParameter *dest) |
417 | d3f24367 | Kevin Wolf | { |
418 | d3f24367 | Kevin Wolf | QEMUOptionParameter *allocated = NULL;
|
419 | d3f24367 | Kevin Wolf | char name[256]; |
420 | d3f24367 | Kevin Wolf | char value[256]; |
421 | d3f24367 | Kevin Wolf | char *param_delim, *value_delim;
|
422 | d3f24367 | Kevin Wolf | char next_delim;
|
423 | d3f24367 | Kevin Wolf | |
424 | d3f24367 | Kevin Wolf | if (list == NULL) { |
425 | d3f24367 | Kevin Wolf | return NULL; |
426 | d3f24367 | Kevin Wolf | } |
427 | d3f24367 | Kevin Wolf | |
428 | d3f24367 | Kevin Wolf | if (dest == NULL) { |
429 | 898c257b | Stefan Hajnoczi | dest = allocated = append_option_parameters(NULL, list);
|
430 | d3f24367 | Kevin Wolf | } |
431 | d3f24367 | Kevin Wolf | |
432 | d3f24367 | Kevin Wolf | while (*param) {
|
433 | d3f24367 | Kevin Wolf | |
434 | d3f24367 | Kevin Wolf | // Find parameter name and value in the string
|
435 | d3f24367 | Kevin Wolf | param_delim = strchr(param, ',');
|
436 | d3f24367 | Kevin Wolf | value_delim = strchr(param, '=');
|
437 | d3f24367 | Kevin Wolf | |
438 | d3f24367 | Kevin Wolf | if (value_delim && (value_delim < param_delim || !param_delim)) {
|
439 | d3f24367 | Kevin Wolf | next_delim = '=';
|
440 | d3f24367 | Kevin Wolf | } else {
|
441 | d3f24367 | Kevin Wolf | next_delim = ',';
|
442 | d3f24367 | Kevin Wolf | value_delim = NULL;
|
443 | d3f24367 | Kevin Wolf | } |
444 | d3f24367 | Kevin Wolf | |
445 | d3f24367 | Kevin Wolf | param = get_opt_name(name, sizeof(name), param, next_delim);
|
446 | d3f24367 | Kevin Wolf | if (value_delim) {
|
447 | d3f24367 | Kevin Wolf | param = get_opt_value(value, sizeof(value), param + 1); |
448 | d3f24367 | Kevin Wolf | } |
449 | d3f24367 | Kevin Wolf | if (*param != '\0') { |
450 | d3f24367 | Kevin Wolf | param++; |
451 | d3f24367 | Kevin Wolf | } |
452 | d3f24367 | Kevin Wolf | |
453 | d3f24367 | Kevin Wolf | // Set the parameter
|
454 | d3f24367 | Kevin Wolf | if (set_option_parameter(dest, name, value_delim ? value : NULL)) { |
455 | d3f24367 | Kevin Wolf | goto fail;
|
456 | d3f24367 | Kevin Wolf | } |
457 | d3f24367 | Kevin Wolf | } |
458 | d3f24367 | Kevin Wolf | |
459 | d3f24367 | Kevin Wolf | return dest;
|
460 | d3f24367 | Kevin Wolf | |
461 | d3f24367 | Kevin Wolf | fail:
|
462 | d3f24367 | Kevin Wolf | // Only free the list if it was newly allocated
|
463 | d3f24367 | Kevin Wolf | free_option_parameters(allocated); |
464 | d3f24367 | Kevin Wolf | return NULL; |
465 | d3f24367 | Kevin Wolf | } |
466 | d3f24367 | Kevin Wolf | |
467 | d3f24367 | Kevin Wolf | /*
|
468 | d3f24367 | Kevin Wolf | * Prints all options of a list that have a value to stdout
|
469 | d3f24367 | Kevin Wolf | */
|
470 | d3f24367 | Kevin Wolf | void print_option_parameters(QEMUOptionParameter *list)
|
471 | d3f24367 | Kevin Wolf | { |
472 | d3f24367 | Kevin Wolf | while (list && list->name) {
|
473 | d3f24367 | Kevin Wolf | switch (list->type) {
|
474 | d3f24367 | Kevin Wolf | case OPT_STRING:
|
475 | d3f24367 | Kevin Wolf | if (list->value.s != NULL) { |
476 | d3f24367 | Kevin Wolf | printf("%s='%s' ", list->name, list->value.s);
|
477 | d3f24367 | Kevin Wolf | } |
478 | d3f24367 | Kevin Wolf | break;
|
479 | d3f24367 | Kevin Wolf | case OPT_FLAG:
|
480 | d3f24367 | Kevin Wolf | printf("%s=%s ", list->name, list->value.n ? "on" : "off"); |
481 | d3f24367 | Kevin Wolf | break;
|
482 | d3f24367 | Kevin Wolf | case OPT_SIZE:
|
483 | d3f24367 | Kevin Wolf | case OPT_NUMBER:
|
484 | d3f24367 | Kevin Wolf | printf("%s=%" PRId64 " ", list->name, list->value.n); |
485 | d3f24367 | Kevin Wolf | break;
|
486 | d3f24367 | Kevin Wolf | default:
|
487 | 07f35073 | Dong Xu Wang | printf("%s=(unknown type) ", list->name);
|
488 | d3f24367 | Kevin Wolf | break;
|
489 | d3f24367 | Kevin Wolf | } |
490 | d3f24367 | Kevin Wolf | list++; |
491 | d3f24367 | Kevin Wolf | } |
492 | d3f24367 | Kevin Wolf | } |
493 | db08adf5 | Kevin Wolf | |
494 | db08adf5 | Kevin Wolf | /*
|
495 | db08adf5 | Kevin Wolf | * Prints an overview of all available options
|
496 | db08adf5 | Kevin Wolf | */
|
497 | db08adf5 | Kevin Wolf | void print_option_help(QEMUOptionParameter *list)
|
498 | db08adf5 | Kevin Wolf | { |
499 | db08adf5 | Kevin Wolf | printf("Supported options:\n");
|
500 | db08adf5 | Kevin Wolf | while (list && list->name) {
|
501 | db08adf5 | Kevin Wolf | printf("%-16s %s\n", list->name,
|
502 | db08adf5 | Kevin Wolf | list->help ? list->help : "No description available");
|
503 | db08adf5 | Kevin Wolf | list++; |
504 | db08adf5 | Kevin Wolf | } |
505 | db08adf5 | Kevin Wolf | } |
506 | e27c88fe | Gerd Hoffmann | |
507 | e27c88fe | Gerd Hoffmann | /* ------------------------------------------------------------------ */
|
508 | e27c88fe | Gerd Hoffmann | |
509 | e27c88fe | Gerd Hoffmann | struct QemuOpt {
|
510 | e27c88fe | Gerd Hoffmann | const char *name; |
511 | e27c88fe | Gerd Hoffmann | const char *str; |
512 | e27c88fe | Gerd Hoffmann | |
513 | 238431a9 | Blue Swirl | const QemuOptDesc *desc;
|
514 | e27c88fe | Gerd Hoffmann | union {
|
515 | f02b77c9 | M. Mohan Kumar | bool boolean;
|
516 | e27c88fe | Gerd Hoffmann | uint64_t uint; |
517 | e27c88fe | Gerd Hoffmann | } value; |
518 | e27c88fe | Gerd Hoffmann | |
519 | e27c88fe | Gerd Hoffmann | QemuOpts *opts; |
520 | 72cf2d4f | Blue Swirl | QTAILQ_ENTRY(QemuOpt) next; |
521 | e27c88fe | Gerd Hoffmann | }; |
522 | e27c88fe | Gerd Hoffmann | |
523 | e27c88fe | Gerd Hoffmann | struct QemuOpts {
|
524 | b09417b9 | Jan Kiszka | char *id;
|
525 | e27c88fe | Gerd Hoffmann | QemuOptsList *list; |
526 | 827b0813 | Markus Armbruster | Location loc; |
527 | dc9ca4ba | Mark McLoughlin | QTAILQ_HEAD(QemuOptHead, QemuOpt) head; |
528 | 72cf2d4f | Blue Swirl | QTAILQ_ENTRY(QemuOpts) next; |
529 | e27c88fe | Gerd Hoffmann | }; |
530 | e27c88fe | Gerd Hoffmann | |
531 | e27c88fe | Gerd Hoffmann | static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name) |
532 | e27c88fe | Gerd Hoffmann | { |
533 | e27c88fe | Gerd Hoffmann | QemuOpt *opt; |
534 | e27c88fe | Gerd Hoffmann | |
535 | dc9ca4ba | Mark McLoughlin | QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) { |
536 | e27c88fe | Gerd Hoffmann | if (strcmp(opt->name, name) != 0) |
537 | e27c88fe | Gerd Hoffmann | continue;
|
538 | e27c88fe | Gerd Hoffmann | return opt;
|
539 | e27c88fe | Gerd Hoffmann | } |
540 | e27c88fe | Gerd Hoffmann | return NULL; |
541 | e27c88fe | Gerd Hoffmann | } |
542 | e27c88fe | Gerd Hoffmann | |
543 | e27c88fe | Gerd Hoffmann | const char *qemu_opt_get(QemuOpts *opts, const char *name) |
544 | e27c88fe | Gerd Hoffmann | { |
545 | e27c88fe | Gerd Hoffmann | QemuOpt *opt = qemu_opt_find(opts, name); |
546 | e27c88fe | Gerd Hoffmann | return opt ? opt->str : NULL; |
547 | e27c88fe | Gerd Hoffmann | } |
548 | e27c88fe | Gerd Hoffmann | |
549 | f02b77c9 | M. Mohan Kumar | bool qemu_opt_get_bool(QemuOpts *opts, const char *name, bool defval) |
550 | e27c88fe | Gerd Hoffmann | { |
551 | e27c88fe | Gerd Hoffmann | QemuOpt *opt = qemu_opt_find(opts, name); |
552 | e27c88fe | Gerd Hoffmann | |
553 | e27c88fe | Gerd Hoffmann | if (opt == NULL) |
554 | e27c88fe | Gerd Hoffmann | return defval;
|
555 | e27c88fe | Gerd Hoffmann | assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL); |
556 | 1f5c1775 | Juan Quintela | return opt->value.boolean;
|
557 | e27c88fe | Gerd Hoffmann | } |
558 | e27c88fe | Gerd Hoffmann | |
559 | e27c88fe | Gerd Hoffmann | uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval) |
560 | e27c88fe | Gerd Hoffmann | { |
561 | e27c88fe | Gerd Hoffmann | QemuOpt *opt = qemu_opt_find(opts, name); |
562 | e27c88fe | Gerd Hoffmann | |
563 | e27c88fe | Gerd Hoffmann | if (opt == NULL) |
564 | e27c88fe | Gerd Hoffmann | return defval;
|
565 | e27c88fe | Gerd Hoffmann | assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER); |
566 | e27c88fe | Gerd Hoffmann | return opt->value.uint;
|
567 | e27c88fe | Gerd Hoffmann | } |
568 | e27c88fe | Gerd Hoffmann | |
569 | e27c88fe | Gerd Hoffmann | uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval) |
570 | e27c88fe | Gerd Hoffmann | { |
571 | e27c88fe | Gerd Hoffmann | QemuOpt *opt = qemu_opt_find(opts, name); |
572 | e27c88fe | Gerd Hoffmann | |
573 | e27c88fe | Gerd Hoffmann | if (opt == NULL) |
574 | e27c88fe | Gerd Hoffmann | return defval;
|
575 | e27c88fe | Gerd Hoffmann | assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE); |
576 | e27c88fe | Gerd Hoffmann | return opt->value.uint;
|
577 | e27c88fe | Gerd Hoffmann | } |
578 | e27c88fe | Gerd Hoffmann | |
579 | e27c88fe | Gerd Hoffmann | static int qemu_opt_parse(QemuOpt *opt) |
580 | e27c88fe | Gerd Hoffmann | { |
581 | e27c88fe | Gerd Hoffmann | if (opt->desc == NULL) |
582 | e27c88fe | Gerd Hoffmann | return 0; |
583 | e27c88fe | Gerd Hoffmann | switch (opt->desc->type) {
|
584 | e27c88fe | Gerd Hoffmann | case QEMU_OPT_STRING:
|
585 | e27c88fe | Gerd Hoffmann | /* nothing */
|
586 | e27c88fe | Gerd Hoffmann | return 0; |
587 | e27c88fe | Gerd Hoffmann | case QEMU_OPT_BOOL:
|
588 | 1f5c1775 | Juan Quintela | return parse_option_bool(opt->name, opt->str, &opt->value.boolean);
|
589 | e27c88fe | Gerd Hoffmann | case QEMU_OPT_NUMBER:
|
590 | e27c88fe | Gerd Hoffmann | return parse_option_number(opt->name, opt->str, &opt->value.uint);
|
591 | e27c88fe | Gerd Hoffmann | case QEMU_OPT_SIZE:
|
592 | e27c88fe | Gerd Hoffmann | return parse_option_size(opt->name, opt->str, &opt->value.uint);
|
593 | e27c88fe | Gerd Hoffmann | default:
|
594 | e27c88fe | Gerd Hoffmann | abort(); |
595 | e27c88fe | Gerd Hoffmann | } |
596 | e27c88fe | Gerd Hoffmann | } |
597 | e27c88fe | Gerd Hoffmann | |
598 | e27c88fe | Gerd Hoffmann | static void qemu_opt_del(QemuOpt *opt) |
599 | e27c88fe | Gerd Hoffmann | { |
600 | 72cf2d4f | Blue Swirl | QTAILQ_REMOVE(&opt->opts->head, opt, next); |
601 | 7267c094 | Anthony Liguori | g_free((/* !const */ char*)opt->name); |
602 | 7267c094 | Anthony Liguori | g_free((/* !const */ char*)opt->str); |
603 | 7267c094 | Anthony Liguori | g_free(opt); |
604 | e27c88fe | Gerd Hoffmann | } |
605 | e27c88fe | Gerd Hoffmann | |
606 | 4f6dd9af | Jan Kiszka | static int opt_set(QemuOpts *opts, const char *name, const char *value, |
607 | 4f6dd9af | Jan Kiszka | bool prepend)
|
608 | e27c88fe | Gerd Hoffmann | { |
609 | e27c88fe | Gerd Hoffmann | QemuOpt *opt; |
610 | 238431a9 | Blue Swirl | const QemuOptDesc *desc = opts->list->desc;
|
611 | dc9ca4ba | Mark McLoughlin | int i;
|
612 | e27c88fe | Gerd Hoffmann | |
613 | dc9ca4ba | Mark McLoughlin | for (i = 0; desc[i].name != NULL; i++) { |
614 | dc9ca4ba | Mark McLoughlin | if (strcmp(desc[i].name, name) == 0) { |
615 | dc9ca4ba | Mark McLoughlin | break;
|
616 | e27c88fe | Gerd Hoffmann | } |
617 | dc9ca4ba | Mark McLoughlin | } |
618 | dc9ca4ba | Mark McLoughlin | if (desc[i].name == NULL) { |
619 | dc9ca4ba | Mark McLoughlin | if (i == 0) { |
620 | dc9ca4ba | Mark McLoughlin | /* empty list -> allow any */;
|
621 | dc9ca4ba | Mark McLoughlin | } else {
|
622 | c64f27d4 | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER, name); |
623 | dc9ca4ba | Mark McLoughlin | return -1; |
624 | e27c88fe | Gerd Hoffmann | } |
625 | e27c88fe | Gerd Hoffmann | } |
626 | dc9ca4ba | Mark McLoughlin | |
627 | 7267c094 | Anthony Liguori | opt = g_malloc0(sizeof(*opt));
|
628 | 7267c094 | Anthony Liguori | opt->name = g_strdup(name); |
629 | dc9ca4ba | Mark McLoughlin | opt->opts = opts; |
630 | 4f6dd9af | Jan Kiszka | if (prepend) {
|
631 | 4f6dd9af | Jan Kiszka | QTAILQ_INSERT_HEAD(&opts->head, opt, next); |
632 | 4f6dd9af | Jan Kiszka | } else {
|
633 | 4f6dd9af | Jan Kiszka | QTAILQ_INSERT_TAIL(&opts->head, opt, next); |
634 | 4f6dd9af | Jan Kiszka | } |
635 | dc9ca4ba | Mark McLoughlin | if (desc[i].name != NULL) { |
636 | dc9ca4ba | Mark McLoughlin | opt->desc = desc+i; |
637 | dc9ca4ba | Mark McLoughlin | } |
638 | e27c88fe | Gerd Hoffmann | if (value) {
|
639 | 7267c094 | Anthony Liguori | opt->str = g_strdup(value); |
640 | e27c88fe | Gerd Hoffmann | } |
641 | e27c88fe | Gerd Hoffmann | if (qemu_opt_parse(opt) < 0) { |
642 | e27c88fe | Gerd Hoffmann | qemu_opt_del(opt); |
643 | e27c88fe | Gerd Hoffmann | return -1; |
644 | e27c88fe | Gerd Hoffmann | } |
645 | e27c88fe | Gerd Hoffmann | return 0; |
646 | e27c88fe | Gerd Hoffmann | } |
647 | e27c88fe | Gerd Hoffmann | |
648 | 4f6dd9af | Jan Kiszka | int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) |
649 | 4f6dd9af | Jan Kiszka | { |
650 | 4f6dd9af | Jan Kiszka | return opt_set(opts, name, value, false); |
651 | 4f6dd9af | Jan Kiszka | } |
652 | 4f6dd9af | Jan Kiszka | |
653 | f02b77c9 | M. Mohan Kumar | int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val) |
654 | f02b77c9 | M. Mohan Kumar | { |
655 | f02b77c9 | M. Mohan Kumar | QemuOpt *opt; |
656 | f02b77c9 | M. Mohan Kumar | const QemuOptDesc *desc = opts->list->desc;
|
657 | f02b77c9 | M. Mohan Kumar | int i;
|
658 | f02b77c9 | M. Mohan Kumar | |
659 | f02b77c9 | M. Mohan Kumar | for (i = 0; desc[i].name != NULL; i++) { |
660 | f02b77c9 | M. Mohan Kumar | if (strcmp(desc[i].name, name) == 0) { |
661 | f02b77c9 | M. Mohan Kumar | break;
|
662 | f02b77c9 | M. Mohan Kumar | } |
663 | f02b77c9 | M. Mohan Kumar | } |
664 | f02b77c9 | M. Mohan Kumar | if (desc[i].name == NULL) { |
665 | f02b77c9 | M. Mohan Kumar | if (i == 0) { |
666 | f02b77c9 | M. Mohan Kumar | /* empty list -> allow any */;
|
667 | f02b77c9 | M. Mohan Kumar | } else {
|
668 | f02b77c9 | M. Mohan Kumar | qerror_report(QERR_INVALID_PARAMETER, name); |
669 | f02b77c9 | M. Mohan Kumar | return -1; |
670 | f02b77c9 | M. Mohan Kumar | } |
671 | f02b77c9 | M. Mohan Kumar | } |
672 | f02b77c9 | M. Mohan Kumar | |
673 | f02b77c9 | M. Mohan Kumar | opt = g_malloc0(sizeof(*opt));
|
674 | f02b77c9 | M. Mohan Kumar | opt->name = g_strdup(name); |
675 | f02b77c9 | M. Mohan Kumar | opt->opts = opts; |
676 | f02b77c9 | M. Mohan Kumar | QTAILQ_INSERT_TAIL(&opts->head, opt, next); |
677 | f02b77c9 | M. Mohan Kumar | if (desc[i].name != NULL) { |
678 | f02b77c9 | M. Mohan Kumar | opt->desc = desc+i; |
679 | f02b77c9 | M. Mohan Kumar | } |
680 | f02b77c9 | M. Mohan Kumar | opt->value.boolean = !!val; |
681 | f02b77c9 | M. Mohan Kumar | return 0; |
682 | f02b77c9 | M. Mohan Kumar | } |
683 | f02b77c9 | M. Mohan Kumar | |
684 | 48026075 | Gerd Hoffmann | int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque, |
685 | 48026075 | Gerd Hoffmann | int abort_on_failure)
|
686 | 48026075 | Gerd Hoffmann | { |
687 | 48026075 | Gerd Hoffmann | QemuOpt *opt; |
688 | 48026075 | Gerd Hoffmann | int rc = 0; |
689 | 48026075 | Gerd Hoffmann | |
690 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(opt, &opts->head, next) { |
691 | 48026075 | Gerd Hoffmann | rc = func(opt->name, opt->str, opaque); |
692 | 48026075 | Gerd Hoffmann | if (abort_on_failure && rc != 0) |
693 | 48026075 | Gerd Hoffmann | break;
|
694 | 48026075 | Gerd Hoffmann | } |
695 | 48026075 | Gerd Hoffmann | return rc;
|
696 | 48026075 | Gerd Hoffmann | } |
697 | 48026075 | Gerd Hoffmann | |
698 | e27c88fe | Gerd Hoffmann | QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id) |
699 | e27c88fe | Gerd Hoffmann | { |
700 | e27c88fe | Gerd Hoffmann | QemuOpts *opts; |
701 | e27c88fe | Gerd Hoffmann | |
702 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(opts, &list->head, next) { |
703 | e27c88fe | Gerd Hoffmann | if (!opts->id) {
|
704 | 4f6dd9af | Jan Kiszka | if (!id) {
|
705 | 4f6dd9af | Jan Kiszka | return opts;
|
706 | 4f6dd9af | Jan Kiszka | } |
707 | e27c88fe | Gerd Hoffmann | continue;
|
708 | e27c88fe | Gerd Hoffmann | } |
709 | e27c88fe | Gerd Hoffmann | if (strcmp(opts->id, id) != 0) { |
710 | e27c88fe | Gerd Hoffmann | continue;
|
711 | e27c88fe | Gerd Hoffmann | } |
712 | e27c88fe | Gerd Hoffmann | return opts;
|
713 | e27c88fe | Gerd Hoffmann | } |
714 | e27c88fe | Gerd Hoffmann | return NULL; |
715 | e27c88fe | Gerd Hoffmann | } |
716 | e27c88fe | Gerd Hoffmann | |
717 | b560a9ab | Markus Armbruster | static int id_wellformed(const char *id) |
718 | b560a9ab | Markus Armbruster | { |
719 | b560a9ab | Markus Armbruster | int i;
|
720 | b560a9ab | Markus Armbruster | |
721 | b560a9ab | Markus Armbruster | if (!qemu_isalpha(id[0])) { |
722 | b560a9ab | Markus Armbruster | return 0; |
723 | b560a9ab | Markus Armbruster | } |
724 | b560a9ab | Markus Armbruster | for (i = 1; id[i]; i++) { |
725 | b560a9ab | Markus Armbruster | if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) { |
726 | b560a9ab | Markus Armbruster | return 0; |
727 | b560a9ab | Markus Armbruster | } |
728 | b560a9ab | Markus Armbruster | } |
729 | b560a9ab | Markus Armbruster | return 1; |
730 | b560a9ab | Markus Armbruster | } |
731 | b560a9ab | Markus Armbruster | |
732 | e27c88fe | Gerd Hoffmann | QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists) |
733 | e27c88fe | Gerd Hoffmann | { |
734 | e27c88fe | Gerd Hoffmann | QemuOpts *opts = NULL;
|
735 | e27c88fe | Gerd Hoffmann | |
736 | e27c88fe | Gerd Hoffmann | if (id) {
|
737 | b560a9ab | Markus Armbruster | if (!id_wellformed(id)) {
|
738 | b560a9ab | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER_VALUE, "id", "an identifier"); |
739 | b560a9ab | Markus Armbruster | error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
|
740 | b560a9ab | Markus Armbruster | return NULL; |
741 | b560a9ab | Markus Armbruster | } |
742 | e27c88fe | Gerd Hoffmann | opts = qemu_opts_find(list, id); |
743 | e27c88fe | Gerd Hoffmann | if (opts != NULL) { |
744 | da93318a | Peter Maydell | if (fail_if_exists && !list->merge_lists) {
|
745 | 975b63a4 | Markus Armbruster | qerror_report(QERR_DUPLICATE_ID, id, list->name); |
746 | e27c88fe | Gerd Hoffmann | return NULL; |
747 | e27c88fe | Gerd Hoffmann | } else {
|
748 | e27c88fe | Gerd Hoffmann | return opts;
|
749 | e27c88fe | Gerd Hoffmann | } |
750 | e27c88fe | Gerd Hoffmann | } |
751 | da93318a | Peter Maydell | } else if (list->merge_lists) { |
752 | da93318a | Peter Maydell | opts = qemu_opts_find(list, NULL);
|
753 | da93318a | Peter Maydell | if (opts) {
|
754 | da93318a | Peter Maydell | return opts;
|
755 | da93318a | Peter Maydell | } |
756 | e27c88fe | Gerd Hoffmann | } |
757 | 7267c094 | Anthony Liguori | opts = g_malloc0(sizeof(*opts));
|
758 | e27c88fe | Gerd Hoffmann | if (id) {
|
759 | 7267c094 | Anthony Liguori | opts->id = g_strdup(id); |
760 | e27c88fe | Gerd Hoffmann | } |
761 | e27c88fe | Gerd Hoffmann | opts->list = list; |
762 | 827b0813 | Markus Armbruster | loc_save(&opts->loc); |
763 | 72cf2d4f | Blue Swirl | QTAILQ_INIT(&opts->head); |
764 | 72cf2d4f | Blue Swirl | QTAILQ_INSERT_TAIL(&list->head, opts, next); |
765 | e27c88fe | Gerd Hoffmann | return opts;
|
766 | e27c88fe | Gerd Hoffmann | } |
767 | e27c88fe | Gerd Hoffmann | |
768 | bb67ab02 | Markus Armbruster | void qemu_opts_reset(QemuOptsList *list)
|
769 | bb67ab02 | Markus Armbruster | { |
770 | bb67ab02 | Markus Armbruster | QemuOpts *opts, *next_opts; |
771 | bb67ab02 | Markus Armbruster | |
772 | bb67ab02 | Markus Armbruster | QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) { |
773 | bb67ab02 | Markus Armbruster | qemu_opts_del(opts); |
774 | bb67ab02 | Markus Armbruster | } |
775 | bb67ab02 | Markus Armbruster | } |
776 | bb67ab02 | Markus Armbruster | |
777 | 94ac7268 | Markus Armbruster | void qemu_opts_loc_restore(QemuOpts *opts)
|
778 | 94ac7268 | Markus Armbruster | { |
779 | 94ac7268 | Markus Armbruster | loc_restore(&opts->loc); |
780 | 94ac7268 | Markus Armbruster | } |
781 | 94ac7268 | Markus Armbruster | |
782 | e27c88fe | Gerd Hoffmann | int qemu_opts_set(QemuOptsList *list, const char *id, |
783 | e27c88fe | Gerd Hoffmann | const char *name, const char *value) |
784 | e27c88fe | Gerd Hoffmann | { |
785 | e27c88fe | Gerd Hoffmann | QemuOpts *opts; |
786 | e27c88fe | Gerd Hoffmann | |
787 | e27c88fe | Gerd Hoffmann | opts = qemu_opts_create(list, id, 1);
|
788 | e27c88fe | Gerd Hoffmann | if (opts == NULL) { |
789 | e27c88fe | Gerd Hoffmann | return -1; |
790 | e27c88fe | Gerd Hoffmann | } |
791 | e27c88fe | Gerd Hoffmann | return qemu_opt_set(opts, name, value);
|
792 | e27c88fe | Gerd Hoffmann | } |
793 | e27c88fe | Gerd Hoffmann | |
794 | 48026075 | Gerd Hoffmann | const char *qemu_opts_id(QemuOpts *opts) |
795 | 48026075 | Gerd Hoffmann | { |
796 | 48026075 | Gerd Hoffmann | return opts->id;
|
797 | 48026075 | Gerd Hoffmann | } |
798 | 48026075 | Gerd Hoffmann | |
799 | e27c88fe | Gerd Hoffmann | void qemu_opts_del(QemuOpts *opts)
|
800 | e27c88fe | Gerd Hoffmann | { |
801 | e27c88fe | Gerd Hoffmann | QemuOpt *opt; |
802 | e27c88fe | Gerd Hoffmann | |
803 | e27c88fe | Gerd Hoffmann | for (;;) {
|
804 | 72cf2d4f | Blue Swirl | opt = QTAILQ_FIRST(&opts->head); |
805 | e27c88fe | Gerd Hoffmann | if (opt == NULL) |
806 | e27c88fe | Gerd Hoffmann | break;
|
807 | e27c88fe | Gerd Hoffmann | qemu_opt_del(opt); |
808 | e27c88fe | Gerd Hoffmann | } |
809 | 72cf2d4f | Blue Swirl | QTAILQ_REMOVE(&opts->list->head, opts, next); |
810 | 7267c094 | Anthony Liguori | g_free(opts->id); |
811 | 7267c094 | Anthony Liguori | g_free(opts); |
812 | e27c88fe | Gerd Hoffmann | } |
813 | e27c88fe | Gerd Hoffmann | |
814 | e27c88fe | Gerd Hoffmann | int qemu_opts_print(QemuOpts *opts, void *dummy) |
815 | e27c88fe | Gerd Hoffmann | { |
816 | e27c88fe | Gerd Hoffmann | QemuOpt *opt; |
817 | e27c88fe | Gerd Hoffmann | |
818 | e27c88fe | Gerd Hoffmann | fprintf(stderr, "%s: %s:", opts->list->name,
|
819 | e27c88fe | Gerd Hoffmann | opts->id ? opts->id : "<noid>");
|
820 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(opt, &opts->head, next) { |
821 | e27c88fe | Gerd Hoffmann | fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
|
822 | e27c88fe | Gerd Hoffmann | } |
823 | e27c88fe | Gerd Hoffmann | fprintf(stderr, "\n");
|
824 | e27c88fe | Gerd Hoffmann | return 0; |
825 | e27c88fe | Gerd Hoffmann | } |
826 | e27c88fe | Gerd Hoffmann | |
827 | 4f6dd9af | Jan Kiszka | static int opts_do_parse(QemuOpts *opts, const char *params, |
828 | 4f6dd9af | Jan Kiszka | const char *firstname, bool prepend) |
829 | e27c88fe | Gerd Hoffmann | { |
830 | d318ff99 | Gerd Hoffmann | char option[128], value[1024]; |
831 | e27c88fe | Gerd Hoffmann | const char *p,*pe,*pc; |
832 | e27c88fe | Gerd Hoffmann | |
833 | 2cfa571f | Mark McLoughlin | for (p = params; *p != '\0'; p++) { |
834 | e27c88fe | Gerd Hoffmann | pe = strchr(p, '=');
|
835 | e27c88fe | Gerd Hoffmann | pc = strchr(p, ',');
|
836 | e27c88fe | Gerd Hoffmann | if (!pe || (pc && pc < pe)) {
|
837 | e27c88fe | Gerd Hoffmann | /* found "foo,more" */
|
838 | e27c88fe | Gerd Hoffmann | if (p == params && firstname) {
|
839 | e27c88fe | Gerd Hoffmann | /* implicitly named first option */
|
840 | e27c88fe | Gerd Hoffmann | pstrcpy(option, sizeof(option), firstname);
|
841 | e27c88fe | Gerd Hoffmann | p = get_opt_value(value, sizeof(value), p);
|
842 | e27c88fe | Gerd Hoffmann | } else {
|
843 | e27c88fe | Gerd Hoffmann | /* option without value, probably a flag */
|
844 | e27c88fe | Gerd Hoffmann | p = get_opt_name(option, sizeof(option), p, ','); |
845 | 96729cbd | Gerd Hoffmann | if (strncmp(option, "no", 2) == 0) { |
846 | e27c88fe | Gerd Hoffmann | memmove(option, option+2, strlen(option+2)+1); |
847 | e27c88fe | Gerd Hoffmann | pstrcpy(value, sizeof(value), "off"); |
848 | e27c88fe | Gerd Hoffmann | } else {
|
849 | e27c88fe | Gerd Hoffmann | pstrcpy(value, sizeof(value), "on"); |
850 | e27c88fe | Gerd Hoffmann | } |
851 | e27c88fe | Gerd Hoffmann | } |
852 | e27c88fe | Gerd Hoffmann | } else {
|
853 | e27c88fe | Gerd Hoffmann | /* found "foo=bar,more" */
|
854 | e27c88fe | Gerd Hoffmann | p = get_opt_name(option, sizeof(option), p, '='); |
855 | e27c88fe | Gerd Hoffmann | if (*p != '=') { |
856 | e27c88fe | Gerd Hoffmann | break;
|
857 | e27c88fe | Gerd Hoffmann | } |
858 | e27c88fe | Gerd Hoffmann | p++; |
859 | e27c88fe | Gerd Hoffmann | p = get_opt_value(value, sizeof(value), p);
|
860 | e27c88fe | Gerd Hoffmann | } |
861 | e27c88fe | Gerd Hoffmann | if (strcmp(option, "id") != 0) { |
862 | e27c88fe | Gerd Hoffmann | /* store and parse */
|
863 | 4f6dd9af | Jan Kiszka | if (opt_set(opts, option, value, prepend) == -1) { |
864 | 96729cbd | Gerd Hoffmann | return -1; |
865 | e27c88fe | Gerd Hoffmann | } |
866 | e27c88fe | Gerd Hoffmann | } |
867 | e27c88fe | Gerd Hoffmann | if (*p != ',') { |
868 | e27c88fe | Gerd Hoffmann | break;
|
869 | e27c88fe | Gerd Hoffmann | } |
870 | e27c88fe | Gerd Hoffmann | } |
871 | 96729cbd | Gerd Hoffmann | return 0; |
872 | 96729cbd | Gerd Hoffmann | } |
873 | 96729cbd | Gerd Hoffmann | |
874 | 4f6dd9af | Jan Kiszka | int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname) |
875 | 4f6dd9af | Jan Kiszka | { |
876 | 4f6dd9af | Jan Kiszka | return opts_do_parse(opts, params, firstname, false); |
877 | 4f6dd9af | Jan Kiszka | } |
878 | 4f6dd9af | Jan Kiszka | |
879 | 4f6dd9af | Jan Kiszka | static QemuOpts *opts_parse(QemuOptsList *list, const char *params, |
880 | 4f6dd9af | Jan Kiszka | int permit_abbrev, bool defaults) |
881 | 96729cbd | Gerd Hoffmann | { |
882 | 8212c64f | Markus Armbruster | const char *firstname; |
883 | d318ff99 | Gerd Hoffmann | char value[1024], *id = NULL; |
884 | 96729cbd | Gerd Hoffmann | const char *p; |
885 | 96729cbd | Gerd Hoffmann | QemuOpts *opts; |
886 | 96729cbd | Gerd Hoffmann | |
887 | 8212c64f | Markus Armbruster | assert(!permit_abbrev || list->implied_opt_name); |
888 | 8212c64f | Markus Armbruster | firstname = permit_abbrev ? list->implied_opt_name : NULL;
|
889 | 8212c64f | Markus Armbruster | |
890 | 96729cbd | Gerd Hoffmann | if (strncmp(params, "id=", 3) == 0) { |
891 | 96729cbd | Gerd Hoffmann | get_opt_value(value, sizeof(value), params+3); |
892 | d510c5cf | Jan Kiszka | id = value; |
893 | 96729cbd | Gerd Hoffmann | } else if ((p = strstr(params, ",id=")) != NULL) { |
894 | 96729cbd | Gerd Hoffmann | get_opt_value(value, sizeof(value), p+4); |
895 | d510c5cf | Jan Kiszka | id = value; |
896 | 96729cbd | Gerd Hoffmann | } |
897 | 4f6dd9af | Jan Kiszka | if (defaults) {
|
898 | 4f6dd9af | Jan Kiszka | if (!id && !QTAILQ_EMPTY(&list->head)) {
|
899 | 4f6dd9af | Jan Kiszka | opts = qemu_opts_find(list, NULL);
|
900 | 4f6dd9af | Jan Kiszka | } else {
|
901 | 4f6dd9af | Jan Kiszka | opts = qemu_opts_create(list, id, 0);
|
902 | 4f6dd9af | Jan Kiszka | } |
903 | 4f6dd9af | Jan Kiszka | } else {
|
904 | 4f6dd9af | Jan Kiszka | opts = qemu_opts_create(list, id, 1);
|
905 | 4f6dd9af | Jan Kiszka | } |
906 | 96729cbd | Gerd Hoffmann | if (opts == NULL) |
907 | 96729cbd | Gerd Hoffmann | return NULL; |
908 | 96729cbd | Gerd Hoffmann | |
909 | 4f6dd9af | Jan Kiszka | if (opts_do_parse(opts, params, firstname, defaults) != 0) { |
910 | 96729cbd | Gerd Hoffmann | qemu_opts_del(opts); |
911 | 96729cbd | Gerd Hoffmann | return NULL; |
912 | 96729cbd | Gerd Hoffmann | } |
913 | 96729cbd | Gerd Hoffmann | |
914 | e27c88fe | Gerd Hoffmann | return opts;
|
915 | e27c88fe | Gerd Hoffmann | } |
916 | e27c88fe | Gerd Hoffmann | |
917 | 4f6dd9af | Jan Kiszka | QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, |
918 | 4f6dd9af | Jan Kiszka | int permit_abbrev)
|
919 | 4f6dd9af | Jan Kiszka | { |
920 | 4f6dd9af | Jan Kiszka | return opts_parse(list, params, permit_abbrev, false); |
921 | 4f6dd9af | Jan Kiszka | } |
922 | 4f6dd9af | Jan Kiszka | |
923 | 4f6dd9af | Jan Kiszka | void qemu_opts_set_defaults(QemuOptsList *list, const char *params, |
924 | 4f6dd9af | Jan Kiszka | int permit_abbrev)
|
925 | 4f6dd9af | Jan Kiszka | { |
926 | 4f6dd9af | Jan Kiszka | QemuOpts *opts; |
927 | 4f6dd9af | Jan Kiszka | |
928 | 4f6dd9af | Jan Kiszka | opts = opts_parse(list, params, permit_abbrev, true);
|
929 | 4f6dd9af | Jan Kiszka | assert(opts); |
930 | 4f6dd9af | Jan Kiszka | } |
931 | 4f6dd9af | Jan Kiszka | |
932 | 01e7f188 | Markus Armbruster | static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque) |
933 | 01e7f188 | Markus Armbruster | { |
934 | 01e7f188 | Markus Armbruster | char buf[32]; |
935 | 01e7f188 | Markus Armbruster | const char *value; |
936 | 01e7f188 | Markus Armbruster | int n;
|
937 | 01e7f188 | Markus Armbruster | |
938 | 01e7f188 | Markus Armbruster | if (!strcmp(key, "id")) { |
939 | 01e7f188 | Markus Armbruster | return;
|
940 | 01e7f188 | Markus Armbruster | } |
941 | 01e7f188 | Markus Armbruster | |
942 | 01e7f188 | Markus Armbruster | switch (qobject_type(obj)) {
|
943 | 01e7f188 | Markus Armbruster | case QTYPE_QSTRING:
|
944 | 01e7f188 | Markus Armbruster | value = qstring_get_str(qobject_to_qstring(obj)); |
945 | 01e7f188 | Markus Armbruster | break;
|
946 | 01e7f188 | Markus Armbruster | case QTYPE_QINT:
|
947 | 01e7f188 | Markus Armbruster | n = snprintf(buf, sizeof(buf), "%" PRId64, |
948 | 01e7f188 | Markus Armbruster | qint_get_int(qobject_to_qint(obj))); |
949 | 01e7f188 | Markus Armbruster | assert(n < sizeof(buf));
|
950 | 01e7f188 | Markus Armbruster | value = buf; |
951 | 01e7f188 | Markus Armbruster | break;
|
952 | 01e7f188 | Markus Armbruster | case QTYPE_QFLOAT:
|
953 | 01e7f188 | Markus Armbruster | n = snprintf(buf, sizeof(buf), "%.17g", |
954 | 01e7f188 | Markus Armbruster | qfloat_get_double(qobject_to_qfloat(obj))); |
955 | 01e7f188 | Markus Armbruster | assert(n < sizeof(buf));
|
956 | 01e7f188 | Markus Armbruster | value = buf; |
957 | 01e7f188 | Markus Armbruster | break;
|
958 | 01e7f188 | Markus Armbruster | case QTYPE_QBOOL:
|
959 | d35215f8 | Blue Swirl | pstrcpy(buf, sizeof(buf),
|
960 | d35215f8 | Blue Swirl | qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off"); |
961 | 01e7f188 | Markus Armbruster | value = buf; |
962 | 01e7f188 | Markus Armbruster | break;
|
963 | 01e7f188 | Markus Armbruster | default:
|
964 | 01e7f188 | Markus Armbruster | return;
|
965 | 01e7f188 | Markus Armbruster | } |
966 | 01e7f188 | Markus Armbruster | qemu_opt_set(opaque, key, value); |
967 | 01e7f188 | Markus Armbruster | } |
968 | 01e7f188 | Markus Armbruster | |
969 | 01e7f188 | Markus Armbruster | /*
|
970 | 01e7f188 | Markus Armbruster | * Create QemuOpts from a QDict.
|
971 | 01e7f188 | Markus Armbruster | * Use value of key "id" as ID if it exists and is a QString.
|
972 | 01e7f188 | Markus Armbruster | * Only QStrings, QInts, QFloats and QBools are copied. Entries with
|
973 | 01e7f188 | Markus Armbruster | * other types are silently ignored.
|
974 | 01e7f188 | Markus Armbruster | */
|
975 | 01e7f188 | Markus Armbruster | QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict)
|
976 | 01e7f188 | Markus Armbruster | { |
977 | 01e7f188 | Markus Armbruster | QemuOpts *opts; |
978 | 01e7f188 | Markus Armbruster | |
979 | 01e7f188 | Markus Armbruster | opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1); |
980 | 01e7f188 | Markus Armbruster | if (opts == NULL) |
981 | 01e7f188 | Markus Armbruster | return NULL; |
982 | 01e7f188 | Markus Armbruster | |
983 | 01e7f188 | Markus Armbruster | qdict_iter(qdict, qemu_opts_from_qdict_1, opts); |
984 | 01e7f188 | Markus Armbruster | return opts;
|
985 | 01e7f188 | Markus Armbruster | } |
986 | 01e7f188 | Markus Armbruster | |
987 | 01e7f188 | Markus Armbruster | /*
|
988 | 01e7f188 | Markus Armbruster | * Convert from QemuOpts to QDict.
|
989 | 01e7f188 | Markus Armbruster | * The QDict values are of type QString.
|
990 | 01e7f188 | Markus Armbruster | * TODO We'll want to use types appropriate for opt->desc->type, but
|
991 | 01e7f188 | Markus Armbruster | * this is enough for now.
|
992 | 01e7f188 | Markus Armbruster | */
|
993 | 01e7f188 | Markus Armbruster | QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict) |
994 | 01e7f188 | Markus Armbruster | { |
995 | 01e7f188 | Markus Armbruster | QemuOpt *opt; |
996 | 01e7f188 | Markus Armbruster | QObject *val; |
997 | 01e7f188 | Markus Armbruster | |
998 | 01e7f188 | Markus Armbruster | if (!qdict) {
|
999 | 01e7f188 | Markus Armbruster | qdict = qdict_new(); |
1000 | 01e7f188 | Markus Armbruster | } |
1001 | 01e7f188 | Markus Armbruster | if (opts->id) {
|
1002 | 01e7f188 | Markus Armbruster | qdict_put(qdict, "id", qstring_from_str(opts->id));
|
1003 | 01e7f188 | Markus Armbruster | } |
1004 | 01e7f188 | Markus Armbruster | QTAILQ_FOREACH(opt, &opts->head, next) { |
1005 | 01e7f188 | Markus Armbruster | val = QOBJECT(qstring_from_str(opt->str)); |
1006 | 01e7f188 | Markus Armbruster | qdict_put_obj(qdict, opt->name, val); |
1007 | 01e7f188 | Markus Armbruster | } |
1008 | 01e7f188 | Markus Armbruster | return qdict;
|
1009 | 01e7f188 | Markus Armbruster | } |
1010 | 01e7f188 | Markus Armbruster | |
1011 | 5dc519ef | Mark McLoughlin | /* Validate parsed opts against descriptions where no
|
1012 | 5dc519ef | Mark McLoughlin | * descriptions were provided in the QemuOptsList.
|
1013 | 5dc519ef | Mark McLoughlin | */
|
1014 | 238431a9 | Blue Swirl | int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc) |
1015 | 5dc519ef | Mark McLoughlin | { |
1016 | 5dc519ef | Mark McLoughlin | QemuOpt *opt; |
1017 | 5dc519ef | Mark McLoughlin | |
1018 | 5dc519ef | Mark McLoughlin | assert(opts->list->desc[0].name == NULL); |
1019 | 5dc519ef | Mark McLoughlin | |
1020 | 5dc519ef | Mark McLoughlin | QTAILQ_FOREACH(opt, &opts->head, next) { |
1021 | 5dc519ef | Mark McLoughlin | int i;
|
1022 | 5dc519ef | Mark McLoughlin | |
1023 | 5dc519ef | Mark McLoughlin | for (i = 0; desc[i].name != NULL; i++) { |
1024 | 5dc519ef | Mark McLoughlin | if (strcmp(desc[i].name, opt->name) == 0) { |
1025 | 5dc519ef | Mark McLoughlin | break;
|
1026 | 5dc519ef | Mark McLoughlin | } |
1027 | 5dc519ef | Mark McLoughlin | } |
1028 | 5dc519ef | Mark McLoughlin | if (desc[i].name == NULL) { |
1029 | db716e9c | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER, opt->name); |
1030 | 5dc519ef | Mark McLoughlin | return -1; |
1031 | 5dc519ef | Mark McLoughlin | } |
1032 | 5dc519ef | Mark McLoughlin | |
1033 | 5dc519ef | Mark McLoughlin | opt->desc = &desc[i]; |
1034 | 5dc519ef | Mark McLoughlin | |
1035 | 5dc519ef | Mark McLoughlin | if (qemu_opt_parse(opt) < 0) { |
1036 | 5dc519ef | Mark McLoughlin | return -1; |
1037 | 5dc519ef | Mark McLoughlin | } |
1038 | 5dc519ef | Mark McLoughlin | } |
1039 | 5dc519ef | Mark McLoughlin | |
1040 | 5dc519ef | Mark McLoughlin | return 0; |
1041 | 5dc519ef | Mark McLoughlin | } |
1042 | 5dc519ef | Mark McLoughlin | |
1043 | e27c88fe | Gerd Hoffmann | int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque, |
1044 | e27c88fe | Gerd Hoffmann | int abort_on_failure)
|
1045 | e27c88fe | Gerd Hoffmann | { |
1046 | 827b0813 | Markus Armbruster | Location loc; |
1047 | e27c88fe | Gerd Hoffmann | QemuOpts *opts; |
1048 | e27c88fe | Gerd Hoffmann | int rc = 0; |
1049 | e27c88fe | Gerd Hoffmann | |
1050 | 827b0813 | Markus Armbruster | loc_push_none(&loc); |
1051 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(opts, &list->head, next) { |
1052 | 827b0813 | Markus Armbruster | loc_restore(&opts->loc); |
1053 | 4a2594dd | Markus Armbruster | rc |= func(opts, opaque); |
1054 | e27c88fe | Gerd Hoffmann | if (abort_on_failure && rc != 0) |
1055 | e27c88fe | Gerd Hoffmann | break;
|
1056 | e27c88fe | Gerd Hoffmann | } |
1057 | 827b0813 | Markus Armbruster | loc_pop(&loc); |
1058 | e27c88fe | Gerd Hoffmann | return rc;
|
1059 | e27c88fe | Gerd Hoffmann | } |