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];
12 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
,
17 for (i
= 0; lists
[i
] != NULL
; i
++) {
18 if (strcmp(lists
[i
]->name
, group
) == 0)
21 if (lists
[i
] == NULL
) {
22 error_set(errp
, QERR_INVALID_OPTION_GROUP
, group
);
27 QemuOptsList
*qemu_find_opts(const char *group
)
30 Error
*local_err
= NULL
;
32 ret
= find_list(vm_config_groups
, group
, &local_err
);
33 if (error_is_set(&local_err
)) {
34 error_report("%s", error_get_pretty(local_err
));
35 error_free(local_err
);
41 static CommandLineParameterInfoList
*query_option_descs(const QemuOptDesc
*desc
)
43 CommandLineParameterInfoList
*param_list
= NULL
, *entry
;
44 CommandLineParameterInfo
*info
;
47 for (i
= 0; desc
[i
].name
!= NULL
; i
++) {
48 info
= g_malloc0(sizeof(*info
));
49 info
->name
= g_strdup(desc
[i
].name
);
51 switch (desc
[i
].type
) {
53 info
->type
= COMMAND_LINE_PARAMETER_TYPE_STRING
;
56 info
->type
= COMMAND_LINE_PARAMETER_TYPE_BOOLEAN
;
59 info
->type
= COMMAND_LINE_PARAMETER_TYPE_NUMBER
;
62 info
->type
= COMMAND_LINE_PARAMETER_TYPE_SIZE
;
67 info
->has_help
= true;
68 info
->help
= g_strdup(desc
[i
].help
);
71 entry
= g_malloc0(sizeof(*entry
));
73 entry
->next
= param_list
;
80 CommandLineOptionInfoList
*qmp_query_command_line_options(bool has_option
,
84 CommandLineOptionInfoList
*conf_list
= NULL
, *entry
;
85 CommandLineOptionInfo
*info
;
88 for (i
= 0; vm_config_groups
[i
] != NULL
; i
++) {
89 if (!has_option
|| !strcmp(option
, vm_config_groups
[i
]->name
)) {
90 info
= g_malloc0(sizeof(*info
));
91 info
->option
= g_strdup(vm_config_groups
[i
]->name
);
92 info
->parameters
= query_option_descs(vm_config_groups
[i
]->desc
);
93 entry
= g_malloc0(sizeof(*entry
));
95 entry
->next
= conf_list
;
100 if (conf_list
== NULL
) {
101 error_setg(errp
, "invalid option name: %s", option
);
107 QemuOptsList
*qemu_find_opts_err(const char *group
, Error
**errp
)
109 return find_list(vm_config_groups
, group
, errp
);
112 void qemu_add_opts(QemuOptsList
*list
)
116 entries
= ARRAY_SIZE(vm_config_groups
);
117 entries
--; /* keep list NULL terminated */
118 for (i
= 0; i
< entries
; i
++) {
119 if (vm_config_groups
[i
] == NULL
) {
120 vm_config_groups
[i
] = list
;
124 fprintf(stderr
, "ran out of space in vm_config_groups");
128 int qemu_set_option(const char *str
)
130 char group
[64], id
[64], arg
[64];
135 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
136 if (rc
< 3 || str
[offset
] != '=') {
137 error_report("can't parse: \"%s\"", str
);
141 list
= qemu_find_opts(group
);
146 opts
= qemu_opts_find(list
, id
);
148 error_report("there is no %s \"%s\" defined",
153 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
159 struct ConfigWriteData
{
164 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
166 struct ConfigWriteData
*data
= opaque
;
168 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
172 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
174 struct ConfigWriteData
*data
= opaque
;
175 const char *id
= qemu_opts_id(opts
);
178 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
180 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
182 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
183 fprintf(data
->fp
, "\n");
187 void qemu_config_write(FILE *fp
)
189 struct ConfigWriteData data
= { .fp
= fp
};
190 QemuOptsList
**lists
= vm_config_groups
;
193 fprintf(fp
, "# qemu config file\n\n");
194 for (i
= 0; lists
[i
] != NULL
; i
++) {
195 data
.list
= lists
[i
];
196 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
200 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
202 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
204 QemuOptsList
*list
= NULL
;
205 Error
*local_err
= NULL
;
206 QemuOpts
*opts
= NULL
;
207 int res
= -1, lno
= 0;
210 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
211 loc_set_file(fname
, ++lno
);
212 if (line
[0] == '\n') {
213 /* skip empty lines */
216 if (line
[0] == '#') {
220 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
222 list
= find_list(lists
, group
, &local_err
);
223 if (error_is_set(&local_err
)) {
224 error_report("%s", error_get_pretty(local_err
));
225 error_free(local_err
);
228 opts
= qemu_opts_create(list
, id
, 1, NULL
);
231 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
232 /* group without id */
233 list
= find_list(lists
, group
, &local_err
);
234 if (error_is_set(&local_err
)) {
235 error_report("%s", error_get_pretty(local_err
));
236 error_free(local_err
);
239 opts
= qemu_opts_create_nofail(list
);
242 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
245 error_report("no group defined");
248 if (qemu_opt_set(opts
, arg
, value
) != 0) {
253 error_report("parse error");
257 error_report("error reading file");
266 int qemu_read_config_file(const char *filename
)
268 FILE *f
= fopen(filename
, "r");
275 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);