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_err(local_err
);
41 QemuOpts
*qemu_find_opts_singleton(const char *group
)
46 list
= qemu_find_opts(group
);
48 opts
= qemu_opts_find(list
, NULL
);
50 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
55 static CommandLineParameterInfoList
*query_option_descs(const QemuOptDesc
*desc
)
57 CommandLineParameterInfoList
*param_list
= NULL
, *entry
;
58 CommandLineParameterInfo
*info
;
61 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
62 info
= g_malloc0(sizeof(*info
));
63 info
->name
= g_strdup(desc
[i
].name
);
65 switch (desc
[i
].type
) {
67 info
->type
= COMMAND_LINE_PARAMETER_TYPE_STRING
;
70 info
->type
= COMMAND_LINE_PARAMETER_TYPE_BOOLEAN
;
73 info
->type
= COMMAND_LINE_PARAMETER_TYPE_NUMBER
;
76 info
->type
= COMMAND_LINE_PARAMETER_TYPE_SIZE
;
81 info
->has_help
= true;
82 info
->help
= g_strdup(desc
[i
].help
);
84 if (desc
[i
].def_value_str
) {
85 info
->has_q_default
= true;
86 info
->q_default
= g_strdup(desc
[i
].def_value_str
);
89 entry
= g_malloc0(sizeof(*entry
));
91 entry
->next
= param_list
;
98 /* remove repeated entry from the info list */
99 static void cleanup_infolist(CommandLineParameterInfoList
*head
)
101 CommandLineParameterInfoList
*pre_entry
, *cur
, *del_entry
;
106 while (pre_entry
!= cur
->next
) {
107 if (!strcmp(pre_entry
->value
->name
, cur
->next
->value
->name
)) {
108 del_entry
= cur
->next
;
109 cur
->next
= cur
->next
->next
;
113 pre_entry
= pre_entry
->next
;
119 /* merge the description items of two parameter infolists */
120 static void connect_infolist(CommandLineParameterInfoList
*head
,
121 CommandLineParameterInfoList
*new)
123 CommandLineParameterInfoList
*cur
;
132 /* access all the local QemuOptsLists for drive option */
133 static CommandLineParameterInfoList
*get_drive_infolist(void)
135 CommandLineParameterInfoList
*head
= NULL
, *cur
;
138 for (i
= 0; drive_config_groups
[i
] != NULL
; i
++) {
140 head
= query_option_descs(drive_config_groups
[i
]->desc
);
142 cur
= query_option_descs(drive_config_groups
[i
]->desc
);
143 connect_infolist(head
, cur
);
146 cleanup_infolist(head
);
151 CommandLineOptionInfoList
*qmp_query_command_line_options(bool has_option
,
155 CommandLineOptionInfoList
*conf_list
= NULL
, *entry
;
156 CommandLineOptionInfo
*info
;
159 for (i
= 0; vm_config_groups
[i
] != NULL
; i
++) {
160 if (!has_option
|| !strcmp(option
, vm_config_groups
[i
]->name
)) {
161 info
= g_malloc0(sizeof(*info
));
162 info
->option
= g_strdup(vm_config_groups
[i
]->name
);
163 if (!strcmp("drive", vm_config_groups
[i
]->name
)) {
164 info
->parameters
= get_drive_infolist();
167 query_option_descs(vm_config_groups
[i
]->desc
);
169 entry
= g_malloc0(sizeof(*entry
));
171 entry
->next
= conf_list
;
176 if (conf_list
== NULL
) {
177 error_setg(errp
, "invalid option name: %s", option
);
183 QemuOptsList
*qemu_find_opts_err(const char *group
, Error
**errp
)
185 return find_list(vm_config_groups
, group
, errp
);
188 void qemu_add_drive_opts(QemuOptsList
*list
)
192 entries
= ARRAY_SIZE(drive_config_groups
);
193 entries
--; /* keep list NULL terminated */
194 for (i
= 0; i
< entries
; i
++) {
195 if (drive_config_groups
[i
] == NULL
) {
196 drive_config_groups
[i
] = list
;
200 fprintf(stderr
, "ran out of space in drive_config_groups");
204 void qemu_add_opts(QemuOptsList
*list
)
208 entries
= ARRAY_SIZE(vm_config_groups
);
209 entries
--; /* keep list NULL terminated */
210 for (i
= 0; i
< entries
; i
++) {
211 if (vm_config_groups
[i
] == NULL
) {
212 vm_config_groups
[i
] = list
;
216 fprintf(stderr
, "ran out of space in vm_config_groups");
220 int qemu_set_option(const char *str
)
222 char group
[64], id
[64], arg
[64];
227 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
228 if (rc
< 3 || str
[offset
] != '=') {
229 error_report("can't parse: \"%s\"", str
);
233 list
= qemu_find_opts(group
);
238 opts
= qemu_opts_find(list
, id
);
240 error_report("there is no %s \"%s\" defined",
245 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
251 struct ConfigWriteData
{
256 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
258 struct ConfigWriteData
*data
= opaque
;
260 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
264 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
266 struct ConfigWriteData
*data
= opaque
;
267 const char *id
= qemu_opts_id(opts
);
270 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
272 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
274 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
275 fprintf(data
->fp
, "\n");
279 void qemu_config_write(FILE *fp
)
281 struct ConfigWriteData data
= { .fp
= fp
};
282 QemuOptsList
**lists
= vm_config_groups
;
285 fprintf(fp
, "# qemu config file\n\n");
286 for (i
= 0; lists
[i
] != NULL
; i
++) {
287 data
.list
= lists
[i
];
288 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
292 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
294 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
296 QemuOptsList
*list
= NULL
;
297 Error
*local_err
= NULL
;
298 QemuOpts
*opts
= NULL
;
299 int res
= -1, lno
= 0;
302 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
303 loc_set_file(fname
, ++lno
);
304 if (line
[0] == '\n') {
305 /* skip empty lines */
308 if (line
[0] == '#') {
312 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
314 list
= find_list(lists
, group
, &local_err
);
316 error_report_err(local_err
);
319 opts
= qemu_opts_create(list
, id
, 1, NULL
);
322 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
323 /* group without id */
324 list
= find_list(lists
, group
, &local_err
);
326 error_report_err(local_err
);
329 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
332 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
335 error_report("no group defined");
338 if (qemu_opt_set(opts
, arg
, value
) != 0) {
343 error_report("parse error");
347 error_report("error reading file");
356 int qemu_read_config_file(const char *filename
)
358 FILE *f
= fopen(filename
, "r");
365 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);
375 static void config_parse_qdict_section(QDict
*options
, QemuOptsList
*opts
,
381 Error
*local_err
= NULL
;
382 size_t orig_size
, enum_size
;
385 prefix
= g_strdup_printf("%s.", opts
->name
);
386 qdict_extract_subqdict(options
, &subqdict
, prefix
);
388 orig_size
= qdict_size(subqdict
);
393 subopts
= qemu_opts_create(opts
, NULL
, 0, &local_err
);
395 error_propagate(errp
, local_err
);
399 qemu_opts_absorb_qdict(subopts
, subqdict
, &local_err
);
401 error_propagate(errp
, local_err
);
405 enum_size
= qdict_size(subqdict
);
406 if (enum_size
< orig_size
&& enum_size
) {
407 error_setg(errp
, "Unknown option '%s' for [%s]",
408 qdict_first(subqdict
)->key
, opts
->name
);
413 /* Multiple, enumerated sections */
414 QListEntry
*list_entry
;
417 /* Not required anymore */
418 qemu_opts_del(subopts
);
420 qdict_array_split(subqdict
, &list
);
421 if (qdict_size(subqdict
)) {
422 error_setg(errp
, "Unused option '%s' for [%s]",
423 qdict_first(subqdict
)->key
, opts
->name
);
427 QLIST_FOREACH_ENTRY(list
, list_entry
) {
428 QDict
*section
= qobject_to_qdict(qlist_entry_obj(list_entry
));
432 error_setg(errp
, "[%s] section (index %u) does not consist of "
433 "keys", opts
->name
, i
);
437 opt_name
= g_strdup_printf("%s.%u", opts
->name
, i
++);
438 subopts
= qemu_opts_create(opts
, opt_name
, 1, &local_err
);
441 error_propagate(errp
, local_err
);
445 qemu_opts_absorb_qdict(subopts
, section
, &local_err
);
447 error_propagate(errp
, local_err
);
448 qemu_opts_del(subopts
);
452 if (qdict_size(section
)) {
453 error_setg(errp
, "[%s] section doesn't support the option '%s'",
454 opts
->name
, qdict_first(section
)->key
);
455 qemu_opts_del(subopts
);
466 void qemu_config_parse_qdict(QDict
*options
, QemuOptsList
**lists
,
470 Error
*local_err
= NULL
;
472 for (i
= 0; lists
[i
]; i
++) {
473 config_parse_qdict_section(options
, lists
[i
], &local_err
);
475 error_propagate(errp
, local_err
);