mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Abhinav Jain <jain.abhinav177@gmail.com>
To: agk@redhat.com, snitzer@kernel.org, mpatocka@redhat.com,
	dm-devel@lists.linux.dev, linux-kernel@vger.kernel.org
Cc: bmarzins@redhat.com, skhan@linuxfoundation.org,
	javier.carrasco.cruz@gmail.com, jain.abhinav177@gmail.com
Subject: [PATCH v2] dm: Add support for escaped characters in str_field_delimit()
Date: Thu, 13 Jun 2024 16:26:32 +0000	[thread overview]
Message-ID: <20240613162632.38065-1-jain.abhinav177@gmail.com> (raw)

Remove all the escape characters that come before separator.
Tested this code by writing a dummy program containing the two
functions and testing it on below input, sharing results:

Original string: "field1\,with\,commas,field2\,with\,more\,commas"
Field: "field1"
Field: "with"
Field: "commas"
Field: "field2"
Field: "with"
Field: "more"
Field: "commas"

Signed-off-by: Abhinav Jain <jain.abhinav177@gmail.com>
---
PATCH v1:
https://lore.kernel.org/all/20240609141721.52344-1-jain.abhinav177@gmail.com/

Changes since v1:
 - Modified the str_field_delimit function as per shared feedback
 - Added remove_escaped_characters function
---
---
 drivers/md/dm-init.c | 53 +++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 47 insertions(+), 6 deletions(-)

diff --git a/drivers/md/dm-init.c b/drivers/md/dm-init.c
index 2a71bcdba92d..0e31ecf1b48e 100644
--- a/drivers/md/dm-init.c
+++ b/drivers/md/dm-init.c
@@ -76,6 +76,24 @@ static void __init dm_setup_cleanup(struct list_head *devices)
 	}
 }
 
+/* Remove escape characters from a given field string. */
+static void __init remove_escape_characters(char *field)
+{
+	char *src = field;
+	char *dest = field;
+
+	while (*src) {
+		if (*src == '\\') {
+			src++;
+			if (*src)
+				*dest++ = *src++;
+		} else {
+			*dest++ = *src++;
+		}
+	}
+	*dest = '\0';
+}
+
 /**
  * str_field_delimit - delimit a string based on a separator char.
  * @str: the pointer to the string to delimit.
@@ -87,16 +105,39 @@ static void __init dm_setup_cleanup(struct list_head *devices)
  */
 static char __init *str_field_delimit(char **str, char separator)
 {
-	char *s;
+	char *s, *escaped, *field;
 
-	/* TODO: add support for escaped characters */
 	*str = skip_spaces(*str);
 	s = strchr(*str, separator);
-	/* Delimit the field and remove trailing spaces */
-	if (s)
+
+	/* Check for escaped character */
+	escaped = strchr(*str, '\\');
+	while (escaped && (s == NULL || escaped < s)) {
+		/*
+		 * Move the separator search ahead if escaped
+		 * character comes before.
+		 */
+		s = strchr(escaped + 1, separator);
+		escaped = strchr(escaped + 1, '\\');
+	}
+
+	/* If we found a separator, we need to handle escape characters */
+	if (s) {
+		*s = '\0';
+
+		remove_escape_characters(*str);
+		field = *str;
+		*str = s + 1;
+	} else {
+		/* Handle the last field when no separator is present */
+		s = *str + strlen(*str);
 		*s = '\0';
-	*str = strim(*str);
-	return s ? ++s : NULL;
+
+		remove_escape_characters(*str);
+		field = *str;
+		*str = s;
+	}
+	return field;
 }
 
 /**
-- 
2.34.1


             reply	other threads:[~2024-06-13 16:26 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-13 16:26 Abhinav Jain [this message]
2024-06-14 20:12 ` Benjamin Marzinski
2024-07-02 15:18 ` Mikulas Patocka
2024-07-02 16:23   ` Benjamin Marzinski

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=20240613162632.38065-1-jain.abhinav177@gmail.com \
    --to=jain.abhinav177@gmail.com \
    --cc=agk@redhat.com \
    --cc=bmarzins@redhat.com \
    --cc=dm-devel@lists.linux.dev \
    --cc=javier.carrasco.cruz@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mpatocka@redhat.com \
    --cc=skhan@linuxfoundation.org \
    --cc=snitzer@kernel.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

all inboxes | Powered by JetHome®