linux-doc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michal Suchanek <msuchanek@suse.de>
To: Jonathan Corbet <corbet@lwn.net>,
	Michal Suchanek <msuchanek@suse.de>,
	Arnd Bergmann <arnd@arndb.de>,
	Frederic Weisbecker <frederic@kernel.org>,
	Ingo Molnar <mingo@kernel.org>, Aaron Wu <Aaron.Wu@analog.com>,
	Tony Luck <tony.luck@intel.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	"Steven Rostedt," <rostedt@goodmis.org>,
	Laura Abbott <lauraa@codeaurora.org>,
	Dominik Brodowski <linux@dominikbrodowski.net>,
	Alexey Dobriyan <adobriyan@gmail.com>,
	Tom Lendacky <thomas.lendacky@amd.com>,
	Jeffrey Hugo <jhugo@codeaurora.org>, Baoquan He <bhe@redhat.com>,
	Ilya Matveychikov <matvejchikov@gmail.com>,
	linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v10 1/5] lib/cmdline.c: Add backslash support to kernel commandline parsing.
Date: Tue,  5 Jun 2018 18:43:08 +0200	[thread overview]
Message-ID: <0005cec6acaf7ef629a64a1a72b09f68f84aae4c.1528215659.git.msuchanek@suse.de> (raw)
In-Reply-To: <cover.1528215659.git.msuchanek@suse.de>
In-Reply-To: <cover.1528215659.git.msuchanek@suse.de>

 - add \ as quoting character in kernel parameters
 - generalize quoting character removal to be able to remove quoting
 character at any position in kernel parameter rather than start and end
 only

This allows passing quotes in kernel arguments.

It is also useful to have quoting grammar more similar to shells and
bootloaders.

Signed-off-by: Michal Suchanek <msuchanek@suse.de>
---
- more verbose commit message
- optimize final quote removal
---
 lib/cmdline.c | 50 ++++++++++++++++++++++++++------------------------
 1 file changed, 26 insertions(+), 24 deletions(-)

diff --git a/lib/cmdline.c b/lib/cmdline.c
index 192848d880ea..aa0086dbb082 100644
--- a/lib/cmdline.c
+++ b/lib/cmdline.c
@@ -191,32 +191,43 @@ bool parse_option_str(const char *str, const char *option)
 	return false;
 }
 
+#define break_arg_end(i) { \
+	if (isspace(args[i]) && !in_quote && !backslash) \
+		break; \
+	}
+
 /*
  * Parse a string to get a param value pair.
- * You can use " around spaces, but can't escape ".
+ * You can use " around spaces, and you can escape with \
  * Hyphens and underscores equivalent in parameter names.
  */
 char *next_arg(char *args, char **param, char **val, int *state_flags)
 {
 	unsigned int i, equals = 0;
-	int in_quote = 0, quoted = 0;
+	int in_quote = 0, backslash = 0;
 	char *next;
 
-	if (*args == '"') {
-		args++;
-		in_quote = 1;
-		quoted = 1;
-	}
-
 	for (i = 0; args[i]; i++) {
-		if (isspace(args[i]) && !in_quote)
-			break;
-		if (equals == 0) {
-			if (args[i] == '=')
-				equals = i;
+		break_arg_end(i);
+
+		if ((equals == 0) && (args[i] == '='))
+			equals = i;
+
+		if (!backslash) {
+			if ((args[i] == '"') || (args[i] == '\\')) {
+				if (args[i] == '"')
+					in_quote = !in_quote;
+				if (args[i] == '\\')
+					backslash = 1;
+
+				break_arg_end(i + 1);
+				memmove(args + 1, args, i);
+				args++;
+				i--;
+			}
+		} else {
+			backslash = 0;
 		}
-		if (args[i] == '"')
-			in_quote = !in_quote;
 	}
 
 	if (state_flags)
@@ -231,16 +242,7 @@ char *next_arg(char *args, char **param, char **val, int *state_flags)
 	else {
 		args[equals] = '\0';
 		*val = args + equals + 1;
-
-		/* Don't include quotes in value. */
-		if (**val == '"') {
-			(*val)++;
-			if (args[i-1] == '"')
-				args[i-1] = '\0';
-		}
 	}
-	if (quoted && args[i-1] == '"')
-		args[i-1] = '\0';
 
 	if (args[i]) {
 		args[i] = '\0';
-- 
2.13.6

--
To unsubscribe from this list: send the line "unsubscribe linux-doc" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  reply	other threads:[~2018-06-05 16:45 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-05 16:43 [PATCH v10 0/5] Kernel parameter parser cleanup/enhancement Michal Suchanek
2018-06-05 16:43 ` Michal Suchanek [this message]
2018-06-05 16:43 ` [PATCH v10 2/5] Documentation/admin-guide: backslash support in kernel arguments Michal Suchanek
2018-06-05 16:43 ` [PATCH v10 3/5] init/main.c: simplify repair_env_string Michal Suchanek
2018-06-05 16:43 ` [PATCH v10 4/5] lib/cmdline.c: Implement single quotes in commandline argument parsing Michal Suchanek
2018-06-05 16:43 ` [PATCH v10 5/5] Documentation/admin-guide: single quotes in kernel arguments Michal Suchanek
2018-06-05 17:05 ` [PATCH v10 0/5] Kernel parameter parser cleanup/enhancement Andy Shevchenko
2018-06-05 17:25   ` Michal Suchánek
2018-06-19 23:36 ` Andrew Morton
2018-06-20 10:47   ` Michal Suchánek

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=0005cec6acaf7ef629a64a1a72b09f68f84aae4c.1528215659.git.msuchanek@suse.de \
    --to=msuchanek@suse.de \
    --cc=Aaron.Wu@analog.com \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=bhe@redhat.com \
    --cc=corbet@lwn.net \
    --cc=frederic@kernel.org \
    --cc=jhugo@codeaurora.org \
    --cc=lauraa@codeaurora.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@dominikbrodowski.net \
    --cc=matvejchikov@gmail.com \
    --cc=mingo@kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    --cc=thomas.lendacky@amd.com \
    --cc=tony.luck@intel.com \
    /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).