All of lore.kernel.org
 help / color / mirror / Atom feed
From: Juergen Gross <jgross@suse.com>
To: xen-devel@lists.xenproject.org
Cc: Juergen Gross <jgross@suse.com>, Wei Liu <wl@xen.org>,
	Anthony PERARD <anthony.perard@citrix.com>
Subject: [PATCH v2 2/3] tools/xl: make split_string_into_pair() more usable
Date: Wed, 22 Mar 2023 08:34:52 +0100	[thread overview]
Message-ID: <20230322073453.7853-3-jgross@suse.com> (raw)
In-Reply-To: <20230322073453.7853-1-jgross@suse.com>

Today split_string_into_pair() will not really do what its name is
suggesting: instead of splitting a string into a pair of strings using
a delimiter, it will return the first two strings of the initial string
by using the delimiter.

This is never what the callers want, so modify split_string_into_pair()
to split the string only at the first delimiter found, resulting in
something like "x=a=b" to be split into "x" and "a=b" when being called
with "=" as the delimiter. Today the returned strings would be "x" and
"a".

At the same time switch the delimiter from "const char *" (allowing
multiple delimiter characters) to "char" (a single character only), as
this makes the function more simple without breaking any use cases.

Suggested-by: Anthony PERARD <anthony.perard@citrix.com>
Signed-off-by: Juergen Gross <jgross@suse.com>
---
V2:
- new patch
---
 tools/xl/xl_parse.c | 23 ++++++++++++-----------
 tools/xl/xl_parse.h |  4 ++--
 2 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/tools/xl/xl_parse.c b/tools/xl/xl_parse.c
index 0e1b6907fa..09cabd2732 100644
--- a/tools/xl/xl_parse.c
+++ b/tools/xl/xl_parse.c
@@ -654,7 +654,7 @@ static void parse_vnuma_config(const XLU_Config *config,
 
                 if (!buf) continue;
 
-                if (split_string_into_pair(buf, "=", &option, &value,
+                if (split_string_into_pair(buf, '=', &option, &value,
                                            isspace)) {
                     fprintf(stderr, "xl: failed to split \"%s\" into pair\n",
                             buf);
@@ -831,7 +831,7 @@ int parse_vdispl_config(libxl_device_vdispl *vdispl, char *token)
         {
             char *resolution;
 
-            rc = split_string_into_pair(connectors[i], ":",
+            rc = split_string_into_pair(connectors[i], ':',
                                         &vdispl->connectors[i].unique_id,
                                         &resolution, NULL);
 
@@ -2358,7 +2358,7 @@ void parse_config_data(const char *config_source,
             for (i = 0; i < len; i++) {
                 char *key, *value;
                 int rc;
-                rc = split_string_into_pair(pairs[i], "=", &key, &value,
+                rc = split_string_into_pair(pairs[i], '=', &key, &value,
                                             isspace);
                 if (rc != 0) {
                     fprintf(stderr, "failed to parse channel configuration: %s",
@@ -3011,26 +3011,27 @@ void trim(char_predicate_t predicate, const char *input, char **output)
     *output = result;
 }
 
-int split_string_into_pair(const char *str, const char *delim,
-                           char **a, char **b, char_predicate_t predicate)
+int split_string_into_pair(const char *str, char delim, char **a, char **b,
+                           char_predicate_t predicate)
 {
-    char *s, *p, *saveptr, *aa = NULL, *bb = NULL;
+    char *s, *p, *aa = NULL, *bb = NULL;
     int rc = 0;
 
     s = xstrdup(str);
 
-    p = strtok_r(s, delim, &saveptr);
+    p = strchr(s, delim);
     if (p == NULL) {
         rc = ERROR_INVAL;
         goto out;
     }
+    *p = 0;
     if (predicate) {
-        trim(predicate, p, &aa);
+        trim(predicate, s, &aa);
     } else {
-        aa = xstrdup(p);
+        aa = xstrdup(s);
     }
-    p = strtok_r(NULL, delim, &saveptr);
-    if (p == NULL) {
+    p++;
+    if (!*p) {
         rc = ERROR_INVAL;
         goto out;
     }
diff --git a/tools/xl/xl_parse.h b/tools/xl/xl_parse.h
index ab35c68545..fe0d586cdd 100644
--- a/tools/xl/xl_parse.h
+++ b/tools/xl/xl_parse.h
@@ -51,8 +51,8 @@ void replace_string(char **str, const char *val);
    and look for CTYPE in libxl_internal.h */
 typedef int (*char_predicate_t)(const int c);
 void trim(char_predicate_t predicate, const char *input, char **output);
-int split_string_into_pair(const char *str, const char *delim,
-                           char **a, char **b, char_predicate_t predicate);
+int split_string_into_pair(const char *str, char delim, char **a, char **b,
+                           char_predicate_t predicate);
 
 const char *get_action_on_shutdown_name(libxl_action_on_shutdown a);
 
-- 
2.35.3



  parent reply	other threads:[~2023-03-22  7:35 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-22  7:34 [PATCH v2 0/3] tools/xl: small cleanup of parsing code Juergen Gross
2023-03-22  7:34 ` [PATCH v2 1/3] tools/xl: allow split_string_into_pair() to trim values Juergen Gross
2023-03-22  7:34 ` Juergen Gross [this message]
2023-03-22 12:29   ` [PATCH v2 2/3] tools/xl: make split_string_into_pair() more usable Jason Andryuk
2023-03-22 17:28     ` Anthony PERARD
2023-03-22  7:34 ` [PATCH v2 3/3] tools/xl: rework p9 config parsing Juergen Gross
2023-03-22 12:32   ` Jason Andryuk
2023-03-22 17:29     ` Anthony PERARD

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=20230322073453.7853-3-jgross@suse.com \
    --to=jgross@suse.com \
    --cc=anthony.perard@citrix.com \
    --cc=wl@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.