root / qemu-option.c @ 0f0bc3f1
History | View | Annotate | Download (20.8 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 | d3f24367 | Kevin Wolf | #include "qemu-option.h" |
32 | d3f24367 | Kevin Wolf | |
33 | d3f24367 | Kevin Wolf | /*
|
34 | d3f24367 | Kevin Wolf | * Extracts the name of an option from the parameter string (p points at the
|
35 | d3f24367 | Kevin Wolf | * first byte of the option name)
|
36 | d3f24367 | Kevin Wolf | *
|
37 | d3f24367 | Kevin Wolf | * The option name is delimited by delim (usually , or =) or the string end
|
38 | d3f24367 | Kevin Wolf | * and is copied into buf. If the option name is longer than buf_size, it is
|
39 | d3f24367 | Kevin Wolf | * truncated. buf is always zero terminated.
|
40 | d3f24367 | Kevin Wolf | *
|
41 | d3f24367 | Kevin Wolf | * The return value is the position of the delimiter/zero byte after the option
|
42 | d3f24367 | Kevin Wolf | * name in p.
|
43 | d3f24367 | Kevin Wolf | */
|
44 | d3f24367 | Kevin Wolf | const char *get_opt_name(char *buf, int buf_size, const char *p, char delim) |
45 | d3f24367 | Kevin Wolf | { |
46 | d3f24367 | Kevin Wolf | char *q;
|
47 | d3f24367 | Kevin Wolf | |
48 | d3f24367 | Kevin Wolf | q = buf; |
49 | d3f24367 | Kevin Wolf | while (*p != '\0' && *p != delim) { |
50 | d3f24367 | Kevin Wolf | if (q && (q - buf) < buf_size - 1) |
51 | d3f24367 | Kevin Wolf | *q++ = *p; |
52 | d3f24367 | Kevin Wolf | p++; |
53 | d3f24367 | Kevin Wolf | } |
54 | d3f24367 | Kevin Wolf | if (q)
|
55 | d3f24367 | Kevin Wolf | *q = '\0';
|
56 | d3f24367 | Kevin Wolf | |
57 | d3f24367 | Kevin Wolf | return p;
|
58 | d3f24367 | Kevin Wolf | } |
59 | d3f24367 | Kevin Wolf | |
60 | d3f24367 | Kevin Wolf | /*
|
61 | d3f24367 | Kevin Wolf | * Extracts the value of an option from the parameter string p (p points at the
|
62 | d3f24367 | Kevin Wolf | * first byte of the option value)
|
63 | d3f24367 | Kevin Wolf | *
|
64 | d3f24367 | Kevin Wolf | * This function is comparable to get_opt_name with the difference that the
|
65 | d3f24367 | Kevin Wolf | * delimiter is fixed to be comma which starts a new option. To specify an
|
66 | d3f24367 | Kevin Wolf | * option value that contains commas, double each comma.
|
67 | d3f24367 | Kevin Wolf | */
|
68 | d3f24367 | Kevin Wolf | const char *get_opt_value(char *buf, int buf_size, const char *p) |
69 | d3f24367 | Kevin Wolf | { |
70 | d3f24367 | Kevin Wolf | char *q;
|
71 | d3f24367 | Kevin Wolf | |
72 | d3f24367 | Kevin Wolf | q = buf; |
73 | d3f24367 | Kevin Wolf | while (*p != '\0') { |
74 | d3f24367 | Kevin Wolf | if (*p == ',') { |
75 | d3f24367 | Kevin Wolf | if (*(p + 1) != ',') |
76 | d3f24367 | Kevin Wolf | break;
|
77 | d3f24367 | Kevin Wolf | p++; |
78 | d3f24367 | Kevin Wolf | } |
79 | d3f24367 | Kevin Wolf | if (q && (q - buf) < buf_size - 1) |
80 | d3f24367 | Kevin Wolf | *q++ = *p; |
81 | d3f24367 | Kevin Wolf | p++; |
82 | d3f24367 | Kevin Wolf | } |
83 | d3f24367 | Kevin Wolf | if (q)
|
84 | d3f24367 | Kevin Wolf | *q = '\0';
|
85 | d3f24367 | Kevin Wolf | |
86 | d3f24367 | Kevin Wolf | return p;
|
87 | d3f24367 | Kevin Wolf | } |
88 | d3f24367 | Kevin Wolf | |
89 | 62c5802e | Gerd Hoffmann | int get_next_param_value(char *buf, int buf_size, |
90 | 62c5802e | Gerd Hoffmann | const char *tag, const char **pstr) |
91 | 62c5802e | Gerd Hoffmann | { |
92 | 62c5802e | Gerd Hoffmann | const char *p; |
93 | 62c5802e | Gerd Hoffmann | char option[128]; |
94 | 62c5802e | Gerd Hoffmann | |
95 | 62c5802e | Gerd Hoffmann | p = *pstr; |
96 | 62c5802e | Gerd Hoffmann | for(;;) {
|
97 | 62c5802e | Gerd Hoffmann | p = get_opt_name(option, sizeof(option), p, '='); |
98 | 62c5802e | Gerd Hoffmann | if (*p != '=') |
99 | 62c5802e | Gerd Hoffmann | break;
|
100 | 62c5802e | Gerd Hoffmann | p++; |
101 | 62c5802e | Gerd Hoffmann | if (!strcmp(tag, option)) {
|
102 | 62c5802e | Gerd Hoffmann | *pstr = get_opt_value(buf, buf_size, p); |
103 | 62c5802e | Gerd Hoffmann | if (**pstr == ',') { |
104 | 62c5802e | Gerd Hoffmann | (*pstr)++; |
105 | 62c5802e | Gerd Hoffmann | } |
106 | 62c5802e | Gerd Hoffmann | return strlen(buf);
|
107 | 62c5802e | Gerd Hoffmann | } else {
|
108 | 62c5802e | Gerd Hoffmann | p = get_opt_value(NULL, 0, p); |
109 | 62c5802e | Gerd Hoffmann | } |
110 | 62c5802e | Gerd Hoffmann | if (*p != ',') |
111 | 62c5802e | Gerd Hoffmann | break;
|
112 | 62c5802e | Gerd Hoffmann | p++; |
113 | 62c5802e | Gerd Hoffmann | } |
114 | 62c5802e | Gerd Hoffmann | return 0; |
115 | 62c5802e | Gerd Hoffmann | } |
116 | 62c5802e | Gerd Hoffmann | |
117 | 62c5802e | Gerd Hoffmann | int get_param_value(char *buf, int buf_size, |
118 | 62c5802e | Gerd Hoffmann | const char *tag, const char *str) |
119 | 62c5802e | Gerd Hoffmann | { |
120 | 62c5802e | Gerd Hoffmann | return get_next_param_value(buf, buf_size, tag, &str);
|
121 | 62c5802e | Gerd Hoffmann | } |
122 | 62c5802e | Gerd Hoffmann | |
123 | 62c5802e | Gerd Hoffmann | int check_params(char *buf, int buf_size, |
124 | 62c5802e | Gerd Hoffmann | const char * const *params, const char *str) |
125 | 62c5802e | Gerd Hoffmann | { |
126 | 62c5802e | Gerd Hoffmann | const char *p; |
127 | 62c5802e | Gerd Hoffmann | int i;
|
128 | 62c5802e | Gerd Hoffmann | |
129 | 62c5802e | Gerd Hoffmann | p = str; |
130 | 62c5802e | Gerd Hoffmann | while (*p != '\0') { |
131 | 62c5802e | Gerd Hoffmann | p = get_opt_name(buf, buf_size, p, '=');
|
132 | 62c5802e | Gerd Hoffmann | if (*p != '=') { |
133 | 62c5802e | Gerd Hoffmann | return -1; |
134 | 62c5802e | Gerd Hoffmann | } |
135 | 62c5802e | Gerd Hoffmann | p++; |
136 | 62c5802e | Gerd Hoffmann | for (i = 0; params[i] != NULL; i++) { |
137 | 62c5802e | Gerd Hoffmann | if (!strcmp(params[i], buf)) {
|
138 | 62c5802e | Gerd Hoffmann | break;
|
139 | 62c5802e | Gerd Hoffmann | } |
140 | 62c5802e | Gerd Hoffmann | } |
141 | 62c5802e | Gerd Hoffmann | if (params[i] == NULL) { |
142 | 62c5802e | Gerd Hoffmann | return -1; |
143 | 62c5802e | Gerd Hoffmann | } |
144 | 62c5802e | Gerd Hoffmann | p = get_opt_value(NULL, 0, p); |
145 | 62c5802e | Gerd Hoffmann | if (*p != ',') { |
146 | 62c5802e | Gerd Hoffmann | break;
|
147 | 62c5802e | Gerd Hoffmann | } |
148 | 62c5802e | Gerd Hoffmann | p++; |
149 | 62c5802e | Gerd Hoffmann | } |
150 | 62c5802e | Gerd Hoffmann | return 0; |
151 | 62c5802e | Gerd Hoffmann | } |
152 | 62c5802e | Gerd Hoffmann | |
153 | d3f24367 | Kevin Wolf | /*
|
154 | d3f24367 | Kevin Wolf | * Searches an option list for an option with the given name
|
155 | d3f24367 | Kevin Wolf | */
|
156 | d3f24367 | Kevin Wolf | QEMUOptionParameter *get_option_parameter(QEMUOptionParameter *list, |
157 | d3f24367 | Kevin Wolf | const char *name) |
158 | d3f24367 | Kevin Wolf | { |
159 | d3f24367 | Kevin Wolf | while (list && list->name) {
|
160 | d3f24367 | Kevin Wolf | if (!strcmp(list->name, name)) {
|
161 | d3f24367 | Kevin Wolf | return list;
|
162 | d3f24367 | Kevin Wolf | } |
163 | d3f24367 | Kevin Wolf | list++; |
164 | d3f24367 | Kevin Wolf | } |
165 | d3f24367 | Kevin Wolf | |
166 | d3f24367 | Kevin Wolf | return NULL; |
167 | d3f24367 | Kevin Wolf | } |
168 | d3f24367 | Kevin Wolf | |
169 | 67b1355b | Gerd Hoffmann | static int parse_option_bool(const char *name, const char *value, int *ret) |
170 | 67b1355b | Gerd Hoffmann | { |
171 | 67b1355b | Gerd Hoffmann | if (value != NULL) { |
172 | 67b1355b | Gerd Hoffmann | if (!strcmp(value, "on")) { |
173 | 67b1355b | Gerd Hoffmann | *ret = 1;
|
174 | 67b1355b | Gerd Hoffmann | } else if (!strcmp(value, "off")) { |
175 | 67b1355b | Gerd Hoffmann | *ret = 0;
|
176 | 67b1355b | Gerd Hoffmann | } else {
|
177 | 67b1355b | Gerd Hoffmann | fprintf(stderr, "Option '%s': Use 'on' or 'off'\n", name);
|
178 | 67b1355b | Gerd Hoffmann | return -1; |
179 | 67b1355b | Gerd Hoffmann | } |
180 | 67b1355b | Gerd Hoffmann | } else {
|
181 | 67b1355b | Gerd Hoffmann | *ret = 1;
|
182 | 67b1355b | Gerd Hoffmann | } |
183 | 67b1355b | Gerd Hoffmann | return 0; |
184 | 67b1355b | Gerd Hoffmann | } |
185 | 67b1355b | Gerd Hoffmann | |
186 | e27c88fe | Gerd Hoffmann | static int parse_option_number(const char *name, const char *value, uint64_t *ret) |
187 | e27c88fe | Gerd Hoffmann | { |
188 | e27c88fe | Gerd Hoffmann | char *postfix;
|
189 | e27c88fe | Gerd Hoffmann | uint64_t number; |
190 | e27c88fe | Gerd Hoffmann | |
191 | e27c88fe | Gerd Hoffmann | if (value != NULL) { |
192 | e27c88fe | Gerd Hoffmann | number = strtoull(value, &postfix, 0);
|
193 | e27c88fe | Gerd Hoffmann | if (*postfix != '\0') { |
194 | e27c88fe | Gerd Hoffmann | fprintf(stderr, "Option '%s' needs a number as parameter\n", name);
|
195 | e27c88fe | Gerd Hoffmann | return -1; |
196 | e27c88fe | Gerd Hoffmann | } |
197 | 21c9f4cd | Gerd Hoffmann | *ret = number; |
198 | e27c88fe | Gerd Hoffmann | } else {
|
199 | e27c88fe | Gerd Hoffmann | fprintf(stderr, "Option '%s' needs a parameter\n", name);
|
200 | e27c88fe | Gerd Hoffmann | return -1; |
201 | e27c88fe | Gerd Hoffmann | } |
202 | e27c88fe | Gerd Hoffmann | return 0; |
203 | e27c88fe | Gerd Hoffmann | } |
204 | e27c88fe | Gerd Hoffmann | |
205 | 7695019b | Gerd Hoffmann | static int parse_option_size(const char *name, const char *value, uint64_t *ret) |
206 | 7695019b | Gerd Hoffmann | { |
207 | 7695019b | Gerd Hoffmann | char *postfix;
|
208 | 7695019b | Gerd Hoffmann | double sizef;
|
209 | 7695019b | Gerd Hoffmann | |
210 | 7695019b | Gerd Hoffmann | if (value != NULL) { |
211 | 7695019b | Gerd Hoffmann | sizef = strtod(value, &postfix); |
212 | 7695019b | Gerd Hoffmann | switch (*postfix) {
|
213 | 7695019b | Gerd Hoffmann | case 'T': |
214 | 7695019b | Gerd Hoffmann | sizef *= 1024;
|
215 | 7695019b | Gerd Hoffmann | case 'G': |
216 | 7695019b | Gerd Hoffmann | sizef *= 1024;
|
217 | 7695019b | Gerd Hoffmann | case 'M': |
218 | 7695019b | Gerd Hoffmann | sizef *= 1024;
|
219 | 7695019b | Gerd Hoffmann | case 'K': |
220 | 7695019b | Gerd Hoffmann | case 'k': |
221 | 7695019b | Gerd Hoffmann | sizef *= 1024;
|
222 | 7695019b | Gerd Hoffmann | case 'b': |
223 | 7695019b | Gerd Hoffmann | case '\0': |
224 | 7695019b | Gerd Hoffmann | *ret = (uint64_t) sizef; |
225 | 7695019b | Gerd Hoffmann | break;
|
226 | 7695019b | Gerd Hoffmann | default:
|
227 | 7695019b | Gerd Hoffmann | fprintf(stderr, "Option '%s' needs size as parameter\n", name);
|
228 | 7695019b | Gerd Hoffmann | fprintf(stderr, "You may use k, M, G or T suffixes for "
|
229 | 7695019b | Gerd Hoffmann | "kilobytes, megabytes, gigabytes and terabytes.\n");
|
230 | 7695019b | Gerd Hoffmann | return -1; |
231 | 7695019b | Gerd Hoffmann | } |
232 | 7695019b | Gerd Hoffmann | } else {
|
233 | 7695019b | Gerd Hoffmann | fprintf(stderr, "Option '%s' needs a parameter\n", name);
|
234 | 7695019b | Gerd Hoffmann | return -1; |
235 | 7695019b | Gerd Hoffmann | } |
236 | 7695019b | Gerd Hoffmann | return 0; |
237 | 7695019b | Gerd Hoffmann | } |
238 | 7695019b | Gerd Hoffmann | |
239 | d3f24367 | Kevin Wolf | /*
|
240 | d3f24367 | Kevin Wolf | * Sets the value of a parameter in a given option list. The parsing of the
|
241 | d3f24367 | Kevin Wolf | * value depends on the type of option:
|
242 | d3f24367 | Kevin Wolf | *
|
243 | d3f24367 | Kevin Wolf | * OPT_FLAG (uses value.n):
|
244 | d3f24367 | Kevin Wolf | * If no value is given, the flag is set to 1.
|
245 | d3f24367 | Kevin Wolf | * Otherwise the value must be "on" (set to 1) or "off" (set to 0)
|
246 | d3f24367 | Kevin Wolf | *
|
247 | d3f24367 | Kevin Wolf | * OPT_STRING (uses value.s):
|
248 | d3f24367 | Kevin Wolf | * value is strdup()ed and assigned as option value
|
249 | d3f24367 | Kevin Wolf | *
|
250 | d3f24367 | Kevin Wolf | * OPT_SIZE (uses value.n):
|
251 | d3f24367 | Kevin Wolf | * The value is converted to an integer. Suffixes for kilobytes etc. are
|
252 | d3f24367 | Kevin Wolf | * allowed (powers of 1024).
|
253 | d3f24367 | Kevin Wolf | *
|
254 | d3f24367 | Kevin Wolf | * Returns 0 on succes, -1 in error cases
|
255 | d3f24367 | Kevin Wolf | */
|
256 | d3f24367 | Kevin Wolf | int set_option_parameter(QEMUOptionParameter *list, const char *name, |
257 | d3f24367 | Kevin Wolf | const char *value) |
258 | d3f24367 | Kevin Wolf | { |
259 | 67b1355b | Gerd Hoffmann | int flag;
|
260 | 67b1355b | Gerd Hoffmann | |
261 | d3f24367 | Kevin Wolf | // Find a matching parameter
|
262 | d3f24367 | Kevin Wolf | list = get_option_parameter(list, name); |
263 | d3f24367 | Kevin Wolf | if (list == NULL) { |
264 | d3f24367 | Kevin Wolf | fprintf(stderr, "Unknown option '%s'\n", name);
|
265 | d3f24367 | Kevin Wolf | return -1; |
266 | d3f24367 | Kevin Wolf | } |
267 | d3f24367 | Kevin Wolf | |
268 | d3f24367 | Kevin Wolf | // Process parameter
|
269 | d3f24367 | Kevin Wolf | switch (list->type) {
|
270 | d3f24367 | Kevin Wolf | case OPT_FLAG:
|
271 | 3df04ac3 | Mark McLoughlin | if (parse_option_bool(name, value, &flag) == -1) |
272 | 67b1355b | Gerd Hoffmann | return -1; |
273 | 67b1355b | Gerd Hoffmann | list->value.n = flag; |
274 | d3f24367 | Kevin Wolf | break;
|
275 | d3f24367 | Kevin Wolf | |
276 | d3f24367 | Kevin Wolf | case OPT_STRING:
|
277 | d3f24367 | Kevin Wolf | if (value != NULL) { |
278 | d4c3fddd | Jean-Christophe DUBOIS | list->value.s = qemu_strdup(value); |
279 | d3f24367 | Kevin Wolf | } else {
|
280 | d3f24367 | Kevin Wolf | fprintf(stderr, "Option '%s' needs a parameter\n", name);
|
281 | d3f24367 | Kevin Wolf | return -1; |
282 | d3f24367 | Kevin Wolf | } |
283 | d3f24367 | Kevin Wolf | break;
|
284 | d3f24367 | Kevin Wolf | |
285 | d3f24367 | Kevin Wolf | case OPT_SIZE:
|
286 | 3df04ac3 | Mark McLoughlin | if (parse_option_size(name, value, &list->value.n) == -1) |
287 | d3f24367 | Kevin Wolf | return -1; |
288 | d3f24367 | Kevin Wolf | break;
|
289 | 7695019b | Gerd Hoffmann | |
290 | d3f24367 | Kevin Wolf | default:
|
291 | d3f24367 | Kevin Wolf | fprintf(stderr, "Bug: Option '%s' has an unknown type\n", name);
|
292 | d3f24367 | Kevin Wolf | return -1; |
293 | d3f24367 | Kevin Wolf | } |
294 | d3f24367 | Kevin Wolf | |
295 | d3f24367 | Kevin Wolf | return 0; |
296 | d3f24367 | Kevin Wolf | } |
297 | d3f24367 | Kevin Wolf | |
298 | d3f24367 | Kevin Wolf | /*
|
299 | d3f24367 | Kevin Wolf | * Sets the given parameter to an integer instead of a string.
|
300 | d3f24367 | Kevin Wolf | * This function cannot be used to set string options.
|
301 | d3f24367 | Kevin Wolf | *
|
302 | d3f24367 | Kevin Wolf | * Returns 0 on success, -1 in error cases
|
303 | d3f24367 | Kevin Wolf | */
|
304 | d3f24367 | Kevin Wolf | int set_option_parameter_int(QEMUOptionParameter *list, const char *name, |
305 | d3f24367 | Kevin Wolf | uint64_t value) |
306 | d3f24367 | Kevin Wolf | { |
307 | d3f24367 | Kevin Wolf | // Find a matching parameter
|
308 | d3f24367 | Kevin Wolf | list = get_option_parameter(list, name); |
309 | d3f24367 | Kevin Wolf | if (list == NULL) { |
310 | d3f24367 | Kevin Wolf | fprintf(stderr, "Unknown option '%s'\n", name);
|
311 | d3f24367 | Kevin Wolf | return -1; |
312 | d3f24367 | Kevin Wolf | } |
313 | d3f24367 | Kevin Wolf | |
314 | d3f24367 | Kevin Wolf | // Process parameter
|
315 | d3f24367 | Kevin Wolf | switch (list->type) {
|
316 | d3f24367 | Kevin Wolf | case OPT_FLAG:
|
317 | d3f24367 | Kevin Wolf | case OPT_NUMBER:
|
318 | d3f24367 | Kevin Wolf | case OPT_SIZE:
|
319 | d3f24367 | Kevin Wolf | list->value.n = value; |
320 | d3f24367 | Kevin Wolf | break;
|
321 | d3f24367 | Kevin Wolf | |
322 | d3f24367 | Kevin Wolf | default:
|
323 | d3f24367 | Kevin Wolf | return -1; |
324 | d3f24367 | Kevin Wolf | } |
325 | d3f24367 | Kevin Wolf | |
326 | d3f24367 | Kevin Wolf | return 0; |
327 | d3f24367 | Kevin Wolf | } |
328 | d3f24367 | Kevin Wolf | |
329 | d3f24367 | Kevin Wolf | /*
|
330 | d3f24367 | Kevin Wolf | * Frees a option list. If it contains strings, the strings are freed as well.
|
331 | d3f24367 | Kevin Wolf | */
|
332 | d3f24367 | Kevin Wolf | void free_option_parameters(QEMUOptionParameter *list)
|
333 | d3f24367 | Kevin Wolf | { |
334 | d3f24367 | Kevin Wolf | QEMUOptionParameter *cur = list; |
335 | d3f24367 | Kevin Wolf | |
336 | d3f24367 | Kevin Wolf | while (cur && cur->name) {
|
337 | d3f24367 | Kevin Wolf | if (cur->type == OPT_STRING) {
|
338 | d4c3fddd | Jean-Christophe DUBOIS | qemu_free(cur->value.s); |
339 | d3f24367 | Kevin Wolf | } |
340 | d3f24367 | Kevin Wolf | cur++; |
341 | d3f24367 | Kevin Wolf | } |
342 | d3f24367 | Kevin Wolf | |
343 | d4c3fddd | Jean-Christophe DUBOIS | qemu_free(list); |
344 | d3f24367 | Kevin Wolf | } |
345 | d3f24367 | Kevin Wolf | |
346 | d3f24367 | Kevin Wolf | /*
|
347 | d3f24367 | Kevin Wolf | * Parses a parameter string (param) into an option list (dest).
|
348 | d3f24367 | Kevin Wolf | *
|
349 | d3f24367 | Kevin Wolf | * list is the templace is. If dest is NULL, a new copy of list is created for
|
350 | d3f24367 | Kevin Wolf | * it. If list is NULL, this function fails.
|
351 | d3f24367 | Kevin Wolf | *
|
352 | d3f24367 | Kevin Wolf | * A parameter string consists of one or more parameters, separated by commas.
|
353 | d3f24367 | Kevin Wolf | * Each parameter consists of its name and possibly of a value. In the latter
|
354 | d3f24367 | Kevin Wolf | * case, the value is delimited by an = character. To specify a value which
|
355 | d3f24367 | Kevin Wolf | * contains commas, double each comma so it won't be recognized as the end of
|
356 | d3f24367 | Kevin Wolf | * the parameter.
|
357 | d3f24367 | Kevin Wolf | *
|
358 | d3f24367 | Kevin Wolf | * For more details of the parsing see above.
|
359 | d3f24367 | Kevin Wolf | *
|
360 | d3f24367 | Kevin Wolf | * Returns a pointer to the first element of dest (or the newly allocated copy)
|
361 | d3f24367 | Kevin Wolf | * or NULL in error cases
|
362 | d3f24367 | Kevin Wolf | */
|
363 | d3f24367 | Kevin Wolf | QEMUOptionParameter *parse_option_parameters(const char *param, |
364 | d3f24367 | Kevin Wolf | QEMUOptionParameter *list, QEMUOptionParameter *dest) |
365 | d3f24367 | Kevin Wolf | { |
366 | d3f24367 | Kevin Wolf | QEMUOptionParameter *cur; |
367 | d3f24367 | Kevin Wolf | QEMUOptionParameter *allocated = NULL;
|
368 | d3f24367 | Kevin Wolf | char name[256]; |
369 | d3f24367 | Kevin Wolf | char value[256]; |
370 | d3f24367 | Kevin Wolf | char *param_delim, *value_delim;
|
371 | d3f24367 | Kevin Wolf | char next_delim;
|
372 | d3f24367 | Kevin Wolf | size_t num_options; |
373 | d3f24367 | Kevin Wolf | |
374 | d3f24367 | Kevin Wolf | if (list == NULL) { |
375 | d3f24367 | Kevin Wolf | return NULL; |
376 | d3f24367 | Kevin Wolf | } |
377 | d3f24367 | Kevin Wolf | |
378 | d3f24367 | Kevin Wolf | if (dest == NULL) { |
379 | d3f24367 | Kevin Wolf | // Count valid options
|
380 | d3f24367 | Kevin Wolf | num_options = 0;
|
381 | d3f24367 | Kevin Wolf | cur = list; |
382 | d3f24367 | Kevin Wolf | while (cur->name) {
|
383 | d3f24367 | Kevin Wolf | num_options++; |
384 | d3f24367 | Kevin Wolf | cur++; |
385 | d3f24367 | Kevin Wolf | } |
386 | d3f24367 | Kevin Wolf | |
387 | d3f24367 | Kevin Wolf | // Create a copy of the option list to fill in values
|
388 | d3f24367 | Kevin Wolf | dest = qemu_mallocz((num_options + 1) * sizeof(QEMUOptionParameter)); |
389 | d3f24367 | Kevin Wolf | allocated = dest; |
390 | d3f24367 | Kevin Wolf | memcpy(dest, list, (num_options + 1) * sizeof(QEMUOptionParameter)); |
391 | d3f24367 | Kevin Wolf | } |
392 | d3f24367 | Kevin Wolf | |
393 | d3f24367 | Kevin Wolf | while (*param) {
|
394 | d3f24367 | Kevin Wolf | |
395 | d3f24367 | Kevin Wolf | // Find parameter name and value in the string
|
396 | d3f24367 | Kevin Wolf | param_delim = strchr(param, ',');
|
397 | d3f24367 | Kevin Wolf | value_delim = strchr(param, '=');
|
398 | d3f24367 | Kevin Wolf | |
399 | d3f24367 | Kevin Wolf | if (value_delim && (value_delim < param_delim || !param_delim)) {
|
400 | d3f24367 | Kevin Wolf | next_delim = '=';
|
401 | d3f24367 | Kevin Wolf | } else {
|
402 | d3f24367 | Kevin Wolf | next_delim = ',';
|
403 | d3f24367 | Kevin Wolf | value_delim = NULL;
|
404 | d3f24367 | Kevin Wolf | } |
405 | d3f24367 | Kevin Wolf | |
406 | d3f24367 | Kevin Wolf | param = get_opt_name(name, sizeof(name), param, next_delim);
|
407 | d3f24367 | Kevin Wolf | if (value_delim) {
|
408 | d3f24367 | Kevin Wolf | param = get_opt_value(value, sizeof(value), param + 1); |
409 | d3f24367 | Kevin Wolf | } |
410 | d3f24367 | Kevin Wolf | if (*param != '\0') { |
411 | d3f24367 | Kevin Wolf | param++; |
412 | d3f24367 | Kevin Wolf | } |
413 | d3f24367 | Kevin Wolf | |
414 | d3f24367 | Kevin Wolf | // Set the parameter
|
415 | d3f24367 | Kevin Wolf | if (set_option_parameter(dest, name, value_delim ? value : NULL)) { |
416 | d3f24367 | Kevin Wolf | goto fail;
|
417 | d3f24367 | Kevin Wolf | } |
418 | d3f24367 | Kevin Wolf | } |
419 | d3f24367 | Kevin Wolf | |
420 | d3f24367 | Kevin Wolf | return dest;
|
421 | d3f24367 | Kevin Wolf | |
422 | d3f24367 | Kevin Wolf | fail:
|
423 | d3f24367 | Kevin Wolf | // Only free the list if it was newly allocated
|
424 | d3f24367 | Kevin Wolf | free_option_parameters(allocated); |
425 | d3f24367 | Kevin Wolf | return NULL; |
426 | d3f24367 | Kevin Wolf | } |
427 | d3f24367 | Kevin Wolf | |
428 | d3f24367 | Kevin Wolf | /*
|
429 | d3f24367 | Kevin Wolf | * Prints all options of a list that have a value to stdout
|
430 | d3f24367 | Kevin Wolf | */
|
431 | d3f24367 | Kevin Wolf | void print_option_parameters(QEMUOptionParameter *list)
|
432 | d3f24367 | Kevin Wolf | { |
433 | d3f24367 | Kevin Wolf | while (list && list->name) {
|
434 | d3f24367 | Kevin Wolf | switch (list->type) {
|
435 | d3f24367 | Kevin Wolf | case OPT_STRING:
|
436 | d3f24367 | Kevin Wolf | if (list->value.s != NULL) { |
437 | d3f24367 | Kevin Wolf | printf("%s='%s' ", list->name, list->value.s);
|
438 | d3f24367 | Kevin Wolf | } |
439 | d3f24367 | Kevin Wolf | break;
|
440 | d3f24367 | Kevin Wolf | case OPT_FLAG:
|
441 | d3f24367 | Kevin Wolf | printf("%s=%s ", list->name, list->value.n ? "on" : "off"); |
442 | d3f24367 | Kevin Wolf | break;
|
443 | d3f24367 | Kevin Wolf | case OPT_SIZE:
|
444 | d3f24367 | Kevin Wolf | case OPT_NUMBER:
|
445 | d3f24367 | Kevin Wolf | printf("%s=%" PRId64 " ", list->name, list->value.n); |
446 | d3f24367 | Kevin Wolf | break;
|
447 | d3f24367 | Kevin Wolf | default:
|
448 | d3f24367 | Kevin Wolf | printf("%s=(unkown type) ", list->name);
|
449 | d3f24367 | Kevin Wolf | break;
|
450 | d3f24367 | Kevin Wolf | } |
451 | d3f24367 | Kevin Wolf | list++; |
452 | d3f24367 | Kevin Wolf | } |
453 | d3f24367 | Kevin Wolf | } |
454 | db08adf5 | Kevin Wolf | |
455 | db08adf5 | Kevin Wolf | /*
|
456 | db08adf5 | Kevin Wolf | * Prints an overview of all available options
|
457 | db08adf5 | Kevin Wolf | */
|
458 | db08adf5 | Kevin Wolf | void print_option_help(QEMUOptionParameter *list)
|
459 | db08adf5 | Kevin Wolf | { |
460 | db08adf5 | Kevin Wolf | printf("Supported options:\n");
|
461 | db08adf5 | Kevin Wolf | while (list && list->name) {
|
462 | db08adf5 | Kevin Wolf | printf("%-16s %s\n", list->name,
|
463 | db08adf5 | Kevin Wolf | list->help ? list->help : "No description available");
|
464 | db08adf5 | Kevin Wolf | list++; |
465 | db08adf5 | Kevin Wolf | } |
466 | db08adf5 | Kevin Wolf | } |
467 | e27c88fe | Gerd Hoffmann | |
468 | e27c88fe | Gerd Hoffmann | /* ------------------------------------------------------------------ */
|
469 | e27c88fe | Gerd Hoffmann | |
470 | e27c88fe | Gerd Hoffmann | struct QemuOpt {
|
471 | e27c88fe | Gerd Hoffmann | const char *name; |
472 | e27c88fe | Gerd Hoffmann | const char *str; |
473 | e27c88fe | Gerd Hoffmann | |
474 | 238431a9 | Blue Swirl | const QemuOptDesc *desc;
|
475 | e27c88fe | Gerd Hoffmann | union {
|
476 | 1f5c1775 | Juan Quintela | int boolean;
|
477 | e27c88fe | Gerd Hoffmann | uint64_t uint; |
478 | e27c88fe | Gerd Hoffmann | } value; |
479 | e27c88fe | Gerd Hoffmann | |
480 | e27c88fe | Gerd Hoffmann | QemuOpts *opts; |
481 | 72cf2d4f | Blue Swirl | QTAILQ_ENTRY(QemuOpt) next; |
482 | e27c88fe | Gerd Hoffmann | }; |
483 | e27c88fe | Gerd Hoffmann | |
484 | e27c88fe | Gerd Hoffmann | struct QemuOpts {
|
485 | b09417b9 | Jan Kiszka | char *id;
|
486 | e27c88fe | Gerd Hoffmann | QemuOptsList *list; |
487 | 827b0813 | Markus Armbruster | Location loc; |
488 | dc9ca4ba | Mark McLoughlin | QTAILQ_HEAD(QemuOptHead, QemuOpt) head; |
489 | 72cf2d4f | Blue Swirl | QTAILQ_ENTRY(QemuOpts) next; |
490 | e27c88fe | Gerd Hoffmann | }; |
491 | e27c88fe | Gerd Hoffmann | |
492 | e27c88fe | Gerd Hoffmann | static QemuOpt *qemu_opt_find(QemuOpts *opts, const char *name) |
493 | e27c88fe | Gerd Hoffmann | { |
494 | e27c88fe | Gerd Hoffmann | QemuOpt *opt; |
495 | e27c88fe | Gerd Hoffmann | |
496 | dc9ca4ba | Mark McLoughlin | QTAILQ_FOREACH_REVERSE(opt, &opts->head, QemuOptHead, next) { |
497 | e27c88fe | Gerd Hoffmann | if (strcmp(opt->name, name) != 0) |
498 | e27c88fe | Gerd Hoffmann | continue;
|
499 | e27c88fe | Gerd Hoffmann | return opt;
|
500 | e27c88fe | Gerd Hoffmann | } |
501 | e27c88fe | Gerd Hoffmann | return NULL; |
502 | e27c88fe | Gerd Hoffmann | } |
503 | e27c88fe | Gerd Hoffmann | |
504 | e27c88fe | Gerd Hoffmann | const char *qemu_opt_get(QemuOpts *opts, const char *name) |
505 | e27c88fe | Gerd Hoffmann | { |
506 | e27c88fe | Gerd Hoffmann | QemuOpt *opt = qemu_opt_find(opts, name); |
507 | e27c88fe | Gerd Hoffmann | return opt ? opt->str : NULL; |
508 | e27c88fe | Gerd Hoffmann | } |
509 | e27c88fe | Gerd Hoffmann | |
510 | e27c88fe | Gerd Hoffmann | int qemu_opt_get_bool(QemuOpts *opts, const char *name, int defval) |
511 | e27c88fe | Gerd Hoffmann | { |
512 | e27c88fe | Gerd Hoffmann | QemuOpt *opt = qemu_opt_find(opts, name); |
513 | e27c88fe | Gerd Hoffmann | |
514 | e27c88fe | Gerd Hoffmann | if (opt == NULL) |
515 | e27c88fe | Gerd Hoffmann | return defval;
|
516 | e27c88fe | Gerd Hoffmann | assert(opt->desc && opt->desc->type == QEMU_OPT_BOOL); |
517 | 1f5c1775 | Juan Quintela | return opt->value.boolean;
|
518 | e27c88fe | Gerd Hoffmann | } |
519 | e27c88fe | Gerd Hoffmann | |
520 | e27c88fe | Gerd Hoffmann | uint64_t qemu_opt_get_number(QemuOpts *opts, const char *name, uint64_t defval) |
521 | e27c88fe | Gerd Hoffmann | { |
522 | e27c88fe | Gerd Hoffmann | QemuOpt *opt = qemu_opt_find(opts, name); |
523 | e27c88fe | Gerd Hoffmann | |
524 | e27c88fe | Gerd Hoffmann | if (opt == NULL) |
525 | e27c88fe | Gerd Hoffmann | return defval;
|
526 | e27c88fe | Gerd Hoffmann | assert(opt->desc && opt->desc->type == QEMU_OPT_NUMBER); |
527 | e27c88fe | Gerd Hoffmann | return opt->value.uint;
|
528 | e27c88fe | Gerd Hoffmann | } |
529 | e27c88fe | Gerd Hoffmann | |
530 | e27c88fe | Gerd Hoffmann | uint64_t qemu_opt_get_size(QemuOpts *opts, const char *name, uint64_t defval) |
531 | e27c88fe | Gerd Hoffmann | { |
532 | e27c88fe | Gerd Hoffmann | QemuOpt *opt = qemu_opt_find(opts, name); |
533 | e27c88fe | Gerd Hoffmann | |
534 | e27c88fe | Gerd Hoffmann | if (opt == NULL) |
535 | e27c88fe | Gerd Hoffmann | return defval;
|
536 | e27c88fe | Gerd Hoffmann | assert(opt->desc && opt->desc->type == QEMU_OPT_SIZE); |
537 | e27c88fe | Gerd Hoffmann | return opt->value.uint;
|
538 | e27c88fe | Gerd Hoffmann | } |
539 | e27c88fe | Gerd Hoffmann | |
540 | e27c88fe | Gerd Hoffmann | static int qemu_opt_parse(QemuOpt *opt) |
541 | e27c88fe | Gerd Hoffmann | { |
542 | e27c88fe | Gerd Hoffmann | if (opt->desc == NULL) |
543 | e27c88fe | Gerd Hoffmann | return 0; |
544 | e27c88fe | Gerd Hoffmann | switch (opt->desc->type) {
|
545 | e27c88fe | Gerd Hoffmann | case QEMU_OPT_STRING:
|
546 | e27c88fe | Gerd Hoffmann | /* nothing */
|
547 | e27c88fe | Gerd Hoffmann | return 0; |
548 | e27c88fe | Gerd Hoffmann | case QEMU_OPT_BOOL:
|
549 | 1f5c1775 | Juan Quintela | return parse_option_bool(opt->name, opt->str, &opt->value.boolean);
|
550 | e27c88fe | Gerd Hoffmann | case QEMU_OPT_NUMBER:
|
551 | e27c88fe | Gerd Hoffmann | return parse_option_number(opt->name, opt->str, &opt->value.uint);
|
552 | e27c88fe | Gerd Hoffmann | case QEMU_OPT_SIZE:
|
553 | e27c88fe | Gerd Hoffmann | return parse_option_size(opt->name, opt->str, &opt->value.uint);
|
554 | e27c88fe | Gerd Hoffmann | default:
|
555 | e27c88fe | Gerd Hoffmann | abort(); |
556 | e27c88fe | Gerd Hoffmann | } |
557 | e27c88fe | Gerd Hoffmann | } |
558 | e27c88fe | Gerd Hoffmann | |
559 | e27c88fe | Gerd Hoffmann | static void qemu_opt_del(QemuOpt *opt) |
560 | e27c88fe | Gerd Hoffmann | { |
561 | 72cf2d4f | Blue Swirl | QTAILQ_REMOVE(&opt->opts->head, opt, next); |
562 | e27c88fe | Gerd Hoffmann | qemu_free((/* !const */ char*)opt->name); |
563 | e27c88fe | Gerd Hoffmann | qemu_free((/* !const */ char*)opt->str); |
564 | e27c88fe | Gerd Hoffmann | qemu_free(opt); |
565 | e27c88fe | Gerd Hoffmann | } |
566 | e27c88fe | Gerd Hoffmann | |
567 | e27c88fe | Gerd Hoffmann | int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) |
568 | e27c88fe | Gerd Hoffmann | { |
569 | e27c88fe | Gerd Hoffmann | QemuOpt *opt; |
570 | 238431a9 | Blue Swirl | const QemuOptDesc *desc = opts->list->desc;
|
571 | dc9ca4ba | Mark McLoughlin | int i;
|
572 | e27c88fe | Gerd Hoffmann | |
573 | dc9ca4ba | Mark McLoughlin | for (i = 0; desc[i].name != NULL; i++) { |
574 | dc9ca4ba | Mark McLoughlin | if (strcmp(desc[i].name, name) == 0) { |
575 | dc9ca4ba | Mark McLoughlin | break;
|
576 | e27c88fe | Gerd Hoffmann | } |
577 | dc9ca4ba | Mark McLoughlin | } |
578 | dc9ca4ba | Mark McLoughlin | if (desc[i].name == NULL) { |
579 | dc9ca4ba | Mark McLoughlin | if (i == 0) { |
580 | dc9ca4ba | Mark McLoughlin | /* empty list -> allow any */;
|
581 | dc9ca4ba | Mark McLoughlin | } else {
|
582 | dc9ca4ba | Mark McLoughlin | fprintf(stderr, "option \"%s\" is not valid for %s\n",
|
583 | dc9ca4ba | Mark McLoughlin | name, opts->list->name); |
584 | dc9ca4ba | Mark McLoughlin | return -1; |
585 | e27c88fe | Gerd Hoffmann | } |
586 | e27c88fe | Gerd Hoffmann | } |
587 | dc9ca4ba | Mark McLoughlin | |
588 | dc9ca4ba | Mark McLoughlin | opt = qemu_mallocz(sizeof(*opt));
|
589 | dc9ca4ba | Mark McLoughlin | opt->name = qemu_strdup(name); |
590 | dc9ca4ba | Mark McLoughlin | opt->opts = opts; |
591 | dc9ca4ba | Mark McLoughlin | QTAILQ_INSERT_TAIL(&opts->head, opt, next); |
592 | dc9ca4ba | Mark McLoughlin | if (desc[i].name != NULL) { |
593 | dc9ca4ba | Mark McLoughlin | opt->desc = desc+i; |
594 | dc9ca4ba | Mark McLoughlin | } |
595 | e27c88fe | Gerd Hoffmann | if (value) {
|
596 | e27c88fe | Gerd Hoffmann | opt->str = qemu_strdup(value); |
597 | e27c88fe | Gerd Hoffmann | } |
598 | e27c88fe | Gerd Hoffmann | if (qemu_opt_parse(opt) < 0) { |
599 | e27c88fe | Gerd Hoffmann | fprintf(stderr, "Failed to parse \"%s\" for \"%s.%s\"\n", opt->str,
|
600 | e27c88fe | Gerd Hoffmann | opts->list->name, opt->name); |
601 | e27c88fe | Gerd Hoffmann | qemu_opt_del(opt); |
602 | e27c88fe | Gerd Hoffmann | return -1; |
603 | e27c88fe | Gerd Hoffmann | } |
604 | e27c88fe | Gerd Hoffmann | return 0; |
605 | e27c88fe | Gerd Hoffmann | } |
606 | e27c88fe | Gerd Hoffmann | |
607 | 48026075 | Gerd Hoffmann | int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque, |
608 | 48026075 | Gerd Hoffmann | int abort_on_failure)
|
609 | 48026075 | Gerd Hoffmann | { |
610 | 48026075 | Gerd Hoffmann | QemuOpt *opt; |
611 | 48026075 | Gerd Hoffmann | int rc = 0; |
612 | 48026075 | Gerd Hoffmann | |
613 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(opt, &opts->head, next) { |
614 | 48026075 | Gerd Hoffmann | rc = func(opt->name, opt->str, opaque); |
615 | 48026075 | Gerd Hoffmann | if (abort_on_failure && rc != 0) |
616 | 48026075 | Gerd Hoffmann | break;
|
617 | 48026075 | Gerd Hoffmann | } |
618 | 48026075 | Gerd Hoffmann | return rc;
|
619 | 48026075 | Gerd Hoffmann | } |
620 | 48026075 | Gerd Hoffmann | |
621 | e27c88fe | Gerd Hoffmann | QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id) |
622 | e27c88fe | Gerd Hoffmann | { |
623 | e27c88fe | Gerd Hoffmann | QemuOpts *opts; |
624 | e27c88fe | Gerd Hoffmann | |
625 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(opts, &list->head, next) { |
626 | e27c88fe | Gerd Hoffmann | if (!opts->id) {
|
627 | e27c88fe | Gerd Hoffmann | continue;
|
628 | e27c88fe | Gerd Hoffmann | } |
629 | e27c88fe | Gerd Hoffmann | if (strcmp(opts->id, id) != 0) { |
630 | e27c88fe | Gerd Hoffmann | continue;
|
631 | e27c88fe | Gerd Hoffmann | } |
632 | e27c88fe | Gerd Hoffmann | return opts;
|
633 | e27c88fe | Gerd Hoffmann | } |
634 | e27c88fe | Gerd Hoffmann | return NULL; |
635 | e27c88fe | Gerd Hoffmann | } |
636 | e27c88fe | Gerd Hoffmann | |
637 | e27c88fe | Gerd Hoffmann | QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists) |
638 | e27c88fe | Gerd Hoffmann | { |
639 | e27c88fe | Gerd Hoffmann | QemuOpts *opts = NULL;
|
640 | e27c88fe | Gerd Hoffmann | |
641 | e27c88fe | Gerd Hoffmann | if (id) {
|
642 | e27c88fe | Gerd Hoffmann | opts = qemu_opts_find(list, id); |
643 | e27c88fe | Gerd Hoffmann | if (opts != NULL) { |
644 | e27c88fe | Gerd Hoffmann | if (fail_if_exists) {
|
645 | e27c88fe | Gerd Hoffmann | fprintf(stderr, "tried to create id \"%s\" twice for \"%s\"\n",
|
646 | e27c88fe | Gerd Hoffmann | id, list->name); |
647 | e27c88fe | Gerd Hoffmann | return NULL; |
648 | e27c88fe | Gerd Hoffmann | } else {
|
649 | e27c88fe | Gerd Hoffmann | return opts;
|
650 | e27c88fe | Gerd Hoffmann | } |
651 | e27c88fe | Gerd Hoffmann | } |
652 | e27c88fe | Gerd Hoffmann | } |
653 | e27c88fe | Gerd Hoffmann | opts = qemu_mallocz(sizeof(*opts));
|
654 | e27c88fe | Gerd Hoffmann | if (id) {
|
655 | e27c88fe | Gerd Hoffmann | opts->id = qemu_strdup(id); |
656 | e27c88fe | Gerd Hoffmann | } |
657 | e27c88fe | Gerd Hoffmann | opts->list = list; |
658 | 827b0813 | Markus Armbruster | loc_save(&opts->loc); |
659 | 72cf2d4f | Blue Swirl | QTAILQ_INIT(&opts->head); |
660 | 72cf2d4f | Blue Swirl | QTAILQ_INSERT_TAIL(&list->head, opts, next); |
661 | e27c88fe | Gerd Hoffmann | return opts;
|
662 | e27c88fe | Gerd Hoffmann | } |
663 | e27c88fe | Gerd Hoffmann | |
664 | e27c88fe | Gerd Hoffmann | int qemu_opts_set(QemuOptsList *list, const char *id, |
665 | e27c88fe | Gerd Hoffmann | const char *name, const char *value) |
666 | e27c88fe | Gerd Hoffmann | { |
667 | e27c88fe | Gerd Hoffmann | QemuOpts *opts; |
668 | e27c88fe | Gerd Hoffmann | |
669 | e27c88fe | Gerd Hoffmann | opts = qemu_opts_create(list, id, 1);
|
670 | e27c88fe | Gerd Hoffmann | if (opts == NULL) { |
671 | e27c88fe | Gerd Hoffmann | return -1; |
672 | e27c88fe | Gerd Hoffmann | } |
673 | e27c88fe | Gerd Hoffmann | return qemu_opt_set(opts, name, value);
|
674 | e27c88fe | Gerd Hoffmann | } |
675 | e27c88fe | Gerd Hoffmann | |
676 | 48026075 | Gerd Hoffmann | const char *qemu_opts_id(QemuOpts *opts) |
677 | 48026075 | Gerd Hoffmann | { |
678 | 48026075 | Gerd Hoffmann | return opts->id;
|
679 | 48026075 | Gerd Hoffmann | } |
680 | 48026075 | Gerd Hoffmann | |
681 | e27c88fe | Gerd Hoffmann | void qemu_opts_del(QemuOpts *opts)
|
682 | e27c88fe | Gerd Hoffmann | { |
683 | e27c88fe | Gerd Hoffmann | QemuOpt *opt; |
684 | e27c88fe | Gerd Hoffmann | |
685 | e27c88fe | Gerd Hoffmann | for (;;) {
|
686 | 72cf2d4f | Blue Swirl | opt = QTAILQ_FIRST(&opts->head); |
687 | e27c88fe | Gerd Hoffmann | if (opt == NULL) |
688 | e27c88fe | Gerd Hoffmann | break;
|
689 | e27c88fe | Gerd Hoffmann | qemu_opt_del(opt); |
690 | e27c88fe | Gerd Hoffmann | } |
691 | 72cf2d4f | Blue Swirl | QTAILQ_REMOVE(&opts->list->head, opts, next); |
692 | b09417b9 | Jan Kiszka | qemu_free(opts->id); |
693 | e27c88fe | Gerd Hoffmann | qemu_free(opts); |
694 | e27c88fe | Gerd Hoffmann | } |
695 | e27c88fe | Gerd Hoffmann | |
696 | e27c88fe | Gerd Hoffmann | int qemu_opts_print(QemuOpts *opts, void *dummy) |
697 | e27c88fe | Gerd Hoffmann | { |
698 | e27c88fe | Gerd Hoffmann | QemuOpt *opt; |
699 | e27c88fe | Gerd Hoffmann | |
700 | e27c88fe | Gerd Hoffmann | fprintf(stderr, "%s: %s:", opts->list->name,
|
701 | e27c88fe | Gerd Hoffmann | opts->id ? opts->id : "<noid>");
|
702 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(opt, &opts->head, next) { |
703 | e27c88fe | Gerd Hoffmann | fprintf(stderr, " %s=\"%s\"", opt->name, opt->str);
|
704 | e27c88fe | Gerd Hoffmann | } |
705 | e27c88fe | Gerd Hoffmann | fprintf(stderr, "\n");
|
706 | e27c88fe | Gerd Hoffmann | return 0; |
707 | e27c88fe | Gerd Hoffmann | } |
708 | e27c88fe | Gerd Hoffmann | |
709 | 96729cbd | Gerd Hoffmann | int qemu_opts_do_parse(QemuOpts *opts, const char *params, const char *firstname) |
710 | e27c88fe | Gerd Hoffmann | { |
711 | d318ff99 | Gerd Hoffmann | char option[128], value[1024]; |
712 | e27c88fe | Gerd Hoffmann | const char *p,*pe,*pc; |
713 | e27c88fe | Gerd Hoffmann | |
714 | 2cfa571f | Mark McLoughlin | for (p = params; *p != '\0'; p++) { |
715 | e27c88fe | Gerd Hoffmann | pe = strchr(p, '=');
|
716 | e27c88fe | Gerd Hoffmann | pc = strchr(p, ',');
|
717 | e27c88fe | Gerd Hoffmann | if (!pe || (pc && pc < pe)) {
|
718 | e27c88fe | Gerd Hoffmann | /* found "foo,more" */
|
719 | e27c88fe | Gerd Hoffmann | if (p == params && firstname) {
|
720 | e27c88fe | Gerd Hoffmann | /* implicitly named first option */
|
721 | e27c88fe | Gerd Hoffmann | pstrcpy(option, sizeof(option), firstname);
|
722 | e27c88fe | Gerd Hoffmann | p = get_opt_value(value, sizeof(value), p);
|
723 | e27c88fe | Gerd Hoffmann | } else {
|
724 | e27c88fe | Gerd Hoffmann | /* option without value, probably a flag */
|
725 | e27c88fe | Gerd Hoffmann | p = get_opt_name(option, sizeof(option), p, ','); |
726 | 96729cbd | Gerd Hoffmann | if (strncmp(option, "no", 2) == 0) { |
727 | e27c88fe | Gerd Hoffmann | memmove(option, option+2, strlen(option+2)+1); |
728 | e27c88fe | Gerd Hoffmann | pstrcpy(value, sizeof(value), "off"); |
729 | e27c88fe | Gerd Hoffmann | } else {
|
730 | e27c88fe | Gerd Hoffmann | pstrcpy(value, sizeof(value), "on"); |
731 | e27c88fe | Gerd Hoffmann | } |
732 | e27c88fe | Gerd Hoffmann | } |
733 | e27c88fe | Gerd Hoffmann | } else {
|
734 | e27c88fe | Gerd Hoffmann | /* found "foo=bar,more" */
|
735 | e27c88fe | Gerd Hoffmann | p = get_opt_name(option, sizeof(option), p, '='); |
736 | e27c88fe | Gerd Hoffmann | if (*p != '=') { |
737 | e27c88fe | Gerd Hoffmann | break;
|
738 | e27c88fe | Gerd Hoffmann | } |
739 | e27c88fe | Gerd Hoffmann | p++; |
740 | e27c88fe | Gerd Hoffmann | p = get_opt_value(value, sizeof(value), p);
|
741 | e27c88fe | Gerd Hoffmann | } |
742 | e27c88fe | Gerd Hoffmann | if (strcmp(option, "id") != 0) { |
743 | e27c88fe | Gerd Hoffmann | /* store and parse */
|
744 | 3df04ac3 | Mark McLoughlin | if (qemu_opt_set(opts, option, value) == -1) { |
745 | 96729cbd | Gerd Hoffmann | return -1; |
746 | e27c88fe | Gerd Hoffmann | } |
747 | e27c88fe | Gerd Hoffmann | } |
748 | e27c88fe | Gerd Hoffmann | if (*p != ',') { |
749 | e27c88fe | Gerd Hoffmann | break;
|
750 | e27c88fe | Gerd Hoffmann | } |
751 | e27c88fe | Gerd Hoffmann | } |
752 | 96729cbd | Gerd Hoffmann | return 0; |
753 | 96729cbd | Gerd Hoffmann | } |
754 | 96729cbd | Gerd Hoffmann | |
755 | 96729cbd | Gerd Hoffmann | QemuOpts *qemu_opts_parse(QemuOptsList *list, const char *params, const char *firstname) |
756 | 96729cbd | Gerd Hoffmann | { |
757 | d318ff99 | Gerd Hoffmann | char value[1024], *id = NULL; |
758 | 96729cbd | Gerd Hoffmann | const char *p; |
759 | 96729cbd | Gerd Hoffmann | QemuOpts *opts; |
760 | 96729cbd | Gerd Hoffmann | |
761 | 96729cbd | Gerd Hoffmann | if (strncmp(params, "id=", 3) == 0) { |
762 | 96729cbd | Gerd Hoffmann | get_opt_value(value, sizeof(value), params+3); |
763 | 96729cbd | Gerd Hoffmann | id = qemu_strdup(value); |
764 | 96729cbd | Gerd Hoffmann | } else if ((p = strstr(params, ",id=")) != NULL) { |
765 | 96729cbd | Gerd Hoffmann | get_opt_value(value, sizeof(value), p+4); |
766 | 96729cbd | Gerd Hoffmann | id = qemu_strdup(value); |
767 | 96729cbd | Gerd Hoffmann | } |
768 | 96729cbd | Gerd Hoffmann | opts = qemu_opts_create(list, id, 1);
|
769 | 96729cbd | Gerd Hoffmann | if (opts == NULL) |
770 | 96729cbd | Gerd Hoffmann | return NULL; |
771 | 96729cbd | Gerd Hoffmann | |
772 | 96729cbd | Gerd Hoffmann | if (qemu_opts_do_parse(opts, params, firstname) != 0) { |
773 | 96729cbd | Gerd Hoffmann | qemu_opts_del(opts); |
774 | 96729cbd | Gerd Hoffmann | return NULL; |
775 | 96729cbd | Gerd Hoffmann | } |
776 | 96729cbd | Gerd Hoffmann | |
777 | e27c88fe | Gerd Hoffmann | return opts;
|
778 | e27c88fe | Gerd Hoffmann | } |
779 | e27c88fe | Gerd Hoffmann | |
780 | 5dc519ef | Mark McLoughlin | /* Validate parsed opts against descriptions where no
|
781 | 5dc519ef | Mark McLoughlin | * descriptions were provided in the QemuOptsList.
|
782 | 5dc519ef | Mark McLoughlin | */
|
783 | 238431a9 | Blue Swirl | int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc) |
784 | 5dc519ef | Mark McLoughlin | { |
785 | 5dc519ef | Mark McLoughlin | QemuOpt *opt; |
786 | 5dc519ef | Mark McLoughlin | |
787 | 5dc519ef | Mark McLoughlin | assert(opts->list->desc[0].name == NULL); |
788 | 5dc519ef | Mark McLoughlin | |
789 | 5dc519ef | Mark McLoughlin | QTAILQ_FOREACH(opt, &opts->head, next) { |
790 | 5dc519ef | Mark McLoughlin | int i;
|
791 | 5dc519ef | Mark McLoughlin | |
792 | 5dc519ef | Mark McLoughlin | for (i = 0; desc[i].name != NULL; i++) { |
793 | 5dc519ef | Mark McLoughlin | if (strcmp(desc[i].name, opt->name) == 0) { |
794 | 5dc519ef | Mark McLoughlin | break;
|
795 | 5dc519ef | Mark McLoughlin | } |
796 | 5dc519ef | Mark McLoughlin | } |
797 | 5dc519ef | Mark McLoughlin | if (desc[i].name == NULL) { |
798 | 5dc519ef | Mark McLoughlin | fprintf(stderr, "option \"%s\" is not valid for %s\n",
|
799 | 5dc519ef | Mark McLoughlin | opt->name, opts->list->name); |
800 | 5dc519ef | Mark McLoughlin | return -1; |
801 | 5dc519ef | Mark McLoughlin | } |
802 | 5dc519ef | Mark McLoughlin | |
803 | 5dc519ef | Mark McLoughlin | opt->desc = &desc[i]; |
804 | 5dc519ef | Mark McLoughlin | |
805 | 5dc519ef | Mark McLoughlin | if (qemu_opt_parse(opt) < 0) { |
806 | 5dc519ef | Mark McLoughlin | return -1; |
807 | 5dc519ef | Mark McLoughlin | } |
808 | 5dc519ef | Mark McLoughlin | } |
809 | 5dc519ef | Mark McLoughlin | |
810 | 5dc519ef | Mark McLoughlin | return 0; |
811 | 5dc519ef | Mark McLoughlin | } |
812 | 5dc519ef | Mark McLoughlin | |
813 | e27c88fe | Gerd Hoffmann | int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque, |
814 | e27c88fe | Gerd Hoffmann | int abort_on_failure)
|
815 | e27c88fe | Gerd Hoffmann | { |
816 | 827b0813 | Markus Armbruster | Location loc; |
817 | e27c88fe | Gerd Hoffmann | QemuOpts *opts; |
818 | e27c88fe | Gerd Hoffmann | int rc = 0; |
819 | e27c88fe | Gerd Hoffmann | |
820 | 827b0813 | Markus Armbruster | loc_push_none(&loc); |
821 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(opts, &list->head, next) { |
822 | 827b0813 | Markus Armbruster | loc_restore(&opts->loc); |
823 | 4a2594dd | Markus Armbruster | rc |= func(opts, opaque); |
824 | e27c88fe | Gerd Hoffmann | if (abort_on_failure && rc != 0) |
825 | e27c88fe | Gerd Hoffmann | break;
|
826 | e27c88fe | Gerd Hoffmann | } |
827 | 827b0813 | Markus Armbruster | loc_pop(&loc); |
828 | e27c88fe | Gerd Hoffmann | return rc;
|
829 | e27c88fe | Gerd Hoffmann | } |