aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGerd Hoffmann <kraxel@redhat.com>2009-10-14 10:39:27 +0200
committerAnthony Liguori <aliguori@us.ibm.com>2009-11-09 08:43:13 -0600
commit42262ba860f9d0186efe7ea74266207e30ddd0d0 (patch)
treecb2e273a3d214412cf67a3342525b1a0cbac9d65
parent9d993394a1a63e500194b4eabd8a01b1af401a8f (diff)
downloadqemu-42262ba860f9d0186efe7ea74266207e30ddd0d0.zip
qemu-42262ba860f9d0186efe7ea74266207e30ddd0d0.tar.gz
qemu-42262ba860f9d0186efe7ea74266207e30ddd0d0.tar.bz2
QemuOpts: parse config from file.
Add functions to parse QemuOpts from a git-style config file. Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
-rw-r--r--qemu-config.c50
-rw-r--r--qemu-config.h1
2 files changed, 51 insertions, 0 deletions
diff --git a/qemu-config.c b/qemu-config.c
index ab2135a..590fc05 100644
--- a/qemu-config.c
+++ b/qemu-config.c
@@ -296,3 +296,53 @@ void qemu_config_write(FILE *fp)
qemu_opts_foreach(data.list, config_write_opts, &data, 0);
}
}
+
+int qemu_config_parse(FILE *fp)
+{
+ char line[1024], group[64], id[64], arg[64], value[1024];
+ QemuOptsList *list = NULL;
+ QemuOpts *opts = NULL;
+
+ while (fgets(line, sizeof(line), fp) != NULL) {
+ if (line[0] == '\n') {
+ /* skip empty lines */
+ continue;
+ }
+ if (line[0] == '#') {
+ /* comment */
+ continue;
+ }
+ if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
+ /* group with id */
+ list = find_list(group);
+ if (list == NULL)
+ return -1;
+ opts = qemu_opts_create(list, id, 1);
+ continue;
+ }
+ if (sscanf(line, "[%63[^]]]", group) == 1) {
+ /* group without id */
+ list = find_list(group);
+ if (list == NULL)
+ return -1;
+ opts = qemu_opts_create(list, NULL, 0);
+ continue;
+ }
+ if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
+ /* arg = value */
+ if (opts == NULL) {
+ fprintf(stderr, "no group defined\n");
+ return -1;
+ }
+ if (qemu_opt_set(opts, arg, value) != 0) {
+ fprintf(stderr, "failed to set \"%s\" for %s\n",
+ arg, group);
+ return -1;
+ }
+ continue;
+ }
+ fprintf(stderr, "parse error: %s\n", line);
+ return -1;
+ }
+ return 0;
+}
diff --git a/qemu-config.h b/qemu-config.h
index 83f9300..b564851 100644
--- a/qemu-config.h
+++ b/qemu-config.h
@@ -11,5 +11,6 @@ extern QemuOptsList qemu_rtc_opts;
int qemu_set_option(const char *str);
void qemu_config_write(FILE *fp);
+int qemu_config_parse(FILE *fp);
#endif /* QEMU_CONFIG_H */