1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #include "qemu/osdep.h"
3 #include "qapi/error.h"
4 #include "qapi/qapi-commands-misc.h"
5 #include "qapi/qmp/qlist.h"
6 #include "qemu/option.h"
7 #include "qemu/config-file.h"
10 static CommandLineParameterInfoList
*query_option_descs(const QemuOptDesc
*desc
)
12 CommandLineParameterInfoList
*param_list
= NULL
;
13 CommandLineParameterInfo
*info
;
16 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
17 info
= g_malloc0(sizeof(*info
));
18 info
->name
= g_strdup(desc
[i
].name
);
20 switch (desc
[i
].type
) {
22 info
->type
= COMMAND_LINE_PARAMETER_TYPE_STRING
;
25 info
->type
= COMMAND_LINE_PARAMETER_TYPE_BOOLEAN
;
28 info
->type
= COMMAND_LINE_PARAMETER_TYPE_NUMBER
;
31 info
->type
= COMMAND_LINE_PARAMETER_TYPE_SIZE
;
35 info
->help
= g_strdup(desc
[i
].help
);
36 info
->q_default
= g_strdup(desc
[i
].def_value_str
);
38 QAPI_LIST_PREPEND(param_list
, info
);
44 /* remove repeated entry from the info list */
45 static void cleanup_infolist(CommandLineParameterInfoList
*head
)
47 CommandLineParameterInfoList
*pre_entry
, *cur
, *del_entry
;
52 while (pre_entry
!= cur
->next
) {
53 if (!strcmp(pre_entry
->value
->name
, cur
->next
->value
->name
)) {
54 del_entry
= cur
->next
;
55 cur
->next
= cur
->next
->next
;
56 del_entry
->next
= NULL
;
57 qapi_free_CommandLineParameterInfoList(del_entry
);
60 pre_entry
= pre_entry
->next
;
66 /* merge the description items of two parameter infolists */
67 static void connect_infolist(CommandLineParameterInfoList
*head
,
68 CommandLineParameterInfoList
*new)
70 CommandLineParameterInfoList
*cur
;
79 /* access all the local QemuOptsLists for drive option */
80 static CommandLineParameterInfoList
*get_drive_infolist(void)
82 CommandLineParameterInfoList
*head
= NULL
, *cur
;
85 for (i
= 0; drive_config_groups
[i
] != NULL
; i
++) {
87 head
= query_option_descs(drive_config_groups
[i
]->desc
);
89 cur
= query_option_descs(drive_config_groups
[i
]->desc
);
90 connect_infolist(head
, cur
);
93 cleanup_infolist(head
);
98 static CommandLineParameterInfo
*objprop_to_cmdline_prop(ObjectProperty
*prop
)
100 CommandLineParameterInfo
*info
;
102 info
= g_malloc0(sizeof(*info
));
103 info
->name
= g_strdup(prop
->name
);
105 if (g_str_equal(prop
->type
, "bool") || g_str_equal(prop
->type
, "OnOffAuto")) {
106 info
->type
= COMMAND_LINE_PARAMETER_TYPE_BOOLEAN
;
107 } else if (g_str_equal(prop
->type
, "int")) {
108 info
->type
= COMMAND_LINE_PARAMETER_TYPE_NUMBER
;
109 } else if (g_str_equal(prop
->type
, "size")) {
110 info
->type
= COMMAND_LINE_PARAMETER_TYPE_SIZE
;
112 info
->type
= COMMAND_LINE_PARAMETER_TYPE_STRING
;
115 if (prop
->description
) {
116 info
->help
= g_strdup(prop
->description
);
122 static CommandLineParameterInfoList
*query_all_machine_properties(void)
124 CommandLineParameterInfoList
*params
= NULL
, *clpiter
;
125 CommandLineParameterInfo
*info
;
126 GSList
*machines
, *curr_mach
;
127 ObjectPropertyIterator op_iter
;
128 ObjectProperty
*prop
;
131 machines
= object_class_get_list(TYPE_MACHINE
, false);
134 /* Loop over all machine classes */
135 for (curr_mach
= machines
; curr_mach
; curr_mach
= curr_mach
->next
) {
136 object_class_property_iter_init(&op_iter
, curr_mach
->data
);
137 /* ... and over the properties of each machine: */
138 while ((prop
= object_property_iter_next(&op_iter
))) {
143 * Check whether the property has already been put into the list
144 * (via another machine class)
147 for (clpiter
= params
; clpiter
!= NULL
; clpiter
= clpiter
->next
) {
148 if (g_str_equal(clpiter
->value
->name
, prop
->name
)) {
153 /* If it hasn't been added before, add it now to the list */
155 info
= objprop_to_cmdline_prop(prop
);
156 QAPI_LIST_PREPEND(params
, info
);
161 g_slist_free(machines
);
163 /* Add entry for the "type" parameter */
164 info
= g_malloc0(sizeof(*info
));
165 info
->name
= g_strdup("type");
166 info
->type
= COMMAND_LINE_PARAMETER_TYPE_STRING
;
167 info
->help
= g_strdup("machine type");
168 QAPI_LIST_PREPEND(params
, info
);
173 CommandLineOptionInfoList
*qmp_query_command_line_options(const char *option
,
176 CommandLineOptionInfoList
*conf_list
= NULL
;
177 CommandLineOptionInfo
*info
;
180 for (i
= 0; vm_config_groups
[i
] != NULL
; i
++) {
181 if (!option
|| !strcmp(option
, vm_config_groups
[i
]->name
)) {
182 info
= g_malloc0(sizeof(*info
));
183 info
->option
= g_strdup(vm_config_groups
[i
]->name
);
184 if (!strcmp("drive", vm_config_groups
[i
]->name
)) {
185 info
->parameters
= get_drive_infolist();
188 query_option_descs(vm_config_groups
[i
]->desc
);
190 QAPI_LIST_PREPEND(conf_list
, info
);
194 if (!option
|| !strcmp(option
, "machine")) {
195 info
= g_malloc0(sizeof(*info
));
196 info
->option
= g_strdup("machine");
197 info
->parameters
= query_all_machine_properties();
198 QAPI_LIST_PREPEND(conf_list
, info
);
201 if (conf_list
== NULL
) {
202 error_setg(errp
, "invalid option name: %s", option
);