diff mbox

[2/4] QemuOpts: dump config.

Message ID 1255509568-10635-3-git-send-email-kraxel@redhat.com
State New
Headers show

Commit Message

Gerd Hoffmann Oct. 14, 2009, 8:39 a.m. UTC
Add a function to write the QemuOpts configuration to a git-style
config file.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 qemu-config.c |   39 +++++++++++++++++++++++++++++++++++++++
 qemu-config.h |    2 ++
 2 files changed, 41 insertions(+), 0 deletions(-)

Comments

Anthony Liguori Oct. 14, 2009, 6:56 p.m. UTC | #1
Gerd Hoffmann wrote:
> Add a function to write the QemuOpts configuration to a git-style
> config file.
>
> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> ---
>  qemu-config.c |   39 +++++++++++++++++++++++++++++++++++++++
>  qemu-config.h |    2 ++
>  2 files changed, 41 insertions(+), 0 deletions(-)
>
> diff --git a/qemu-config.c b/qemu-config.c
> index f02dd42..fa236e9 100644
> --- a/qemu-config.c
> +++ b/qemu-config.c
> @@ -238,3 +238,42 @@ int qemu_set_option(const char *str)
>      return 0;
>  }
>  
> +struct ConfigWriteData {
> +    QemuOptsList *list;
> +    FILE *fp;
> +};
> +
> +static int config_write_opt(const char *name, const char *value, void *opaque)
> +{
> +    struct ConfigWriteData *data = opaque;
> +
> +    fprintf(data->fp, "  %s = \"%s\"\n", name, value);
> +    return 0;
> +}
> +
> +static int config_write_opts(QemuOpts *opts, void *opaque)
> +{
> +    struct ConfigWriteData *data = opaque;
> +    const char *id = qemu_opts_id(opts);
> +
> +    if (id) {
> +        fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
>   

This id syntax is a good idea.

Regards,

Anthony Liguori
diff mbox

Patch

diff --git a/qemu-config.c b/qemu-config.c
index f02dd42..fa236e9 100644
--- a/qemu-config.c
+++ b/qemu-config.c
@@ -238,3 +238,42 @@  int qemu_set_option(const char *str)
     return 0;
 }
 
+struct ConfigWriteData {
+    QemuOptsList *list;
+    FILE *fp;
+};
+
+static int config_write_opt(const char *name, const char *value, void *opaque)
+{
+    struct ConfigWriteData *data = opaque;
+
+    fprintf(data->fp, "  %s = \"%s\"\n", name, value);
+    return 0;
+}
+
+static int config_write_opts(QemuOpts *opts, void *opaque)
+{
+    struct ConfigWriteData *data = opaque;
+    const char *id = qemu_opts_id(opts);
+
+    if (id) {
+        fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
+    } else {
+        fprintf(data->fp, "[%s]\n", data->list->name);
+    }
+    qemu_opt_foreach(opts, config_write_opt, data, 0);
+    fprintf(data->fp, "\n");
+    return 0;
+}
+
+void qemu_config_write(FILE *fp)
+{
+    struct ConfigWriteData data = { .fp = fp };
+    int i;
+
+    fprintf(fp, "# qemu config file\n\n");
+    for (i = 0; lists[i] != NULL; i++) {
+        data.list = lists[i];
+        qemu_opts_foreach(data.list, config_write_opts, &data, 0);
+    }
+}
diff --git a/qemu-config.h b/qemu-config.h
index cdad5ac..33fce7e 100644
--- a/qemu-config.h
+++ b/qemu-config.h
@@ -9,4 +9,6 @@  extern QemuOptsList qemu_rtc_opts;
 
 int qemu_set_option(const char *str);
 
+void qemu_config_write(FILE *fp);
+
 #endif /* QEMU_CONFIG_H */