virtio-x-bus: fix allow_hotplug assertion.
[qemu/ar7.git] / util / qemu-config.c
blob01ca8901cf26cff93c916b806a8d59fc8b009b6a
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"
6 #include "hw/qdev.h"
7 #include "qapi/error.h"
9 static QemuOptsList *vm_config_groups[32];
11 static QemuOptsList *find_list(QemuOptsList **lists, const char *group,
12 Error **errp)
14 int i;
16 for (i = 0; lists[i] != NULL; i++) {
17 if (strcmp(lists[i]->name, group) == 0)
18 break;
20 if (lists[i] == NULL) {
21 error_set(errp, QERR_INVALID_OPTION_GROUP, group);
23 return lists[i];
26 QemuOptsList *qemu_find_opts(const char *group)
28 QemuOptsList *ret;
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);
37 return ret;
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)
47 int entries, i;
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;
54 return;
57 fprintf(stderr, "ran out of space in vm_config_groups");
58 abort();
61 int qemu_set_option(const char *str)
63 char group[64], id[64], arg[64];
64 QemuOptsList *list;
65 QemuOpts *opts;
66 int rc, offset;
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);
71 return -1;
74 list = qemu_find_opts(group);
75 if (list == NULL) {
76 return -1;
79 opts = qemu_opts_find(list, id);
80 if (!opts) {
81 error_report("there is no %s \"%s\" defined",
82 list->name, id);
83 return -1;
86 if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
87 return -1;
89 return 0;
92 struct ConfigWriteData {
93 QemuOptsList *list;
94 FILE *fp;
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);
102 return 0;
105 static int config_write_opts(QemuOpts *opts, void *opaque)
107 struct ConfigWriteData *data = opaque;
108 const char *id = qemu_opts_id(opts);
110 if (id) {
111 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
112 } else {
113 fprintf(data->fp, "[%s]\n", data->list->name);
115 qemu_opt_foreach(opts, config_write_opt, data, 0);
116 fprintf(data->fp, "\n");
117 return 0;
120 void qemu_config_write(FILE *fp)
122 struct ConfigWriteData data = { .fp = fp };
123 QemuOptsList **lists = vm_config_groups;
124 int i;
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];
136 Location loc;
137 QemuOptsList *list = NULL;
138 Error *local_err = NULL;
139 QemuOpts *opts = NULL;
140 int res = -1, lno = 0;
142 loc_push_none(&loc);
143 while (fgets(line, sizeof(line), fp) != NULL) {
144 loc_set_file(fname, ++lno);
145 if (line[0] == '\n') {
146 /* skip empty lines */
147 continue;
149 if (line[0] == '#') {
150 /* comment */
151 continue;
153 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
154 /* group with id */
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);
159 goto out;
161 opts = qemu_opts_create(list, id, 1, NULL);
162 continue;
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);
170 goto out;
172 opts = qemu_opts_create_nofail(list);
173 continue;
175 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
176 /* arg = value */
177 if (opts == NULL) {
178 error_report("no group defined");
179 goto out;
181 if (qemu_opt_set(opts, arg, value) != 0) {
182 goto out;
184 continue;
186 error_report("parse error");
187 goto out;
189 if (ferror(fp)) {
190 error_report("error reading file");
191 goto out;
193 res = 0;
194 out:
195 loc_pop(&loc);
196 return res;
199 int qemu_read_config_file(const char *filename)
201 FILE *f = fopen(filename, "r");
202 int ret;
204 if (f == NULL) {
205 return -errno;
208 ret = qemu_config_parse(f, vm_config_groups, filename);
209 fclose(f);
211 if (ret == 0) {
212 return 0;
213 } else {
214 return -EINVAL;