qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-stable@nongnu.org, qemu-devel@nongnu.org, armbru@redhat.com
Subject: Re: [PATCH 1/3] qemu-config: parse configuration files to a QDict
Date: Wed, 19 May 2021 16:40:30 +0200	[thread overview]
Message-ID: <YKUjXnThNZk6YfTZ@merkur.fritz.box> (raw)
In-Reply-To: <20210518154059.3002446-1-pbonzini@redhat.com>

Am 18.05.2021 um 17:40 hat Paolo Bonzini geschrieben:
> Change the parser to put the values into a QDict and pass them
> to a callback.  qemu_config_parse's QemuOpts creation is
> itself turned into a callback function.
> 
> This is useful for -readconfig to support keyval-based options;
> getting a QDict from the parser removes a roundtrip from
> QDict to QemuOpts and then back to QDict.
> 
> Cc: Kevin Wolf <kwolf@redhat.com>
> Cc: Markus Armbruster <armbru@redhat.com>
> Cc: qemu-stable@nongnu.org
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

> diff --git a/util/qemu-config.c b/util/qemu-config.c
> index 34974c4b47..7bca153c6c 100644
> --- a/util/qemu-config.c
> +++ b/util/qemu-config.c
> @@ -351,19 +351,19 @@ void qemu_config_write(FILE *fp)
>  }
>  
>  /* Returns number of config groups on success, -errno on error */
> -int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname, Error **errp)
> +static int qemu_config_foreach(FILE *fp, QEMUConfigCB *cb, void *opaque,
> +                               const char *fname, Error **errp)
>  {
> -    char line[1024], group[64], id[64], arg[64], value[1024];
> +    char line[1024], prev_group[64], group[64], arg[64], value[1024];
>      Location loc;
> -    QemuOptsList *list = NULL;
>      Error *local_err = NULL;
> -    QemuOpts *opts = NULL;
> +    QDict *qdict = NULL;
>      int res = -EINVAL, lno = 0;
>      int count = 0;
>  
>      loc_push_none(&loc);
>      while (fgets(line, sizeof(line), fp) != NULL) {
> -        loc_set_file(fname, ++lno);
> +        ++lno;
>          if (line[0] == '\n') {
>              /* skip empty lines */
>              continue;
> @@ -372,39 +372,39 @@ int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname, Error *
>              /* comment */
>              continue;
>          }
> -        if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
> -            /* group with id */
> -            list = find_list(lists, group, &local_err);
> -            if (local_err) {
> -                error_propagate(errp, local_err);
> -                goto out;
> +        if (line[0] == '[') {
> +            QDict *prev = qdict;
> +            if (sscanf(line, "[%63s \"%63[^\"]\"]", group, value) == 2) {
> +                qdict = qdict_new();
> +                qdict_put_str(qdict, "id", value);
> +                count++;
> +            } else if (sscanf(line, "[%63[^]]]", group) == 1) {
> +                qdict = qdict_new();
> +                count++;
>              }
> -            opts = qemu_opts_create(list, id, 1, NULL);
> -            count++;
> -            continue;
> -        }
> -        if (sscanf(line, "[%63[^]]]", group) == 1) {
> -            /* group without id */
> -            list = find_list(lists, group, &local_err);
> -            if (local_err) {
> -                error_propagate(errp, local_err);
> -                goto out;
> +            if (qdict != prev) {
> +                if (prev) {
> +                    cb(prev_group, prev, opaque, &local_err);
> +                    qobject_unref(prev);
> +                    if (local_err) {
> +                        error_propagate(errp, local_err);
> +                        goto out;
> +                    }
> +                }
> +                strcpy(prev_group, group);
> +                continue;
>              }
> -            opts = qemu_opts_create(list, NULL, 0, &error_abort);
> -            count++;
> -            continue;
>          }
> +        loc_set_file(fname, lno);

Error reporting is going to suffer quite a bit from this delayed
parsing, reporting the last line of a group for most cases now.

I think it's acceptable for an option that we want to get rid of in the
long term anyway, but worth noting.

>          value[0] = '\0';
>          if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2 ||
>              sscanf(line, " %63s = \"\"", arg) == 1) {
>              /* arg = value */
> -            if (opts == NULL) {
> +            if (qdict == NULL) {
>                  error_setg(errp, "no group defined");
>                  goto out;
>              }
> -            if (!qemu_opt_set(opts, arg, value, errp)) {
> -                goto out;
> -            }
> +            qdict_put_str(qdict, arg, value);
>              continue;
>          }
>          error_setg(errp, "parse error");
> @@ -418,10 +418,41 @@ int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname, Error *
>      res = count;
>  out:
>      loc_pop(&loc);
> +    if (qdict) {
> +        cb(group, qdict, opaque, errp);
> +        qobject_unref(qdict);
> +    }
>      return res;
>  }
>  
> -int qemu_read_config_file(const char *filename, Error **errp)
> +void qemu_config_do_parse(const char *group, QDict *qdict, void *opaque, Error **errp)
> +{
> +    QemuOptsList **lists = opaque;
> +    const char *id = qdict_get_try_str(qdict, "id");
> +    QemuOptsList *list;
> +    QemuOpts *opts;
> +
> +    list = find_list(lists, group, errp);
> +    if (!list) {
> +        return;
> +    }
> +
> +    opts = qemu_opts_create(list, id, 1, errp);
> +    if (!opts) {
> +        return;
> +    }
> +    if (id) {
> +        qdict_del(qdict, "id");
> +    }
> +    qemu_opts_absorb_qdict(opts, qdict, errp);

Shouldn't we check that qdict is empty now and return an error if there
are any options that the QemuOptsList doesn't accept?

Kevin



  reply	other threads:[~2021-05-19 14:42 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-18 15:40 [PATCH 0/3] vl: add -object support back into -readconfig Paolo Bonzini
2021-05-18 15:40 ` [PATCH 1/3] qemu-config: parse configuration files to a QDict Paolo Bonzini
2021-05-19 14:40   ` Kevin Wolf [this message]
2021-05-20  7:41     ` Paolo Bonzini
2021-05-18 15:40 ` [PATCH 2/3] vl: plumb keyval-based options into -readconfig Paolo Bonzini
2021-05-19 16:35   ` Kevin Wolf
2021-05-18 15:40 ` [PATCH 3/3] vl: plug -object back " Paolo Bonzini
2021-05-19 16:41   ` Kevin Wolf
2021-05-19 13:58 ` [PATCH 0/3] vl: add -object support " Kevin Wolf
2021-05-19 14:46   ` Paolo Bonzini
2021-05-19 14:09 ` no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YKUjXnThNZk6YfTZ@merkur.fritz.box \
    --to=kwolf@redhat.com \
    --cc=armbru@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-stable@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).