xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Wei Liu <wei.liu2@citrix.com>
To: Xen-devel <xen-devel@lists.xenproject.org>
Cc: Ian Jackson <ian.jackson@eu.citrix.com>,
	xen-devel@lists.xen.org, Wei Liu <wei.liu2@citrix.com>,
	Ian Campbell <ian.campbell@citrix.com>,
	Stefano Stabellini <stefano.stabellini@eu.citrix.com>
Subject: [PATCH 4/4] xl: rework vtpm config parsing code
Date: Fri, 22 Jan 2016 11:50:54 +0000	[thread overview]
Message-ID: <1453463454-4114-5-git-send-email-wei.liu2@citrix.com> (raw)
In-Reply-To: <1453463454-4114-1-git-send-email-wei.liu2@citrix.com>

Follow the same pattern as vif config parse to introduce
parse_vtpm_config_token, parse_vtpm_config_one,
parse_vtpm_config_multistring and parse_vtpm_config. Then replace
open-coded parsing code with appropriate functions.

Signed-off-by: Wei Liu <wei.liu2@citrix.com>
---
 tools/libxl/xl_cmdimpl.c | 119 ++++++++++++++++++++++++++++++-----------------
 1 file changed, 76 insertions(+), 43 deletions(-)

diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
index f736f95..1444b0b 100644
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -1110,6 +1110,76 @@ static void parse_nic_config(XLU_Config **config, const char *buf,
     parse_nic_config_multistring(config, 1, &buf, nic);
 }
 
