This is the mail archive of the cygwin@sources.redhat.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: [PATCH] Fix for backslash quoting in argument list passing (spawn_guts)


On Mon, Nov 20, 2000 at 09:22:39PM +0100, Pierre Bogossian wrote:
>>I've included what I think is a much simpler patch below.  It seems to
>>work correctly using a vc compiled program which echos its args.
>
>The problem with your patch is that it can double backslashes even if
>they don't precede a '"', and that shouldn't be done !
>Here is what I get with your patch:
>
>/tmp $ ./print_args_cygwin.exe 'a a\\a'
>1: a a\\a
>/tmp $ ./print_args_nocygwin.exe 'a a\\a'
>1: a a\\\\a
>
>Another issue is that backslashes that are just before the closing '"'
>have to be doubled too:
>
>/tmp $ ./print_args_cygwin.exe 'a a\'
>1: a a\
>/tmp $ ./print_args_nocygwin.exe 'a a\'
>1: a a"
>
>My print_args_nocygwin.exe is compiled with gcc, but I guess the
>result is the same with vc.

Ok.  How about this, then?

cgf

Index: spawn.cc
===================================================================
RCS file: /cvs/uberbaum/winsup/cygwin/spawn.cc,v
retrieving revision 1.63
diff -u -r1.63 spawn.cc
--- spawn.cc	2000/11/15 21:04:02	1.63
+++ spawn.cc	2000/11/20 22:00:20
@@ -480,12 +480,31 @@
 	  else
 	    {
 	      one_line.add ("\"", 1);
+	      /* Handle embedded special characters " and \.
+		 A " is always preceded by a \.
+		 A \ is not special unless it precedes a ".  If it does,
+		 then all preceding \'s must be doubled to avoid having
+		 the Windows command line parser interpret the \ as quoting
+		 the ".  This rule applies to a string of \'s before the end
+		 of the string, since cygwin/windows uses a " to delimit the
+		 argument. */
 	      for (; (p = strpbrk (a, "\"\\")); a = ++p)
 		{
 		  one_line.add (a, p - a);
-		  if ((*p == '\\' && p[1] == '"') || *p == '"')
-		    one_line.add ("\\", 1);
-		  one_line.add (p, 1);
+		  /* Find length of string of backslashes */
+		  int n = strspn (p, "\\");
+		  if (!n)
+		    one_line.add ("\\\"", 2);	/* No backslashes, so it must be a ".
+						   The " has to be protected with a backslash. */
+		  else
+		    {
+		      one_line.add (p, n);	/* Add the run of backslashes */
+		      /* Need to double up all of the preceding
+			 backslashes if they precede a quote or EOS. */
+		      if (!p[n] || p[n] == '"')
+			one_line.add (p, n);
+		      p += n - 1;		/* Point to last backslash */
+		    }
 		}
 	      if (*a)
 		one_line.add (a);

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]