git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org, Johannes Sixt <johannes.sixt@telecom.at>
Subject: [MinGW PATCH] Rework quote_arg()
Date: Sat, 27 Oct 2007 20:30:40 +0700	[thread overview]
Message-ID: <20071027133040.GA32312@laptop> (raw)

MS Windows command line is handled in a weird way. This patch addresses:
 - Quote empty arguments
 - Only escape backslashes and double quotation marks inside quoted arguments
 - Quote arguments if they have asterisk or question marks to prevent expansion

The last one is not documented in the link provided in the patch. I encountered
that behavior on cmd.exe, Windows XP. MSYS not tested.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 compat/mingw.c |   31 ++++++++++++++++++++++++++++---
 1 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/compat/mingw.c b/compat/mingw.c
index 22b5e10..90dc080 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -303,6 +303,7 @@ void openlog(const char *ident, int option, int facility)
 {
 }
 
+/* See http://msdn2.microsoft.com/en-us/library/17w5ykft(vs.71).aspx (Parsing C++ Command-Line Arguments */
 static const char *quote_arg(const char *arg)
 {
 	/* count chars to quote */
@@ -310,11 +311,23 @@ static const char *quote_arg(const char *arg)
 	int force_quotes = 0;
 	char *q, *d;
 	const char *p = arg;
+	if (!*p) force_quotes = 1;
 	while (*p) {
-		if (isspace(*p))
+		if (isspace(*p) || *p == '*' || *p == '?')
 			force_quotes = 1;
-		else if (*p == '"' || *p == '\\')
+		else if (*p == '"')
 			n++;
+		else if (*p == '\\') {
+			int count = 0;
+			while (*p == '\\') {
+				count++;
+				p++;
+				len++;
+			}
+			if (*p == '"')
+				n += count*2 + 1;
+			continue;
+		}
 		len++;
 		p++;
 	}
@@ -325,8 +338,20 @@ static const char *quote_arg(const char *arg)
 	d = q = xmalloc(len+n+3);
 	*d++ = '"';
 	while (*arg) {
-		if (*arg == '"' || *arg == '\\')
+		if (*arg == '"')
 			*d++ = '\\';
+		else if (*arg == '\\') {
+			int count = 0;
+			while (*arg == '\\') {
+				count++;
+				*d++ = *arg++;
+			}
+			if (*arg == '"') {
+				while (count-- > 0)
+					*d++ = '\\';
+				*d++ = '\\';
+			}
+		}
 		*d++ = *arg++;
 	}
 	*d++ = '"';
-- 
1.5.3.rc4.3.gab089

                 reply	other threads:[~2007-10-27 13:31 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20071027133040.GA32312@laptop \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=johannes.sixt@telecom.at \
    /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).