1 #include "qemu-common.h"
2 #include "qemu/error-report.h"
3 #include "qemu/option.h"
4 #include "qemu/config-file.h"
6 #include "qapi/error.h"
8 static QemuOptsList
*vm_config_groups
[32];
10 static QemuOptsList
*find_list(QemuOptsList
**lists
, const char *group
,
15 for (i
= 0; lists
[i
] != NULL
; i
++) {
16 if (strcmp(lists
[i
]->name
, group
) == 0)
19 if (lists
[i
] == NULL
) {
20 error_set(errp
, QERR_INVALID_OPTION_GROUP
, group
);
25 QemuOptsList
*qemu_find_opts(const char *group
)
28 Error
*local_err
= NULL
;
30 ret
= find_list(vm_config_groups
, group
, &local_err
);
31 if (error_is_set(&local_err
)) {
32 error_report("%s", error_get_pretty(local_err
));
33 error_free(local_err
);
39 QemuOptsList
*qemu_find_opts_err(const char *group
, Error
**errp
)
41 return find_list(vm_config_groups
, group
, errp
);
44 void qemu_add_opts(QemuOptsList
*list
)
48 entries
= ARRAY_SIZE(vm_config_groups
);
49 entries
--; /* keep list NULL terminated */
50 for (i
= 0; i
< entries
; i
++) {
51 if (vm_config_groups
[i
] == NULL
) {
52 vm_config_groups
[i
] = list
;
56 fprintf(stderr
, "ran out of space in vm_config_groups");
60 int qemu_set_option(const char *str
)
62 char group
[64], id
[64], arg
[64];
67 rc
= sscanf(str
, "%63[^.].%63[^.].%63[^=]%n", group
, id
, arg
, &offset
);
68 if (rc
< 3 || str
[offset
] != '=') {
69 error_report("can't parse: \"%s\"", str
);
73 list
= qemu_find_opts(group
);
78 opts
= qemu_opts_find(list
, id
);
80 error_report("there is no %s \"%s\" defined",
85 if (qemu_opt_set(opts
, arg
, str
+offset
+1) == -1) {
91 struct ConfigWriteData
{
96 static int config_write_opt(const char *name
, const char *value
, void *opaque
)
98 struct ConfigWriteData
*data
= opaque
;
100 fprintf(data
->fp
, " %s = \"%s\"\n", name
, value
);
104 static int config_write_opts(QemuOpts
*opts
, void *opaque
)
106 struct ConfigWriteData
*data
= opaque
;
107 const char *id
= qemu_opts_id(opts
);
110 fprintf(data
->fp
, "[%s \"%s\"]\n", data
->list
->name
, id
);
112 fprintf(data
->fp
, "[%s]\n", data
->list
->name
);
114 qemu_opt_foreach(opts
, config_write_opt
, data
, 0);
115 fprintf(data
->fp
, "\n");
119 void qemu_config_write(FILE *fp
)
121 struct ConfigWriteData data
= { .fp
= fp
};
122 QemuOptsList
**lists
= vm_config_groups
;
125 fprintf(fp
, "# qemu config file\n\n");
126 for (i
= 0; lists
[i
] != NULL
; i
++) {
127 data
.list
= lists
[i
];
128 qemu_opts_foreach(data
.list
, config_write_opts
, &data
, 0);
132 int qemu_config_parse(FILE *fp
, QemuOptsList
**lists
, const char *fname
)
134 char line
[1024], group
[64], id
[64], arg
[64], value
[1024];
136 QemuOptsList
*list
= NULL
;
137 Error
*local_err
= NULL
;
138 QemuOpts
*opts
= NULL
;
139 int res
= -1, lno
= 0;
142 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
143 loc_set_file(fname
, ++lno
);
144 if (line
[0] == '\n') {
145 /* skip empty lines */
148 if (line
[0] == '#') {
152 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, id
) == 2) {
154 list
= find_list(lists
, group
, &local_err
);
155 if (error_is_set(&local_err
)) {
156 error_report("%s", error_get_pretty(local_err
));
157 error_free(local_err
);
160 opts
= qemu_opts_create(list
, id
, 1, NULL
);
163 if (sscanf(line
, "[%63[^]]]", group
) == 1) {
164 /* group without id */
165 list
= find_list(lists
, group
, &local_err
);
166 if (error_is_set(&local_err
)) {
167 error_report("%s", error_get_pretty(local_err
));
168 error_free(local_err
);
171 opts
= qemu_opts_create_nofail(list
);
174 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2) {
177 error_report("no group defined");
180 if (qemu_opt_set(opts
, arg
, value
) != 0) {
185 error_report("parse error");
189 error_report("error reading file");
198 int qemu_read_config_file(const char *filename
)
200 FILE *f
= fopen(filename
, "r");
207 ret
= qemu_config_parse(f
, vm_config_groups
, filename
);