1 #include "qemu-common.h"
2 #include "qemu/error-report.h"
3 #include "qemu/option.h"
4 #include "qemu/config-file.h"
5 #include "qapi/qmp/qerror.h"
7 #include "qapi/error.h"
8 #include "qmp-commands.h"
10 static QemuOptsList
*vm_config_groups
[32];
11 static QemuOptsList
*drive_config_groups
[4];
13 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
,
18 for (i
= 0; lists
[i
] != NULL
; i
++) {
19 if (strcmp(lists
[i
]->name
, group
) == 0)
22 if (lists
[i
] == NULL
) {
23 error_setg(errp
, "There is no option group '%s'", group
);
28 QemuOptsList
*qemu_find_opts(const char *group
)
31 Error
*local_err
= NULL
;
33 ret
= find_list(vm_config_groups
, group
, &local_err
);
35 error_report("%s", error_get_pretty(local_err
));
36 error_free(local_err
);
42 QemuOpts
*qemu_find_opts_singleton(const char *group
)
47 list
= qemu_find_opts(group
);
49 opts
= qemu_opts_find(list
, NULL
);
51 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
56 static CommandLineParameterInfoList
*query_option_descs(const QemuOptDesc
*desc
)
58 CommandLineParameterInfoList
*param_list
= NULL
, *entry
;
59 CommandLineParameterInfo
*info
;
62 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
63 info
= g_malloc0(sizeof(*info
));
64 info
->name
= g_strdup(desc
[i
].name
);
66 switch (desc
[i
].type
) {
68 info
->type
= COMMAND_LINE_PARAMETER_TYPE_STRING
;
71 info
->type
= COMMAND_LINE_PARAMETER_TYPE_BOOLEAN
;
74 info
->type
= COMMAND_LINE_PARAMETER_TYPE_NUMBER
;
77 info
->type
= COMMAND_LINE_PARAMETER_TYPE_SIZE
;
82 info
->has_help
= true;
83 info
->help
= g_strdup(desc
[i
].help
);
85 if (desc
[i
].def_value_str
) {
86 info
->has_q_default
= true;
87 info
->q_default
= g_strdup(desc
[i
].def_value_str
);
90 entry
= g_malloc0(sizeof(*entry
));
92 entry
->next
= param_list
;
99 /* remove repeated entry from the info list */
100 static void cleanup_infolist(CommandLineParameterInfoList
*head
)
102 CommandLineParameterInfoList
*pre_entry
, *cur
, *del_entry
;
107 while (pre_entry
!= cur
->next
) {
108 if (!strcmp(pre_entry
->value
->name
, cur
->next
->value
->name
)) {
109 del_entry
= cur
->next
;
110 cur
->next
= cur
->next
->next
;
114 pre_entry
= pre_entry
->next
;
120 /* merge the description items of two parameter infolists */
121 static void connect_infolist(CommandLineParameterInfoList
*head
,
122 CommandLineParameterInfoList
*new)
124 CommandLineParameterInfoList
*cur
;
133 /* access all the local QemuOptsLists for drive option */
134 static CommandLineParameterInfoList
*get_drive_infolist(void)
136 CommandLineParameterInfoList
*head
= NULL
, *cur
;
139 for (i
= 0; drive_config_groups
[i
] != NULL
; i
++) {
141 head
= query_option_descs(drive_config_groups
[i
]->desc
);
143 cur
= query_option_descs(drive_config_groups
[i
]->desc
);
144 connect_infolist(head
, cur
);
147 cleanup_infolist(head
);
152 CommandLineOptionInfoList
*qmp_query_command_line_options(bool has_option
,
156 CommandLineOptionInfoList
*conf_list
= NULL
, *entry
;
157 CommandLineOptionInfo
*info
;
160 for (i
= 0; vm_config_groups
[i
] != NULL
; i
++) {
161 if (!has_option
|| !strcmp(option
, vm_config_groups
[i
]->name
)) {
162 info
= g_malloc0(sizeof(*info
));
163 info
->option
= g_strdup(vm_config_groups
[i
]->name
);
164 if (!strcmp("drive", vm_config_groups
[i
]->name
)) {
165 info
->parameters
= get_drive_infolist();
168 query_option_descs(vm_config_groups
[i
]->desc
);
170 entry
= g_malloc0(sizeof(*entry
));
172 entry
->next
= conf_list
;
177 if (conf_list
== NULL
) {
178 error_setg(errp
, "invalid option name: %s", option
);
184 QemuOptsList
*qemu_find_opts_err(const char *group
, Error
**errp
)
186 return find_list(vm_config_groups
, group
, errp
);
189 void qemu_add_drive_opts(QemuOptsList
*list
)
193 entries
= ARRAY_SIZE(drive_config_groups
);
194 entries
--; /* keep list NULL terminated */
195 for (i
= 0; i
< entries
; i
++) {
196 if (drive_config_groups
[i
] == NULL
) {
197 drive_config_groups
[i
] = list
;
201 fprintf(stderr
, "ran out of space in drive_config_groups");
205 void qemu_add_opts(QemuOptsList
*list
)
209 entries
= ARRAY_SIZE(vm_config_groups
);
210 entries
--; /* keep list NULL terminated */
211 for (i
= 0; i
< entries
; i
++) {
212 if (vm_config_groups
[i
] == NULL
) {
213 vm_config_groups
[i
] = list
;
217 fprintf(stderr
, "ran out of space in vm_config_groups");
221 int qemu_set_option(const char *str
)
223 char group
[64], id
[64], arg
[64];
228 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
229 if (rc
< 3 || str
[offset
] != '=') {
230 error_report("can't parse: \"%s\"", str
);
234 list
= qemu_find_opts(group
);
239 opts
= qemu_opts_find(list
, id
);
241 error_report("there is no %s \"%s\" defined",
246 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
252 struct ConfigWriteData
{
257 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
259 struct ConfigWriteData
*data
= opaque
;
261 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
265 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
267 struct ConfigWriteData
*data
= opaque
;
268 const char *id
= qemu_opts_id(opts
);
271 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
273 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
275 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
276 fprintf(data
->fp
, "\n");
280 void qemu_config_write(FILE *fp
)
282 struct ConfigWriteData data
= { .fp
= fp
};
283 QemuOptsList
**lists
= vm_config_groups
;
286 fprintf(fp
, "# qemu config file\n\n");
287 for (i
= 0; lists
[i
] != NULL
; i
++) {
288 data
.list
= lists
[i
];
289 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
293 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
295 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
297 QemuOptsList
*list
= NULL
;
298 Error
*local_err
= NULL
;
299 QemuOpts
*opts
= NULL
;
300 int res
= -1, lno
= 0;
303 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
304 loc_set_file(fname
, ++lno
);
305 if (line
[0] == '\n') {
306 /* skip empty lines */
309 if (line
[0] == '#') {
313 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
315 list
= find_list(lists
, group
, &local_err
);
317 error_report("%s", error_get_pretty(local_err
));
318 error_free(local_err
);
321 opts
= qemu_opts_create(list
, id
, 1, NULL
);
324 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
325 /* group without id */
326 list
= find_list(lists
, group
, &local_err
);
328 error_report("%s", error_get_pretty(local_err
));
329 error_free(local_err
);
332 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
335 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
338 error_report("no group defined");
341 if (qemu_opt_set(opts
, arg
, value
) != 0) {
346 error_report("parse error");
350 error_report("error reading file");
359 int qemu_read_config_file(const char *filename
)
361 FILE *f
= fopen(filename
, "r");
368 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);
378 static void config_parse_qdict_section(QDict
*options
, QemuOptsList
*opts
,
384 Error
*local_err
= NULL
;
385 size_t orig_size
, enum_size
;
388 prefix
= g_strdup_printf("%s.", opts
->name
);
389 qdict_extract_subqdict(options
, &subqdict
, prefix
);
391 orig_size
= qdict_size(subqdict
);
396 subopts
= qemu_opts_create(opts
, NULL
, 0, &local_err
);
398 error_propagate(errp
, local_err
);
402 qemu_opts_absorb_qdict(subopts
, subqdict
, &local_err
);
404 error_propagate(errp
, local_err
);
408 enum_size
= qdict_size(subqdict
);
409 if (enum_size
< orig_size
&& enum_size
) {
410 error_setg(errp
, "Unknown option '%s' for [%s]",
411 qdict_first(subqdict
)->key
, opts
->name
);
416 /* Multiple, enumerated sections */
417 QListEntry
*list_entry
;
420 /* Not required anymore */
421 qemu_opts_del(subopts
);
423 qdict_array_split(subqdict
, &list
);
424 if (qdict_size(subqdict
)) {
425 error_setg(errp
, "Unused option '%s' for [%s]",
426 qdict_first(subqdict
)->key
, opts
->name
);
430 QLIST_FOREACH_ENTRY(list
, list_entry
) {
431 QDict
*section
= qobject_to_qdict(qlist_entry_obj(list_entry
));
435 error_setg(errp
, "[%s] section (index %u) does not consist of "
436 "keys", opts
->name
, i
);
440 opt_name
= g_strdup_printf("%s.%u", opts
->name
, i
++);
441 subopts
= qemu_opts_create(opts
, opt_name
, 1, &local_err
);
444 error_propagate(errp
, local_err
);
448 qemu_opts_absorb_qdict(subopts
, section
, &local_err
);
450 error_propagate(errp
, local_err
);
451 qemu_opts_del(subopts
);
455 if (qdict_size(section
)) {
456 error_setg(errp
, "[%s] section doesn't support the option '%s'",
457 opts
->name
, qdict_first(section
)->key
);
458 qemu_opts_del(subopts
);
469 void qemu_config_parse_qdict(QDict
*options
, QemuOptsList
**lists
,
473 Error
*local_err
= NULL
;
475 for (i
= 0; lists
[i
]; i
++) {
476 config_parse_qdict_section(options
, lists
[i
], &local_err
);
478 error_propagate(errp
, local_err
);