]>
Commit | Line | Data |
---|---|---|
7282a033 | 1 | #include "qemu-common.h" |
1de7afc9 PB |
2 | #include "qemu/error-report.h" |
3 | #include "qemu/option.h" | |
4 | #include "qemu/config-file.h" | |
b4a42f81 | 5 | #include "qapi/qmp/qerror.h" |
d0fef6fb | 6 | #include "hw/qdev.h" |
7b1b5d19 | 7 | #include "qapi/error.h" |
1f8f987d | 8 | #include "qmp-commands.h" |
7282a033 | 9 | |
4d454574 | 10 | static QemuOptsList *vm_config_groups[32]; |
968854c8 | 11 | static QemuOptsList *drive_config_groups[4]; |
d058fe03 | 12 | |
2ac20613 LC |
13 | static QemuOptsList *find_list(QemuOptsList **lists, const char *group, |
14 | Error **errp) | |
ddc97855 GH |
15 | { |
16 | int i; | |
17 | ||
18 | for (i = 0; lists[i] != NULL; i++) { | |
19 | if (strcmp(lists[i]->name, group) == 0) | |
20 | break; | |
21 | } | |
22 | if (lists[i] == NULL) { | |
2ac20613 | 23 | error_set(errp, QERR_INVALID_OPTION_GROUP, group); |
ddc97855 GH |
24 | } |
25 | return lists[i]; | |
26 | } | |
27 | ||
490b648e KW |
28 | QemuOptsList *qemu_find_opts(const char *group) |
29 | { | |
2ac20613 LC |
30 | QemuOptsList *ret; |
31 | Error *local_err = NULL; | |
32 | ||
33 | ret = find_list(vm_config_groups, group, &local_err); | |
34 | if (error_is_set(&local_err)) { | |
312fd5f2 | 35 | error_report("%s", error_get_pretty(local_err)); |
2ac20613 LC |
36 | error_free(local_err); |
37 | } | |
38 | ||
39 | return ret; | |
490b648e KW |
40 | } |
41 | ||
1f8f987d AK |
42 | static CommandLineParameterInfoList *query_option_descs(const QemuOptDesc *desc) |
43 | { | |
44 | CommandLineParameterInfoList *param_list = NULL, *entry; | |
45 | CommandLineParameterInfo *info; | |
46 | int i; | |
47 | ||
48 | for (i = 0; desc[i].name != NULL; i++) { | |
49 | info = g_malloc0(sizeof(*info)); | |
50 | info->name = g_strdup(desc[i].name); | |
51 | ||
52 | switch (desc[i].type) { | |
53 | case QEMU_OPT_STRING: | |
54 | info->type = COMMAND_LINE_PARAMETER_TYPE_STRING; | |
55 | break; | |
56 | case QEMU_OPT_BOOL: | |
57 | info->type = COMMAND_LINE_PARAMETER_TYPE_BOOLEAN; | |
58 | break; | |
59 | case QEMU_OPT_NUMBER: | |
60 | info->type = COMMAND_LINE_PARAMETER_TYPE_NUMBER; | |
61 | break; | |
62 | case QEMU_OPT_SIZE: | |
63 | info->type = COMMAND_LINE_PARAMETER_TYPE_SIZE; | |
64 | break; | |
65 | } | |
66 | ||
67 | if (desc[i].help) { | |
68 | info->has_help = true; | |
69 | info->help = g_strdup(desc[i].help); | |
70 | } | |
71 | ||
72 | entry = g_malloc0(sizeof(*entry)); | |
73 | entry->value = info; | |
74 | entry->next = param_list; | |
75 | param_list = entry; | |
76 | } | |
77 | ||
78 | return param_list; | |
79 | } | |
80 | ||
968854c8 AK |
81 | /* remove repeated entry from the info list */ |
82 | static void cleanup_infolist(CommandLineParameterInfoList *head) | |
83 | { | |
84 | CommandLineParameterInfoList *pre_entry, *cur, *del_entry; | |
85 | ||
86 | cur = head; | |
87 | while (cur->next) { | |
88 | pre_entry = head; | |
89 | while (pre_entry != cur->next) { | |
90 | if (!strcmp(pre_entry->value->name, cur->next->value->name)) { | |
91 | del_entry = cur->next; | |
92 | cur->next = cur->next->next; | |
93 | g_free(del_entry); | |
94 | break; | |
95 | } | |
96 | pre_entry = pre_entry->next; | |
97 | } | |
98 | cur = cur->next; | |
99 | } | |
100 | } | |
101 | ||
102 | /* merge the description items of two parameter infolists */ | |
103 | static void connect_infolist(CommandLineParameterInfoList *head, | |
104 | CommandLineParameterInfoList *new) | |
105 | { | |
106 | CommandLineParameterInfoList *cur; | |
107 | ||
108 | cur = head; | |
109 | while (cur->next) { | |
110 | cur = cur->next; | |
111 | } | |
112 | cur->next = new; | |
113 | } | |
114 | ||
115 | /* access all the local QemuOptsLists for drive option */ | |
116 | static CommandLineParameterInfoList *get_drive_infolist(void) | |
117 | { | |
118 | CommandLineParameterInfoList *head = NULL, *cur; | |
119 | int i; | |
120 | ||
121 | for (i = 0; drive_config_groups[i] != NULL; i++) { | |
122 | if (!head) { | |
123 | head = query_option_descs(drive_config_groups[i]->desc); | |
124 | } else { | |
125 | cur = query_option_descs(drive_config_groups[i]->desc); | |
126 | connect_infolist(head, cur); | |
127 | } | |
128 | } | |
129 | cleanup_infolist(head); | |
130 | ||
131 | return head; | |
132 | } | |
133 | ||
1f8f987d AK |
134 | CommandLineOptionInfoList *qmp_query_command_line_options(bool has_option, |
135 | const char *option, | |
136 | Error **errp) | |
137 | { | |
138 | CommandLineOptionInfoList *conf_list = NULL, *entry; | |
139 | CommandLineOptionInfo *info; | |
140 | int i; | |
141 | ||
142 | for (i = 0; vm_config_groups[i] != NULL; i++) { | |
143 | if (!has_option || !strcmp(option, vm_config_groups[i]->name)) { | |
144 | info = g_malloc0(sizeof(*info)); | |
145 | info->option = g_strdup(vm_config_groups[i]->name); | |
968854c8 AK |
146 | if (!strcmp("drive", vm_config_groups[i]->name)) { |
147 | info->parameters = get_drive_infolist(); | |
148 | } else { | |
149 | info->parameters = | |
150 | query_option_descs(vm_config_groups[i]->desc); | |
151 | } | |
1f8f987d AK |
152 | entry = g_malloc0(sizeof(*entry)); |
153 | entry->value = info; | |
154 | entry->next = conf_list; | |
155 | conf_list = entry; | |
156 | } | |
157 | } | |
158 | ||
159 | if (conf_list == NULL) { | |
160 | error_setg(errp, "invalid option name: %s", option); | |
161 | } | |
162 | ||
163 | return conf_list; | |
164 | } | |
165 | ||
60d5666f LC |
166 | QemuOptsList *qemu_find_opts_err(const char *group, Error **errp) |
167 | { | |
168 | return find_list(vm_config_groups, group, errp); | |
169 | } | |
170 | ||
968854c8 AK |
171 | void qemu_add_drive_opts(QemuOptsList *list) |
172 | { | |
173 | int entries, i; | |
174 | ||
175 | entries = ARRAY_SIZE(drive_config_groups); | |
176 | entries--; /* keep list NULL terminated */ | |
177 | for (i = 0; i < entries; i++) { | |
178 | if (drive_config_groups[i] == NULL) { | |
179 | drive_config_groups[i] = list; | |
180 | return; | |
181 | } | |
182 | } | |
183 | fprintf(stderr, "ran out of space in drive_config_groups"); | |
184 | abort(); | |
185 | } | |
186 | ||
dfe795e7 GH |
187 | void qemu_add_opts(QemuOptsList *list) |
188 | { | |
189 | int entries, i; | |
190 | ||
191 | entries = ARRAY_SIZE(vm_config_groups); | |
192 | entries--; /* keep list NULL terminated */ | |
193 | for (i = 0; i < entries; i++) { | |
194 | if (vm_config_groups[i] == NULL) { | |
195 | vm_config_groups[i] = list; | |
196 | return; | |
197 | } | |
198 | } | |
199 | fprintf(stderr, "ran out of space in vm_config_groups"); | |
200 | abort(); | |
201 | } | |
202 | ||
d058fe03 GH |
203 | int qemu_set_option(const char *str) |
204 | { | |
205 | char group[64], id[64], arg[64]; | |
ddc97855 | 206 | QemuOptsList *list; |
d058fe03 | 207 | QemuOpts *opts; |
ddc97855 | 208 | int rc, offset; |
d058fe03 GH |
209 | |
210 | rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset); | |
211 | if (rc < 3 || str[offset] != '=') { | |
1ecda02b | 212 | error_report("can't parse: \"%s\"", str); |
d058fe03 GH |
213 | return -1; |
214 | } | |
215 | ||
304329ee | 216 | list = qemu_find_opts(group); |
ddc97855 | 217 | if (list == NULL) { |
d058fe03 GH |
218 | return -1; |
219 | } | |
220 | ||
ddc97855 | 221 | opts = qemu_opts_find(list, id); |
d058fe03 | 222 | if (!opts) { |
1ecda02b MA |
223 | error_report("there is no %s \"%s\" defined", |
224 | list->name, id); | |
d058fe03 GH |
225 | return -1; |
226 | } | |
227 | ||
3df04ac3 | 228 | if (qemu_opt_set(opts, arg, str+offset+1) == -1) { |
d058fe03 GH |
229 | return -1; |
230 | } | |
231 | return 0; | |
232 | } | |
233 | ||
9d993394 GH |
234 | struct ConfigWriteData { |
235 | QemuOptsList *list; | |
236 | FILE *fp; | |
237 | }; | |
238 | ||
239 | static int config_write_opt(const char *name, const char *value, void *opaque) | |
240 | { | |
241 | struct ConfigWriteData *data = opaque; | |
242 | ||
243 | fprintf(data->fp, " %s = \"%s\"\n", name, value); | |
244 | return 0; | |
245 | } | |
246 | ||
247 | static int config_write_opts(QemuOpts *opts, void *opaque) | |
248 | { | |
249 | struct ConfigWriteData *data = opaque; | |
250 | const char *id = qemu_opts_id(opts); | |
251 | ||
252 | if (id) { | |
253 | fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id); | |
254 | } else { | |
255 | fprintf(data->fp, "[%s]\n", data->list->name); | |
256 | } | |
257 | qemu_opt_foreach(opts, config_write_opt, data, 0); | |
258 | fprintf(data->fp, "\n"); | |
259 | return 0; | |
260 | } | |
261 | ||
262 | void qemu_config_write(FILE *fp) | |
263 | { | |
264 | struct ConfigWriteData data = { .fp = fp }; | |
490b648e | 265 | QemuOptsList **lists = vm_config_groups; |
9d993394 GH |
266 | int i; |
267 | ||
268 | fprintf(fp, "# qemu config file\n\n"); | |
269 | for (i = 0; lists[i] != NULL; i++) { | |
270 | data.list = lists[i]; | |
271 | qemu_opts_foreach(data.list, config_write_opts, &data, 0); | |
272 | } | |
273 | } | |
42262ba8 | 274 | |
490b648e | 275 | int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname) |
42262ba8 GH |
276 | { |
277 | char line[1024], group[64], id[64], arg[64], value[1024]; | |
cf5a65aa | 278 | Location loc; |
42262ba8 | 279 | QemuOptsList *list = NULL; |
2ac20613 | 280 | Error *local_err = NULL; |
42262ba8 | 281 | QemuOpts *opts = NULL; |
cf5a65aa | 282 | int res = -1, lno = 0; |
42262ba8 | 283 | |
cf5a65aa | 284 | loc_push_none(&loc); |
42262ba8 | 285 | while (fgets(line, sizeof(line), fp) != NULL) { |
cf5a65aa | 286 | loc_set_file(fname, ++lno); |
42262ba8 GH |
287 | if (line[0] == '\n') { |
288 | /* skip empty lines */ | |
289 | continue; | |
290 | } | |
291 | if (line[0] == '#') { | |
292 | /* comment */ | |
293 | continue; | |
294 | } | |
295 | if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) { | |
296 | /* group with id */ | |
2ac20613 LC |
297 | list = find_list(lists, group, &local_err); |
298 | if (error_is_set(&local_err)) { | |
312fd5f2 | 299 | error_report("%s", error_get_pretty(local_err)); |
2ac20613 | 300 | error_free(local_err); |
cf5a65aa | 301 | goto out; |
2ac20613 | 302 | } |
8be7e7e4 | 303 | opts = qemu_opts_create(list, id, 1, NULL); |
42262ba8 GH |
304 | continue; |
305 | } | |
306 | if (sscanf(line, "[%63[^]]]", group) == 1) { | |
307 | /* group without id */ | |
2ac20613 LC |
308 | list = find_list(lists, group, &local_err); |
309 | if (error_is_set(&local_err)) { | |
312fd5f2 | 310 | error_report("%s", error_get_pretty(local_err)); |
2ac20613 | 311 | error_free(local_err); |
cf5a65aa | 312 | goto out; |
2ac20613 | 313 | } |
e478b448 | 314 | opts = qemu_opts_create_nofail(list); |
42262ba8 GH |
315 | continue; |
316 | } | |
317 | if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) { | |
318 | /* arg = value */ | |
319 | if (opts == NULL) { | |
cf5a65aa MA |
320 | error_report("no group defined"); |
321 | goto out; | |
42262ba8 GH |
322 | } |
323 | if (qemu_opt_set(opts, arg, value) != 0) { | |
cf5a65aa | 324 | goto out; |
42262ba8 GH |
325 | } |
326 | continue; | |
327 | } | |
cf5a65aa MA |
328 | error_report("parse error"); |
329 | goto out; | |
42262ba8 | 330 | } |
ef82516d MA |
331 | if (ferror(fp)) { |
332 | error_report("error reading file"); | |
333 | goto out; | |
334 | } | |
cf5a65aa MA |
335 | res = 0; |
336 | out: | |
337 | loc_pop(&loc); | |
338 | return res; | |
42262ba8 | 339 | } |
dcfb0939 KW |
340 | |
341 | int qemu_read_config_file(const char *filename) | |
342 | { | |
343 | FILE *f = fopen(filename, "r"); | |
019e78ba KW |
344 | int ret; |
345 | ||
dcfb0939 KW |
346 | if (f == NULL) { |
347 | return -errno; | |
348 | } | |
349 | ||
019e78ba | 350 | ret = qemu_config_parse(f, vm_config_groups, filename); |
dcfb0939 KW |
351 | fclose(f); |
352 | ||
019e78ba KW |
353 | if (ret == 0) { |
354 | return 0; | |
355 | } else { | |
356 | return -EINVAL; | |
357 | } | |
dcfb0939 | 358 | } |