linux-user: Don't treat AArch64 cpu names specially
[qemu/ar7.git] / util / qemu-config.c
bloba59568d07077c711d3218733d44a1605e8ded2ab
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"
8 #include "qmp-commands.h"
10 static QemuOptsList *vm_config_groups[32];
12 static QemuOptsList *find_list(QemuOptsList **lists, const char *group,
13 Error **errp)
15 int i;
17 for (i = 0; lists[i] != NULL; i++) {
18 if (strcmp(lists[i]->name, group) == 0)
19 break;
21 if (lists[i] == NULL) {
22 error_set(errp, QERR_INVALID_OPTION_GROUP, group);
24 return lists[i];
27 QemuOptsList *qemu_find_opts(const char *group)
29 QemuOptsList *ret;
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);
38 return ret;
41 static CommandLineParameterInfoList *query_option_descs(const QemuOptDesc *desc)
43 CommandLineParameterInfoList *param_list = NULL, *entry;
44 CommandLineParameterInfo *info;
45 int i;
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) {
52 case QEMU_OPT_STRING:
53 info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
54 break;
55 case QEMU_OPT_BOOL:
56 info->type = COMMAND_LINE_PARAMETER_TYPE_BOOLEAN;
57 break;
58 case QEMU_OPT_NUMBER:
59 info->type = COMMAND_LINE_PARAMETER_TYPE_NUMBER;
60 break;
61 case QEMU_OPT_SIZE:
62 info->type = COMMAND_LINE_PARAMETER_TYPE_SIZE;
63 break;
66 if (desc[i].help) {
67 info->has_help = true;
68 info->help = g_strdup(desc[i].help);
71 entry = g_malloc0(sizeof(*entry));
72 entry->value = info;
73 entry->next = param_list;
74 param_list = entry;
77 return param_list;
80 CommandLineOptionInfoList *qmp_query_command_line_options(bool has_option,
81 const char *option,
82 Error **errp)
84 CommandLineOptionInfoList *conf_list = NULL, *entry;
85 CommandLineOptionInfo *info;
86 int i;
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));
94 entry->value = info;
95 entry->next = conf_list;
96 conf_list = entry;
100 if (conf_list == NULL) {
101 error_setg(errp, "invalid option name: %s", option);
104 return conf_list;
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)
114 int entries, i;
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;
121 return;
124 fprintf(stderr, "ran out of space in vm_config_groups");
125 abort();
128 int qemu_set_option(const char *str)
130 char group[64], id[64], arg[64];
131 QemuOptsList *list;
132 QemuOpts *opts;
133 int rc, offset;
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);
138 return -1;
141 list = qemu_find_opts(group);
142 if (list == NULL) {
143 return -1;
146 opts = qemu_opts_find(list, id);
147 if (!opts) {
148 error_report("there is no %s \"%s\" defined",
149 list->name, id);
150 return -1;
153 if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
154 return -1;
156 return 0;
159 struct ConfigWriteData {
160 QemuOptsList *list;
161 FILE *fp;
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);
169 return 0;
172 static int config_write_opts(QemuOpts *opts, void *opaque)
174 struct ConfigWriteData *data = opaque;
175 const char *id = qemu_opts_id(opts);
177 if (id) {
178 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
179 } else {
180 fprintf(data->fp, "[%s]\n", data->list->name);
182 qemu_opt_foreach(opts, config_write_opt, data, 0);
183 fprintf(data->fp, "\n");
184 return 0;
187 void qemu_config_write(FILE *fp)
189 struct ConfigWriteData data = { .fp = fp };
190 QemuOptsList **lists = vm_config_groups;
191 int i;
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];
203 Location loc;
204 QemuOptsList *list = NULL;
205 Error *local_err = NULL;
206 QemuOpts *opts = NULL;
207 int res = -1, lno = 0;
209 loc_push_none(&loc);
210 while (fgets(line, sizeof(line), fp) != NULL) {
211 loc_set_file(fname, ++lno);
212 if (line[0] == '\n') {
213 /* skip empty lines */
214 continue;
216 if (line[0] == '#') {
217 /* comment */
218 continue;
220 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
221 /* group with id */
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);
226 goto out;
228 opts = qemu_opts_create(list, id, 1, NULL);
229 continue;
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);
237 goto out;
239 opts = qemu_opts_create_nofail(list);
240 continue;
242 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
243 /* arg = value */
244 if (opts == NULL) {
245 error_report("no group defined");
246 goto out;
248 if (qemu_opt_set(opts, arg, value) != 0) {
249 goto out;
251 continue;
253 error_report("parse error");
254 goto out;
256 if (ferror(fp)) {
257 error_report("error reading file");
258 goto out;
260 res = 0;
261 out:
262 loc_pop(&loc);
263 return res;
266 int qemu_read_config_file(const char *filename)
268 FILE *f = fopen(filename, "r");
269 int ret;
271 if (f == NULL) {
272 return -errno;
275 ret = qemu_config_parse(f, vm_config_groups, filename);
276 fclose(f);
278 if (ret == 0) {
279 return 0;
280 } else {
281 return -EINVAL;