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"
9 static QemuOptsList
*vm_config_groups
[32];
11 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
,
16 for (i
= 0; lists
[i
] != NULL
; i
++) {
17 if (strcmp(lists
[i
]->name
, group
) == 0)
20 if (lists
[i
] == NULL
) {
21 error_set(errp
, QERR_INVALID_OPTION_GROUP
, group
);
26 QemuOptsList
*qemu_find_opts(const char *group
)
29 Error
*local_err
= NULL
;
31 ret
= find_list(vm_config_groups
, group
, &local_err
);
32 if (error_is_set(&local_err
)) {
33 error_report("%s", error_get_pretty(local_err
));
34 error_free(local_err
);
40 QemuOptsList
*qemu_find_opts_err(const char *group
, Error
**errp
)
42 return find_list(vm_config_groups
, group
, errp
);
45 void qemu_add_opts(QemuOptsList
*list
)
49 entries
= ARRAY_SIZE(vm_config_groups
);
50 entries
--; /* keep list NULL terminated */
51 for (i
= 0; i
< entries
; i
++) {
52 if (vm_config_groups
[i
] == NULL
) {
53 vm_config_groups
[i
] = list
;
57 fprintf(stderr
, "ran out of space in vm_config_groups");
61 int qemu_set_option(const char *str
)
63 char group
[64], id
[64], arg
[64];
68 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
69 if (rc
< 3 || str
[offset
] != '=') {
70 error_report("can't parse: \"%s\"", str
);
74 list
= qemu_find_opts(group
);
79 opts
= qemu_opts_find(list
, id
);
81 error_report("there is no %s \"%s\" defined",
86 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
92 struct ConfigWriteData
{
97 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
99 struct ConfigWriteData
*data
= opaque
;
101 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
105 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
107 struct ConfigWriteData
*data
= opaque
;
108 const char *id
= qemu_opts_id(opts
);
111 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
113 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
115 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
116 fprintf(data
->fp
, "\n");
120 void qemu_config_write(FILE *fp
)
122 struct ConfigWriteData data
= { .fp
= fp
};
123 QemuOptsList
**lists
= vm_config_groups
;
126 fprintf(fp
, "# qemu config file\n\n");
127 for (i
= 0; lists
[i
] != NULL
; i
++) {
128 data
.list
= lists
[i
];
129 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
133 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
135 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
137 QemuOptsList
*list
= NULL
;
138 Error
*local_err
= NULL
;
139 QemuOpts
*opts
= NULL
;
140 int res
= -1, lno
= 0;
143 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
144 loc_set_file(fname
, ++lno
);
145 if (line
[0] == '\n') {
146 /* skip empty lines */
149 if (line
[0] == '#') {
153 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
155 list
= find_list(lists
, group
, &local_err
);
156 if (error_is_set(&local_err
)) {
157 error_report("%s", error_get_pretty(local_err
));
158 error_free(local_err
);
161 opts
= qemu_opts_create(list
, id
, 1, NULL
);
164 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
165 /* group without id */
166 list
= find_list(lists
, group
, &local_err
);
167 if (error_is_set(&local_err
)) {
168 error_report("%s", error_get_pretty(local_err
));
169 error_free(local_err
);
172 opts
= qemu_opts_create_nofail(list
);
175 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
178 error_report("no group defined");
181 if (qemu_opt_set(opts
, arg
, value
) != 0) {
186 error_report("parse error");
190 error_report("error reading file");
199 int qemu_read_config_file(const char *filename
)
201 FILE *f
= fopen(filename
, "r");
208 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);