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_set(errp
, QERR_INVALID_OPTION_GROUP
, group
);
28 QemuOptsList
*qemu_find_opts(const char *group
)
31 Error
*local_err
= NULL
;
33 ret
= find_list(vm_config_groups
, group
, &local_err
);
34 if (error_is_set(&local_err
)) {
35 error_report("%s", error_get_pretty(local_err
));
36 error_free(local_err
);
42 static CommandLineParameterInfoList
*query_option_descs(const QemuOptDesc
*desc
)
44 CommandLineParameterInfoList
*param_list
= NULL
, *entry
;
45 CommandLineParameterInfo
*info
;
48 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
49 info
= g_malloc0(sizeof(*info
));
50 info
->name
= g_strdup(desc
[i
].name
);
52 switch (desc
[i
].type
) {
54 info
->type
= COMMAND_LINE_PARAMETER_TYPE_STRING
;
57 info
->type
= COMMAND_LINE_PARAMETER_TYPE_BOOLEAN
;
60 info
->type
= COMMAND_LINE_PARAMETER_TYPE_NUMBER
;
63 info
->type
= COMMAND_LINE_PARAMETER_TYPE_SIZE
;
68 info
->has_help
= true;
69 info
->help
= g_strdup(desc
[i
].help
);
72 entry
= g_malloc0(sizeof(*entry
));
74 entry
->next
= param_list
;
81 /* remove repeated entry from the info list */
82 static void cleanup_infolist(CommandLineParameterInfoList
*head
)
84 CommandLineParameterInfoList
*pre_entry
, *cur
, *del_entry
;
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
;
96 pre_entry
= pre_entry
->next
;
102 /* merge the description items of two parameter infolists */
103 static void connect_infolist(CommandLineParameterInfoList
*head
,
104 CommandLineParameterInfoList
*new)
106 CommandLineParameterInfoList
*cur
;
115 /* access all the local QemuOptsLists for drive option */
116 static CommandLineParameterInfoList
*get_drive_infolist(void)
118 CommandLineParameterInfoList
*head
= NULL
, *cur
;
121 for (i
= 0; drive_config_groups
[i
] != NULL
; i
++) {
123 head
= query_option_descs(drive_config_groups
[i
]->desc
);
125 cur
= query_option_descs(drive_config_groups
[i
]->desc
);
126 connect_infolist(head
, cur
);
129 cleanup_infolist(head
);
134 CommandLineOptionInfoList
*qmp_query_command_line_options(bool has_option
,
138 CommandLineOptionInfoList
*conf_list
= NULL
, *entry
;
139 CommandLineOptionInfo
*info
;
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
);
146 if (!strcmp("drive", vm_config_groups
[i
]->name
)) {
147 info
->parameters
= get_drive_infolist();
150 query_option_descs(vm_config_groups
[i
]->desc
);
152 entry
= g_malloc0(sizeof(*entry
));
154 entry
->next
= conf_list
;
159 if (conf_list
== NULL
) {
160 error_setg(errp
, "invalid option name: %s", option
);
166 QemuOptsList
*qemu_find_opts_err(const char *group
, Error
**errp
)
168 return find_list(vm_config_groups
, group
, errp
);
171 void qemu_add_drive_opts(QemuOptsList
*list
)
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
;
183 fprintf(stderr
, "ran out of space in drive_config_groups");
187 void qemu_add_opts(QemuOptsList
*list
)
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
;
199 fprintf(stderr
, "ran out of space in vm_config_groups");
203 int qemu_set_option(const char *str
)
205 char group
[64], id
[64], arg
[64];
210 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
211 if (rc
< 3 || str
[offset
] != '=') {
212 error_report("can't parse: \"%s\"", str
);
216 list
= qemu_find_opts(group
);
221 opts
= qemu_opts_find(list
, id
);
223 error_report("there is no %s \"%s\" defined",
228 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
234 struct ConfigWriteData
{
239 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
241 struct ConfigWriteData
*data
= opaque
;
243 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
247 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
249 struct ConfigWriteData
*data
= opaque
;
250 const char *id
= qemu_opts_id(opts
);
253 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
255 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
257 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
258 fprintf(data
->fp
, "\n");
262 void qemu_config_write(FILE *fp
)
264 struct ConfigWriteData data
= { .fp
= fp
};
265 QemuOptsList
**lists
= vm_config_groups
;
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);
275 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
277 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
279 QemuOptsList
*list
= NULL
;
280 Error
*local_err
= NULL
;
281 QemuOpts
*opts
= NULL
;
282 int res
= -1, lno
= 0;
285 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
286 loc_set_file(fname
, ++lno
);
287 if (line
[0] == '\n') {
288 /* skip empty lines */
291 if (line
[0] == '#') {
295 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
297 list
= find_list(lists
, group
, &local_err
);
298 if (error_is_set(&local_err
)) {
299 error_report("%s", error_get_pretty(local_err
));
300 error_free(local_err
);
303 opts
= qemu_opts_create(list
, id
, 1, NULL
);
306 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
307 /* group without id */
308 list
= find_list(lists
, group
, &local_err
);
309 if (error_is_set(&local_err
)) {
310 error_report("%s", error_get_pretty(local_err
));
311 error_free(local_err
);
314 opts
= qemu_opts_create_nofail(list
);
317 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
320 error_report("no group defined");
323 if (qemu_opt_set(opts
, arg
, value
) != 0) {
328 error_report("parse error");
332 error_report("error reading file");
341 int qemu_read_config_file(const char *filename
)
343 FILE *f
= fopen(filename
, "r");
350 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);