]>
Commit | Line | Data |
---|---|---|
7282a033 | 1 | #include "qemu-common.h" |
1de7afc9 PB |
2 | #include "qemu/error-report.h" |
3 | #include "qemu/option.h" | |
4 | #include "qemu/config-file.h" | |
b4a42f81 | 5 | #include "qapi/qmp/qerror.h" |
d0fef6fb | 6 | #include "hw/qdev.h" |
7b1b5d19 | 7 | #include "qapi/error.h" |
7282a033 | 8 | |
4d454574 | 9 | static QemuOptsList *vm_config_groups[32]; |
d058fe03 | 10 | |
2ac20613 LC |
11 | static QemuOptsList *find_list(QemuOptsList **lists, const char *group, |
12 | Error **errp) | |
ddc97855 GH |
13 | { |
14 | int i; | |
15 | ||
16 | for (i = 0; lists[i] != NULL; i++) { | |
17 | if (strcmp(lists[i]->name, group) == 0) | |
18 | break; | |
19 | } | |
20 | if (lists[i] == NULL) { | |
2ac20613 | 21 | error_set(errp, QERR_INVALID_OPTION_GROUP, group); |
ddc97855 GH |
22 | } |
23 | return lists[i]; | |
24 | } | |
25 | ||
490b648e KW |
26 | QemuOptsList *qemu_find_opts(const char *group) |
27 | { | |
2ac20613 LC |
28 | QemuOptsList *ret; |
29 | Error *local_err = NULL; | |
30 | ||
31 | ret = find_list(vm_config_groups, group, &local_err); | |
32 | if (error_is_set(&local_err)) { | |
312fd5f2 | 33 | error_report("%s", error_get_pretty(local_err)); |
2ac20613 LC |
34 | error_free(local_err); |
35 | } | |
36 | ||
37 | return ret; | |
490b648e KW |
38 | } |
39 | ||
60d5666f LC |
40 | QemuOptsList *qemu_find_opts_err(const char *group, Error **errp) |
41 | { | |
42 | return find_list(vm_config_groups, group, errp); | |
43 | } | |
44 | ||
dfe795e7 GH |
45 | void qemu_add_opts(QemuOptsList *list) |
46 | { | |
47 | int entries, i; | |
48 | ||
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; | |
55 | } | |
56 | } | |
57 | fprintf(stderr, "ran out of space in vm_config_groups"); | |
58 | abort(); | |
59 | } | |
60 | ||
d058fe03 GH |
61 | int qemu_set_option(const char *str) |
62 | { | |
63 | char group[64], id[64], arg[64]; | |
ddc97855 | 64 | QemuOptsList *list; |
d058fe03 | 65 | QemuOpts *opts; |
ddc97855 | 66 | int rc, offset; |
d058fe03 GH |
67 | |
68 | rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset); | |
69 | if (rc < 3 || str[offset] != '=') { | |
1ecda02b | 70 | error_report("can't parse: \"%s\"", str); |
d058fe03 GH |
71 | return -1; |
72 | } | |
73 | ||
304329ee | 74 | list = qemu_find_opts(group); |
ddc97855 | 75 | if (list == NULL) { |
d058fe03 GH |
76 | return -1; |
77 | } | |
78 | ||
ddc97855 | 79 | opts = qemu_opts_find(list, id); |
d058fe03 | 80 | if (!opts) { |
1ecda02b MA |
81 | error_report("there is no %s \"%s\" defined", |
82 | list->name, id); | |
d058fe03 GH |
83 | return -1; |
84 | } | |
85 | ||
3df04ac3 | 86 | if (qemu_opt_set(opts, arg, str+offset+1) == -1) { |
d058fe03 GH |
87 | return -1; |
88 | } | |
89 | return 0; | |
90 | } | |
91 | ||
9d993394 GH |
92 | struct ConfigWriteData { |
93 | QemuOptsList *list; | |
94 | FILE *fp; | |
95 | }; | |
96 | ||
97 | static int config_write_opt(const char *name, const char *value, void *opaque) | |
98 | { | |
99 | struct ConfigWriteData *data = opaque; | |
100 | ||
101 | fprintf(data->fp, " %s = \"%s\"\n", name, value); | |
102 | return 0; | |
103 | } | |
104 | ||
105 | static int config_write_opts(QemuOpts *opts, void *opaque) | |
106 | { | |
107 | struct ConfigWriteData *data = opaque; | |
108 | const char *id = qemu_opts_id(opts); | |
109 | ||
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); | |
114 | } | |
115 | qemu_opt_foreach(opts, config_write_opt, data, 0); | |
116 | fprintf(data->fp, "\n"); | |
117 | return 0; | |
118 | } | |
119 | ||
120 | void qemu_config_write(FILE *fp) | |
121 | { | |
122 | struct ConfigWriteData data = { .fp = fp }; | |
490b648e | 123 | QemuOptsList **lists = vm_config_groups; |
9d993394 GH |
124 | int i; |
125 | ||
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); | |
130 | } | |
131 | } | |
42262ba8 | 132 | |
490b648e | 133 | int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname) |
42262ba8 GH |
134 | { |
135 | char line[1024], group[64], id[64], arg[64], value[1024]; | |
cf5a65aa | 136 | Location loc; |
42262ba8 | 137 | QemuOptsList *list = NULL; |
2ac20613 | 138 | Error *local_err = NULL; |
42262ba8 | 139 | QemuOpts *opts = NULL; |
cf5a65aa | 140 | int res = -1, lno = 0; |
42262ba8 | 141 | |
cf5a65aa | 142 | loc_push_none(&loc); |
42262ba8 | 143 | while (fgets(line, sizeof(line), fp) != NULL) { |
cf5a65aa | 144 | loc_set_file(fname, ++lno); |
42262ba8 GH |
145 | if (line[0] == '\n') { |
146 | /* skip empty lines */ | |
147 | continue; | |
148 | } | |
149 | if (line[0] == '#') { | |
150 | /* comment */ | |
151 | continue; | |
152 | } | |
153 | if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) { | |
154 | /* group with id */ | |
2ac20613 LC |
155 | list = find_list(lists, group, &local_err); |
156 | if (error_is_set(&local_err)) { | |
312fd5f2 | 157 | error_report("%s", error_get_pretty(local_err)); |
2ac20613 | 158 | error_free(local_err); |
cf5a65aa | 159 | goto out; |
2ac20613 | 160 | } |
8be7e7e4 | 161 | opts = qemu_opts_create(list, id, 1, NULL); |
42262ba8 GH |
162 | continue; |
163 | } | |
164 | if (sscanf(line, "[%63[^]]]", group) == 1) { | |
165 | /* group without id */ | |
2ac20613 LC |
166 | list = find_list(lists, group, &local_err); |
167 | if (error_is_set(&local_err)) { | |
312fd5f2 | 168 | error_report("%s", error_get_pretty(local_err)); |
2ac20613 | 169 | error_free(local_err); |
cf5a65aa | 170 | goto out; |
2ac20613 | 171 | } |
e478b448 | 172 | opts = qemu_opts_create_nofail(list); |
42262ba8 GH |
173 | continue; |
174 | } | |
175 | if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) { | |
176 | /* arg = value */ | |
177 | if (opts == NULL) { | |
cf5a65aa MA |
178 | error_report("no group defined"); |
179 | goto out; | |
42262ba8 GH |
180 | } |
181 | if (qemu_opt_set(opts, arg, value) != 0) { | |
cf5a65aa | 182 | goto out; |
42262ba8 GH |
183 | } |
184 | continue; | |
185 | } | |
cf5a65aa MA |
186 | error_report("parse error"); |
187 | goto out; | |
42262ba8 | 188 | } |
ef82516d MA |
189 | if (ferror(fp)) { |
190 | error_report("error reading file"); | |
191 | goto out; | |
192 | } | |
cf5a65aa MA |
193 | res = 0; |
194 | out: | |
195 | loc_pop(&loc); | |
196 | return res; | |
42262ba8 | 197 | } |
dcfb0939 KW |
198 | |
199 | int qemu_read_config_file(const char *filename) | |
200 | { | |
201 | FILE *f = fopen(filename, "r"); | |
019e78ba KW |
202 | int ret; |
203 | ||
dcfb0939 KW |
204 | if (f == NULL) { |
205 | return -errno; | |
206 | } | |
207 | ||
019e78ba | 208 | ret = qemu_config_parse(f, vm_config_groups, filename); |
dcfb0939 KW |
209 | fclose(f); |
210 | ||
019e78ba KW |
211 | if (ret == 0) { |
212 | return 0; | |
213 | } else { | |
214 | return -EINVAL; | |
215 | } | |
dcfb0939 | 216 | } |