+static int parse_vtpm_config_token(XLU_Config **config, char *token,
+                                   libxl_device_vtpm *vtpm)
+{
+    char *oparg;
+
+    if (MATCH_OPTION("uuid", token, oparg)) {
+        if(libxl_uuid_from_string(&vtpm->uuid, oparg)) {
+            fprintf(stderr, "Invalid uuid specified (%s)\n", oparg);
+            return 1;
+        }
+    } else if (MATCH_OPTION("backend", token, oparg)) {
+        replace_string(&vtpm->backend_domname, oparg);
+    } else {
+        fprintf(stderr, "unrecognized argument `%s'\n", token);
+        return 1;
+    }
+
+    return 0;
+}
+
+static int parse_vtpm_config_one(XLU_Config **config, const char *config_str,
+                                 libxl_device_vtpm *vtpm)
+{
+    char *buf = xstrdup(config_str);
+    char *p;
+    int ret;
+
+    p = strtok(buf, ",");
+    if (!p) {
+        ret = 0;
+        goto out;
+    }
+
+    do {
+        while (*p == ' ')
+            p++;
+        ret = parse_vtpm_config_token(config, p, vtpm);
+    } while ((p = strtok(NULL, ",")) != NULL && ret == 0);
+
+out:
+    free(buf);
+
+    return ret;
+}
+
+static void parse_vtpm_config_multistring(XLU_Config **config,
+                                          int nspecs, const char *const *specs,
+                                          libxl_device_vtpm *vtpm)
+{
+    int i;
+
+    libxl_device_vtpm_init(vtpm);
+
+    if (!*config) {
+        *config = xlu_cfg_init(stderr, "command line");
+        if (!*config) { perror("xlu_cfg_init"); exit(-1); }
+    }
+
+    for (i = 0; i < nspecs; i++) {
+        if (parse_vtpm_config_one(config, specs[i], vtpm))
+            exit(EXIT_FAILURE);
+    }
+}
+
+static void parse_vtpm_config(XLU_Config **config, const char *buf,
+                              libxl_device_vtpm *vtpm)
+{
+    parse_vtpm_config_multistring(config, 1, &buf, vtpm);
+}
+
 static unsigned long parse_ulong(const char *str)
 {
     char *endptr;
@@ -1858,42 +1928,17 @@ static void parse_config_data(const char *config_source,
         while ((buf = xlu_cfg_get_listitem (vtpms, d_config->num_vtpms))
                != NULL) {
             libxl_device_vtpm *vtpm;
-            char * buf2 = strdup(buf);
-            char *p, *p2;
-            bool got_backend = false;
 
             vtpm = ARRAY_EXTEND_INIT(d_config->vtpms,
                                      d_config->num_vtpms,
                                      libxl_device_vtpm_init);
 
-            p = strtok(buf2, ",");
-            if(p) {
-               do {
-                  while(*p == ' ')
-                     ++p;
-                  if ((p2 = strchr(p, '=')) == NULL)
-                     break;
-                  *p2 = '\0';
-                  if (!strcmp(p, "backend")) {
-                     vtpm->backend_domname = strdup(p2 + 1);
-                     got_backend = true;
-                  } else if(!strcmp(p, "uuid")) {
-                     if( libxl_uuid_from_string(&vtpm->uuid, p2 + 1) ) {
-                        fprintf(stderr,
-                              "Failed to parse vtpm UUID: %s\n", p2 + 1);
-                        exit(1);
-                    }
-                  } else {
-                     fprintf(stderr, "Unknown string `%s' in vtpm spec\n", p);
-                     exit(1);
-                  }
-               } while ((p = strtok(NULL, ",")) != NULL);
-            }
-            if(!got_backend) {
+            parse_vtpm_config(&config, buf, vtpm);
+
+            if(!vtpm->backend_domname) {
                fprintf(stderr, "vtpm spec missing required backend field!\n");
                exit(1);
             }
-            free(buf2);
         }
     }
 
@@ -6812,8 +6857,8 @@ int main_vtpmattach(int argc, char **argv)
 {
     int opt;
     libxl_device_vtpm vtpm;
-    char *oparg;
     uint32_t domid;
+    XLU_Config *config = 0;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "vtpm-attach", 1) {
         /* No options */
@@ -6825,20 +6870,8 @@ int main_vtpmattach(int argc, char **argv)
     }
     ++optind;
 
-    libxl_device_vtpm_init(&vtpm);
-    for (argv += optind, argc -= optind; argc > 0; ++argv, --argc) {
-        if (MATCH_OPTION("uuid", *argv, oparg)) {
-            if(libxl_uuid_from_string(&(vtpm.uuid), oparg)) {
-                fprintf(stderr, "Invalid uuid specified (%s)\n", oparg);
-                return 1;
-            }
-        } else if (MATCH_OPTION("backend", *argv, oparg)) {
-            replace_string(&vtpm.backend_domname, oparg);
-        } else {
-            fprintf(stderr, "unrecognized argument `%s'\n", *argv);
-            return 1;
-        }
-    }
+    parse_vtpm_config_multistring(&config, argc-optind,
+                                  (const char* const*) argv + optind, &vtpm);
 
     if(dryrun_only) {
        char* json = libxl_device_vtpm_to_json(ctx, &vtpm);
-- 
2.1.4

  parent reply	other threads:[~2016-01-22 11:50 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-22 11:50 [PATCH 0/4] xl: consolidate adhoc parsers Wei Liu
2016-01-22 11:50 ` [PATCH 1/4] xl: remove unused macros Wei Liu
2016-01-25 12:17   ` Ian Campbell
2016-01-22 11:50 ` [PATCH 2/4] xl: wrap long lines where possible Wei Liu
2016-01-25 12:17   ` Ian Campbell
2016-01-22 11:50 ` [PATCH 3/4] xl: rework vif config parsing code Wei Liu
2016-01-22 11:50 ` Wei Liu [this message]
2016-01-25 12:25 ` [PATCH 0/4] xl: consolidate adhoc parsers Ian Campbell
2016-02-16 22:09 ` Jim Fehlig
2016-02-16 23:20   ` Wei Liu
2016-02-17 10:01     ` Ian Campbell
2016-02-17 12:00       ` Wei Liu

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=1453463454-4114-5-git-send-email-wei.liu2@citrix.com \
    --to=wei.liu2@citrix.com \
    --cc=ian.campbell@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=stefano.stabellini@eu.citrix.com \
    --cc=xen-devel@lists.xen.org \
    --cc=xen-devel@lists.xenproject.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).