root / qemu-option.c @ 8e00128d
History | View | Annotate | Download (24.4 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 | 67b1355b | Gerd Hoffmann | static int parse_option_bool(const char *name, const char *value, int *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 | 7695019b | Gerd Hoffmann | case 'G': |
218 | 7695019b | Gerd Hoffmann | sizef *= 1024;
|
219 | 7695019b | Gerd Hoffmann | case 'M': |
220 | 7695019b | Gerd Hoffmann | sizef *= 1024;
|
221 | 7695019b | Gerd Hoffmann | case 'K': |
222 | 7695019b | Gerd Hoffmann | case 'k': |
223 | 7695019b | Gerd Hoffmann | sizef *= 1024;
|
224 | 7695019b | Gerd Hoffmann | case 'b': |
225 | 7695019b | Gerd Hoffmann | case '\0': |
226 | 7695019b | Gerd Hoffmann | *ret = (uint64_t) sizef; |
227 | 7695019b | Gerd Hoffmann | break;
|
228 | 7695019b | Gerd Hoffmann | default:
|
229 | c64f27d4 | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size");
|
230 | c64f27d4 | Markus Armbruster | error_printf_unless_qmp("You may use k, M, G or T suffixes for "
|
231 | 7695019b | Gerd Hoffmann | "kilobytes, megabytes, gigabytes and terabytes.\n");
|
232 | 7695019b | Gerd Hoffmann | return -1; |
233 | 7695019b | Gerd Hoffmann | } |
234 | 7695019b | Gerd Hoffmann | } else {
|
235 | c64f27d4 | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER_VALUE, name, "a size");
|
236 | 7695019b | Gerd Hoffmann | return -1; |
237 | 7695019b | Gerd Hoffmann | } |
238 | 7695019b | Gerd Hoffmann | return 0; |
239 | 7695019b | Gerd Hoffmann | } |
240 | 7695019b | Gerd Hoffmann | |
241 | d3f24367 | Kevin Wolf | /*
|
242 | d3f24367 | Kevin Wolf | * Sets the value of a parameter in a given option list. The parsing of the
|
243 | d3f24367 | Kevin Wolf | * value depends on the type of option:
|
244 | d3f24367 | Kevin Wolf | *
|
245 | d3f24367 | Kevin Wolf | * OPT_FLAG (uses value.n):
|
246 | d3f24367 | Kevin Wolf | * If no value is given, the flag is set to 1.
|
247 | d3f24367 | Kevin Wolf | * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
|
248 | d3f24367 | Kevin Wolf | *
|
249 | d3f24367 | Kevin Wolf | * OPT_STRING (uses value.s):
|
250 | d3f24367 | Kevin Wolf | * value is strdup()ed and assigned as option value
|
251 | d3f24367 | Kevin Wolf | *
|
252 | d3f24367 | Kevin Wolf | * OPT_SIZE (uses value.n):
|
253 | d3f24367 | Kevin Wolf | * The value is converted to an integer. Suffixes for kilobytes etc. are
|
254 | d3f24367 | Kevin Wolf | * allowed (powers of 1024).
|
255 | d3f24367 | Kevin Wolf | *
|
256 | d3f24367 | Kevin Wolf | * Returns 0 on succes, -1 in error cases
|
257 | d3f24367 | Kevin Wolf | */
|
258 | d3f24367 | Kevin Wolf | int set_option_parameter(QEMUOptionParameter *list, const char *name, |
259 | d3f24367 | Kevin Wolf | const char *value) |
260 | d3f24367 | Kevin Wolf | { |
261 | 67b1355b | Gerd Hoffmann | int flag;
|
262 | 67b1355b | Gerd Hoffmann | |
263 | d3f24367 | Kevin Wolf | // Find a matching parameter
|
264 | d3f24367 | Kevin Wolf | list = get_option_parameter(list, name); |
265 | d3f24367 | Kevin Wolf | if (list == NULL) { |
266 | d3f24367 | Kevin Wolf | fprintf(stderr, "Unknown option '%s'\n", name);
|
267 | d3f24367 | Kevin Wolf | return -1; |
268 | d3f24367 | Kevin Wolf | } |
269 | d3f24367 | Kevin Wolf | |
270 | d3f24367 | Kevin Wolf | // Process parameter
|
271 | d3f24367 | Kevin Wolf | switch (list->type) {
|
272 | d3f24367 | Kevin Wolf | case OPT_FLAG:
|
273 | 3df04ac3 | Mark McLoughlin | if (parse_option_bool(name, value, &flag) == -1) |
274 | 67b1355b | Gerd Hoffmann | return -1; |
275 | 67b1355b | Gerd Hoffmann | list->value.n = flag; |
276 | d3f24367 | Kevin Wolf | break;
|
277 | d3f24367 | Kevin Wolf | |
278 | d3f24367 | Kevin Wolf | case OPT_STRING:
|
279 | d3f24367 | Kevin Wolf | if (value != NULL) { |
280 | d4c3fddd | Jean-Christophe DUBOIS | list->value.s = qemu_strdup(value); |
281 | d3f24367 | Kevin Wolf | } else {
|
282 | d3f24367 | Kevin Wolf | fprintf(stderr, "Option '%s' needs a parameter\n", name);
|
283 | d3f24367 | Kevin Wolf | return -1; |
284 | d3f24367 | Kevin Wolf | } |
285 | d3f24367 | Kevin Wolf | break;
|
286 | d3f24367 | Kevin Wolf | |
287 | d3f24367 | Kevin Wolf | case OPT_SIZE:
|
288 | 3df04ac3 | Mark McLoughlin | if (parse_option_size(name, value, &list->value.n) == -1) |
289 | d3f24367 | Kevin Wolf | return -1; |
290 | d3f24367 | Kevin Wolf | break;
|
291 | 7695019b | Gerd Hoffmann | |
292 | d3f24367 | Kevin Wolf | default:
|
293 | d3f24367 | Kevin Wolf | fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
|
294 | d3f24367 | Kevin Wolf | return -1; |
295 | d3f24367 | Kevin Wolf | } |
296 | d3f24367 | Kevin Wolf | |
297 | d3f24367 | Kevin Wolf | return 0; |
298 | d3f24367 | Kevin Wolf | } |
299 | d3f24367 | Kevin Wolf | |
300 | d3f24367 | Kevin Wolf | /*
|
301 | d3f24367 | Kevin Wolf | * Sets the given parameter to an integer instead of a string.
|
302 | d3f24367 | Kevin Wolf | * This function cannot be used to set string options.
|
303 | d3f24367 | Kevin Wolf | *
|
304 | d3f24367 | Kevin Wolf | * Returns 0 on success, -1 in error cases
|
305 | d3f24367 | Kevin Wolf | */
|
306 | d3f24367 | Kevin Wolf | int set_option_parameter_int(QEMUOptionParameter *list, const char *name, |
307 | d3f24367 | Kevin Wolf | uint64_t value) |
308 | d3f24367 | Kevin Wolf | { |
309 | d3f24367 | Kevin Wolf | // Find a matching parameter
|
310 | d3f24367 | Kevin Wolf | list = get_option_parameter(list, name); |
311 | d3f24367 | Kevin Wolf | if (list == NULL) { |
312 | d3f24367 | Kevin Wolf | fprintf(stderr, "Unknown option '%s'\n", name);
|
313 | d3f24367 | Kevin Wolf | return -1; |
314 | d3f24367 | Kevin Wolf | } |
315 | d3f24367 | Kevin Wolf | |
316 | d3f24367 | Kevin Wolf | // Process parameter
|
317 | d3f24367 | Kevin Wolf | switch (list->type) {
|
318 | d3f24367 | Kevin Wolf | case OPT_FLAG:
|
319 | d3f24367 | Kevin Wolf | case OPT_NUMBER:
|
320 | d3f24367 | Kevin Wolf | case OPT_SIZE:
|
321 | d3f24367 | Kevin Wolf | list->value.n = value; |
322 | d3f24367 | Kevin Wolf | break;
|
323 | d3f24367 | Kevin Wolf | |
324 | d3f24367 | Kevin Wolf | default:
|
325 | d3f24367 | Kevin Wolf | return -1; |
326 | d3f24367 | Kevin Wolf | } |
327 | d3f24367 | Kevin Wolf | |
328 | d3f24367 | Kevin Wolf | return 0; |
329 | d3f24367 | Kevin Wolf | } |
330 | d3f24367 | Kevin Wolf | |
331 | d3f24367 | Kevin Wolf | /*
|
332 | d3f24367 | Kevin Wolf | * Frees a option list. If it contains strings, the strings are freed as well.
|
333 | d3f24367 | Kevin Wolf | */
|
334 | d3f24367 | Kevin Wolf | void free_option_parameters(QEMUOptionParameter *list)
|
335 | d3f24367 | Kevin Wolf | { |
336 | d3f24367 | Kevin Wolf | QEMUOptionParameter *cur = list; |
337 | d3f24367 | Kevin Wolf | |
338 | d3f24367 | Kevin Wolf | while (cur && cur->name) {
|
339 | d3f24367 | Kevin Wolf | if (cur->type == OPT_STRING) {
|
340 | d4c3fddd | Jean-Christophe DUBOIS | qemu_free(cur->value.s); |
341 | d3f24367 | Kevin Wolf | } |
342 | d3f24367 | Kevin Wolf | cur++; |
343 | d3f24367 | Kevin Wolf | } |
344 | d3f24367 | Kevin Wolf | |
345 | d4c3fddd | Jean-Christophe DUBOIS | qemu_free(list); |
346 | d3f24367 | Kevin Wolf | } |
347 | d3f24367 | Kevin Wolf | |
348 | d3f24367 | Kevin Wolf | /*
|
349 | b50cbabc | MORITA Kazutaka | * Count valid options in list
|
350 | b50cbabc | MORITA Kazutaka | */
|
351 | b50cbabc | MORITA Kazutaka | static size_t count_option_parameters(QEMUOptionParameter *list)
|
352 | b50cbabc | MORITA Kazutaka | { |
353 | b50cbabc | MORITA Kazutaka | size_t num_options = 0;
|
354 | b50cbabc | MORITA Kazutaka | |
355 | b50cbabc | MORITA Kazutaka | while (list && list->name) {
|
356 | b50cbabc | MORITA Kazutaka | num_options++; |
357 | b50cbabc | MORITA Kazutaka | list++; |
358 | b50cbabc | MORITA Kazutaka | } |
359 | b50cbabc | MORITA Kazutaka | |
360 | b50cbabc | MORITA Kazutaka | return num_options;
|
361 | b50cbabc | MORITA Kazutaka | } |
362 | b50cbabc | MORITA Kazutaka | |
363 | b50cbabc | MORITA Kazutaka | /*
|
364 | b50cbabc | MORITA Kazutaka | * Append an option list (list) to an option list (dest).
|
365 | b50cbabc | MORITA Kazutaka | *
|
366 | b50cbabc | MORITA Kazutaka | * If dest is NULL, a new copy of list is created.
|
367 | b50cbabc | MORITA Kazutaka | *
|
368 | b50cbabc | MORITA Kazutaka | * Returns a pointer to the first element of dest (or the newly allocated copy)
|
369 | b50cbabc | MORITA Kazutaka | */
|
370 | b50cbabc | MORITA Kazutaka | QEMUOptionParameter *append_option_parameters(QEMUOptionParameter *dest, |
371 | b50cbabc | MORITA Kazutaka | QEMUOptionParameter *list) |
372 | b50cbabc | MORITA Kazutaka | { |
373 | b50cbabc | MORITA Kazutaka | size_t num_options, num_dest_options; |
374 | b50cbabc | MORITA Kazutaka | |
375 | b50cbabc | MORITA Kazutaka | num_options = count_option_parameters(dest); |
376 | b50cbabc | MORITA Kazutaka | num_dest_options = num_options; |
377 | b50cbabc | MORITA Kazutaka | |
378 | b50cbabc | MORITA Kazutaka | num_options += count_option_parameters(list); |
379 | b50cbabc | MORITA Kazutaka | |
380 | b50cbabc | MORITA Kazutaka | dest = qemu_realloc(dest, (num_options + 1) * sizeof(QEMUOptionParameter)); |
381 | bd69fe84 | Kevin Wolf | dest[num_dest_options].name = NULL;
|
382 | b50cbabc | MORITA Kazutaka | |
383 | b50cbabc | MORITA Kazutaka | while (list && list->name) {
|
384 | b50cbabc | MORITA Kazutaka | if (get_option_parameter(dest, list->name) == NULL) { |
385 | b50cbabc | MORITA Kazutaka | dest[num_dest_options++] = *list; |
386 | b50cbabc | MORITA Kazutaka | dest[num_dest_options].name = NULL;
|
387 | b50cbabc | MORITA Kazutaka | } |
388 | b50cbabc | MORITA Kazutaka | list++; |
389 | b50cbabc | MORITA Kazutaka | } |
390 | b50cbabc | MORITA Kazutaka | |
391 | b50cbabc | MORITA Kazutaka | return dest;
|
392 | b50cbabc | MORITA Kazutaka | } |
393 | b50cbabc | MORITA Kazutaka | |
394 | b50cbabc | MORITA Kazutaka | /*
|
395 | d3f24367 | Kevin Wolf | * Parses a parameter string (param) into an option list (dest).
|
396 | d3f24367 | Kevin Wolf | *
|
397 | d3f24367 | Kevin Wolf | * list is the templace is. If dest is NULL, a new copy of list is created for
|
398 | d3f24367 | Kevin Wolf | * it. If list is NULL, this function fails.
|
399 | d3f24367 | Kevin Wolf | *
|
400 | d3f24367 | Kevin Wolf | * A parameter string consists of one or more parameters, separated by commas.
|
401 | d3f24367 | Kevin Wolf | * Each parameter consists of its name and possibly of a value. In the latter
|
402 | d3f24367 | Kevin Wolf | * case, the value is delimited by an = character. To specify a value which
|
403 | d3f24367 | Kevin Wolf | * contains commas, double each comma so it won't be recognized as the end of
|
404 | d3f24367 | Kevin Wolf | * the parameter.
|
405 | d3f24367 | Kevin Wolf | *
|
406 | d3f24367 | Kevin Wolf | * For more details of the parsing see above.
|
407 | d3f24367 | Kevin Wolf | *
|
408 | d3f24367 | Kevin Wolf | * Returns a pointer to the first element of dest (or the newly allocated copy)
|
409 | d3f24367 | Kevin Wolf | * or NULL in error cases
|
410 | d3f24367 | Kevin Wolf | */
|
411 | d3f24367 | Kevin Wolf | QEMUOptionParameter *parse_option_parameters(const char *param, |
412 | d3f24367 | Kevin Wolf | QEMUOptionParameter *list, QEMUOptionParameter *dest) |
413 | d3f24367 | Kevin Wolf | { |
414 | d3f24367 | Kevin Wolf | QEMUOptionParameter *allocated = NULL;
|
415 | d3f24367 | Kevin Wolf | char name[256]; |
416 | d3f24367 | Kevin Wolf | char value[256]; |
417 | d3f24367 | Kevin Wolf | char *param_delim, *value_delim;
|
418 | d3f24367 | Kevin Wolf | char next_delim;
|
419 | d3f24367 | Kevin Wolf | size_t num_options; |
420 | d3f24367 | Kevin Wolf | |
421 | d3f24367 | Kevin Wolf | if (list == NULL) { |
422 | d3f24367 | Kevin Wolf | return NULL; |
423 | d3f24367 | Kevin Wolf | } |
424 | d3f24367 | Kevin Wolf | |
425 | d3f24367 | Kevin Wolf | if (dest == NULL) { |
426 | d3f24367 | Kevin Wolf | // Count valid options
|
427 | b50cbabc | MORITA Kazutaka | num_options = count_option_parameters(list); |
428 | d3f24367 | Kevin Wolf | |
429 | d3f24367 | Kevin Wolf | // Create a copy of the option list to fill in values
|
430 | d3f24367 | Kevin Wolf | dest = qemu_mallocz((num_options + 1) * sizeof(QEMUOptionParameter)); |
431 | d3f24367 | Kevin Wolf | allocated = dest; |
432 | d3f24367 | Kevin Wolf | memcpy(dest, list, (num_options + 1) * sizeof(QEMUOptionParameter)); |
433 | d3f24367 | Kevin Wolf | } |
434 | d3f24367 | Kevin Wolf | |
435 | d3f24367 | Kevin Wolf | while (*param) {
|
436 | d3f24367 | Kevin Wolf | |
437 | d3f24367 | Kevin Wolf | // Find parameter name and value in the string
|
438 | d3f24367 | Kevin Wolf | param_delim = strchr(param, ',');
|
439 | d3f24367 | Kevin Wolf | value_delim = strchr(param, '=');
|
440 | d3f24367 | Kevin Wolf | |
441 | d3f24367 | Kevin Wolf | if (value_delim && (value_delim < param_delim || !param_delim)) {
|
442 | d3f24367 | Kevin Wolf | next_delim = '=';
|
443 | d3f24367 | Kevin Wolf | } else {
|
444 | d3f24367 | Kevin Wolf | next_delim = ',';
|
445 | d3f24367 | Kevin Wolf | value_delim = NULL;
|
446 | d3f24367 | Kevin Wolf | } |
447 | d3f24367 | Kevin Wolf | |
448 | d3f24367 | Kevin Wolf | param = get_opt_name(name, sizeof(name), param, next_delim);
|
449 | d3f24367 | Kevin Wolf | if (value_delim) {
|
450 | d3f24367 | Kevin Wolf | param = get_opt_value(value, sizeof(value), param + 1); |
451 | d3f24367 | Kevin Wolf | } |
452 | d3f24367 | Kevin Wolf | if (*param != '\0') { |
453 | d3f24367 | Kevin Wolf | param++; |
454 | d3f24367 | Kevin Wolf | } |
455 | d3f24367 | Kevin Wolf | |
456 | d3f24367 | Kevin Wolf | // Set the parameter
|
457 | d3f24367 | Kevin Wolf | if (set_option_parameter(dest, name, value_delim ? value : NULL)) { |
458 | d3f24367 | Kevin Wolf | goto fail;
|
459 | d3f24367 | Kevin Wolf | } |
460 | d3f24367 | Kevin Wolf | } |
461 | d3f24367 | Kevin Wolf | |
462 | d3f24367 | Kevin Wolf | return dest;
|
463 | d3f24367 | Kevin Wolf | |
464 | d3f24367 | Kevin Wolf | fail:
|
465 | d3f24367 | Kevin Wolf | // Only free the list if it was newly allocated
|
466 | d3f24367 | Kevin Wolf | free_option_parameters(allocated); |
467 | d3f24367 | Kevin Wolf | return NULL; |
468 | d3f24367 | Kevin Wolf | } |
469 | d3f24367 | Kevin Wolf | |
470 | d3f24367 | Kevin Wolf | /*
|
471 | d3f24367 | Kevin Wolf | * Prints all options of a list that have a value to stdout
|
472 | d3f24367 | Kevin Wolf | */
|
473 | d3f24367 | Kevin Wolf | void print_option_parameters(QEMUOptionParameter *list)
|
474 | d3f24367 | Kevin Wolf | { |
475 | d3f24367 | Kevin Wolf | while (list && list->name) {
|
476 | d3f24367 | Kevin Wolf | switch (list->type) {
|
477 | d3f24367 | Kevin Wolf | case OPT_STRING:
|
478 | d3f24367 | Kevin Wolf | if (list->value.s != NULL) { |
479 | d3f24367 | Kevin Wolf | printf("%s='%s' ", list->name, list->value.s);
|
480 | d3f24367 | Kevin Wolf | } |
481 | d3f24367 | Kevin Wolf | break;
|
482 | d3f24367 | Kevin Wolf | case OPT_FLAG:
|
483 | d3f24367 | Kevin Wolf | printf("%s=%s ", list->name, list->value.n ? "on" : "off"); |
484 | d3f24367 | Kevin Wolf | break;
|
485 | d3f24367 | Kevin Wolf | case OPT_SIZE:
|
486 | d3f24367 | Kevin Wolf | case OPT_NUMBER:
|
487 | d3f24367 | Kevin Wolf | printf("%s=%" PRId64 " ", list->name, list->value.n); |
488 | d3f24367 | Kevin Wolf | break;
|
489 | d3f24367 | Kevin Wolf | default:
|
490 | d3f24367 | Kevin Wolf | printf("%s=(unkown type) ", list->name);
|
491 | d3f24367 | Kevin Wolf | break;
|
492 | d3f24367 | Kevin Wolf | } |
493 | d3f24367 | Kevin Wolf | list++; |
494 | d3f24367 | Kevin Wolf | } |
495 | d3f24367 | Kevin Wolf | } |
496 | db08adf5 | Kevin Wolf | |
497 | db08adf5 | Kevin Wolf | /*
|
498 | db08adf5 | Kevin Wolf | * Prints an overview of all available options
|
499 | db08adf5 | Kevin Wolf | */
|
500 | db08adf5 | Kevin Wolf | void print_option_help(QEMUOptionParameter *list)
|
501 | db08adf5 | Kevin Wolf | { |
502 | db08adf5 | Kevin Wolf | printf("Supported options:\n");
|
503 | db08adf5 | Kevin Wolf | while (list && list->name) {
|
504 | db08adf5 | Kevin Wolf | printf("%-16s %s\n", list->name,
|
505 | db08adf5 | Kevin Wolf | list->help ? list->help : "No description available");
|
506 | db08adf5 | Kevin Wolf | list++; |
507 | db08adf5 | Kevin Wolf | } |
508 | db08adf5 | Kevin Wolf | } |
509 | e27c88fe | Gerd Hoffmann | |
510 | e27c88fe | Gerd Hoffmann | /* ------------------------------------------------------------------ */
|
511 | e27c88fe | Gerd Hoffmann | |
512 | e27c88fe | Gerd Hoffmann | struct QemuOpt {
|
513 | e27c88fe | Gerd Hoffmann | const char *name; |
514 | e27c88fe | Gerd Hoffmann | const char *str; |
515 | e27c88fe | Gerd Hoffmann | |
516 | 238431a9 | Blue Swirl | const QemuOptDesc *desc;
|
517 | e27c88fe | Gerd Hoffmann | union {
|
518 | 1f5c1775 | Juan Quintela | int boolean;
|
519 | e27c88fe | Gerd Hoffmann | uint64_t uint; |
520 | e27c88fe | Gerd Hoffmann | } value; |
521 | e27c88fe | Gerd Hoffmann | |
522 | e27c88fe | Gerd Hoffmann | QemuOpts *opts; |
523 | 72cf2d4f | Blue Swirl | QTAILQ_ENTRY(QemuOpt) next; |
524 | e27c88fe | Gerd Hoffmann | }; |
525 | e27c88fe | Gerd Hoffmann | |
526 | e27c88fe | Gerd Hoffmann | struct QemuOpts {
|
527 | b09417b9 | Jan Kiszka | char *id;
|
528 | e27c88fe | Gerd Hoffmann | QemuOptsList *list; |
529 | 827b0813 | Markus Armbruster | Location loc; |
530 | dc9ca4ba | Mark McLoughlin | QTAILQ_HEAD(QemuOptHead, QemuOpt) head; |
531 | 72cf2d4f | Blue Swirl | QTAILQ_ENTRY(QemuOpts) next; |
532 | e27c88fe | Gerd Hoffmann | }; |
533 | e27c88fe | Gerd Hoffmann | |
534 | e27c88fe | Gerd Hoffmann | static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name) |
535 | e27c88fe | Gerd Hoffmann | { |
536 | e27c88fe | Gerd Hoffmann | QemuOpt *opt; |
537 | e27c88fe | Gerd Hoffmann | |
538 | dc9ca4ba | Mark McLoughlin | QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) { |
539 | e27c88fe | Gerd Hoffmann | if (strcmp(opt->name, name) != 0) |
540 | e27c88fe | Gerd Hoffmann | continue;
|
541 | e27c88fe | Gerd Hoffmann | return opt;
|
542 | e27c88fe | Gerd Hoffmann | } |
543 | e27c88fe | Gerd Hoffmann | return NULL; |
544 | e27c88fe | Gerd Hoffmann | } |
545 | e27c88fe | Gerd Hoffmann | |
546 | e27c88fe | Gerd Hoffmann | const char *qemu_opt_get(QemuOpts *opts, const char *name) |
547 | e27c88fe | Gerd Hoffmann | { |
548 | e27c88fe | Gerd Hoffmann | QemuOpt *opt = qemu_opt_find(opts, name); |
549 | e27c88fe | Gerd Hoffmann | return opt ? opt->str : NULL; |
550 | e27c88fe | Gerd Hoffmann | } |
551 | e27c88fe | Gerd Hoffmann | |
552 | e27c88fe | Gerd Hoffmann | int qemu_opt_get_bool(QemuOpts *opts, const char *name, int defval) |
553 | e27c88fe | Gerd Hoffmann | { |
554 | e27c88fe | Gerd Hoffmann | QemuOpt *opt = qemu_opt_find(opts, name); |
555 | e27c88fe | Gerd Hoffmann | |
556 | e27c88fe | Gerd Hoffmann | if (opt == NULL) |
557 | e27c88fe | Gerd Hoffmann | return defval;
|
558 | e27c88fe | Gerd Hoffmann | assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL); |
559 | 1f5c1775 | Juan Quintela | return opt->value.boolean;
|
560 | e27c88fe | Gerd Hoffmann | } |
561 | e27c88fe | Gerd Hoffmann | |
562 | e27c88fe | Gerd Hoffmann | uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval) |
563 | e27c88fe | Gerd Hoffmann | { |
564 | e27c88fe | Gerd Hoffmann | QemuOpt *opt = qemu_opt_find(opts, name); |
565 | e27c88fe | Gerd Hoffmann | |
566 | e27c88fe | Gerd Hoffmann | if (opt == NULL) |
567 | e27c88fe | Gerd Hoffmann | return defval;
|
568 | e27c88fe | Gerd Hoffmann | assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER); |
569 | e27c88fe | Gerd Hoffmann | return opt->value.uint;
|
570 | e27c88fe | Gerd Hoffmann | } |
571 | e27c88fe | Gerd Hoffmann | |
572 | e27c88fe | Gerd Hoffmann | uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval) |
573 | e27c88fe | Gerd Hoffmann | { |
574 | e27c88fe | Gerd Hoffmann | QemuOpt *opt = qemu_opt_find(opts, name); |
575 | e27c88fe | Gerd Hoffmann | |
576 | e27c88fe | Gerd Hoffmann | if (opt == NULL) |
577 | e27c88fe | Gerd Hoffmann | return defval;
|
578 | e27c88fe | Gerd Hoffmann | assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE); |
579 | e27c88fe | Gerd Hoffmann | return opt->value.uint;
|
580 | e27c88fe | Gerd Hoffmann | } |
581 | e27c88fe | Gerd Hoffmann | |
582 | e27c88fe | Gerd Hoffmann | static int qemu_opt_parse(QemuOpt *opt) |
583 | e27c88fe | Gerd Hoffmann | { |
584 | e27c88fe | Gerd Hoffmann | if (opt->desc == NULL) |
585 | e27c88fe | Gerd Hoffmann | return 0; |
586 | e27c88fe | Gerd Hoffmann | switch (opt->desc->type) {
|
587 | e27c88fe | Gerd Hoffmann | case QEMU_OPT_STRING:
|
588 | e27c88fe | Gerd Hoffmann | /* nothing */
|
589 | e27c88fe | Gerd Hoffmann | return 0; |
590 | e27c88fe | Gerd Hoffmann | case QEMU_OPT_BOOL:
|
591 | 1f5c1775 | Juan Quintela | return parse_option_bool(opt->name, opt->str, &opt->value.boolean);
|
592 | e27c88fe | Gerd Hoffmann | case QEMU_OPT_NUMBER:
|
593 | e27c88fe | Gerd Hoffmann | return parse_option_number(opt->name, opt->str, &opt->value.uint);
|
594 | e27c88fe | Gerd Hoffmann | case QEMU_OPT_SIZE:
|
595 | e27c88fe | Gerd Hoffmann | return parse_option_size(opt->name, opt->str, &opt->value.uint);
|
596 | e27c88fe | Gerd Hoffmann | default:
|
597 | e27c88fe | Gerd Hoffmann | abort(); |
598 | e27c88fe | Gerd Hoffmann | } |
599 | e27c88fe | Gerd Hoffmann | } |
600 | e27c88fe | Gerd Hoffmann | |
601 | e27c88fe | Gerd Hoffmann | static void qemu_opt_del(QemuOpt *opt) |
602 | e27c88fe | Gerd Hoffmann | { |
603 | 72cf2d4f | Blue Swirl | QTAILQ_REMOVE(&opt->opts->head, opt, next); |
604 | e27c88fe | Gerd Hoffmann | qemu_free((/* !const */ char*)opt->name); |
605 | e27c88fe | Gerd Hoffmann | qemu_free((/* !const */ char*)opt->str); |
606 | e27c88fe | Gerd Hoffmann | qemu_free(opt); |
607 | e27c88fe | Gerd Hoffmann | } |
608 | e27c88fe | Gerd Hoffmann | |
609 | e27c88fe | Gerd Hoffmann | int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) |
610 | e27c88fe | Gerd Hoffmann | { |
611 | e27c88fe | Gerd Hoffmann | QemuOpt *opt; |
612 | 238431a9 | Blue Swirl | const QemuOptDesc *desc = opts->list->desc;
|
613 | dc9ca4ba | Mark McLoughlin | int i;
|
614 | e27c88fe | Gerd Hoffmann | |
615 | dc9ca4ba | Mark McLoughlin | for (i = 0; desc[i].name != NULL; i++) { |
616 | dc9ca4ba | Mark McLoughlin | if (strcmp(desc[i].name, name) == 0) { |
617 | dc9ca4ba | Mark McLoughlin | break;
|
618 | e27c88fe | Gerd Hoffmann | } |
619 | dc9ca4ba | Mark McLoughlin | } |
620 | dc9ca4ba | Mark McLoughlin | if (desc[i].name == NULL) { |
621 | dc9ca4ba | Mark McLoughlin | if (i == 0) { |
622 | dc9ca4ba | Mark McLoughlin | /* empty list -> allow any */;
|
623 | dc9ca4ba | Mark McLoughlin | } else {
|
624 | c64f27d4 | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER, name); |
625 | dc9ca4ba | Mark McLoughlin | return -1; |
626 | e27c88fe | Gerd Hoffmann | } |
627 | e27c88fe | Gerd Hoffmann | } |
628 | dc9ca4ba | Mark McLoughlin | |
629 | dc9ca4ba | Mark McLoughlin | opt = qemu_mallocz(sizeof(*opt));
|
630 | dc9ca4ba | Mark McLoughlin | opt->name = qemu_strdup(name); |
631 | dc9ca4ba | Mark McLoughlin | opt->opts = opts; |
632 | dc9ca4ba | Mark McLoughlin | QTAILQ_INSERT_TAIL(&opts->head, opt, next); |
633 | dc9ca4ba | Mark McLoughlin | if (desc[i].name != NULL) { |
634 | dc9ca4ba | Mark McLoughlin | opt->desc = desc+i; |
635 | dc9ca4ba | Mark McLoughlin | } |
636 | e27c88fe | Gerd Hoffmann | if (value) {
|
637 | e27c88fe | Gerd Hoffmann | opt->str = qemu_strdup(value); |
638 | e27c88fe | Gerd Hoffmann | } |
639 | e27c88fe | Gerd Hoffmann | if (qemu_opt_parse(opt) < 0) { |
640 | e27c88fe | Gerd Hoffmann | qemu_opt_del(opt); |
641 | e27c88fe | Gerd Hoffmann | return -1; |
642 | e27c88fe | Gerd Hoffmann | } |
643 | e27c88fe | Gerd Hoffmann | return 0; |
644 | e27c88fe | Gerd Hoffmann | } |
645 | e27c88fe | Gerd Hoffmann | |
646 | 48026075 | Gerd Hoffmann | int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque, |
647 | 48026075 | Gerd Hoffmann | int abort_on_failure)
|
648 | 48026075 | Gerd Hoffmann | { |
649 | 48026075 | Gerd Hoffmann | QemuOpt *opt; |
650 | 48026075 | Gerd Hoffmann | int rc = 0; |
651 | 48026075 | Gerd Hoffmann | |
652 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(opt, &opts->head, next) { |
653 | 48026075 | Gerd Hoffmann | rc = func(opt->name, opt->str, opaque); |
654 | 48026075 | Gerd Hoffmann | if (abort_on_failure && rc != 0) |
655 | 48026075 | Gerd Hoffmann | break;
|
656 | 48026075 | Gerd Hoffmann | } |
657 | 48026075 | Gerd Hoffmann | return rc;
|
658 | 48026075 | Gerd Hoffmann | } |
659 | 48026075 | Gerd Hoffmann | |
660 | e27c88fe | Gerd Hoffmann | QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id) |
661 | e27c88fe | Gerd Hoffmann | { |
662 | e27c88fe | Gerd Hoffmann | QemuOpts *opts; |
663 | e27c88fe | Gerd Hoffmann | |
664 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(opts, &list->head, next) { |
665 | e27c88fe | Gerd Hoffmann | if (!opts->id) {
|
666 | e27c88fe | Gerd Hoffmann | continue;
|
667 | e27c88fe | Gerd Hoffmann | } |
668 | e27c88fe | Gerd Hoffmann | if (strcmp(opts->id, id) != 0) { |
669 | e27c88fe | Gerd Hoffmann | continue;
|
670 | e27c88fe | Gerd Hoffmann | } |
671 | e27c88fe | Gerd Hoffmann | return opts;
|
672 | e27c88fe | Gerd Hoffmann | } |
673 | e27c88fe | Gerd Hoffmann | return NULL; |
674 | e27c88fe | Gerd Hoffmann | } |
675 | e27c88fe | Gerd Hoffmann | |
676 | b560a9ab | Markus Armbruster | static int id_wellformed(const char *id) |
677 | b560a9ab | Markus Armbruster | { |
678 | b560a9ab | Markus Armbruster | int i;
|
679 | b560a9ab | Markus Armbruster | |
680 | b560a9ab | Markus Armbruster | if (!qemu_isalpha(id[0])) { |
681 | b560a9ab | Markus Armbruster | return 0; |
682 | b560a9ab | Markus Armbruster | } |
683 | b560a9ab | Markus Armbruster | for (i = 1; id[i]; i++) { |
684 | b560a9ab | Markus Armbruster | if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) { |
685 | b560a9ab | Markus Armbruster | return 0; |
686 | b560a9ab | Markus Armbruster | } |
687 | b560a9ab | Markus Armbruster | } |
688 | b560a9ab | Markus Armbruster | return 1; |
689 | b560a9ab | Markus Armbruster | } |
690 | b560a9ab | Markus Armbruster | |
691 | e27c88fe | Gerd Hoffmann | QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists) |
692 | e27c88fe | Gerd Hoffmann | { |
693 | e27c88fe | Gerd Hoffmann | QemuOpts *opts = NULL;
|
694 | e27c88fe | Gerd Hoffmann | |
695 | e27c88fe | Gerd Hoffmann | if (id) {
|
696 | b560a9ab | Markus Armbruster | if (!id_wellformed(id)) {
|
697 | b560a9ab | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER_VALUE, "id", "an identifier"); |
698 | b560a9ab | Markus Armbruster | error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
|
699 | b560a9ab | Markus Armbruster | return NULL; |
700 | b560a9ab | Markus Armbruster | } |
701 | e27c88fe | Gerd Hoffmann | opts = qemu_opts_find(list, id); |
702 | e27c88fe | Gerd Hoffmann | if (opts != NULL) { |
703 | e27c88fe | Gerd Hoffmann | if (fail_if_exists) {
|
704 | 975b63a4 | Markus Armbruster | qerror_report(QERR_DUPLICATE_ID, id, list->name); |
705 | e27c88fe | Gerd Hoffmann | return NULL; |
706 | e27c88fe | Gerd Hoffmann | } else {
|
707 | e27c88fe | Gerd Hoffmann | return opts;
|
708 | e27c88fe | Gerd Hoffmann | } |
709 | e27c88fe | Gerd Hoffmann | } |
710 | e27c88fe | Gerd Hoffmann | } |
711 | e27c88fe | Gerd Hoffmann | opts = qemu_mallocz(sizeof(*opts));
|
712 | e27c88fe | Gerd Hoffmann | if (id) {
|
713 | e27c88fe | Gerd Hoffmann | opts->id = qemu_strdup(id); |
714 | e27c88fe | Gerd Hoffmann | } |
715 | e27c88fe | Gerd Hoffmann | opts->list = list; |
716 | 827b0813 | Markus Armbruster | loc_save(&opts->loc); |
717 | 72cf2d4f | Blue Swirl | QTAILQ_INIT(&opts->head); |
718 | 72cf2d4f | Blue Swirl | QTAILQ_INSERT_TAIL(&list->head, opts, next); |
719 | e27c88fe | Gerd Hoffmann | return opts;
|
720 | e27c88fe | Gerd Hoffmann | } |
721 | e27c88fe | Gerd Hoffmann | |
722 | bb67ab02 | Markus Armbruster | void qemu_opts_reset(QemuOptsList *list)
|
723 | bb67ab02 | Markus Armbruster | { |
724 | bb67ab02 | Markus Armbruster | QemuOpts *opts, *next_opts; |
725 | bb67ab02 | Markus Armbruster | |
726 | bb67ab02 | Markus Armbruster | QTAILQ_FOREACH_SAFE(opts, &list->head, next, next_opts) { |
727 | bb67ab02 | Markus Armbruster | qemu_opts_del(opts); |
728 | bb67ab02 | Markus Armbruster | } |
729 | bb67ab02 | Markus Armbruster | } |
730 | bb67ab02 | Markus Armbruster | |
731 | 94ac7268 | Markus Armbruster | void qemu_opts_loc_restore(QemuOpts *opts)
|
732 | 94ac7268 | Markus Armbruster | { |
733 | 94ac7268 | Markus Armbruster | loc_restore(&opts->loc); |
734 | 94ac7268 | Markus Armbruster | } |
735 | 94ac7268 | Markus Armbruster | |
736 | e27c88fe | Gerd Hoffmann | int qemu_opts_set(QemuOptsList *list, const char *id, |
737 | e27c88fe | Gerd Hoffmann | const char *name, const char *value) |
738 | e27c88fe | Gerd Hoffmann | { |
739 | e27c88fe | Gerd Hoffmann | QemuOpts *opts; |
740 | e27c88fe | Gerd Hoffmann | |
741 | e27c88fe | Gerd Hoffmann | opts = qemu_opts_create(list, id, 1);
|
742 | e27c88fe | Gerd Hoffmann | if (opts == NULL) { |
743 | e27c88fe | Gerd Hoffmann | return -1; |
744 | e27c88fe | Gerd Hoffmann | } |
745 | e27c88fe | Gerd Hoffmann | return qemu_opt_set(opts, name, value);
|
746 | e27c88fe | Gerd Hoffmann | } |
747 | e27c88fe | Gerd Hoffmann | |
748 | 48026075 | Gerd Hoffmann | const char *qemu_opts_id(QemuOpts *opts) |
749 | 48026075 | Gerd Hoffmann | { |
750 | 48026075 | Gerd Hoffmann | return opts->id;
|
751 | 48026075 | Gerd Hoffmann | } |
752 | 48026075 | Gerd Hoffmann | |
753 | e27c88fe | Gerd Hoffmann | void qemu_opts_del(QemuOpts *opts)
|
754 | e27c88fe | Gerd Hoffmann | { |
755 | e27c88fe | Gerd Hoffmann | QemuOpt *opt; |
756 | e27c88fe | Gerd Hoffmann | |
757 | e27c88fe | Gerd Hoffmann | for (;;) {
|
758 | 72cf2d4f | Blue Swirl | opt = QTAILQ_FIRST(&opts->head); |
759 | e27c88fe | Gerd Hoffmann | if (opt == NULL) |
760 | e27c88fe | Gerd Hoffmann | break;
|
761 | e27c88fe | Gerd Hoffmann | qemu_opt_del(opt); |
762 | e27c88fe | Gerd Hoffmann | } |
763 | 72cf2d4f | Blue Swirl | QTAILQ_REMOVE(&opts->list->head, opts, next); |
764 | b09417b9 | Jan Kiszka | qemu_free(opts->id); |
765 | e27c88fe | Gerd Hoffmann | qemu_free(opts); |
766 | e27c88fe | Gerd Hoffmann | } |
767 | e27c88fe | Gerd Hoffmann | |
768 | e27c88fe | Gerd Hoffmann | int qemu_opts_print(QemuOpts *opts, void *dummy) |
769 | e27c88fe | Gerd Hoffmann | { |
770 | e27c88fe | Gerd Hoffmann | QemuOpt *opt; |
771 | e27c88fe | Gerd Hoffmann | |
772 | e27c88fe | Gerd Hoffmann | fprintf(stderr, "%s: %s:", opts->list->name,
|
773 | e27c88fe | Gerd Hoffmann | opts->id ? opts->id : "<noid>");
|
774 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(opt, &opts->head, next) { |
775 | e27c88fe | Gerd Hoffmann | fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
|
776 | e27c88fe | Gerd Hoffmann | } |
777 | e27c88fe | Gerd Hoffmann | fprintf(stderr, "\n");
|
778 | e27c88fe | Gerd Hoffmann | return 0; |
779 | e27c88fe | Gerd Hoffmann | } |
780 | e27c88fe | Gerd Hoffmann | |
781 | 96729cbd | Gerd Hoffmann | int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname) |
782 | e27c88fe | Gerd Hoffmann | { |
783 | d318ff99 | Gerd Hoffmann | char option[128], value[1024]; |
784 | e27c88fe | Gerd Hoffmann | const char *p,*pe,*pc; |
785 | e27c88fe | Gerd Hoffmann | |
786 | 2cfa571f | Mark McLoughlin | for (p = params; *p != '\0'; p++) { |
787 | e27c88fe | Gerd Hoffmann | pe = strchr(p, '=');
|
788 | e27c88fe | Gerd Hoffmann | pc = strchr(p, ',');
|
789 | e27c88fe | Gerd Hoffmann | if (!pe || (pc && pc < pe)) {
|
790 | e27c88fe | Gerd Hoffmann | /* found "foo,more" */
|
791 | e27c88fe | Gerd Hoffmann | if (p == params && firstname) {
|
792 | e27c88fe | Gerd Hoffmann | /* implicitly named first option */
|
793 | e27c88fe | Gerd Hoffmann | pstrcpy(option, sizeof(option), firstname);
|
794 | e27c88fe | Gerd Hoffmann | p = get_opt_value(value, sizeof(value), p);
|
795 | e27c88fe | Gerd Hoffmann | } else {
|
796 | e27c88fe | Gerd Hoffmann | /* option without value, probably a flag */
|
797 | e27c88fe | Gerd Hoffmann | p = get_opt_name(option, sizeof(option), p, ','); |
798 | 96729cbd | Gerd Hoffmann | if (strncmp(option, "no", 2) == 0) { |
799 | e27c88fe | Gerd Hoffmann | memmove(option, option+2, strlen(option+2)+1); |
800 | e27c88fe | Gerd Hoffmann | pstrcpy(value, sizeof(value), "off"); |
801 | e27c88fe | Gerd Hoffmann | } else {
|
802 | e27c88fe | Gerd Hoffmann | pstrcpy(value, sizeof(value), "on"); |
803 | e27c88fe | Gerd Hoffmann | } |
804 | e27c88fe | Gerd Hoffmann | } |
805 | e27c88fe | Gerd Hoffmann | } else {
|
806 | e27c88fe | Gerd Hoffmann | /* found "foo=bar,more" */
|
807 | e27c88fe | Gerd Hoffmann | p = get_opt_name(option, sizeof(option), p, '='); |
808 | e27c88fe | Gerd Hoffmann | if (*p != '=') { |
809 | e27c88fe | Gerd Hoffmann | break;
|
810 | e27c88fe | Gerd Hoffmann | } |
811 | e27c88fe | Gerd Hoffmann | p++; |
812 | e27c88fe | Gerd Hoffmann | p = get_opt_value(value, sizeof(value), p);
|
813 | e27c88fe | Gerd Hoffmann | } |
814 | e27c88fe | Gerd Hoffmann | if (strcmp(option, "id") != 0) { |
815 | e27c88fe | Gerd Hoffmann | /* store and parse */
|
816 | 3df04ac3 | Mark McLoughlin | if (qemu_opt_set(opts, option, value) == -1) { |
817 | 96729cbd | Gerd Hoffmann | return -1; |
818 | e27c88fe | Gerd Hoffmann | } |
819 | e27c88fe | Gerd Hoffmann | } |
820 | e27c88fe | Gerd Hoffmann | if (*p != ',') { |
821 | e27c88fe | Gerd Hoffmann | break;
|
822 | e27c88fe | Gerd Hoffmann | } |
823 | e27c88fe | Gerd Hoffmann | } |
824 | 96729cbd | Gerd Hoffmann | return 0; |
825 | 96729cbd | Gerd Hoffmann | } |
826 | 96729cbd | Gerd Hoffmann | |
827 | 8212c64f | Markus Armbruster | QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, |
828 | 8212c64f | Markus Armbruster | int permit_abbrev)
|
829 | 96729cbd | Gerd Hoffmann | { |
830 | 8212c64f | Markus Armbruster | const char *firstname; |
831 | d318ff99 | Gerd Hoffmann | char value[1024], *id = NULL; |
832 | 96729cbd | Gerd Hoffmann | const char *p; |
833 | 96729cbd | Gerd Hoffmann | QemuOpts *opts; |
834 | 96729cbd | Gerd Hoffmann | |
835 | 8212c64f | Markus Armbruster | assert(!permit_abbrev || list->implied_opt_name); |
836 | 8212c64f | Markus Armbruster | firstname = permit_abbrev ? list->implied_opt_name : NULL;
|
837 | 8212c64f | Markus Armbruster | |
838 | 96729cbd | Gerd Hoffmann | if (strncmp(params, "id=", 3) == 0) { |
839 | 96729cbd | Gerd Hoffmann | get_opt_value(value, sizeof(value), params+3); |
840 | d510c5cf | Jan Kiszka | id = value; |
841 | 96729cbd | Gerd Hoffmann | } else if ((p = strstr(params, ",id=")) != NULL) { |
842 | 96729cbd | Gerd Hoffmann | get_opt_value(value, sizeof(value), p+4); |
843 | d510c5cf | Jan Kiszka | id = value; |
844 | 96729cbd | Gerd Hoffmann | } |
845 | 96729cbd | Gerd Hoffmann | opts = qemu_opts_create(list, id, 1);
|
846 | 96729cbd | Gerd Hoffmann | if (opts == NULL) |
847 | 96729cbd | Gerd Hoffmann | return NULL; |
848 | 96729cbd | Gerd Hoffmann | |
849 | 96729cbd | Gerd Hoffmann | if (qemu_opts_do_parse(opts, params, firstname) != 0) { |
850 | 96729cbd | Gerd Hoffmann | qemu_opts_del(opts); |
851 | 96729cbd | Gerd Hoffmann | return NULL; |
852 | 96729cbd | Gerd Hoffmann | } |
853 | 96729cbd | Gerd Hoffmann | |
854 | e27c88fe | Gerd Hoffmann | return opts;
|
855 | e27c88fe | Gerd Hoffmann | } |
856 | e27c88fe | Gerd Hoffmann | |
857 | 01e7f188 | Markus Armbruster | static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque) |
858 | 01e7f188 | Markus Armbruster | { |
859 | 01e7f188 | Markus Armbruster | char buf[32]; |
860 | 01e7f188 | Markus Armbruster | const char *value; |
861 | 01e7f188 | Markus Armbruster | int n;
|
862 | 01e7f188 | Markus Armbruster | |
863 | 01e7f188 | Markus Armbruster | if (!strcmp(key, "id")) { |
864 | 01e7f188 | Markus Armbruster | return;
|
865 | 01e7f188 | Markus Armbruster | } |
866 | 01e7f188 | Markus Armbruster | |
867 | 01e7f188 | Markus Armbruster | switch (qobject_type(obj)) {
|
868 | 01e7f188 | Markus Armbruster | case QTYPE_QSTRING:
|
869 | 01e7f188 | Markus Armbruster | value = qstring_get_str(qobject_to_qstring(obj)); |
870 | 01e7f188 | Markus Armbruster | break;
|
871 | 01e7f188 | Markus Armbruster | case QTYPE_QINT:
|
872 | 01e7f188 | Markus Armbruster | n = snprintf(buf, sizeof(buf), "%" PRId64, |
873 | 01e7f188 | Markus Armbruster | qint_get_int(qobject_to_qint(obj))); |
874 | 01e7f188 | Markus Armbruster | assert(n < sizeof(buf));
|
875 | 01e7f188 | Markus Armbruster | value = buf; |
876 | 01e7f188 | Markus Armbruster | break;
|
877 | 01e7f188 | Markus Armbruster | case QTYPE_QFLOAT:
|
878 | 01e7f188 | Markus Armbruster | n = snprintf(buf, sizeof(buf), "%.17g", |
879 | 01e7f188 | Markus Armbruster | qfloat_get_double(qobject_to_qfloat(obj))); |
880 | 01e7f188 | Markus Armbruster | assert(n < sizeof(buf));
|
881 | 01e7f188 | Markus Armbruster | value = buf; |
882 | 01e7f188 | Markus Armbruster | break;
|
883 | 01e7f188 | Markus Armbruster | case QTYPE_QBOOL:
|
884 | d35215f8 | Blue Swirl | pstrcpy(buf, sizeof(buf),
|
885 | d35215f8 | Blue Swirl | qbool_get_int(qobject_to_qbool(obj)) ? "on" : "off"); |
886 | 01e7f188 | Markus Armbruster | value = buf; |
887 | 01e7f188 | Markus Armbruster | break;
|
888 | 01e7f188 | Markus Armbruster | default:
|
889 | 01e7f188 | Markus Armbruster | return;
|
890 | 01e7f188 | Markus Armbruster | } |
891 | 01e7f188 | Markus Armbruster | qemu_opt_set(opaque, key, value); |
892 | 01e7f188 | Markus Armbruster | } |
893 | 01e7f188 | Markus Armbruster | |
894 | 01e7f188 | Markus Armbruster | /*
|
895 | 01e7f188 | Markus Armbruster | * Create QemuOpts from a QDict.
|
896 | 01e7f188 | Markus Armbruster | * Use value of key "id" as ID if it exists and is a QString.
|
897 | 01e7f188 | Markus Armbruster | * Only QStrings, QInts, QFloats and QBools are copied. Entries with
|
898 | 01e7f188 | Markus Armbruster | * other types are silently ignored.
|
899 | 01e7f188 | Markus Armbruster | */
|
900 | 01e7f188 | Markus Armbruster | QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict)
|
901 | 01e7f188 | Markus Armbruster | { |
902 | 01e7f188 | Markus Armbruster | QemuOpts *opts; |
903 | 01e7f188 | Markus Armbruster | |
904 | 01e7f188 | Markus Armbruster | opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1); |
905 | 01e7f188 | Markus Armbruster | if (opts == NULL) |
906 | 01e7f188 | Markus Armbruster | return NULL; |
907 | 01e7f188 | Markus Armbruster | |
908 | 01e7f188 | Markus Armbruster | qdict_iter(qdict, qemu_opts_from_qdict_1, opts); |
909 | 01e7f188 | Markus Armbruster | return opts;
|
910 | 01e7f188 | Markus Armbruster | } |
911 | 01e7f188 | Markus Armbruster | |
912 | 01e7f188 | Markus Armbruster | /*
|
913 | 01e7f188 | Markus Armbruster | * Convert from QemuOpts to QDict.
|
914 | 01e7f188 | Markus Armbruster | * The QDict values are of type QString.
|
915 | 01e7f188 | Markus Armbruster | * TODO We'll want to use types appropriate for opt->desc->type, but
|
916 | 01e7f188 | Markus Armbruster | * this is enough for now.
|
917 | 01e7f188 | Markus Armbruster | */
|
918 | 01e7f188 | Markus Armbruster | QDict *qemu_opts_to_qdict(QemuOpts *opts, QDict *qdict) |
919 | 01e7f188 | Markus Armbruster | { |
920 | 01e7f188 | Markus Armbruster | QemuOpt *opt; |
921 | 01e7f188 | Markus Armbruster | QObject *val; |
922 | 01e7f188 | Markus Armbruster | |
923 | 01e7f188 | Markus Armbruster | if (!qdict) {
|
924 | 01e7f188 | Markus Armbruster | qdict = qdict_new(); |
925 | 01e7f188 | Markus Armbruster | } |
926 | 01e7f188 | Markus Armbruster | if (opts->id) {
|
927 | 01e7f188 | Markus Armbruster | qdict_put(qdict, "id", qstring_from_str(opts->id));
|
928 | 01e7f188 | Markus Armbruster | } |
929 | 01e7f188 | Markus Armbruster | QTAILQ_FOREACH(opt, &opts->head, next) { |
930 | 01e7f188 | Markus Armbruster | val = QOBJECT(qstring_from_str(opt->str)); |
931 | 01e7f188 | Markus Armbruster | qdict_put_obj(qdict, opt->name, val); |
932 | 01e7f188 | Markus Armbruster | } |
933 | 01e7f188 | Markus Armbruster | return qdict;
|
934 | 01e7f188 | Markus Armbruster | } |
935 | 01e7f188 | Markus Armbruster | |
936 | 5dc519ef | Mark McLoughlin | /* Validate parsed opts against descriptions where no
|
937 | 5dc519ef | Mark McLoughlin | * descriptions were provided in the QemuOptsList.
|
938 | 5dc519ef | Mark McLoughlin | */
|
939 | 238431a9 | Blue Swirl | int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc) |
940 | 5dc519ef | Mark McLoughlin | { |
941 | 5dc519ef | Mark McLoughlin | QemuOpt *opt; |
942 | 5dc519ef | Mark McLoughlin | |
943 | 5dc519ef | Mark McLoughlin | assert(opts->list->desc[0].name == NULL); |
944 | 5dc519ef | Mark McLoughlin | |
945 | 5dc519ef | Mark McLoughlin | QTAILQ_FOREACH(opt, &opts->head, next) { |
946 | 5dc519ef | Mark McLoughlin | int i;
|
947 | 5dc519ef | Mark McLoughlin | |
948 | 5dc519ef | Mark McLoughlin | for (i = 0; desc[i].name != NULL; i++) { |
949 | 5dc519ef | Mark McLoughlin | if (strcmp(desc[i].name, opt->name) == 0) { |
950 | 5dc519ef | Mark McLoughlin | break;
|
951 | 5dc519ef | Mark McLoughlin | } |
952 | 5dc519ef | Mark McLoughlin | } |
953 | 5dc519ef | Mark McLoughlin | if (desc[i].name == NULL) { |
954 | db716e9c | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER, opt->name); |
955 | 5dc519ef | Mark McLoughlin | return -1; |
956 | 5dc519ef | Mark McLoughlin | } |
957 | 5dc519ef | Mark McLoughlin | |
958 | 5dc519ef | Mark McLoughlin | opt->desc = &desc[i]; |
959 | 5dc519ef | Mark McLoughlin | |
960 | 5dc519ef | Mark McLoughlin | if (qemu_opt_parse(opt) < 0) { |
961 | 5dc519ef | Mark McLoughlin | return -1; |
962 | 5dc519ef | Mark McLoughlin | } |
963 | 5dc519ef | Mark McLoughlin | } |
964 | 5dc519ef | Mark McLoughlin | |
965 | 5dc519ef | Mark McLoughlin | return 0; |
966 | 5dc519ef | Mark McLoughlin | } |
967 | 5dc519ef | Mark McLoughlin | |
968 | e27c88fe | Gerd Hoffmann | int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque, |
969 | e27c88fe | Gerd Hoffmann | int abort_on_failure)
|
970 | e27c88fe | Gerd Hoffmann | { |
971 | 827b0813 | Markus Armbruster | Location loc; |
972 | e27c88fe | Gerd Hoffmann | QemuOpts *opts; |
973 | e27c88fe | Gerd Hoffmann | int rc = 0; |
974 | e27c88fe | Gerd Hoffmann | |
975 | 827b0813 | Markus Armbruster | loc_push_none(&loc); |
976 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(opts, &list->head, next) { |
977 | 827b0813 | Markus Armbruster | loc_restore(&opts->loc); |
978 | 4a2594dd | Markus Armbruster | rc |= func(opts, opaque); |
979 | e27c88fe | Gerd Hoffmann | if (abort_on_failure && rc != 0) |
980 | e27c88fe | Gerd Hoffmann | break;
|
981 | e27c88fe | Gerd Hoffmann | } |
982 | 827b0813 | Markus Armbruster | loc_pop(&loc); |
983 | e27c88fe | Gerd Hoffmann | return rc;
|
984 | e27c88fe | Gerd Hoffmann | } |